0% found this document useful (0 votes)
55 views36 pages

Lecture 4 R and Time Series Analysis

The document discusses time series analysis using R software. It provides an overview of time series data and describes some commonly used R functions for time series analysis, including functions to identify patterns, fit models, analyze residuals, difference data, and generate forecasts.

Uploaded by

linconab93
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)
55 views36 pages

Lecture 4 R and Time Series Analysis

The document discusses time series analysis using R software. It provides an overview of time series data and describes some commonly used R functions for time series analysis, including functions to identify patterns, fit models, analyze residuals, difference data, and generate forecasts.

Uploaded by

linconab93
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/ 36

R and Time Series Analysis

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya

2022-11-29

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 1 / 18
Bibliography

Bartlett, M. S. 1946. “On the Theoretical Specification and Sampling


Properties of Autocorrelated Time Series.” Journal of the Royal
Statistical Society, Ser. B8 27:27–41.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 2 / 18
Bibliography

Bartlett, M. S. 1946. “On the Theoretical Specification and Sampling


Properties of Autocorrelated Time Series.” Journal of the Royal
Statistical Society, Ser. B8 27:27–41.
Box, G. E. P., and G. Jenkins. 1976. Time Series Analysis: Forecasting
and Control. Second. San Francisco: Holden Day.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 2 / 18
Bibliography

Bartlett, M. S. 1946. “On the Theoretical Specification and Sampling


Properties of Autocorrelated Time Series.” Journal of the Royal
Statistical Society, Ser. B8 27:27–41.
Box, G. E. P., and G. Jenkins. 1976. Time Series Analysis: Forecasting
and Control. Second. San Francisco: Holden Day.
Ljung, G. M., and G. E. P. Box. 1978. “On a Measure of Lack of Fit
in Time Series Models.” Biometrika 65:297–303.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 2 / 18
Bibliography

Bartlett, M. S. 1946. “On the Theoretical Specification and Sampling


Properties of Autocorrelated Time Series.” Journal of the Royal
Statistical Society, Ser. B8 27:27–41.
Box, G. E. P., and G. Jenkins. 1976. Time Series Analysis: Forecasting
and Control. Second. San Francisco: Holden Day.
Ljung, G. M., and G. E. P. Box. 1978. “On a Measure of Lack of Fit
in Time Series Models.” Biometrika 65:297–303.
Racine, J. S. 2019. Reproducible Econometrics using R. Oxford
University Press, New York.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 2 / 18
Introduction

Real time observations that are not survey datasets are mostly time
series nowadays.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 3 / 18
Introduction

Real time observations that are not survey datasets are mostly time
series nowadays.
These data are synchronized or sampled at different high and low
frequencies ranging from 5-minutely,. . . , daily, . . . , annually.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 3 / 18
Introduction

Real time observations that are not survey datasets are mostly time
series nowadays.
These data are synchronized or sampled at different high and low
frequencies ranging from 5-minutely,. . . , daily, . . . , annually.
R software allows for a robust time series analysis, with function
achieving different analyses in time domain and frequency domain.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 3 / 18
Introduction (cont’d)

Some of these function are:


The ts ( ) function casts a data vector as numeric. This is a very
important step. It sets the data as time series vector.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 4 / 18
Introduction (cont’d)

Some of these function are:


The ts ( ) function casts a data vector as numeric. This is a very
important step. It sets the data as time series vector.
plot( ) and many functions will automatically recognize time series
objects and plot them accordingly.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 4 / 18
Introduction (cont’d)

Some of these function are:


The ts ( ) function casts a data vector as numeric. This is a very
important step. It sets the data as time series vector.
plot( ) and many functions will automatically recognize time series
objects and plot them accordingly.
for a linear model, the function lm ( ) or dyn$lm ( ) is used but the
latter requires a dyn package in R.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 4 / 18
Some Useful R Functions for Time Series Analysis

During R installation, some basic functions are loaded/installed by


default while a lot of functions need to be loaded from optional
packages.

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 5 / 18
Some Useful R Functions for Time Series Analysis

During R installation, some basic functions are loaded/installed by


default while a lot of functions need to be loaded from optional
packages.
accuracy( ) : summary measures of the forecast accuracy

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 5 / 18
Some Useful R Functions for Time Series Analysis

During R installation, some basic functions are loaded/installed by


default while a lot of functions need to be loaded from optional
packages.
accuracy( ) : summary measures of the forecast accuracy
acf( ) : autocorrelation function

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 5 / 18
Some Useful R Functions for Time Series Analysis

During R installation, some basic functions are loaded/installed by


default while a lot of functions need to be loaded from optional
packages.
accuracy( ) : summary measures of the forecast accuracy
acf( ) : autocorrelation function
adf.test( ) : augmented Dickey-Fuller test

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 5 / 18
Some Useful R Functions for Time Series Analysis

During R installation, some basic functions are loaded/installed by


