Forecasting using R - Rob J. HyndmanOutline 1Vector autoregressions 2Time series with complex...

Post on 21-Jun-2020

3 views 0 download

Transcript of Forecasting using R - Rob J. HyndmanOutline 1Vector autoregressions 2Time series with complex...

Forecasting using R 1

Forecasting using R

Rob J Hyndman

3.4 Advanced methods

Outline

1 Vector autoregressions

2 Time series with complex seasonality

3 Lab session 17

4 Neural network models

5 Lab session 18

6 Lab session 19

Forecasting using R Vector autoregressions 2

Vector autoregressions

Dynamic regression assumes a unidirectionalrelationship: forecast variable influenced by predictorvariables, but not vice versa.Vector AR allow for feedback relationships. All variablestreated symmetrically.i.e., all variables are now treated as “endogenous”.

Personal consumption may be affected by disposableincome, and vice-versa.e.g., Govt stimulus package in Dec 2008 increasedChristmas spending which increased incomes.

Forecasting using R Vector autoregressions 3

Vector autoregressions

Dynamic regression assumes a unidirectionalrelationship: forecast variable influenced by predictorvariables, but not vice versa.Vector AR allow for feedback relationships. All variablestreated symmetrically.i.e., all variables are now treated as “endogenous”.

Personal consumption may be affected by disposableincome, and vice-versa.e.g., Govt stimulus package in Dec 2008 increasedChristmas spending which increased incomes.

Forecasting using R Vector autoregressions 3

Vector autoregressionsVAR(1)

y1,t = c1 + φ11,1y1,t−1 + φ12,1y2,t−1 + e1,ty2,t = c2 + φ21,1y1,t−1 + φ22,1y2,t−1 + e2,t

Forecasts:

y1,T+1|T = c1 + φ11,1y1,T + φ12,1y2,Ty2,T+1|T = c2 + φ21,1y1,T + φ22,1y2,T.

y1,T+2|T = c1 + φ11,1y1,T+1 + φ12,1y2,T+1

y2,T+2|T = c2 + φ21,1y1,T+1 + φ22,1y2,T+1.

Forecasting using R Vector autoregressions 4

Vector autoregressionsVAR(1)

y1,t = c1 + φ11,1y1,t−1 + φ12,1y2,t−1 + e1,ty2,t = c2 + φ21,1y1,t−1 + φ22,1y2,t−1 + e2,t

Forecasts:

y1,T+1|T = c1 + φ11,1y1,T + φ12,1y2,Ty2,T+1|T = c2 + φ21,1y1,T + φ22,1y2,T.

y1,T+2|T = c1 + φ11,1y1,T+1 + φ12,1y2,T+1

y2,T+2|T = c2 + φ21,1y1,T+1 + φ22,1y2,T+1.

Forecasting using R Vector autoregressions 4

VARs are useful when

forecasting a collection of related variables where noexplicit interpretation is required;testing whether one variable is useful in forecastinganother (the basis of Granger causality tests);impulse response analysis, where the response ofone variable to a sudden but temporary change inanother variable is analysed;forecast error variance decomposition, where theproportion of the forecast variance of one variable isattributed to the effect of other variables.

Forecasting using R Vector autoregressions 5

VARs are useful when

forecasting a collection of related variables where noexplicit interpretation is required;testing whether one variable is useful in forecastinganother (the basis of Granger causality tests);impulse response analysis, where the response ofone variable to a sudden but temporary change inanother variable is analysed;forecast error variance decomposition, where theproportion of the forecast variance of one variable isattributed to the effect of other variables.

Forecasting using R Vector autoregressions 5

VARs are useful when

forecasting a collection of related variables where noexplicit interpretation is required;testing whether one variable is useful in forecastinganother (the basis of Granger causality tests);impulse response analysis, where the response ofone variable to a sudden but temporary change inanother variable is analysed;forecast error variance decomposition, where theproportion of the forecast variance of one variable isattributed to the effect of other variables.

Forecasting using R Vector autoregressions 5

VARs are useful when

forecasting a collection of related variables where noexplicit interpretation is required;testing whether one variable is useful in forecastinganother (the basis of Granger causality tests);impulse response analysis, where the response ofone variable to a sudden but temporary change inanother variable is analysed;forecast error variance decomposition, where theproportion of the forecast variance of one variable isattributed to the effect of other variables.

