PACF和ACF图表未显示

时间:2018-02-22 20:55:59

标签: python charts line partial

#I successfully converted dates to date format that is proper for time series.
#Turn 'variable' column data into dates
homeprice['variable'] = pd.to_datetime(homeprice['variable'], format='%Y-%m-%d')
homeprice

#Did some further cleaning and column name changing - not including code

#This block worked as expected and created a simple time series'able data frame for me
homepriceTS = homeprice.set_index('date').rolling(2).mean()
homepriceTS.head()

#Remove NaN's from the dataframe
homepriceTS.dropna(inplace=True)
homepriceTS.head()

#tested for stationarity (it was not stationary). not including code for brevity

#There is clearly an upward trend in the line chart, which breaks stationarity rules. So lets transform with log
homepriceTS_log = np.log(homepriceTS)
plt.plot(homepriceTS_log)

#In this approach, we take average of ‘k’ consecutive values depending on the frequency of time series. Here we can take the average over the past 1 year, i.e. last 12 values. Pandas has specific functions defined for determining rolling statistics.
moving_avg = pd.rolling_mean(homepriceTS_log,12)
plt.plot(homepriceTS_log)
plt.plot(moving_avg, color='red')

#The red line above shows the rolling mean. Lets subtract this from the original series. Note that since we are taking average of last 12 values, rolling mean is not defined for first 11 values
homepriceTS_log_moving_avg_diff = homepriceTS_log - moving_avg
homepriceTS_log_moving_avg_diff.head(n=12)

#Drop the first 11 NaN rows since we chose a 12 month roll. Then we'll test for stationarity again.
homepriceTS_log_moving_avg_diff.dropna(inplace=True)
homepriceTS_log_moving_avg_diff.head()

#tested for stationarity again. It was much better, so I'll go with it.

#homepriceTS_log_diff = homepriceTS_log - homepriceTS_log.shift()
plt.plot(homepriceTS_log_diff)

#ACF and PACF plots:
from statsmodels.tsa.stattools import acf, pacf

# I wanted to use method = 'ols'... but it just kept throwing me errors so I gave up and used 'yw' (not even sure if that makes sense to do in this case)
lag_acf = acf(homepriceTS_log_diff, nlags=12)
lag_pacf = pacf(homepriceTS_log_diff, nlags=12, method='yw')

----------这里我开始遇到问题-----------

#Plot ACF: 
plt.subplot(121)
plt.plot(lag_acf)
#plt.axis([xmin,xmax,ymin,ymax])
plt.axhline(y=0,linestyle='--',color='gray')
plt.axhline(y=-1.96/np.sqrt(len(homepriceTS_log_moving_avg_diff)),linestyle='--',color='gray')
plt.axhline(y=1.96/np.sqrt(len(homepriceTS_log_moving_avg_diff)),linestyle='--',color='gray')
plt.title('Autocorrelation Function')

#Plot PACF:
plt.subplot(122)
plt.plot(lag_pacf)
#plt.axis([xmin,xmax,ymin,ymax])
plt.axhline(y=0,linestyle='--',color='gray')
plt.axhline(y=-1.96/np.sqrt(len(homepriceTS_log_moving_avg_diff)),linestyle='--',color='gray')
plt.axhline(y=1.96/np.sqrt(len(homepriceTS_log_moving_avg_diff)),linestyle='--',color='gray')
plt.title('Partial Autocorrelation Function')
plt.tight_layout()`

我正在执行时间序列分析并运行我的ACF(自相关函数)和PACF(部分自相关函数)。当我绘制它们时,我得到一个空白图表,显示上限,下限和零......但没有线图。我假设线图的代码不起作用,或者我的ACF和PACF图的窗口大小需要更改(我无法在谷歌或移动+标签中的任何位置找到蟒蛇)。 Doe有谁知道如何让线条显示在图表上?

1 个答案:

答案 0 :(得分:0)

或者,您可以使用plot_acf()函数并指定滞后。在这种情况下,我将时间作为索引,并且该序列从Thousands of Passengers数据集中被称为airline_passengers.csv

# import the plotting functions for act and pacf  
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plot_acf(df1['Thousands of Passengers'], lags=40);