Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated...

24
Forecasting using R 1 Forecasting using R Rob J Hyndman 2.2 Transformations

Transcript of Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated...

Page 1: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Forecasting using R 1

Forecasting using R

Rob J Hyndman

2.2 Transformations

Page 2: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Outline

1 Variance stabilization

2 Box-Cox transformations

3 Lab session 9

Forecasting using R Variance stabilization 2

Page 3: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Variance stabilizationIf the data show different variation at different levels ofthe series, then a transformation can be useful.Denote original observations as y1, . . . , yn andtransformed observations as w1, . . . ,wn.Mathematical transformations for stabilizing variation

Square root wt =√yt ↓

Cube root wt = 3√yt Increasing

Logarithm wt = log(yt) strength

Logarithms, in particular, are useful because they are moreinterpretable: changes in a log value are relative (percent)changes on the original scale.

Forecasting using R Variance stabilization 3

Page 4: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Variance stabilizationIf the data show different variation at different levels ofthe series, then a transformation can be useful.Denote original observations as y1, . . . , yn andtransformed observations as w1, . . . ,wn.Mathematical transformations for stabilizing variation

Square root wt =√yt ↓

Cube root wt = 3√yt Increasing

Logarithm wt = log(yt) strength

Logarithms, in particular, are useful because they are moreinterpretable: changes in a log value are relative (percent)changes on the original scale.

Forecasting using R Variance stabilization 3

Page 5: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Variance stabilizationIf the data show different variation at different levels ofthe series, then a transformation can be useful.Denote original observations as y1, . . . , yn andtransformed observations as w1, . . . ,wn.Mathematical transformations for stabilizing variation

Square root wt =√yt ↓

Cube root wt = 3√yt Increasing

Logarithm wt = log(yt) strength

Logarithms, in particular, are useful because they are moreinterpretable: changes in a log value are relative (percent)changes on the original scale.

Forecasting using R Variance stabilization 3

Page 6: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Variance stabilizationIf the data show different variation at different levels ofthe series, then a transformation can be useful.Denote original observations as y1, . . . , yn andtransformed observations as w1, . . . ,wn.Mathematical transformations for stabilizing variation

Square root wt =√yt ↓

Cube root wt = 3√yt Increasing

Logarithm wt = log(yt) strength

Logarithms, in particular, are useful because they are moreinterpretable: changes in a log value are relative (percent)changes on the original scale.

Forecasting using R Variance stabilization 3

Page 7: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Variance stabilization

50

75

100

125

1960 1970 1980 1990Year

Square root electricity production

Forecasting using R Variance stabilization 4

Page 8: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Variance stabilization

15

20

25

1960 1970 1980 1990Year

Cube root electricity production

Forecasting using R Variance stabilization 5

Page 9: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Variance stabilization

8

9

1960 1970 1980 1990Year

Log electricity production

Forecasting using R Variance stabilization 6

Page 10: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Variance stabilization

−8e−04

−6e−04

−4e−04

−2e−04

1960 1970 1980 1990Year

Inverse electricity production

Forecasting using R Variance stabilization 7

Page 11: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Outline

1 Variance stabilization

2 Box-Cox transformations

3 Lab session 9

Forecasting using R Box-Cox transformations 8

Page 12: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Box-Cox transformations

Each of these transformations is close to a member of thefamily of Box-Cox transformations:

wt = log(yt), λ = 0;(yλt − 1)/λ, λ 6= 0.

λ = 1: (No substantive transformation)λ = 1

2 : (Square root plus linear transformation)λ = 0: (Natural logarithm)λ = −1: (Inverse plus 1)

Forecasting using R Box-Cox transformations 9

Page 13: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Box-Cox transformations

Each of these transformations is close to a member of thefamily of Box-Cox transformations:

wt = log(yt), λ = 0;(yλt − 1)/λ, λ 6= 0.

λ = 1: (No substantive transformation)λ = 1

2 : (Square root plus linear transformation)λ = 0: (Natural logarithm)λ = −1: (Inverse plus 1)

Forecasting using R Box-Cox transformations 9

Page 14: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Box-Cox transformations

Forecasting using R Box-Cox transformations 10

Page 15: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Box-Cox transformations

autoplot(BoxCox(elec,lambda=1/3))

30

40

50

60

70

1960 1970 1980 1990Time

Box

Cox

(ele

c, la

mbd

a =

1/3

)

Forecasting using R Box-Cox transformations 11

Page 16: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Box-Cox transformationsyλt for λ close to zero behaves like logs.If some yt = 0, then must have λ > 0if some yt < 0, no power transformation is possibleunless all yt adjusted by adding a constant to allvalues.Choose a simple value of λ. It makes explanationeasier.Results are relatively insensitive to value of λOften no transformation (λ = 1) needed.Transformation often makes little difference toforecasts but has large effect on PI.Choosing λ = 0 is a simple way to force forecasts tobe positive

Forecasting using R Box-Cox transformations 12

Page 17: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Automated Box-Cox transformations

(BoxCox.lambda(elec))

## [1] 0.2654076

This attempts to balance the seasonal fluctuationsand random variation across the series.Always check the results.A low value of λ can give extremely large predictionintervals.

Forecasting using R Box-Cox transformations 13

Page 18: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Automated Box-Cox transformations

(BoxCox.lambda(elec))

## [1] 0.2654076

This attempts to balance the seasonal fluctuationsand random variation across the series.Always check the results.A low value of λ can give extremely large predictionintervals.

Forecasting using R Box-Cox transformations 13

Page 19: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Back-transformation

Wemust reverse the transformation (or back-transform) toobtain forecasts on the original scale. The reverse Box-Coxtransformations are given by

yt = exp(wt), λ = 0;(λWt + 1)1/λ, λ 6= 0.

Forecasting using R Box-Cox transformations 14

Page 20: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Back-transformation

fit <- snaive(elec, lambda=1/3)autoplot(fit)

5000

10000

15000

1960 1970 1980 1990Time

y

level

80

95

Forecasts from Seasonal naive method

Forecasting using R Box-Cox transformations 15

Page 21: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Back-transformation

autoplot(fit, include=120)

10000

12000

14000

16000

1987.5 1990.0 1992.5 1995.0 1997.5Time

y

level

80

95

Forecasts from Seasonal naive method

Forecasting using R Box-Cox transformations 16

Page 22: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

ETS and transformations

A Box-Cox transformation followed by an additive ETSmodel is often better than an ETS model withouttransformation.It makes no sense to use a Box-Cox transformationand a non-additive ETS model.

Forecasting using R Box-Cox transformations 17

Page 23: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Outline

1 Variance stabilization

2 Box-Cox transformations

3 Lab session 9

Forecasting using R Lab session 9 18

Page 24: Forecasting using R - Rob J. Hyndman · Forecasting using R Box-Cox transformations 12. Automated Box-Cox transformations (BoxCox.lambda (elec)) ## [1] 0.2654076 This attempts to

Lab Session 9

Forecasting using R Lab session 9 19