Programming for Applications

Chapter 23: Time Series Analysis

Yu-You Liou (NTU)

Shih Chien University

2026-08-02

Data Over Time

Why Time Series Are Different

Time series carry long-term trends and periodic patterns that ordinary summary statistics miss. Analyzing them needs tools that respect order and lag. We revisit the turkey-price series from Chapter 7:

temp <- tempfile(fileext = ".rda")
download.file("https://raw.githubusercontent.com/cran/nutshell/master/data/turkey.price.ts.rda",
              temp, mode = "wb")
load(temp)

Autocorrelation

acf and pacf

The autocorrelation function measures how correlated a series is with its own past, by lag — the signature of seasonality and cycles:

acf(x, lag.max = NULL, type = c("correlation", "covariance", "partial"),
    plot = TRUE, na.action, demean = TRUE, ...)
pacf(x, lag.max, plot, na.action, ...)   # alias: type = "partial"
acf(turkey.price.ts, plot=FALSE)

Autocorrelations of series 'turkey.price.ts', by lag

0.0000 0.0833 0.1667 0.2500 0.3333 0.4167 0.5000 0.5833 0.6667 0.7500 0.8333 
 1.000  0.465 -0.019 -0.165 -0.145 -0.219 -0.215 -0.122 -0.136 -0.200 -0.016 
0.9167 1.0000 1.0833 1.1667 1.2500 1.3333 1.4167 1.5000 1.5833 
 0.368  0.723  0.403 -0.013 -0.187 -0.141 -0.180 -0.226 -0.130 
  • acf plots by default; plot=FALSE returns the numbers.
  • pacf (partial autocorrelation) — correlation at a lag after removing the influence of shorter lags; it helps identify the order of an AR model.

The turkey series shows strong positive autocorrelation at 12-month lags, negative at 6 — clear annual seasonality.

Time Series Models

Autoregressive Models: ar

Time-series models predict future values from past ones. The autoregressive (AR) model regresses each value on its predecessors — ar even picks the order automatically by AIC:

ar(x, aic = TRUE, order.max = NULL, method = c("yule-walker","burg",
   "ols","mle","yw"), ...)
turkey.price.ts.ar <- ar(turkey.price.ts)
turkey.price.ts.ar$order      # order chosen by AIC
[1] 12
  • aic=TRUE selects the order; order.max caps it; method chooses the estimation algorithm.
  • The fitted object reports the chosen order and the AR coefficients.

Forecasting with predict

predict on an ar object projects future values, with standard errors:

turkey.forecast <- predict(turkey.price.ts.ar, n.ahead=12)
turkey.forecast$pred
           Jan       Feb       Mar       Apr       May       Jun       Jul
2008                                         1.8827277 1.7209182 1.7715016
2009 1.5439290 1.6971933 1.5849406 1.7800358                              
           Aug       Sep       Oct       Nov       Dec
2008 1.9416776 1.7791961 1.4822070 0.9894343 1.1588863
2009                                                  
ts.plot(turkey.price.ts, turkey.forecast$pred, col=c("black","blue"), lty=c(1,2))

ARIMA and Beyond

For richer dynamics, arima fits autoregressive integrated moving-average models — the order (p, d, q) capturing AR terms, differencing, and MA terms; seasonal terms extend it to SARIMA:

turkey.arima <- arima(turkey.price.ts, order=c(1,0,0),
                      seasonal=list(order=c(1,0,0), period=12))
turkey.arima

Call:
arima(x = turkey.price.ts, order = c(1, 0, 0), seasonal = list(order = c(1, 
    0, 0), period = 12))

Coefficients:
         ar1    sar1  intercept
      0.1744  0.8596     1.5741
s.e.  0.1127  0.0475     0.0956

sigma^2 estimated as 0.02875:  log likelihood = 23.22,  aic = -38.44
predict(turkey.arima, n.ahead=6)$pred
          May      Jun      Jul      Aug      Sep      Oct
2008 1.846322 1.917925 1.916521 1.863201 1.845705 1.639351

Supporting tools: diff (differencing to remove trend), Box.test (testing residual autocorrelation), HoltWinters (exponential smoothing), StructTS (structural models), and decompose/stl (splitting a series into trend, seasonal, and remainder).

Tip

The Box-Jenkins rhythm. Plot the series; check acf/pacf; difference until stationary; fit an arima; check that the residuals look like white noise (acf, Box.test); then forecast. The modern forecast and fable packages automate much of this (auto.arima).

Additional Topics

The book closes by pointing onward: time series feed into broader analyses — regression with lagged predictors, volatility models (GARCH, package tseries), and multivariate series (VAR). The ts class (Chapter 7) is the common substrate; specialized classes like zoo and xts handle irregular spacing and richer date indexing.