Introduction to Time series modelling

common time series models AR,MA,ARMA,ARIMA,SARIMA,SARIMAX

Session 8

#Analyze and vualize time series data #Load and visualize time series data

Simulating a time series

Time Series:
Start = 1 
End = 6 
Frequency = 1 
[1] 0.000000 3.782260 4.970523 4.869208 4.601592 5.063738

###convert to time series

          Jan      Feb      Mar      Apr      May      Jun
2000 0.000000 3.782260 4.970523 4.869208 4.601592 5.063738

###plot the time series

###Check stationarity


    Augmented Dickey-Fuller Test

data:  ts_data
Dickey-Fuller = -2.3566, Lag order = 4, p-value = 0.4289
alternative hypothesis: stationary

###Take the First difference and test for stationarity


    Augmented Dickey-Fuller Test

data:  diff_data
Dickey-Fuller = -5.1891, Lag order = 4, p-value = 0.01
alternative hypothesis: stationary

Plot the ACF and PACF

Fit ARIMA

Series: ts_data 
ARIMA(2,1,0) 

Coefficients:
         ar1      ar2
      0.8628  -0.2869
s.e.  0.0992   0.0991

sigma^2 = 1.075:  log likelihood = -144.86
AIC=295.72   AICc=295.97   BIC=303.54

Training set error measures:
                       ME     RMSE       MAE      MPE    MAPE      MASE
Training set -0.004501306 1.021084 0.8020696 1.423103 28.8683 0.1455238
                    ACF1
Training set -0.04999451

Manual estimation of ARIMA


Call:
arima(x = ts_data, order = c(2, 1, 2))

Coefficients:
        ar1      ar2      ma1      ma2
      1.485  -0.5173  -0.6416  -0.3584
s.e.  0.121   0.1227   0.1339   0.1300

sigma^2 estimated as 1.02:  log likelihood = -144.03,  aic = 298.06

Training set error measures:
                      ME     RMSE       MAE      MPE    MAPE      MASE
Training set -0.02307641 1.005127 0.7853738 5.009873 32.5992 0.7063212
                    ACF1
Training set -0.05670285


    Ljung-Box test

data:  Residuals from ARIMA(2,1,0)
Q* = 20.049, df = 18, p-value = 0.3301

Model df: 2.   Total lags used: 20

Forecasting

$method
[1] "ARIMA(2,1,0)"

$model
Series: ts_data 
ARIMA(2,1,0) 

Coefficients:
         ar1      ar2
      0.8628  -0.2869
s.e.  0.0992   0.0991

sigma^2 = 1.075:  log likelihood = -144.86
AIC=295.72   AICc=295.97   BIC=303.54

$level
[1] 80 95

$mean
            Jan        Feb        Mar        Apr        May        Jun
2008                                                        -0.3245634
2009 -0.3421972 -0.3418831 -0.3417120 -0.3416545 -0.3416539           
            Jul        Aug        Sep        Oct        Nov        Dec
2008 -0.3226548 -0.3301819 -0.3372235 -0.3411392 -0.3424971 -0.3425452
2009                                                                  

$lower
                80%        95%
Jun 2008  -1.653013  -2.356252
Jul 2008  -3.131262  -4.618050
Aug 2008  -4.500140  -6.707583
Sep 2008  -5.641174  -8.448916
Oct 2008  -6.573136  -9.872155
Nov 2008  -7.353071 -11.064244
Dec 2008  -8.032384 -12.103138
Jan 2009  -8.645953 -13.041696
Feb 2009  -9.214291 -13.911060
Mar 2009  -9.748626 -14.728346
Apr 2009 -10.255177 -15.503079
May 2009 -10.737807 -16.241199

$upper
               80%       95%
Jun 2008  1.003886  1.707125
Jul 2008  2.485953  3.972740
Aug 2008  3.839776  6.047219
Sep 2008  4.966727  7.774469
Oct 2008  5.890857  9.189877
Nov 2008  6.668077 10.379250
Dec 2008  7.347293 11.418048
Jan 2009  7.961559 12.357301
Feb 2009  8.530525 13.227294
Mar 2009  9.065202 14.044922
Apr 2009  9.571868 14.819770
May 2009 10.054500 15.557891

Plot forecasted values