繁体   English   中英

PACF和ACF图未显示

[英]PACF and ACF graphs not showing

#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图的窗口大小需要更改(我在google或python中的shift + tab指令中找不到任何位置)。 有人知道我如何使线条显示在图形上吗?

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

# 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM