So I have a lot of timeseries that I check for stationarity by using ADF and KPSS tests. Depending on the results of each test, I can find out if it is stationary, non-stationary, trend stationary or difference stationary. Accordingly I would detrend or difference the timeseries and apply both tests again, to see if the timeseries is now stationary.
Question: If I differenced or detrended a timeseries and the tests show, that the timeseries is still not stationary, can I apply differencing or detrending again? And check again? How often is one allowed to try to convert a timeseries to a stationary process?
These are my methods:
def stationary_test(adf_results, kpss_results):
if adf_results[1] > 0.05:
adf_h0 = True
if kpss_results[1] > 0.05:
kpss_h0 = True
stationary_string = 'trend stationary'
else:
kpss_h0 = False
stationary_string = 'non-stationary'
else:
adf_h0 = False
if kpss_results[1] > 0.05:
kpss_h0 = True
stationary_string = 'stationary'
else:
kpss_h0 = False
stationary_string = 'difference stationary'
return adf_h0, kpss_h0, stationary_string
def differencing(df):
differenced = pd.DataFrame(df[col] - df[col].shift(1))
return differenced
def detrending(df):
detrended = signal.detrend(df[col])
return detrended