Forecasting using R Vector autoregressions 5

VAR example> ar(usconsumption,order=3)$ar, , 1 consumption incomeconsumption 0.222 0.0424income 0.475 -0.2390

, , 2 consumption incomeconsumption 0.2001 -0.0977income 0.0288 -0.1097

, , 3 consumption incomeconsumption 0.235 -0.0238income 0.406 -0.0923

$var.predconsumption income

consumption 0.393 0.193income 0.193 0.735

Forecasting using R Vector autoregressions 6

VAR example

> library(vars)> VARselect(usconsumption, lag.max=8, type="const")$selectionAIC(n) HQ(n) SC(n) FPE(n)

5 1 1 5> var <- VAR(usconsumption, p=3, type="const")> serial.test(var, lags.pt=10, type="PT.asymptotic")Portmanteau Test (asymptotic)data: Residuals of VAR object varChi-squared = 33.3837, df = 28, p-value = 0.2219

Forecasting using R Vector autoregressions 7

VAR example> summary(var)VAR Estimation Results:=========================Endogenous variables: consumption, incomeDeterministic variables: constSample size: 161

Estimation results for equation consumption:============================================

Estimate Std. Error t value Pr(>|t|)consumption.l1 0.22280 0.08580 2.597 0.010326 *income.l1 0.04037 0.06230 0.648 0.518003consumption.l2 0.20142 0.09000 2.238 0.026650 *income.l2 -0.09830 0.06411 -1.533 0.127267consumption.l3 0.23512 0.08824 2.665 0.008530 **income.l3 -0.02416 0.06139 -0.394 0.694427const 0.31972 0.09119 3.506 0.000596 ***

Forecasting using R Vector autoregressions 8

VAR exampleEstimation results for equation income:=======================================

Estimate Std. Error t value Pr(>|t|)consumption.l1 0.48705 0.11637 4.186 4.77e-05 ***income.l1 -0.24881 0.08450 -2.945 0.003736 **consumption.l2 0.03222 0.12206 0.264 0.792135income.l2 -0.11112 0.08695 -1.278 0.203170consumption.l3 0.40297 0.11967 3.367 0.000959 ***income.l3 -0.09150 0.08326 -1.099 0.273484const 0.36280 0.12368 2.933 0.003865 **---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation matrix of residuals:consumption income

consumption 1.0000 0.3639income 0.3639 1.0000

Forecasting using R Vector autoregressions 9

VAR example

fcst <- forecast(var)plot(fcst, xlab="Year")

Forecasting using R Vector autoregressions 10

VAR example

Forecasting using R Vector autoregressions 11

cons

umpt

ion

−2

−1

01

2

inco

me

−2

01

23

4

1970 1980 1990 2000 2010

Year

Forecasts from VAR(3)

Outline

1 Vector autoregressions

2 Time series with complex seasonality

3 Lab session 17

4 Neural network models

5 Lab session 18

6 Lab session 19

Forecasting using R Time series with complex seasonality 12

Examples

Forecasting using R Time series with complex seasonality 13

7

8

9

10

1995 2000 2005Year

Tho

usan

ds o

f bar

rels

per

day

US finished motor gasoline products

Examples

Forecasting using R Time series with complex seasonality 13

0

100

200

300

400

500

1 2 3 4 5 6 7 8 9 10 11 12 13Week

Num

ber

of c

alls

in 5

min

ute

inte

rval

Number of calls to large American bank (7am...9pm)

Examples

Forecasting using R Time series with complex seasonality 13

10000

15000

20000

25000

2000 2002 2004 2006 2008 2010Year

Ele

ctric

ity d

eman

d (M

W)

Turkish daily electricity demand

TBATS model

TBATSTrigonometric terms for seasonalityBox-Cox transformations for heterogeneityARMA errors for short-term dynamicsTrend (possibly damped)Seasonal (including multiple and

non-integer periods)

Forecasting using R Time series with complex seasonality 14

TBATS model

yt = observation at time t

y(ω)t =

