Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am trying to run Dickey-Fuller test in statsmodels in Python but getting error

I am trying to run Dickey-Fuller test in statsmodels in Python but getting error P Running from python 2.7 & Pandas version 0.19.2. Dataset is from Github and imported the same

enter code here

 from statsmodels.tsa.stattools import adfuller
    def test_stationarity(timeseries):

    print 'Results of Dickey-Fuller Test:'
        dftest = ts.adfuller(timeseries, autolag='AIC' )
        dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
        for key,value in dftest[4].items():
            dfoutput['Critical Value (%s)'%key] = value
        print dfoutput


    test_stationarity(tr)

Gives me following error :

Results of Dickey-Fuller Test:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-10ab4b87e558> in <module>()
----> 1 test_stationarity(tr)

<ipython-input-14-d779e1ed35b3> in test_stationarity(timeseries)
     19     #Perform Dickey-Fuller test:
     20     print 'Results of Dickey-Fuller Test:'
---> 21     dftest = ts.adfuller(timeseries, autolag='AIC' )
     22     #dftest = adfuller(timeseries, autolag='AIC')
     23     dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])

C:\Users\SONY\Anaconda2\lib\site-packages\statsmodels\tsa\stattools.pyc in adfuller(x, maxlag, regression, autolag, store, regresults)
    209 
    210     xdiff = np.diff(x)
--> 211     xdall = lagmat(xdiff[:, None], maxlag, trim='both', original='in')
    212     nobs = xdall.shape[0]  # pylint: disable=E1103
    213 

C:\Users\SONY\Anaconda2\lib\site-packages\statsmodels\tsa\tsatools.pyc in lagmat(x, maxlag, trim, original)
    322     if x.ndim == 1:
    323         x = x[:,None]
--> 324     nobs, nvar = x.shape
    325     if original in ['ex','sep']:
    326         dropidx = nvar

ValueError: too many values to unpack
like image 222
N.R Avatar asked Mar 29 '17 17:03

N.R


People also ask

What is Adfuller test Python?

ADF (Augmented Dickey-Fuller) test is a statistical significance test which means the test will give results in hypothesis tests with null and alternative hypotheses. As a result, we will have a p-value from which we will need to make inferences about the time series, whether it is stationary or not.

What is Maxlag Adfuller?

The maxlag parameter is the maximum parameter adfuller will try, but not necessarily use. If none is specified it determines the maxpar by computing [ceil(12*(n/100)^(1/4))], so that for longer data sets it assumes that higher order lags could be present (n is amount of observations here).

How do you read an ADF test?

The augmented Dickey–Fuller (ADF) statistic, used in the test, is a negative number. The more negative it is, the stronger the rejection of the hypothesis that there is a unit root at some level of confidence.

What is Autolag AIC?

autolag{“AIC”, “BIC”, “t-stat”, None } Method to use when automatically determining the lag length among the values 0, 1, …, maxlag. If “AIC” (default) or “BIC”, then the number of lags is chosen to minimize the corresponding information criterion. “t-stat” based choice of maxlag.


1 Answers

tr must be a 1d array-like, as you can see here. I don't know what is tr in your case. Assuming that you defined tr as the dataframe that contains the time serie's data, you should do something like this:

tr = tr.iloc[:,0].values

Then adfuller will be able to read the data.

like image 122
Pedro Marcelino Avatar answered Jan 23 '23 02:01

Pedro Marcelino