Time Series Exercises in R: 40 Practice Problems

Forty practice problems on time series in R: ts objects, decomposition, stationarity, autocorrelation, ARIMA, ETS, forecasting. Hidden solutions.

RRun this once before any exercise
library(forecast) library(tseries) library(dplyr) library(tibble)

  

Section 1. ts objects and basics (8 problems)

Exercise 1.1: Create a ts

Difficulty: Beginner. Monthly series starting Jan 2020.

Show solution
RInteractive R
ts(rnorm(24), frequency = 12, start = c(2020, 1))

  

Exercise 1.2: Plot ts

Difficulty: Beginner.

Show solution
RInteractive R
plot(AirPassengers)

  

Exercise 1.3: Frequency

Difficulty: Beginner.

Show solution
RInteractive R
frequency(AirPassengers)

  

Exercise 1.4: Window subset

Difficulty: Intermediate.

Show solution
RInteractive R
window(AirPassengers, start = c(1955,1), end = c(1957,12))

  

Exercise 1.5: Aggregate to yearly

Difficulty: Intermediate.

Show solution
RInteractive R
aggregate(AirPassengers, FUN = sum)

  

Exercise 1.6: Lag

Difficulty: Intermediate.

Show solution
RInteractive R
lag(AirPassengers, k = -1)

  

Exercise 1.7: Difference

Difficulty: Intermediate.

Show solution
RInteractive R
diff(AirPassengers)

  

Exercise 1.8: Seasonal lag-12 difference

Difficulty: Advanced.

Show solution
RInteractive R
diff(AirPassengers, lag = 12)

  

Section 2. Decomposition (6 problems)

Exercise 2.1: Classical decomposition

Difficulty: Intermediate.

Show solution
RInteractive R
plot(decompose(AirPassengers))

  

Exercise 2.2: Multiplicative decomposition

Difficulty: Intermediate.

Show solution
RInteractive R
plot(decompose(AirPassengers, type = "multiplicative"))

  

Exercise 2.3: STL decomposition

Difficulty: Advanced.

Show solution
RInteractive R
plot(stl(AirPassengers, s.window = "periodic"))

  

Exercise 2.4: Extract trend

Difficulty: Advanced.

Show solution
RInteractive R
decompose(AirPassengers)$trend

  

Exercise 2.5: Seasonally adjusted

Difficulty: Advanced.

Show solution
RInteractive R
d <- decompose(AirPassengers) AirPassengers - d$seasonal

  

Exercise 2.6: Strength of seasonality

Difficulty: Advanced.

Show solution
RInteractive R
mstl(AirPassengers) |> head()

  

Section 3. Stationarity and autocorrelation (6 problems)

Exercise 3.1: ACF plot

Difficulty: Beginner.

Show solution
RInteractive R
acf(AirPassengers)

  

Exercise 3.2: PACF plot

Difficulty: Intermediate.

Show solution
RInteractive R
pacf(AirPassengers)

  

Exercise 3.3: ADF test for stationarity

Difficulty: Intermediate.

Show solution
RInteractive R
adf.test(AirPassengers)

  

Exercise 3.4: KPSS test

Difficulty: Intermediate.

Show solution
RInteractive R
kpss.test(AirPassengers)

  

Exercise 3.5: Difference until stationary

Difficulty: Advanced.

Show solution
RInteractive R
ndiffs(AirPassengers)

  

Exercise 3.6: Seasonal differences needed

Difficulty: Advanced.

Show solution
RInteractive R
nsdiffs(AirPassengers)

  

Section 4. ARIMA (8 problems)

Exercise 4.1: Auto ARIMA

Difficulty: Intermediate.

Show solution
RInteractive R
fit <- auto.arima(AirPassengers) fit

  

Exercise 4.2: Specific ARIMA(p,d,q)

Difficulty: Intermediate.

Show solution
RInteractive R
arima(AirPassengers, order = c(1, 1, 1))

  

Exercise 4.3: Seasonal ARIMA

Difficulty: Advanced.

Show solution
RInteractive R
arima(AirPassengers, order = c(1,1,1), seasonal = c(1,1,1))

  

