7.8 Exercises

7.8.1. Consider the pigs series - the number of pigs slaughtered in Victoria each month.

a. Use the ses() function in R to find the optimal values of alpha and l0 and generate forecasts for the next four months.

From summary(fc), we know that alpha = 0.2971 and l0 = 77260.06. For n the forcasts for the next 4 months, we see that it has a “flat” forecast of 98816.41.

fc <- ses(pigs, h = 4)
fc
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Sep 1995       98816.41 85605.43 112027.4 78611.97 119020.8
## Oct 1995       98816.41 85034.52 112598.3 77738.83 119894.0
## Nov 1995       98816.41 84486.34 113146.5 76900.46 120732.4
## Dec 1995       98816.41 83958.37 113674.4 76092.99 121539.8
#fitted(fc)
summary(fc)
## 
## Forecast method: Simple exponential smoothing
## 
## Model Information:
## Simple exponential smoothing 
## 
## Call:
##  ses(y = pigs, h = 4) 
## 
##   Smoothing parameters:
##     alpha = 0.2971 
## 
##   Initial states:
##     l = 77260.0561 
## 
##   sigma:  10308.58
## 
##      AIC     AICc      BIC 
## 4462.955 4463.086 4472.665 
## 
## Error measures:
##                    ME    RMSE      MAE       MPE     MAPE      MASE
## Training set 385.8721 10253.6 7961.383 -0.922652 9.274016 0.7966249
##                    ACF1
## Training set 0.01282239
## 
## Forecasts:
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Sep 1995       98816.41 85605.43 112027.4 78611.97 119020.8
## Oct 1995       98816.41 85034.52 112598.3 77738.83 119894.0
## Nov 1995       98816.41 84486.34 113146.5 76900.46 120732.4
## Dec 1995       98816.41 83958.37 113674.4 76092.99 121539.8

b. Compute a 95% prediction interval for the first forecast using y_^±1.96s where s is the standard deviation of the residuals. Compare your interval with the interval produced by R.

From manual calculation, we know Lo 95 = 78679.97 and Hi 95 = 118952.8 for Sep 1995. From ses output, we know Lo 95 = 78611.59 and Hi 95 = 119021.2 for Sep 1995.

lo95 = fc$mean[1] - (sd(fc$residuals) * 1.96)
hi95 = fc$mean[1] + (sd(fc$residuals) * 1.96)

lo95
## [1] 78679.97
hi95
## [1] 118952.8
fc
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Sep 1995       98816.41 85605.43 112027.4 78611.97 119020.8
## Oct 1995       98816.41 85034.52 112598.3 77738.83 119894.0
## Nov 1995       98816.41 84486.34 113146.5 76900.46 120732.4
## Dec 1995       98816.41 83958.37 113674.4 76092.99 121539.8
# plot the data with fitted values to forcast
autoplot(fc) + autolayer(fc$fitted)

7.8.3 Modify your function from the previous exercise to return the sum of squared errors rather than the forecast of the next observation. Then use the optim() function to find the optimal values of alpha and l0. Do you get the same values as the ses() function?

The results are almost the same but not exactly the same. I think this is due to the scale difference in alpha and l0 when it comes to calculation.

# create ses function manually to test forecasted value
ses_manual <- function(y, alpha, l0){
  y_hat <- l0
  for(index in 1:length(y)){
   y_hat <- alpha*y[index] + (1 - alpha)*y_hat 
  }
  cat("Forecast result by ses_optim function: ",
      as.character(y_hat),
      sep = "\n")
}

# Let's compare the forecasting results from built-in SES function in R to manual function using optim
a <- fc$model$par[1]
l0 <- fc$model$par[2]

# They are the same!
ses_manual(pigs, alpha = a, l0 = l0)
## Forecast result by ses_optim function: 
## 98816.4061115907
cat("Forecast result by built-in ses function: ", fc$mean[1], sep = "\n")
## Forecast result by built-in ses function: 
## 98816.41
# now, let's change the above function to get alpha and l0
ses_param <- function(pars = c(alpha, l0), y){
  error <- 0
  SSE <- 0
  alpha <- pars[1]
  l0 <- pars[2]
  y_hat <- l0
  
  for(index in 1:length(y)){
    error <- y[index] - y_hat
    SSE <- SSE + error^2
    
    y_hat <- alpha*y[index] + (1 - alpha)*y_hat 
  }
  
  return(SSE)
}

# Let's compare the results for alphan and l0 from built-in SES function in R to manual function using optim
optim_param <- optim(par = c(0.5, pigs[1]), y = pigs, fn = ses_param)

# Almost same
cat("Parameters estimate by manual ses_param function: ", optim_param$par[1], optim_param$par[2], sep = "\n")
## Parameters estimate by manual ses_param function: 
## 0.2990081
## 76379.27
cat("Parameters estimate by built-in ses function: ", fc$model$par[1], fc$model$par[2])
## Parameters estimate by built-in ses function:  0.2971488 77260.06