0% found this document useful (0 votes)
141 views26 pages

A Complete Introduction To Time Series Analysis (With R) - SARIMA Models

The document is a tutorial on time series analysis and forecasting using R. It introduces key concepts in time series such as decomposition, differencing, autocorrelation and partial autocorrelation. It walks through an example of analyzing and building an ARIMA model for a time series dataset of monthly electronics equipment sales. Statistical tests and plots are used to identify the appropriate ARIMA model and check that the residuals exhibit white noise.

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views26 pages

A Complete Introduction To Time Series Analysis (With R) - SARIMA Models

The document is a tutorial on time series analysis and forecasting using R. It introduces key concepts in time series such as decomposition, differencing, autocorrelation and partial autocorrelation. It walks through an example of analyzing and building an ARIMA model for a time series dataset of monthly electronics equipment sales. Statistical tests and plots are used to identify the appropriate ARIMA model and check that the residuals exhibit white noise.

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

1 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

2 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

3 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

4 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

5 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

library(tidyverse)
library(tidyquant)
library(gridExtra)
library(tibbletime)
library(forecast)
library(itsmr)
library(here)
library(fpp2)
library(tseries)
knitr::opts_chunk$set(comment=NA,tidy=FALSE)

6 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

autoplot(elecequip)

sarima2.r hosted with by GitHub view raw

stl

elecequip_decomp = stl(elecequip,s.window=12) # estimate the seasonal component


elecequip_adjseason = elecequip - seasonal(elecequip_decomp) # deseason the data
autoplot(elecequip_adjseason) # plot

sarima2.r hosted with by GitHub view raw

7 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

ggAcf(elecequip_adjseason) # ACF
ggPacf(elecequip_adjseason) # PACF

sarima3.r hosted with by GitHub view raw

8 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

9 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

adf.test(elecequip_adjseason)
kpss.test(elecequip_adjseason)
#####################################################################
OUT:

Augmented Dickey-Fuller Test

data: elecequip_adjseason
Dickey-Fuller = -2.4971, Lag order = 5, p-value = 0.368
alternative hypothesis: stationary

KPSS Test for Level Stationarity

data: elecequip_adjseason

elecequip_adjseason_diff = diff(elecequip_adjseason,1) # difference once


autoplot(elecequip_adjseason_diff) # plot

sarima5.r hosted with by GitHub view raw

10 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

ggAcf(elecequip_adjseason_diff)
ggPacf(elecequip_adjseason_diff)

sarima6.r hosted with by GitHub view raw

11 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

12 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

adf.test(elecequip_adjseason_diff)
adf.test(elecequip_adjseason_diff,k=3)
kpss.test(elecequip_adjseason_diff)
###############################################################
OUT:

p-value smaller than printed p-value


Augmented Dickey-Fuller Test

data: elecequip_adjseason_diff
Dickey-Fuller = -4.0924, Lag order = 5, p-value = 0.01
alternative hypothesis: stationary

p-value smaller than printed p-value


Augmented Dickey-Fuller Test

data: elecequip_adjseason_diff
Dickey-Fuller = -5.4491, Lag order = 3, p-value = 0.01
alternative hypothesis: stationary

p-value greater than printed p-value


KPSS Test for Level Stationarity

auto.sarima

13 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

# Fit an ARIMA model using


elecequip_arima = auto.arima(elecequip_adjseason,
stepwise=FALSE,
approximation=FALSE,
seasonal=FALSE,
allowdrift=FALSE
)
summary(elecequip_arima)
##################################################################
OUT:

Series: elecequip_adjseason
ARIMA(3,1,1)

Coefficients:
ar1 ar2 ar3 ma1
0.0313 0.1025 0.3674 -0.3875
s.e. 0.2041 0.0884 0.0670 0.2235

sigma^2 estimated as 8.439: log likelihood=-480.41


AIC=970.82 AICc=971.14 BIC=987.16

autoplot(residuals(elecequip_arima)) # plot the residuals


plot(elecequip_arima) # inspect the roots
ggAcf(residuals(elecequip_arima),lag.max=36) # inspect the ACF
ggPacf(residuals(elecequip_arima),lag.max=36) # inspect the PACF

sarima9.r hosted with by GitHub view raw

