ARIMA Exercises in R: 25 Practice Problems

Twenty-five practice problems on ARIMA: identification (ACF/PACF), differencing, fitting, seasonal ARIMA, diagnostics, forecasts. Hidden solutions.

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

  

Exercise 1: ACF

Difficulty: Beginner.

Show solution
RInteractive R
acf(AirPassengers)

  

Exercise 2: PACF

Difficulty: Intermediate.

Show solution
RInteractive R
pacf(AirPassengers)

  

Exercise 3: ADF test

Difficulty: Intermediate.

Show solution
RInteractive R
adf.test(AirPassengers)

  

Exercise 4: KPSS test

Difficulty: Intermediate.

Show solution
RInteractive R
kpss.test(AirPassengers)

  

Exercise 5: ndiffs

Difficulty: Intermediate.

Show solution
RInteractive R
ndiffs(AirPassengers)

  

Exercise 6: nsdiffs (seasonal)

Difficulty: Intermediate.

Show solution
RInteractive R
nsdiffs(AirPassengers)

  

Exercise 7: First difference

Difficulty: Beginner.

Show solution
RInteractive R
diff(AirPassengers)

  

Exercise 8: Seasonal difference

Difficulty: Intermediate.

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

  

Exercise 9: Combined differencing

Difficulty: Advanced.

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

  

Exercise 10: arima(p,d,q)

Difficulty: Intermediate.

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

  

Exercise 11: SARIMA

Difficulty: Advanced.

Show solution
RInteractive R
arima(AirPassengers, order = c(0,1,1), seasonal = list(order = c(0,1,1), period = 12))

  

Exercise 12: auto.arima

Difficulty: Intermediate.

Show solution
RInteractive R
auto.arima(AirPassengers)

  

Exercise 13: stepwise=FALSE for thorough search

Difficulty: Advanced.

Show solution
RInteractive R
auto.arima(AirPassengers, stepwise = FALSE, approximation = FALSE)

  

Exercise 14: Forecast 24 periods

Difficulty: Intermediate.

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

  

Exercise 15: Plot forecast

Difficulty: Intermediate.

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

  

Exercise 16: 80% and 95% prediction intervals

Difficulty: Intermediate.

Show solution
RInteractive R
fit <- auto.arima(AirPassengers) forecast(fit, h = 12, level = c(80, 95))

  

Exercise 17: Residual diagnostics

Difficulty: Advanced.

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

  

Exercise 18: Ljung-Box test

Difficulty: Advanced.

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

  

Exercise 19: AIC compare two ARIMAs

Difficulty: Intermediate.

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))

  

Exercise 20: Train-test eval

Difficulty: Advanced.

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

  

Exercise 21: tsCV 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 22: Log transform before ARIMA

Difficulty: Advanced.

Show solution
RInteractive R
log_ap <- log(AirPassengers) fit <- auto.arima(log_ap) fc <- forecast(fit, h = 12) exp(fc$mean)

  

Exercise 23: ARIMA with regressor (xreg)

Difficulty: Advanced.

Show solution
RInteractive R
y <- AirPassengers trend <- 1:length(y) fit <- auto.arima(y, xreg = trend) forecast(fit, h = 12, xreg = (length(y)+1):(length(y)+12))

  

Exercise 24: Compare ARIMA vs ETS

Difficulty: Advanced.

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

  

Exercise 25: ARIMA from differenced fit interpretation

Difficulty: Advanced.

Show solution
RInteractive R
fit <- arima(AirPassengers, order = c(0,1,1), seasonal = c(0,1,1)) coef(fit) # MA(1) and seasonal MA(1) coefficients

  

What to do next

  • Time-Series-Exercises (shipped), broader time series.
  • Linear-Regression-Exercises (shipped), regression on time features.