suppressMessages(library(fpp2))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))(lambda <- BoxCox.lambda(cangas))## [1] 0.5767759
autoplot(BoxCox(cangas,lambda))autoplot(cangas)Its doesnt seem a lot effect on inmproving the trend.
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.
myts.train <- window(myts, end=c(2010,12))
myts.test <- window(myts, start=2011)autoplot(myts) +
autolayer(myts.train, series="Training") +
autolayer(myts.test, series="Test")fc <- snaive(myts.train)
#fc2 <- meanf(myts.train)
#fc3 <- rwf(myts.train)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)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