14 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

15 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

16 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

adf.test(residuals(elecequip_arima))
kpss.test(residuals(elecequip_arima))
#################################################################
OUT:

p-value smaller than printed p-value


Augmented Dickey-Fuller Test

data: residuals(elecequip_arima)
Dickey-Fuller = -5.0567, Lag order = 5, p-value = 0.01
alternative hypothesis: stationary

p-value greater than printed p-value


KPSS Test for Level Stationarity

checkresiduals(elecequip_arima)
####################################################################
OUT:

Ljung-Box test

data: Residuals from ARIMA(3,1,1)


Q* = 30.171, df = 20, p-value = 0.06713

Model df: 4. Total lags used: 24

17 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

seasonal

auto.arima TRUE

approximation TRUE

parallel=True

18 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

elecequip_sarima = auto.arima(elecequip,
stepwise=FALSE,
approximation=FALSE,
seasonal=TRUE, # This will extent to SARIMA
allowdrift=FALSE,
# parallel = TRUE, # speeds up computation, but tracing not a
trace=TRUE)
summary(elecequip_sarima)
############################################################################################
OUT:

ARIMA(0,0,0)(0,1,0)[12] : 1398.971
ARIMA(0,0,0)(0,1,1)[12] : 1398.3
ARIMA(0,0,0)(0,1,2)[12] : 1373.953
ARIMA(0,0,0)(1,1,0)[12] : 1399.965
ARIMA(0,0,0)(1,1,1)[12] : Inf
ARIMA(0,0,0)(1,1,2)[12] : Inf
ARIMA(0,0,0)(2,1,0)[12] : 1384.654
ARIMA(0,0,0)(2,1,1)[12] : Inf
ARIMA(0,0,0)(2,1,2)[12] : Inf
ARIMA(0,0,1)(0,1,0)[12] : 1251.574
ARIMA(0,0,1)(0,1,1)[12] : 1242.284
ARIMA(0,0,1)(0,1,2)[12] : Inf
ARIMA(0,0,1)(1,1,0)[12] : 1249.068
ARIMA(0,0,1)(1,1,1)[12] : Inf
ARIMA(0,0,1)(1,1,2)[12] : Inf
ARIMA(0,0,1)(2,1,0)[12] : 1237.455
ARIMA(0,0,1)(2,1,1)[12] : Inf
ARIMA(0,0,1)(2,1,2)[12] : Inf
ARIMA(0,0,2)(0,1,0)[12] : 1199.664
ARIMA(0,0,2)(0,1,1)[12] : 1186.702
ARIMA(0,0,2)(0,1,2)[12] : Inf
ARIMA(0,0,2)(1,1,0)[12] : 1195.824
ARIMA(0,0,2)(1,1,1)[12] : Inf
ARIMA(0,0,2)(1,1,2)[12] : Inf
ARIMA(0,0,2)(2,1,0)[12] : 1182.585
ARIMA(0,0,2)(2,1,1)[12] : Inf
ARIMA(0,0,3)(0,1,0)[12] : 1132.348
ARIMA(0,0,3)(0,1,1)[12] : 1113.232
ARIMA(0,0,3)(0,1,2)[12] : Inf
ARIMA(0,0,3)(1,1,0)[12] : 1126.601
ARIMA(0,0,3)(1,1,1)[12] : Inf

19 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

