-1

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:

  1. 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?

  2. 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?

  3. 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.freq

    How do I fix this error?

James Z
  • 12,209
  • 10
  • 24
  • 44
Pavan
  • 381
  • 1
  • 4
  • 19

1 Answers1

0

In ARIMA model in statsmodel, parameter order is (p,d,q).your parameter order is wrong.Correct order is (1,0,1).

model = ARIMA(data_log, order=(1, 0, 1))
Rajith Thennakoon
  • 3,975
  • 2
  • 14
  • 24
  • Parameter d has to be 0 right because I have to difference it once to make the series stationary, so order is 1,1,1 right – Pavan Apr 21 '20 at 02:47
  • yes,you can make the difference and input data with d=0 or make let the algorithm do the difference with d=1. – Rajith Thennakoon Apr 21 '20 at 02:49
  • So if my input original data (log transform) then d =1. If you observe my code my input is my original data not the differenced one.. so d =1 is right...the problem is with the q value..I am getting error when my q value is 1 – Pavan Apr 21 '20 at 08:13
  • try to set `start_ar_lags` parameter in ARIMA model – Rajith Thennakoon Apr 21 '20 at 15:06
  • How do I obtain best value for start_ar_lags – Pavan Apr 22 '20 at 02:21