Consider the the number of pigs slaughtered in Victoria, available in the aus_livestock dataset.
Use the ETS() function in R to estimate the equivalent model for simple exponential smoothing. Find the optimal values of \(\alpha\) and \(\ell_0\), and generate forecasts for the next four months.
fit <- aus_livestock |>filter(Animal =="Pigs", State =="Victoria") |>model(ses =ETS(Count ~error("A") +trend("N") +season("N")))report(fit)
Optimal values are \(\alpha = 0.3221247\) and \(\ell_0 = 100646.6\)
fc <- fit |>forecast(h ="4 months")fc
# A fable: 4 x 6 [1M]
# Key: Animal, State, .model [1]
Animal State .model Month
<fct> <fct> <chr> <mth>
1 Pigs Victoria ses 2019 Jan
2 Pigs Victoria ses 2019 Feb
3 Pigs Victoria ses 2019 Mar
4 Pigs Victoria ses 2019 Apr
# ℹ 2 more variables: Count <dist>, .mean <dbl>
fc |>autoplot(filter(aus_livestock, Month >=yearmonth("2010 Jan")))
Compute a 95% prediction interval for the first forecast using \(\hat{y} \pm 1.96s\) where \(s\) is the standard deviation of the residuals. Compare your interval with the interval produced by R.
s <-augment(fit) |>pull(.resid) |>sd()yhat <- fc |>pull(.mean) |>head(1)yhat +c(-1, 1) *1.96* s
[1] 76871.01 113502.10
fc |>head(1) |>mutate(interval =hilo(Count, 95)) |>pull(interval)
<hilo[1]>
[1] [76854.79, 113518.3]95
The intervals are close but not identical. This is because R estimates the variance of the residuals differently, taking account of the degrees of freedom properly (and also using a more accurate critical value rather than just 1.96).
Try the following.
res <-augment(fit) |>pull(.resid)s <-sqrt(sum(res^2) / (length(res) -NROW(tidy(fit))))yhat +c(-1, 1) *qnorm(0.975) * s
[1] 76854.79 113518.33
fpp3 8.8, Ex2
Write your own function to implement simple exponential smoothing. The function should take arguments y (the response data), alpha (the smoothing parameter \(\alpha\)) and level (the initial level \(\ell_0\)). It should return the forecast of the next observation in the series. Does it give the same forecast as ETS()?
Yes, the same forecasts are obtained. The slight differences are due to rounding of \(\alpha\) and \(\ell_0\).
fpp3 8.8, Ex3
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 \(\ell_0\). Do you get the same values as the ETS() function?
# A tibble: 2 × 5
Animal State .model term estimate
<fct> <fct> <chr> <chr> <dbl>
1 Pigs Victoria ses alpha 0.322
2 Pigs Victoria ses l[0] 100647.
Similar, but not identical estimates. This is due to different starting values being used.
fpp3 8.8, Ex4
Combine your previous two functions to produce a function that both finds the optimal values of \(\alpha\) and \(\ell_0\), and produces a forecast of the next observation in the series.