default while a lot of functions need to be loaded from optional
packages.
accuracy( ) : summary measures of the forecast accuracy
acf( ) : autocorrelation function
adf.test( ) : augmented Dickey-Fuller test
AIc( ) : Information Criterion

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 5 / 18
Some Useful R Functions for Time Series Analysis

During R installation, some basic functions are loaded/installed by


default while a lot of functions need to be loaded from optional
packages.
accuracy( ) : summary measures of the forecast accuracy
acf( ) : autocorrelation function
adf.test( ) : augmented Dickey-Fuller test
AIc( ) : Information Criterion
ar( ) : fit an autoregressive time series model

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 5 / 18
Some Useful R Functions for Time Series Analysis
(cont’d)

arima( ) : fit an ARIMA model

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 6 / 18
Some Useful R Functions for Time Series Analysis
(cont’d)

arima( ) : fit an ARIMA model


checkresiduals( ) : check residuals for white noise

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 6 / 18
Some Useful R Functions for Time Series Analysis
(cont’d)

arima( ) : fit an ARIMA model


checkresiduals( ) : check residuals for white noise
cycle( ) : gives the positions in the cycle (period) of each observation

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 6 / 18
Some Useful R Functions for Time Series Analysis
(cont’d)

arima( ) : fit an ARIMA model


checkresiduals( ) : check residuals for white noise
cycle( ) : gives the positions in the cycle (period) of each observation
diff( ) : difference the time series d time

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 6 / 18
Some Useful R Functions for Time Series Analysis
(cont’d)

arima( ) : fit an ARIMA model


checkresiduals( ) : check residuals for white noise
cycle( ) : gives the positions in the cycle (period) of each observation
diff( ) : difference the time series d time
partial autocorrelation function (stats)

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 6 / 18
Example 1: ts ( ), plot ( ) and autoplot ( ) functions
## Gold
## 1 289.30
## 2 302.70
## 3 299.40
## 4 301.20
## 5 306.90
## 6 293.20
## 7 297.20
## 8 286.70
## 9 275.80
## 10 297.20
## 11 292.80
## 12 293.60
## 13 288.50
## 14 286.40
## 15 287.30
## E.16Olubusoye,
Olusanya 280.30
Isaac Essi & OlaOluwa S. Yaya
R and Time Series Analysis 2022-11-29 7 / 18
ts (), plot () and autoplot () functions (cont’d)
## Jan Feb Mar Apr May Jun Ju
## 1997
## 1998 302.70 299.40 301.20 306.90 293.20 297.20 286.7
## 1999 286.40 287.30 280.30 286.80 270.80 261.25 256.2
## 2000 283.90 292.70 279.40 273.60 272.60 289.30 277.5
## 2001 266.55 267.40 258.20 263.80 266.10 271.10 267.1
## 2002 282.95 296.80 302.75 308.70 326.80 314.70 303.8
## 2003 368.15 349.95 336.55 338.55 364.85 345.45 355.3
## 2004 401.60 396.00 426.70 386.70 395.10 393.30 390.6
## 2005 422.10 435.50 427.80 434.50 416.80 435.00 429.3
## 2006 568.70 561.20 583.50 653.70 642.60 615.20 635.5
## 2007 652.70 669.10 663.20 677.70 660.60 649.10 663.5
## 2008 925.00 973.50 915.80 876.70 886.30 924.60 913.3
## 2009 926.50 941.60 917.40 885.50 978.57 926.35 953.5
## 2010 1080.67 1117.30 1112.89 1178.72 1216.00 1242.13 1180.5
## 2011 1332.43 1410.96 1431.83 1563.30 1535.37 1499.67 1626.7
## E.2012
Olusanya 1737.28
Olubusoye, 1696.48
Isaac Essi & OlaOluwa R 1667.56
S. Yayaand Time Series1664.46
Analysis 1560.422022-11-29
1597.07 1614.2
8 / 18
ts (), plot () and autoplot () functions (cont’d)
2000
1500
Gold

1000
500

2000 2005 2010 2015 2020

Time
Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya
R and Time Series Analysis 2022-11-29 9 / 18
ts (), plot () and autoplot () functions (cont’d)
2000

1500
US Dollar/Troy

1000

500

2000 2005 2010 2015 2020


Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya
R and Time Series
Date Analysis 2022-11-29 10 / 18
ts ( ), plot ( ) and autoplot ( ) functions (cont’d)
Figure 14: London Bullion Gold Price

2000

1500
US dollar/troy

1000

500

2000 2005 2010 2015 2020


Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya
R and Time Series
Year Analysis 2022-11-29 11 / 18
Example 2: diff( ) function
This is used to find the unit differences (integration/unit root) in time
series. for a nonstationary series, the series requires 1 or 2 unit differences
to make it stationary
Recall,