ARIMA(0,0,4)(1,1,0)[12] : 1105.068
ARIMA(0,0,5)(0,1,0)[12] : 1085.54
ARIMA(1,0,0)(0,1,0)[12] : 1070.102
ARIMA(1,0,0)(0,1,1)[12] : 1020.092
ARIMA(1,0,0)(0,1,2)[12] : Inf
ARIMA(1,0,0)(1,1,0)[12] : 1046.619
ARIMA(1,0,0)(1,1,1)[12] : Inf
ARIMA(1,0,0)(1,1,2)[12] : Inf
ARIMA(1,0,0)(2,1,0)[12] : 1033.071
ARIMA(1,0,0)(2,1,1)[12] : Inf
ARIMA(1,0,0)(2,1,2)[12] : Inf
ARIMA(1,0,1)(0,1,0)[12] : 1063.915
ARIMA(1,0,1)(0,1,1)[12] : 1005.31
ARIMA(1,0,1)(0,1,2)[12] : Inf
ARIMA(1,0,1)(1,1,0)[12] : 1037.745
ARIMA(1,0,1)(1,1,1)[12] : Inf
ARIMA(1,0,1)(1,1,2)[12] : Inf
ARIMA(1,0,1)(2,1,0)[12] : 1021.917
ARIMA(1,0,1)(2,1,1)[12] : Inf
ARIMA(1,0,2)(0,1,0)[12] : 1059.197
ARIMA(1,0,2)(0,1,1)[12] : 998.7203
ARIMA(1,0,2)(0,1,2)[12] : Inf
ARIMA(1,0,2)(1,1,0)[12] : 1030.779
ARIMA(1,0,2)(1,1,1)[12] : Inf
ARIMA(1,0,2)(2,1,0)[12] : 1016.242
ARIMA(1,0,3)(0,1,0)[12] : 1052.495
ARIMA(1,0,3)(0,1,1)[12] : 990.9978
ARIMA(1,0,3)(1,1,0)[12] : 1022.184
ARIMA(1,0,4)(0,1,0)[12] : 1054.615
ARIMA(2,0,0)(0,1,0)[12] : 1062.174
ARIMA(2,0,0)(0,1,1)[12] : 1004.646
ARIMA(2,0,0)(0,1,2)[12] : Inf
ARIMA(2,0,0)(1,1,0)[12] : 1035.879
ARIMA(2,0,0)(1,1,1)[12] : Inf
ARIMA(2,0,0)(1,1,2)[12] : Inf
ARIMA(2,0,0)(2,1,0)[12] : 1020.245
ARIMA(2,0,0)(2,1,1)[12] : Inf
ARIMA(2,0,1)(0,1,0)[12] : 1064.261
ARIMA(2,0,1)(0,1,1)[12] : 1005.587
ARIMA(2,0,1)(0,1,2)[12] : Inf

20 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

ARIMA(3,0,0)(0,1,0)[12] : 1064.252
ARIMA(3,0,0)(0,1,1)[12] : 1002.743
ARIMA(3,0,0)(0,1,2)[12] : Inf
ARIMA(3,0,0)(1,1,0)[12] : 1037.743
ARIMA(3,0,0)(1,1,1)[12] : Inf
ARIMA(3,0,0)(2,1,0)[12] : 1021.545
ARIMA(3,0,1)(0,1,0)[12] : 1058.647
ARIMA(3,0,1)(0,1,1)[12] : 997.4304
ARIMA(3,0,1)(1,1,0)[12] : 1035.26
ARIMA(3,0,2)(0,1,0)[12] : Inf
ARIMA(4,0,0)(0,1,0)[12] : 1050.608
ARIMA(4,0,0)(0,1,1)[12] : 983.3669
105 ARIMA(4,0,0)(1,1,0)[12] : 1018.572

autoplot(residuals(elecequip_sarima))
ggAcf(residuals(elecequip_sarima),lag=36)
ggPacf(residuals(elecequip_sarima),lag=36)

sarima13.r hosted with by GitHub view raw

21 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

22 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

23 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

# correctly specify the series


elecequip_ts <- ts(elecequip, frequency=12, start=c(1996, 1), end = c(2012,3))

# split the data into train and test


elecequip_train <- window(elecequip_ts, end=c(2010,12))
elecequip_test <- window(elecequip_ts, start=c(2011,1))

# train sarima model


elecequip_sarima2 = auto.arima(elecequip_train,
stepwise=FALSE,
approximation=FALSE,
seasonal=TRUE, # This will extent to SARIMA
allowdrift=FALSE,
parallel = TRUE, # speeds up computation, but tracing not avai
trace=FALSE)

## produce forecasts
myforecasts <- forecast::forecast(elecequip_sarima2, h=40)

## plot the forecasts


autoplot(myforecasts) + autolayer(elecequip_ts) + xlim(2005,2013)

24 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

25 of 26 7/5/2021, 2:23 PM
A Complete Introduction To Time Series Analysis (with R)::... https://medium.com/analytics-vidhya/a-complete-introduction...

26 of 26 7/5/2021, 2:23 PM

You might also like