suppressMessages(library(fpp2))

3.1-) For the following series, find an appropriate Box-Cox transformation in order to stabilise the variance.

usnetelec usgdp mcopper enplanements

(lambda <- BoxCox.lambda(usnetelec))
## [1] 0.5167714
autoplot(BoxCox(usnetelec,lambda))

lambda
## [1] 0.5167714
(lambda <- BoxCox.lambda(usgdp))
## [1] 0.366352
autoplot(BoxCox(usgdp,lambda))

(lambda <- BoxCox.lambda(mcopper))
## [1] 0.1919047
autoplot(BoxCox(mcopper,lambda))

(lambda <- BoxCox.lambda(enplanements))
## [1] -0.2269461
autoplot(BoxCox(enplanements,lambda))

3.2-) Why is a Box-Cox transformation unhelpful for the cangas data?

(lambda <- BoxCox.lambda(cangas))
## [1] 0.5767759
autoplot(BoxCox(cangas,lambda))

autoplot(cangas)

Its doesnt seem a lot effect on inmproving the trend.

3.3-) What Box-Cox transformation would you select for your retail data (from Exercise 3 in Section 2.10)?

retaildata <- readxl::read_excel("retail.xlsx", skip=1)


#retaildata

myts <- ts(retaildata[,"A3349709X"],
  frequency=12, start=c(1982,4))


autoplot(myts)

ggseasonplot(myts)

ggsubseriesplot(myts)

gglagplot(myts)

ggAcf(myts)

(lambda <- BoxCox.lambda(myts))
## [1] 0.1147219
autoplot(BoxCox(myts,lambda))

fc <- rwf(myts, drift=TRUE, lambda=0, h=50, level=80)
fc2 <- rwf(myts, drift=TRUE, lambda=0, h=50, level=80,
  biasadj=TRUE)
## Warning in `/.default`(0.5 * fvar * (1 - lambda), (out)^(2 * lambda)): Recycling array of length 1 in array-vector arithmetic is deprecated.
##   Use c() or as.vector() instead.
autoplot(myts) +
  autolayer(fc, series="Simple back transformation") +
  autolayer(fc2, series="Bias adjusted", PI=FALSE) +
  guides(colour=guide_legend(title="Forecast"))

Its seem its better when we ajusted the Bias.

3.8-) For your retail time series (from Exercise 3 in Section 2.10):

a. Split the data into two parts using

myts.train <- window(myts, end=c(2010,12))
myts.test <- window(myts, start=2011)

b. Check that your data have been split appropriately by producing the following plot.

autoplot(myts) +
  autolayer(myts.train, series="Training") +
  autolayer(myts.test, series="Test")

c. Calculate forecasts using snaive applied to myts.train.

fc <- snaive(myts.train)

#fc2 <- meanf(myts.train)
#fc3 <- rwf(myts.train)

d. Compare the accuracy of your forecasts against the actual values stored in myts.test.

accuracy(fc,myts.test)
##                    ME    RMSE      MAE      MPE     MAPE    MASE      ACF1
## Training set 14.13033 42.0516 31.47868 6.071647 12.66073 1.00000 0.8466104
## Test set     51.61667 64.7207 54.35833 9.718876 10.32797 1.72683 0.7380221
##              Theil's U
## Training set        NA
## Test set      1.759924
#accuracy(fc2,myts.test)
#accuracy(fc3,myts.test)

e. Check the residuals.

checkresiduals(fc)

## 
##  Ljung-Box test
## 
## data:  Residuals from Seasonal naive method
## Q* = 1609.7, df = 24, p-value < 2.2e-16
## 
## Model df: 0.   Total lags used: 24

The Residual seem a little bit normaly distributed but when we see the ACF-Residuals they are a lot variation.The RMSE we got higher number against the test data but in genereal its not a lot big variation.

Citation

Code Source from book: https://otexts.com/fpp2/toolbox-exercises.html