0% found this document useful (0 votes)
160 views24 pages

DataCamp - ForECASTING USING R - Dynamic Regression

The document discusses dynamic regression models and harmonic regression models for time series forecasting in R. Dynamic regression allows the errors of a regression model to follow an ARIMA process. Harmonic regression uses Fourier terms of sine and cosine waves to model periodic seasonality in a time series. The TBATS model is also discussed as an automated forecasting method in R that handles various features like transformations, trends, and multiple seasonal periods. Examples on consumption and income data, cafe sales data, and gasoline data are presented to illustrate the methods.

Uploaded by

jomasool
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)
160 views24 pages

DataCamp - ForECASTING USING R - Dynamic Regression

The document discusses dynamic regression models and harmonic regression models for time series forecasting in R. Dynamic regression allows the errors of a regression model to follow an ARIMA process. Harmonic regression uses Fourier terms of sine and cosine waves to model periodic seasonality in a time series. The TBATS model is also discussed as an automated forecasting method in R that handles various features like transformations, trends, and multiple seasonal periods. Examples on consumption and income data, cafe sales data, and gasoline data are presented to illustrate the methods.

Uploaded by

jomasool
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/ 24

FORECASTING USING R

Dynamic
regression

Rob Hyndman
Author, forecast
Forecasting Using R

Dynamic regression
Regression model with ARIMA errors

yt = β0 + β1 x1,t + · · · + βr xr ,t + et

● yt modeled as function of r explanatory variables x1,t , ... , xr ,t

● In dynamic regression, we allow et to be an ARIMA process

● In ordinary regression, we assume that et is white noise


Forecasting Using R

US personal consumption and income


> autoplot(uschange[,1:2], facets = TRUE) +
xlab("Year") + ylab("") +
ggtitle("Quarterly changes in US consumption
and personal income")
Forecasting Using R

US personal consumption and income


> ggplot(aes(x = Income, y = Consumption),
data = as.data.frame(uschange)) +
geom_point() +
ggtitle("Quarterly changes in US consumption and
personal income")
Forecasting Using R

Dynamic regression model for US personal consumption

> fit <- auto.arima(uschange[,"Consumption"],


xreg = uschange[,"Income"])
> fit

Series: uschange[, "Consumption"]


Regression with ARIMA(1,0,2) errors

Coefficients:
ar1 ma1 ma2 intercept origxreg
0.6191 -0.5424 0.2367 0.6099 0.2492
s.e. 0.1422 0.1475 0.0685 0.0777 0.0459

sigma^2 estimated as 0.334: log likelihood=-195.22


AIC=402.44 AICc=402.82 BIC=422.99
Forecasting Using R

Residuals from dynamic regression model


> checkresiduals(fit)
Ljung-Box test
data: residuals
Q* = 5.5543, df = 3, p-value = 0.1354
Model df: 5. Total lags used: 8
Forecasting Using R

Forecasts from dynamic regression model


> fcast <- forecast(fit, xreg = rep(0.8, 8))
> autoplot(fcast) +
xlab("Year") + ylab("Percentage change")
FORECASTING USING R

Let’s practice!
FORECASTING USING R

Dynamic harmonic
regression
Forecasting Using R

Dynamic harmonic regression


Periodic seasonality can be handled using pairs of Fourier terms:
! " ! "
2πkt 2πkt
sk (t) = sin ck (t) = cos
m m

!
K
yt = β0 + [αk sk (t) + γk ck (t)] + et
k=1

● m = seasonal period
● Every periodic function can be approximated by sums of sin and cos terms
for large enough K

● Regression coefficients: αk and γk

● et can be modeled as a non-seasonal ARIMA process

● Assumes seasonal pa"ern is unchanging


Forecasting Using R

Example: Australian cafe expenditure


> fit <- auto.arima(cafe, xreg = fourier(cafe, K = 1),
seasonal = FALSE, lambda = 0)
> fit %>% forecast(xreg = fourier(cafe, K = 1, h = 24)) %>%
autoplot() + ylim(1.6, 5.1)
Forecasting Using R

Example: Australian cafe expenditure


> fit <- auto.arima(cafe, xreg = fourier(cafe, K = 2),
seasonal = FALSE, lambda = 0)
> fit %>% forecast(xreg = fourier(cafe, K = 2, h = 24)) %>%
autoplot() + ylim(1.6, 5.1)
Forecasting Using R

Example: Australian cafe expenditure


> fit <- auto.arima(cafe, xreg = fourier(cafe, K = 3),
seasonal = FALSE, lambda = 0)
> fit %>% forecast(xreg = fourier(cafe, K = 3, h = 24)) %>%
autoplot() + ylim(1.6, 5.1)
Forecasting Using R

Example: Australian cafe expenditure


> fit <- auto.arima(cafe, xreg = fourier(cafe, K = 4),
seasonal = FALSE, lambda = 0)
> fit %>% forecast(xreg = fourier(cafe, K = 4, h = 24)) %>%
autoplot() + ylim(1.6, 5.1)
Forecasting Using R

Example: Australian cafe expenditure


> fit <- auto.arima(cafe, xreg = fourier(cafe, K = 5),
seasonal = FALSE, lambda = 0)
> fit %>% forecast(xreg = fourier(cafe, K = 5, h = 24)) %>%
autoplot() + ylim(1.6, 5.1)
Forecasting Using R

Example: Australian cafe expenditure


> fit <- auto.arima(cafe, xreg = fourier(cafe, K = 6),
seasonal = FALSE, lambda = 0)
> fit %>% forecast(xreg = fourier(cafe, K = 6, h = 24)) %>%
autoplot() + ylim(1.6, 5.1)
Forecasting Using R

Dynamic harmonic regression


!
K
yt = β0 + β1 xt,1 + · · · + βt,r xt,r + [αk sk (t) + γk ck (t)] + et
k=1

● Other predictor variables can be added as well: xt,1 , ... , xt,r


● Choose K to minimize the AICc

● K can not be more than m/2

● This is particularly useful for weekly data, daily data and sub-daily
data
data.
FORECASTING USING R

Let’s practice!
FORECASTING USING R

TBATS models
Forecasting Using R

TBATS model
● Trigonometric terms for seasonality

● Box-Cox transformations for heterogeneity

● ARMA errors for short-term dynamics

● Trend (possibly damped)

● Seasonal (including multiple and non-integer periods)


Forecasting Using R

US Gasoline data
> gasoline %>% tbats() %>% forecast() %>%
autoplot() +
xlab("Year") + ylab("thousand barrels per day")
Forecasting Using R

Call center data


> calls %>% window(start = 20) %>%
tbats() %>% forecast() %>%
autoplot() + xlab("Weeks") + ylab("Calls")
Forecasting Using R

TBATS model
● Trigonometric terms for seasonality

● Box-Cox transformations for heterogeneity

● ARMA errors for short-term dynamics

● Trend (possibly damped)

● Seasonal (including multiple and non-integer periods)


● Handles non-integer seasonality, multiple seasonal periods

● Entirely automated

● Prediction intervals o#en too wide

● Very slow on long series


FORECASTING USING R

Let’s practice!

You might also like