#导入包from statsmodels.tsa.stattools import adfullerdef test_stationarity(timeseries): #Determing rolling statistics rolmean=timeseries.rolling(7).mean() rolstd=timeseries.rolling(7).std() #Plot rolling statistics: orig=plt.plot(timeseries,color='blue',label='Original') mean=plt.plot(rolmean,color='red',label='Rolling Mean') #均值 std=plt.plot(rolstd,color='black',label='Rolling Std') #标准差 plt.legend(loc='best') plt.title('Rolling Mean & Standard Deviation') plt.show(block=False) #Perform Dickey-Fuller Test: print('Results of Dickey-Fuller Test:') dftest=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(df_test['客流量'])
Results of Dickey-Fuller Test:Test Statistic -4.608773p-value 0.000124#Lags Used 15.000000Number of Observations Used 483.000000Critical Value (1%) -3.443962Critical Value (5%) -2.867543Critical Value (10%) -2.569967dtype: float64
Returns ------- adf : float The test statistic. pvalue : float MacKinnon"s approximate p-value based on MacKinnon (1994, 2010). usedlag : int The number of lags used. nobs : int The number of observations used for the ADF regression and calculation of the critical values. critical values : dict Critical values for the test statistic at the 1 %, 5 %, and 10 % levels. Based on MacKinnon (2010). icbest : float The maximized information criterion if autolag is not None. resstore : ResultStore, optional A dummy class with results attached as attributes.