yt = (1 − B)d Xt
where yt is the resulting stationary series, and Xt is the (original)
nonstationary series. For a series requiring 1 unit difference, i.e. d = 1, then,
we have,

yt = (1 − B)1 Xt = (1 − B)Xt = Xt − BXt = Xt − Xt−1

Note, in the case of fractional or decimal value of d, this leads to fractional


unit root often known as fractional integration.
Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya
R and Time Series Analysis 2022-11-29 12 / 18
Example 2: diff( ) function (cont’d)
## Jan Feb Mar Apr May Jun Ju
## 1998 13.40 -3.30 1.80 5.70 -13.70 4.00 -10.5
## 1999 -2.10 0.90 -7.00 6.50 -16.00 -9.55 -5.0
## 2000 -5.10 8.80 -13.30 -5.80 -1.00 16.70 -11.8
## 2001 -5.95 0.85 -9.20 5.60 2.30 5.00 -4.0
## 2002 3.75 13.85 5.95 5.95 18.10 -12.10 -10.9
## 2003 20.10 -18.20 -13.40 2.00 26.30 -19.40 9.8
## 2004 -14.00 -5.60 30.70 -40.00 8.40 -1.80 -2.7
## 2005 -15.69 13.40 -7.70 6.70 -17.70 18.20 -5.7
## 2006 52.70 -7.50 22.30 70.20 -11.10 -27.40 20.3
## 2007 17.10 16.40 -5.90 14.50 -17.10 -11.50 14.4
## 2008 93.50 48.50 -57.70 -39.10 9.60 38.30 -11.3
## 2009 60.60 15.10 -24.20 -31.90 93.07 -52.22 27.1
## 2010 -17.63 36.63 -4.41 65.83 37.28 26.13 -61.5
## 2011 -85.76 78.53 20.87 131.47 -27.93 -35.70 127.1
## 2012 172.81 -40.80 -28.92 -3.10 -104.04 36.65 17.1
## E.2013
Olusanya 0.95
Olubusoye, Isaac -84.06
Essi & OlaOluwa R and17.24
S. Yaya Time Series-120.37
Analysis -88.712022-11-29
-153.50 1390.7
/ 18
Example 2: diff( ) function (cont’d)

Note, in diff (seriesname, 1), “1’ ’ is the lag difference, i.e. from xt to xt−1 .
if it is xt to xt−2 , it means it will be diff (seriesname, 2).
Thus, for first difference of a time series, it is usually diff (seriesname, 1).

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 14 / 18
Example 2: diff( ) function (cont’d)
200
100
Gold

0
-100
-200

2000 2005 2010 2015 2020

Time
Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya
R and Time Series Analysis 2022-11-29 15 / 18
Example 3: acf() function
Gold
1.0
0.8
0.6
ACF

0.4
0.2
0.0

0.0 0.5 1.0 1.5 2.0

Lag
Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya
R and Time Series Analysis 2022-11-29 16 / 18
Example 5: pacf() function
Series data3
0.15
0.10
0.05
Partial ACF

0.00
-0.05
-0.10
-0.15

0.5 1.0 1.5 2.0

Lag
Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya
R and Time Series Analysis 2022-11-29 17 / 18
Example 6: adf.test() function
The ADF (Augmented Dickey-Fuller) unit root testing in R requires the
library “tseries’ ’.
##
## Augmented Dickey-Fuller Test
##
## data: data3
## Dickey-Fuller = -5.595, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
Note, the ADF test has the null hypothesis, H0 that the series is
nonstationary (has a unit root) against the alternative, H1 that is it
stationary (no unit root).

Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya


R and Time Series Analysis 2022-11-29 18 / 18
Example 6: adf.test() function
The ADF (Augmented Dickey-Fuller) unit root testing in R requires the
library “tseries’ ’.
##
## Augmented Dickey-Fuller Test
##
## data: data3
## Dickey-Fuller = -5.595, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
Note, the ADF test has the null hypothesis, H0 that the series is
nonstationary (has a unit root) against the alternative, H1 that is it
stationary (no unit root).
Selected lag length is for the augmentation component. Such a lag
length is determined in the ADF regression based on minimum
information criterion used.
Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya
R and Time Series Analysis 2022-11-29 18 / 18
Example 6: adf.test() function
The ADF (Augmented Dickey-Fuller) unit root testing in R requires the
library “tseries’ ’.
##
## Augmented Dickey-Fuller Test
##
## data: data3
## Dickey-Fuller = -5.595, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
Note, the ADF test has the null hypothesis, H0 that the series is
nonstationary (has a unit root) against the alternative, H1 that is it
stationary (no unit root).
Selected lag length is for the augmentation component. Such a lag
length is determined in the ADF regression based on minimum
information criterion used.
β2022-11-29
The t-statistic value is obtained by computing t =
Olusanya E. Olubusoye, Isaac Essi & OlaOluwa S. Yaya
R and Time Series Analysis . 18 / 18

You might also like