I have built ARIMA model for prediction for my time series data. 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. But my arima model doesn't work.
(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. Instead it works for p=1, q=0.
What is the problem in p=1, q=1 parameter values?
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. % 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)
what is the reason for this warning and how do i fix it?
I am getting an error for parameters p=1, q=0. The error is in line for plotting the RSS value
TypeError Traceback (most recent call last)
----> 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. Instead of adding/subtracting
n
, use `n * obj.freqHow do I fix this error?