{(yωt − 1)/ω if ω 6= 0;log yt if ω = 0.

y(ω)t = `t−1 + φbt−1 +M∑i=1

s(i)t−mi+ dt

`t = `t−1 + φbt−1 + αdtbt = (1− φ)b+ φbt−1 + βdt

dt =p∑

i=1

φidt−i +

q∑j=1

θjεt−j + εt

s(i)t =

ki∑j=1

s(i)j,t

Forecasting using R Time series with complex seasonality 15

s(i)j,t = s(i)j,t−1 cosλ(i)j + s∗(i)j,t−1 sinλ

(i)j + γ

(i)1 dt

s(i)j,t = −s(i)j,t−1 sinλ

(i)j + s∗(i)j,t−1 cosλ

(i)j + γ

(i)2 dt

TBATS model

yt = observation at time t

y(ω)t =

{(yωt − 1)/ω if ω 6= 0;log yt if ω = 0.

y(ω)t = `t−1 + φbt−1 +M∑i=1

s(i)t−mi+ dt

`t = `t−1 + φbt−1 + αdtbt = (1− φ)b+ φbt−1 + βdt

dt =p∑

i=1

φidt−i +

q∑j=1

θjεt−j + εt

s(i)t =

ki∑j=1

s(i)j,t

Forecasting using R Time series with complex seasonality 15

s(i)j,t = s(i)j,t−1 cosλ(i)j + s∗(i)j,t−1 sinλ

(i)j + γ

(i)1 dt

s(i)j,t = −s(i)j,t−1 sinλ

(i)j + s∗(i)j,t−1 cosλ

(i)j + γ

(i)2 dt

Box-Cox transformation

TBATS model

yt = observation at time t

y(ω)t =

{(yωt − 1)/ω if ω 6= 0;log yt if ω = 0.

y(ω)t = `t−1 + φbt−1 +M∑i=1

s(i)t−mi+ dt

`t = `t−1 + φbt−1 + αdtbt = (1− φ)b+ φbt−1 + βdt

dt =p∑

i=1

φidt−i +

q∑j=1

θjεt−j + εt

s(i)t =

ki∑j=1

s(i)j,t

Forecasting using R Time series with complex seasonality 15

s(i)j,t = s(i)j,t−1 cosλ(i)j + s∗(i)j,t−1 sinλ

(i)j + γ

(i)1 dt

s(i)j,t = −s(i)j,t−1 sinλ

(i)j + s∗(i)j,t−1 cosλ

(i)j + γ

(i)2 dt

Box-Cox transformation

M seasonal periods

TBATS model

yt = observation at time t

y(ω)t =

{(yωt − 1)/ω if ω 6= 0;log yt if ω = 0.

y(ω)t = `t−1 + φbt−1 +M∑i=1

s(i)t−mi+ dt

`t = `t−1 + φbt−1 + αdtbt = (1− φ)b+ φbt−1 + βdt

dt =p∑

i=1

φidt−i +

q∑j=1

θjεt−j + εt

s(i)t =

ki∑j=1

s(i)j,t

Forecasting using R Time series with complex seasonality 15

s(i)j,t = s(i)j,t−1 cosλ(i)j + s∗(i)j,t−1 sinλ

(i)j + γ

(i)1 dt

s(i)j,t = −s(i)j,t−1 sinλ

(i)j + s∗(i)j,t−1 cosλ

(i)j + γ

(i)2 dt

Box-Cox transformation

M seasonal periods

global and local trend

TBATS model

yt = observation at time t

y(ω)t =

{(yωt − 1)/ω if ω 6= 0;log yt if ω = 0.

y(ω)t = `t−1 + φbt−1 +M∑i=1

s(i)t−mi+ dt

`t = `t−1 + φbt−1 + αdtbt = (1− φ)b+ φbt−1 + βdt

dt =p∑

i=1

φidt−i +

q∑j=1

θjεt−j + εt

s(i)t =

ki∑j=1

s(i)j,t

Forecasting using R Time series with complex seasonality 15

s(i)j,t = s(i)j,t−1 cosλ(i)j + s∗(i)j,t−1 sinλ

(i)j + γ

(i)1 dt

s(i)j,t = −s(i)j,t−1 sinλ

(i)j + s∗(i)j,t−1 cosλ

(i)j + γ

(i)2 dt

Box-Cox transformation

M seasonal periods

global and local trend

ARMA error

TBATS model

yt = observation at time t

y(ω)t =

{(yωt − 1)/ω if ω 6= 0;log yt if ω = 0.

y(ω)t = `t−1 + φbt−1 +M∑i=1

s(i)t−mi+ dt

`t = `t−1 + φbt−1 + αdtbt = (1− φ)b+ φbt−1 + βdt

dt =p∑

i=1

φidt−i +

q∑j=1

θjεt−j + εt

s(i)t =

ki∑j=1

s(i)j,t

Forecasting using R Time series with complex seasonality 15

s(i)j,t = s(i)j,t−1 cosλ(i)j + s∗(i)j,t−1 sinλ

(i)j + γ

(i)1 dt

s(i)j,t = −s(i)j,t−1 sinλ

(i)j + s∗(i)j,t−1 cosλ

(i)j + γ

(i)2 dt

Box-Cox transformation

M seasonal periods

global and local trend

ARMA error

Fourier-like seasonal terms

TBATS model

yt = observation at time t

y(ω)t =

{(yωt − 1)/ω if ω 6= 0;log yt if ω = 0.

y(ω)t = `t−1 + φbt−1 +M∑i=1

s(i)t−mi+ dt

`t = `t−1 + φbt−1 + αdtbt = (1− φ)b+ φbt−1 + βdt

dt =p∑

i=1

φidt−i +

q∑j=1

θjεt−j + εt

s(i)t =

ki∑j=1

s(i)j,t

Forecasting using R Time series with complex seasonality 15

s(i)j,t = s(i)j,t−1 cosλ(i)j + s∗(i)j,t−1 sinλ

(i)j + γ

(i)1 dt

s(i)j,t = −s(i)j,t−1 sinλ

(i)j + s∗(i)j,t−1 cosλ

(i)j + γ

(i)2 dt

Box-Cox transformation

M seasonal periods

global and local trend

ARMA error

Fourier-like seasonal terms

TBATSTrigonometricBox-CoxARMATrendSeasonal

Complex seasonality

Forecasting using R Time series with complex seasonality 16

7

8

9

10

1995 2000 2005Year

Tho

usan

ds o

f bar

rels

per

day

US finished motor gasoline products

Complex seasonality

Forecasting using R Time series with complex seasonality 16

7

8

9

10

1995 2000 2005Year

Tho

usan

ds o

f bar

rels

per

day

Forecasts from TBATS(1, {0,0}, 1, {<52.1785714285714,9>})

Complex seasonality

Forecasting using R Time series with complex seasonality 17

7

8

9

10

1995 2000 2005Year

Tho

usan

ds o

f bar

rels

per

day

Forecasts from TBATS(1, {0,0}, 1, {<52.1785714285714,9>})

fit <- tbats(gasoline)fcast <- forecast(fit)plot(fcast)

Complex seasonality

Forecasting using R Time series with complex seasonality 18

0

100

200

300

400

500

1 2 3 4 5 6 7 8 9 10 11 12 13Week

Num

ber

of c

alls

in 5

min

ute

inte

rval

Number of calls to large American bank (7am...9pm)

Complex seasonality

Forecasting using R Time series with complex seasonality 18

0

100

200

300

400

500

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Week

Num

ber

of c

alls

in 5

min

ute

inte

rval

Forecasts from TBATS(1, {0,0}, −, {<169,15>, <845,3>})

Complex seasonality

Forecasting using R Time series with complex seasonality 19

0

100

200

300

400

500

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Week

Num

ber

of c

alls

in 5

min

ute

inte

rval

Forecasts from TBATS(1, {0,0}, −, {<169,15>, <845,3>})

fit <- tbats(calls)fcast <- forecast(fit)plot(fcast)

Complex seasonality

Forecasting using R Time series with complex seasonality 20

10000

15000

20000

25000

2000 2002 2004 2006 2008 2010Year

Ele

ctric

ity d

eman

d (M

W)

Turkish daily electricity demand

Complex seasonality

Forecasting using R Time series with complex seasonality 20

10000

15000

20000

25000

2000 2002 2004 2006 2008 2010Year

Ele

ctric

ity d

eman

d (M

W)

Forecasts from TBATS(0, {4,2}, 0.913, {<7,3>, <354.37,6>, <365.25,6>})

Complex seasonality

Forecasting using R Time series with complex seasonality 21

10000

15000

20000

25000

2000 2002 2004 2006 2008 2010Year

Ele

ctric

ity d

eman

d (M

W)

Forecasts from TBATS(0, {4,2}, 0.913, {<7,3>, <354.37,6>, <365.25,6>})

fit <- tbats(telec)fcast <- forecast(fit)plot(fcast)

TBATS model

TBATSTrigonometric terms for seasonalityBox-Cox transformations for heterogeneityARMA errors for short-term dynamicsTrend (possibly damped)Seasonal (including multiple and non-integer periods)

Handles non-integer seasonality, multiple seasonalperiods.Entirely automatedPrediction intervals often too wideVery slow on long series

Forecasting using R Time series with complex seasonality 22

TBATS model

TBATSTrigonometric terms for seasonalityBox-Cox transformations for heterogeneityARMA errors for short-term dynamicsTrend (possibly damped)Seasonal (including multiple and non-integer periods)

Handles non-integer seasonality, multiple seasonalperiods.Entirely automatedPrediction intervals often too wideVery slow on long series

Forecasting using R Time series with complex seasonality 22

TBATS model

TBATSTrigonometric terms for seasonalityBox-Cox transformations for heterogeneityARMA errors for short-term dynamicsTrend (possibly damped)Seasonal (including multiple and non-integer periods)

Handles non-integer seasonality, multiple seasonalperiods.Entirely automatedPrediction intervals often too wideVery slow on long series

Forecasting using R Time series with complex seasonality 22

TBATS model

TBATSTrigonometric terms for seasonalityBox-Cox transformations for heterogeneityARMA errors for short-term dynamicsTrend (possibly damped)Seasonal (including multiple and non-integer periods)

Handles non-integer seasonality, multiple seasonalperiods.Entirely automatedPrediction intervals often too wideVery slow on long series

Forecasting using R Time series with complex seasonality 22

Outline

1 Vector autoregressions

2 Time series with complex seasonality

3 Lab session 17

4 Neural network models

5 Lab session 18

6 Lab session 19

Forecasting using R Lab session 17 23

Lab Session 17

Forecasting using R Lab session 17 24

Outline

1 Vector autoregressions

2 Time series with complex seasonality

3 Lab session 17

4 Neural network models

5 Lab session 18

6 Lab session 19

Forecasting using R Neural network models 25

Neural network modelsSimplest version: linear regression

Input #1

Input #2

Input #3

Input #4

Output

Inputlayer

Outputlayer

Coefficients attached to predictors are called “weights”.Forecasts are obtained by a linear combination of inputs.Weights selected using a “learning algorithm” that minimises a“cost function”.

Forecasting using R Neural network models 26

Neural network modelsSimplest version: linear regression

Input #1

Input #2

Input #3

Input #4

Output

Inputlayer

Outputlayer

Coefficients attached to predictors are called “weights”.Forecasts are obtained by a linear combination of inputs.Weights selected using a “learning algorithm” that minimises a“cost function”.

Forecasting using R Neural network models 26

Neural network modelsSimplest version: linear regression

Input #1

Input #2

Input #3

Input #4

Output

Inputlayer

Outputlayer

Coefficients attached to predictors are called “weights”.Forecasts are obtained by a linear combination of inputs.Weights selected using a “learning algorithm” that minimises a“cost function”.

Forecasting using R Neural network models 26

Neural network modelsSimplest version: linear regression

Input #1

Input #2

Input #3

Input #4

Output

Inputlayer

Outputlayer

Coefficients attached to predictors are called “weights”.Forecasts are obtained by a linear combination of inputs.Weights selected using a “learning algorithm” that minimises a“cost function”.

Forecasting using R Neural network models 26

Neural network modelsNonlinear model with one hidden layer

Input #1

Input #2

Input #3

Input #4

Output

Hiddenlayer

Inputlayer

Outputlayer

Amultilayer feed-forward network where each layer of nodesreceives inputs from the previous layers.Inputs to each node combined using linear combination.Result modified by nonlinear function before being output.

Forecasting using R Neural network models 27

Neural network modelsNonlinear model with one hidden layer

Input #1

Input #2

Input #3

Input #4

Output

Hiddenlayer

Inputlayer

Outputlayer

Amultilayer feed-forward network where each layer of nodesreceives inputs from the previous layers.Inputs to each node combined using linear combination.Result modified by nonlinear function before being output.

Forecasting using R Neural network models 27

Neural network modelsNonlinear model with one hidden layer

Input #1

Input #2

Input #3

Input #4

Output

Hiddenlayer

Inputlayer

Outputlayer

Amultilayer feed-forward network where each layer of nodesreceives inputs from the previous layers.Inputs to each node combined using linear combination.Result modified by nonlinear function before being output.

Forecasting using R Neural network models 27

Neural network modelsNonlinear model with one hidden layer

Input #1

Input #2

Input #3

Input #4

Output

Hiddenlayer

Inputlayer

Outputlayer

Amultilayer feed-forward network where each layer of nodesreceives inputs from the previous layers.Inputs to each node combined using linear combination.Result modified by nonlinear function before being output.

Forecasting using R Neural network models 27

Neural network models

Inputs to hidden neuron j linearly combined:

zj = bj +4∑i=1

wi,jxi.

Modified using nonlinear function such as a sigmoid:

s(z) =1

1+ e−z,

This tends to reduce the effect of extreme input values,thus making the network somewhat robust to outliers.

Forecasting using R Neural network models 28

Neural network models

Weights take random values to begin with, which arethen updated using the observed data.There is an element of randomness in thepredictions. So the network is usually trained severaltimes using different random starting points, and theresults are averaged.Number of hidden layers, and the number of nodesin each hidden layer, must be specified in advance.

Forecasting using R Neural network models 29

Neural network models

Weights take random values to begin with, which arethen updated using the observed data.There is an element of randomness in thepredictions. So the network is usually trained severaltimes using different random starting points, and theresults are averaged.Number of hidden layers, and the number of nodesin each hidden layer, must be specified in advance.

Forecasting using R Neural network models 29

Neural network models

Weights take random values to begin with, which arethen updated using the observed data.There is an element of randomness in thepredictions. So the network is usually trained severaltimes using different random starting points, and theresults are averaged.Number of hidden layers, and the number of nodesin each hidden layer, must be specified in advance.

Forecasting using R Neural network models 29

NNAR modelsLagged values of the time series can be used asinputs to a neural network.NNAR(p, k): p lagged inputs and k nodes in the singlehidden layer.NNAR(p, 0) model is equivalent to an ARIMA(p, 0, 0)model but without stationarity restrictions.Seasonal NNAR(p, P, k): inputs(yt−1, yt−2, . . . , yt−p, yt−m, yt−2m, yt−Pm) and kneurons in the hidden layer.NNAR(p, P, 0)m model is equivalent to anARIMA(p, 0, 0)(P,0,0)m model but withoutstationarity restrictions.

Forecasting using R Neural network models 30

NNAR modelsLagged values of the time series can be used asinputs to a neural network.NNAR(p, k): p lagged inputs and k nodes in the singlehidden layer.NNAR(p, 0) model is equivalent to an ARIMA(p, 0, 0)model but without stationarity restrictions.Seasonal NNAR(p, P, k): inputs(yt−1, yt−2, . . . , yt−p, yt−m, yt−2m, yt−Pm) and kneurons in the hidden layer.NNAR(p, P, 0)m model is equivalent to anARIMA(p, 0, 0)(P,0,0)m model but withoutstationarity restrictions.

Forecasting using R Neural network models 30

NNAR modelsLagged values of the time series can be used asinputs to a neural network.NNAR(p, k): p lagged inputs and k nodes in the singlehidden layer.NNAR(p, 0) model is equivalent to an ARIMA(p, 0, 0)model but without stationarity restrictions.Seasonal NNAR(p, P, k): inputs(yt−1, yt−2, . . . , yt−p, yt−m, yt−2m, yt−Pm) and kneurons in the hidden layer.NNAR(p, P, 0)m model is equivalent to anARIMA(p, 0, 0)(P,0,0)m model but withoutstationarity restrictions.

Forecasting using R Neural network models 30

NNAR modelsLagged values of the time series can be used asinputs to a neural network.NNAR(p, k): p lagged inputs and k nodes in the singlehidden layer.NNAR(p, 0) model is equivalent to an ARIMA(p, 0, 0)model but without stationarity restrictions.Seasonal NNAR(p, P, k): inputs(yt−1, yt−2, . . . , yt−p, yt−m, yt−2m, yt−Pm) and kneurons in the hidden layer.NNAR(p, P, 0)m model is equivalent to anARIMA(p, 0, 0)(P,0,0)m model but withoutstationarity restrictions.

Forecasting using R Neural network models 30

NNAR modelsLagged values of the time series can be used asinputs to a neural network.NNAR(p, k): p lagged inputs and k nodes in the singlehidden layer.NNAR(p, 0) model is equivalent to an ARIMA(p, 0, 0)model but without stationarity restrictions.Seasonal NNAR(p, P, k): inputs(yt−1, yt−2, . . . , yt−p, yt−m, yt−2m, yt−Pm) and kneurons in the hidden layer.NNAR(p, P, 0)m model is equivalent to anARIMA(p, 0, 0)(P,0,0)m model but withoutstationarity restrictions.

Forecasting using R Neural network models 30

NNAR models in R

The nnetar() function fits an NNAR(p, P, k)m model.If p and P are not specified, they are automaticallyselected.For non-seasonal time series, default p = optimalnumber of lags (according to the AIC) for a linearAR(p) model.For seasonal time series, defaults are P = 1 and p ischosen from the optimal linear model fitted to theseasonally adjusted data.Default k = (p+ P+ 1)/2 (rounded to the nearestinteger).

Forecasting using R Neural network models 31

NNAR models in R

The nnetar() function fits an NNAR(p, P, k)m model.If p and P are not specified, they are automaticallyselected.For non-seasonal time series, default p = optimalnumber of lags (according to the AIC) for a linearAR(p) model.For seasonal time series, defaults are P = 1 and p ischosen from the optimal linear model fitted to theseasonally adjusted data.Default k = (p+ P+ 1)/2 (rounded to the nearestinteger).

Forecasting using R Neural network models 31

NNAR models in R

The nnetar() function fits an NNAR(p, P, k)m model.If p and P are not specified, they are automaticallyselected.For non-seasonal time series, default p = optimalnumber of lags (according to the AIC) for a linearAR(p) model.For seasonal time series, defaults are P = 1 and p ischosen from the optimal linear model fitted to theseasonally adjusted data.Default k = (p+ P+ 1)/2 (rounded to the nearestinteger).

Forecasting using R Neural network models 31

NNAR models in R

The nnetar() function fits an NNAR(p, P, k)m model.If p and P are not specified, they are automaticallyselected.For non-seasonal time series, default p = optimalnumber of lags (according to the AIC) for a linearAR(p) model.For seasonal time series, defaults are P = 1 and p ischosen from the optimal linear model fitted to theseasonally adjusted data.Default k = (p+ P+ 1)/2 (rounded to the nearestinteger).

Forecasting using R Neural network models 31

NNAR models in R

The nnetar() function fits an NNAR(p, P, k)m model.If p and P are not specified, they are automaticallyselected.For non-seasonal time series, default p = optimalnumber of lags (according to the AIC) for a linearAR(p) model.For seasonal time series, defaults are P = 1 and p ischosen from the optimal linear model fitted to theseasonally adjusted data.Default k = (p+ P+ 1)/2 (rounded to the nearestinteger).

Forecasting using R Neural network models 31

Sunspots

Surface of the sun contains magnetic regions thatappear as dark spots.These affect the propagation of radio waves and sotelecommunication companies like to predictsunspot activity in order to plan for any futuredifficulties.Sunspots follow a cycle of length between 9 and 14years.

Forecasting using R Neural network models 32

Sunspots

Surface of the sun contains magnetic regions thatappear as dark spots.These affect the propagation of radio waves and sotelecommunication companies like to predictsunspot activity in order to plan for any futuredifficulties.Sunspots follow a cycle of length between 9 and 14years.

Forecasting using R Neural network models 32

Sunspots

Surface of the sun contains magnetic regions thatappear as dark spots.These affect the propagation of radio waves and sotelecommunication companies like to predictsunspot activity in order to plan for any futuredifficulties.Sunspots follow a cycle of length between 9 and 14years.

Forecasting using R Neural network models 32

NNAR(9,5) model for sunspots

Forecasting using R Neural network models 33

Forecasts from NNAR(9,5)

1900 1950 2000

050

010

0015

0020

0025

0030

00 fit <- nnetar(sunspotarea)plot(forecast(fit,h=20))

Outline

1 Vector autoregressions

2 Time series with complex seasonality

3 Lab session 17

4 Neural network models

5 Lab session 18

6 Lab session 19

Forecasting using R Lab session 18 34

Lab Session 18

Forecasting using R Lab session 18 35

Outline

1 Vector autoregressions

2 Time series with complex seasonality

3 Lab session 17

4 Neural network models

5 Lab session 18

6 Lab session 19

Forecasting using R Lab session 19 36

Lab Session 19

Forecasting using R Lab session 19 37