plot(co2)
This section is based on the Chapter 3 (“Time series decomposition”) and Chapter 8 (“Exponential smoothing”) of Forecasting: Principles and Practice (FPP). The specific sections from these chapters are 3.2–3.4 and 3.6 (components, moving averages, classical decomposition, STL) and 8.1–8.4 (simple exponential smoothing, trend, seasonality, taxonomy).
A common decomposition separates the observations into
so that, for an additive decomposition, \[y_t = T_t + S_t + R_t.\]
As an example we use the monthly atmospheric CO\(_2\) concentration (ppm) measured at Mauna Loa, 1959–1997:
plot(co2)
The object co2 is of class ts, and provides its frequency (12), the third value of the time series parameters attribute:
attributes(co2)$tsp
[1] 1959.000 1997.917 12.000
$class
[1] "ts"
tsp holds the start (1959.0 is January 1959), the end (1997.917 is December 1997) and the frequency (12 observations per year).ts object with the right frequency.Classical decomposition (FPP section 3.4) has three steps:
co2_dec <- decompose(co2)
plot(co2_dec)
Known weaknesses:
head(co2_dec$trend, 12) Jan Feb Mar Apr May Jun Jul Aug
1959 NA NA NA NA NA NA 315.8613 315.9175
Sep Oct Nov Dec
1959 315.9767 316.0696 316.1967 316.3287
co2_dec$figure holds the 12 values);stl: Seasonal and Trend decomposition using Loessstl addresses all four weaknesses. Its simplest form is:
co2_stl <- stl(co2, s.window = "periodic")
plot(co2_stl)
Loess (“locally estimated scatterplot smoothing”) fits a smooth curve through a cloud of points. The smoothed value at a point \(x_0\) is found by:
span in loess(): the fraction of the data used);nile <- data.frame(year = as.numeric(time(Nile)), flow = as.numeric(Nile))
plot(flow ~ year, nile, main = "Annual flow of the Nile at Aswan")
lines(nile$year, fitted(loess(flow ~ year, nile, span = 0.75)), col = "blue", lwd = 2)
lines(nile$year, fitted(loess(flow ~ year, nile, span = 0.2)), col = "red", lwd = 2)
legend("topright", c("span = 0.75", "span = 0.2"), col = c("blue", "red"), lwd = 2)
Compared to classical decomposition, STL
s.window,t.window,robust = TRUE),Its limitations:
The components are stored as a multivariate time series in the element time.series of the stl object:
co2_per <- stl(co2, s.window = "periodic")
head(co2_per$time.series) seasonal trend remainder
Jan 1959 -0.06100103 315.1954 0.28564410
Feb 1959 0.59463870 315.3023 0.41305456
Mar 1959 1.32899651 315.4093 -0.23825306
Apr 1959 2.46904706 315.5147 -0.42371128
May 1959 2.95704630 315.6201 -0.44711820
Jun 1959 2.31835208 315.7194 -0.03777682
co2_per$time.series[, "seasonal"].acf() applied to a monthly ts labels the lags in years: lag 1.0 means 12 months.To predict a new observation \(y_{T+1}\) from a time series \(y_1, \ldots, y_T\):
A compromise between these two extremes is exponential smoothing, a weighted average with weights that decay exponentially as observations get older:
\[\hat{y}_{T+1|T} = \alpha y_T + \alpha(1-\alpha) y_{T-1} + \alpha(1-\alpha)^2 y_{T-2} + \cdots = \sum_{j=0}^{T-1} \alpha(1-\alpha)^j\, y_{T-j} \;+\; (1-\alpha)^T \ell_0\]
with \(0 \le \alpha \le 1\).
j <- 0:15
alphas <- c(0.2, 0.5, 0.8)
w <- sapply(alphas, function(a) a * (1 - a)^j)
matplot(j, w, type = "b", pch = 1:3, lty = 1, col = 1:3,
xlab = "j (steps back in time)", ylab = "weight of observation j steps back")
legend("topright", legend = paste("alpha =", alphas), pch = 1:3, col = 1:3, lty = 1)
The same method can be written as a recursion. The level \(\ell_t\) is the estimate of the underlying value of the series at time \(t\), with the noise smoothed out:
\[ \begin{aligned} \text{forecast equation:} \quad & \hat{y}_{t+h|t} = \ell_t \\ \text{level equation:} \quad & \ell_t = \alpha\, y_t + (1-\alpha)\, \ell_{t-1} \end{aligned} \]
SES works well for data with a constant or slowly wandering mean, without trend or seasonal component. For data with a trend, Holt’s method adds a second component, the slope \(b_t\):
HoltWinters call \(b_t\) the “trend”, but it is the slope of the local trend line: the expected change per time step. (In a decomposition, the trend \(T_t\) is the whole smooth curve.)\[ \begin{aligned} \text{forecast:} \quad & \hat{y}_{t+h|t} = \ell_t + h\, b_t \\ \text{level:} \quad & \ell_t = \alpha\, y_t + (1-\alpha)(\ell_{t-1} + b_{t-1}) \\ \text{slope:} \quad & b_t = \beta^*(\ell_t - \ell_{t-1}) + (1-\beta^*)\, b_{t-1} \end{aligned} \]
As an example we take the seasonally adjusted CO\(_2\) series, with the seasonal component of the stl result above removed. HoltWinters with gamma = FALSE fits Holt’s method:
co2_sa <- co2 - co2_stl$time.series[, "seasonal"] # seasonally adjusted
holt <- HoltWinters(co2_sa, gamma = FALSE)
c(alpha = holt$alpha[[1]], beta = holt$beta[[1]]) alpha beta
0.77943817 0.02762752
holt$coefficients # final level (a) and slope (b, in ppm per month) a b
365.103833 0.140949
plot(fitted(holt))
The panels show the one-step forecasts (xhat), the level and the slope (named trend in R). Forecasting ten years ahead:
plot(holt, predict(holt, n.ahead = 120))
See section 8.3 in the FPP book.
For the additive method:
\[ \begin{aligned} \text{forecast:} \quad & \hat{y}_{t+h|t} = \ell_t + h\, b_t + s_{t+h-m(k+1)} \\ \text{level:} \quad & \ell_t = \alpha\,(y_t - s_{t-m}) + (1-\alpha)(\ell_{t-1} + b_{t-1}) \\ \text{slope:} \quad & b_t = \beta^*(\ell_t - \ell_{t-1}) + (1-\beta^*)\, b_{t-1} \\ \text{season:} \quad & s_t = \gamma\,(y_t - \ell_{t-1} - b_{t-1}) + (1-\gamma)\, s_{t-m} \end{aligned} \]
How the seasonal indices behave:
stl.s.window: \(\gamma\) close to 0 keeps the seasonal pattern nearly constant (like "periodic"), a larger \(\gamma\) lets it change faster.HoltWinters(co2) fits the additive method, estimating \(\alpha\), \(\beta\) and \(\gamma\) by minimising the sum of squared one-step forecast errors with optim:
hw <- HoltWinters(co2)
c(alpha = hw$alpha[[1]], beta = hw$beta[[1]], gamma = hw$gamma[[1]]) alpha beta gamma
0.512648444 0.009497669 0.472886788
plot(hw)
The fitted components over time:
plot(fitted(HoltWinters(co2)))
Look at the names of the panels and what they contain, at the time range of the plot, and at which observations each component at time \(t\) is based on.
Forecasting five years ahead, with prediction intervals:
plot(hw, predict(hw, n.ahead = 60, prediction.interval = TRUE))
This content is created using AI model (claude Opus 5)