[英]how do I fix the following bugs in my ARIMA model?
I have built ARIMA model for prediction for my time series data.我已经构建了 ARIMA model 来预测我的时间序列数据。 click this link to access data file.
单击此链接访问数据文件。
I am getting few bugs in my code.我的代码中几乎没有错误。
code:代码:
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
data = pd .read_csv('datasets/car_sales.csv')
print(data.head())
data['Month']= pd.to_datetime(data['Month'], infer_datetime_format = True)
indexed_data = data.set_index(['Month'])
print(indexed_data.head())
fig, ax = plt.subplots(figsize = (8,6))
sb.lineplot(data = indexed_data, ax = ax)
plt.show()
#define a function to check stationarity of the data (rolling stats and Dickey-fuller tst)
def test_stationary(timeseries):
#Determing rolling statistics
moving_avg = timeseries.rolling(window=12).mean()
moving_std = timeseries.rolling(window=12).std()
#Plot rolling statistics:
fig, ax = plt.subplots(figsize = (10,4))
sb.lineplot(data = timeseries, legend = False, label = 'Original')
sb.lineplot(data = moving_avg, palette = ['red'], legend = False, label = 'Rollmean')
sb.lineplot(data = moving_std, palette = ['black'], legend = False, label = 'Rollstd')
plt.title('Rolling statistics to check stationarity')
plt.legend(loc='best')
plt.show()
#Perform Dickey-Fuller test:
from statsmodels.tsa.stattools import adfuller
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_stationary(indexed_data)
# data is non stationary, Therefore we have to make it stationary
# apply differencing technique to make the data stationary
data_log = np.log(indexed_data)
data_log_diff = data_log - data_log.shift()
data_log_diff.dropna(inplace = True)
test_stationary(data_log_diff)
# we observe the data is stationary and can be used for prediction
# prediction is done using ARIMA
# let us plot ACF and PACF to determine p and q parameters for ARIMA model
from statsmodels.tsa.stattools import acf, pacf
lag_acf = acf(data_log_diff, nlags=20, fft=True)
lag_pacf = pacf(data_log_diff, nlags=20, method='ols')
#Plot ACF:
fig, ax = plt.subplots(figsize = (10,6))
sb.lineplot(data = lag_acf, ax = ax)
ax.set_xticks(range(1,len(lag_acf)))#Plot PACF:
fig, ax = plt.subplots(figsize = (10,6))
sb.lineplot(data = lag_pacf, ax = ax)
ax.set_xticks(range(1,len(lag_pacf)))
plt.axhline(y=0,linestyle='--',color='gray')
plt.axhline(y=-1.96/np.sqrt(len(data_log_diff)),linestyle='--',color='gray')
plt.axhline(y=1.96/np.sqrt(len(data_log_diff)),linestyle='--',color='gray')
plt.title('Partial Autocorrelation Function')
plt.show()
#Plot PACF:
fig, ax = plt.subplots(figsize = (10,6))
sb.lineplot(data = lag_pacf, ax = ax)
ax.set_xticks(range(1,len(lag_pacf)))
plt.axhline(y=0,linestyle='--',color='gray')
plt.axhline(y=-1.96/np.sqrt(len(data_log_diff)),linestyle='--',color='gray')
plt.axhline(y=1.96/np.sqrt(len(data_log_diff)),linestyle='--',color='gray')
plt.title('Partial Autocorrelation Function')
plt.show()
# based on acf and pacf plots the ARMA parameter can be p = 1, q = 1
#ARIMA model for data
from statsmodels.tsa.arima_model import ARIMA
model = ARIMA(data_log, order=(1, 1, 0))
results_ARIMA = model.fit(disp = -1)
plt.plot(data_log_diff)
plt.plot(results_ARIMA.fittedvalues, color='red')
plt.title('RSS: %.4f'% sum((results_ARIMA.fittedvalues-data_log_diff)**2))
when I run the above code I am encountering the following bugs:当我运行上面的代码时,我遇到了以下错误:
Based on acf and pacf plots, the p and q parameters are found to be p=1 and q=1.根据 acf 和 pacf 图,发现 p 和 q 参数为 p=1 和 q=1。 But my arima model doesn't work.
但是我的 arima model 不起作用。
(I get ValueError: The computed initial MA coefficients are not invertible You should induce invertibility, choose a different model order, or you can pass your own start_params.) for p=1,q=1.
(我得到 ValueError:计算的初始 MA 系数不可逆您应该诱导可逆性,选择不同的 model 顺序,或者您可以传递自己的 start_params。)对于 p=1,q=1。 Instead it works for p=1, q=0.
相反,它适用于 p=1,q=0。
What is the problem in p=1, q=1 parameter values? p=1, q=1 参数值有什么问题?
I am getting a warning:我收到警告:
C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py:162: ValueWarning: No frequency information was provided, so inferred frequency MS will be used.
C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py:162:ValueWarning:未提供频率信息,因此将使用推断频率 MS。 % freq, ValueWarning) C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py:162: ValueWarning: No frequency information was provided, so inferred frequency MS will be used.
% freq, ValueWarning) C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py:162:ValueWarning:未提供频率信息,因此将使用推断频率 MS。 % freq, ValueWarning)
% 频率,值警告)
what is the reason for this warning and how do i fix it?这个警告的原因是什么,我该如何解决?
I am getting an error for parameters p=1, q=0.我收到参数 p=1、q=0 的错误。 The error is in line for plotting the RSS value
该错误与绘制 RSS 值一致
TypeError Traceback (most recent call last)
TypeError Traceback(最近一次调用最后一次)
----> 6 plt.title('RSS: %.4f'% sum((results_ARIMA.fittedvalues-data_log_diff)**2))
----> 6 plt.title('RSS: %.4f'% sum((results_ARIMA.fittedvalues-data_log_diff)**2))
TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported.
TypeError:不再支持带时间戳的整数和整数数组的加法/减法。 Instead of adding/subtracting
n
, use `n * obj.freq而不是加/减
n
,使用 `n * obj.freq
How do I fix this error?如何修复此错误?
In ARIMA model in statsmodel, parameter order is (p,d,q) .your parameter order is wrong.Correct order is (1,0,1).在 statsmodel 的 ARIMA model 中,参数顺序为(p,d,q) 。您的参数顺序错误。正确的顺序为 (1,0,1)。
model = ARIMA(data_log, order=(1, 0, 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.