Exercise 4.4: Forecast horizon

Difficulty: Intermediate.

Show solution
RInteractive R
fit <- auto.arima(AirPassengers) forecast(fit, h = 24)

  

Exercise 4.5: Plot forecast

Difficulty: Intermediate.

Show solution
RInteractive R
fit <- auto.arima(AirPassengers) plot(forecast(fit, h = 24))

  

Exercise 4.6: Residual diagnostics

Difficulty: Advanced.

Show solution
RInteractive R
fit <- auto.arima(AirPassengers) checkresiduals(fit)

  

Exercise 4.7: Ljung-Box test

Difficulty: Advanced.

Show solution
RInteractive R
fit <- auto.arima(AirPassengers) Box.test(residuals(fit), lag = 12, type = "Ljung-Box")

  

Exercise 4.8: AIC comparison

Difficulty: Advanced.

Show solution
RInteractive R
f1 <- arima(AirPassengers, order = c(1,1,1)) f2 <- arima(AirPassengers, order = c(2,1,2)) c(AIC(f1), AIC(f2))

  

Section 5. ETS and others (6 problems)

Exercise 5.1: Auto ETS

Difficulty: Intermediate.

Show solution
RInteractive R
ets(AirPassengers)

  

Exercise 5.2: Specific ETS

Difficulty: Advanced. Holt-Winters multiplicative.

Show solution
RInteractive R
ets(AirPassengers, model = "MAM")

  

Exercise 5.3: Holt-Winters

Difficulty: Intermediate.

Show solution
RInteractive R
HoltWinters(AirPassengers)

  

Exercise 5.4: Naive forecast

Difficulty: Beginner.

Show solution
RInteractive R
naive(AirPassengers, h = 12) |> autoplot()

  

Exercise 5.5: Seasonal naive

Difficulty: Beginner.

Show solution
RInteractive R
snaive(AirPassengers, h = 12)

  

Exercise 5.6: Mean forecast

Difficulty: Beginner.

Show solution
RInteractive R
meanf(AirPassengers, h = 12)

  

Section 6. Train-test and accuracy (6 problems)

Exercise 6.1: Train-test split

Difficulty: Intermediate.

Show solution
RInteractive R
train <- window(AirPassengers, end = c(1958, 12)) test <- window(AirPassengers, start = c(1959, 1)) list(train = length(train), test = length(test))

  

Exercise 6.2: Forecast accuracy

Difficulty: Intermediate.

Show solution
RInteractive R
train <- window(AirPassengers, end = c(1958, 12)) test <- window(AirPassengers, start = c(1959, 1)) fit <- auto.arima(train) fc <- forecast(fit, h = length(test)) accuracy(fc, test)

  

Exercise 6.3: Compare ARIMA vs ETS

Difficulty: Advanced.

Show solution
RInteractive R
train <- window(AirPassengers, end = c(1958, 12)) test <- window(AirPassengers, start = c(1959, 1)) f1 <- forecast(auto.arima(train), h = length(test)) f2 <- forecast(ets(train), h = length(test)) list(arima = accuracy(f1, test)[2,"RMSE"], ets = accuracy(f2, test)[2,"RMSE"])

  

Exercise 6.4: Cross-validation rolling origin

Difficulty: Advanced.

Show solution
RInteractive R
e <- tsCV(AirPassengers, forecastfunction = function(x, h) forecast(auto.arima(x), h = h), h = 1) sqrt(mean(e^2, na.rm = TRUE))

  

Exercise 6.5: MAPE

Difficulty: Intermediate.

Show solution
RInteractive R
train <- window(AirPassengers, end = c(1958, 12)) test <- window(AirPassengers, start = c(1959, 1)) fc <- forecast(auto.arima(train), h = length(test)) accuracy(fc, test)[, "MAPE"]

  

Exercise 6.6: Out-of-sample forecast plot

Difficulty: Intermediate.

Show solution
RInteractive R
fit <- auto.arima(AirPassengers) autoplot(forecast(fit, h = 36))

  

What to do next

  • ARIMA-Exercises (coming), focused ARIMA drilling.
  • Linear-Regression-Exercises (shipped), regression on time-features.