packages <- c("fredr", # FRED API client
"urca", # unit-root tests: augmented Dickey-Fuller
"knitr") # tables in this document
missing <- packages[!vapply(packages, requireNamespace, logical(1), quietly = TRUE)]
if (length(missing) > 0) utils::install.packages(missing, repos = "https://cloud.r-project.org")
library(fredr)
library(urca)
library(knitr)
# stats, utils and graphics are part of base R and always loadedFed Up with Uncertainty
Forecasting US GDP growth with an AR(1): from FRED data to density forecasts
We build, end to end, the kind of GDP forecast a central-bank forecasting division produces: download real GDP from FRED, transform it, choose and estimate a model, check it, forecast with uncertainty bands, and evaluate the forecasts out of sample. The model is deliberately the simplest one, an AR(1), so that every step stays visible.
The question and the plan
Question. What will US real GDP growth be over the next four quarters, and how uncertain is that forecast?
Model. The simplest dynamic model there is, an autoregression of order one:
\[ y_t = c + \phi\, y_{t-1} + \varepsilon_t, \qquad \varepsilon_t \sim WN(0,\sigma^2), \qquad \mu = \frac{c}{1-\phi}, \]
where \(y_t = 100\,(\log Y_t - \log Y_{t-1})\) is quarterly GDP growth in percent.
Three samples. Everything that is chosen (transformation, order \(p\), checks) is decided on the first part of the data only. The rest is kept aside to judge the forecasts honestly.
1985Q1 ............. R = 2009Q4 ................. T (last quarter)
|---- estimation sample ----|------ evaluation sample ------|
Sections 2-3 (choose, fit) Sections 5-6 (forecast, evaluate)
The final forecast (Section 4) re-estimates the model on everything up to \(T\).
Click Show the R code to unfold any chunk. The </> Code button at the top right lets you copy the whole source. All numbers in the text are computed when the document is rendered, so they update automatically with each new FRED release.
Packages
Three packages beyond base R. Missing ones are installed the first time. In the code, every function that comes from a package is written as package::function() (for example stats::arima(), urca::ur.df()), so you always see where it comes from.
Settings
Everything you may want to change is here.
series_id <- "GDPC1" # real GDP, quarterly, seasonally adjusted (FRED)
est_start <- as.Date("1985-01-01") # first quarter of the sample
eval_start <- as.Date("2009-10-01") # R = last quarter of the estimation sample = first forecast origin
H <- 4 # forecast horizon (quarters)
n_origins <- 6 # number of vintages in the vintage table (Section 7)
p_max <- 4 # largest AR order compared with AIC / BIC
adf_lags <- 4 # lagged differences in the ADF test
winsor_level <- 0.99 # winsorisation bounds: 0.5% and 99.5% quantiles of the whole sample
gfc <- as.Date(c("2007-10-01", "2009-04-01")) # Great Financial Crisis (NBER: 2007Q4-2009Q2)
covid <- as.Date(c("2020-01-01", "2021-10-01")) # COVID; also excluded in "ex-COVID" resultsShow the helpers (colours, labels, small functions)
navy <- "#1F3A7D"; red <- "#BE3C32"; grey <- "grey55"; lightnavy <- "#C9D3EA"
qlab <- function(d) paste0(format(d, "%Y"), "Q", (as.integer(format(d, "%m")) - 1) %/% 3 + 1) # date -> "2025Q4"
add_q <- function(d, k) seq(d, by = "3 months", length.out = k + 1)[k + 1] # date + k quarters
fmt <- function(x, k = 2) formatC(x, format = "f", digits = k) # number -> text
# a clean table for the document
tab <- function(df, caption = NULL, align = NULL) knitr::kable(df, caption = caption, align = align, escape = FALSE, row.names = FALSE)
# standard look for base-R figures
nice_par <- function(...) par(mar = c(3, 4.2, 1.2, 1), las = 1, mgp = c(2.6, .7, 0), bty = "l",
col.axis = "grey25", fg = "grey45", ...)1 Data: real-time access through FRED
FRED (Federal Reserve Bank of St. Louis) publishes GDPC1, US real GDP in chained 2017 dollars, quarterly and already seasonally adjusted.
- Create an account on fred.stlouisfed.org (My Account).
- My Account → API Keys → Request API Key.
- Write a one-line description of what you will use it for and accept the terms of use.
- Copy the 32-character key. It is personal: do not share it and do not push it to GitHub.
Store it once, outside the code, so it never appears in the document: create a file named .Renviron in the folder of this document (or in your home folder) containing the single line FRED_API_KEY=your_key, then restart R. For a quick test you can also run fredr::fredr_set_key("YOUR_32_CHARACTER_KEY") in the console (this session only).
The data are downloaded with the FRED API through the {fredr} package. The key is read from .Renviron, so it is never written in the code.
Show the R code
fredr::fredr_set_key(Sys.getenv("FRED_API_KEY")) # personal key, read from .Renviron
gdp <- fredr::fredr_series_observations(
series_id = series_id, # "GDPC1": real GDP
observation_start = as.Date("1947-01-01"),
frequency = "q", # quarterly
units = "lin" # levels, no FRED transformation
)
utils::head(gdp) # columns: date, series_id, value, ... date series_id value realtime_start realtime_end
1 1947-01-01 GDPC1 2182.681 2026-10-13 2026-10-13
2 1947-04-01 GDPC1 2176.892 2026-10-13 2026-10-13
3 1947-07-01 GDPC1 2172.432 2026-10-13 2026-10-13
4 1947-10-01 GDPC1 2206.452 2026-10-13 2026-10-13
5 1948-01-01 GDPC1 2239.682 2026-10-13 2026-10-13
6 1948-04-01 GDPC1 2276.690 2026-10-13 2026-10-13
Show the R code
raw <- data.frame(date = gdp$date, value = gdp$value)
raw <- raw[!is.na(raw$value), ]Downloaded from FRED: the series runs from 1947Q1 to 2026Q2 (318 quarters).
Show the R code
nice_par()
plot(raw$date, raw$value / 1000, type = "l", lwd = 2, col = navy, xlab = "", ylab = "Trillions of chained 2017 USD")
grid(col = "grey90")2 Data processing: trends, outliers and stationarity
2.1 Trends: from the level to the growth rate
The level trends upward, so we model its growth rate \(y_t = 100\,\Delta \log Y_t\) (percent per quarter). Since GDPC1 is already seasonally adjusted at the source, no seasonal step is needed. A raw series would go through X-13ARIMA-SEATS, e.g. with seasonal::seas().
Show the R code
dat <- data.frame(date = raw$date[-1], y = 100 * diff(log(raw$value)))
dat <- dat[dat$date >= est_start, ]
y <- dat$y # raw growth: used to EVALUATE
Tn <- length(y) # number of quarters = position of T
covid_flag <- dat$date >= covid[1] & dat$date <= covid[2]2.2 Outliers and intervention: winsorisation
A few crisis quarters are so extreme that they would dominate any estimate. The bounds are the 0.5% and 99.5% quantiles of the whole sample, but the clipping is applied only in the two crisis windows: the Great Financial Crisis (2007Q4–2009Q2) and COVID (2020Q1–2021Q4): \(y^w_t = \min\big(\max(y_t, q_{lo}),\, q_{hi}\big)\) in those quarters, \(y^w_t = y_t\) otherwise. Only the series fed to the AR(1) is clipped. Forecasts are always judged against the raw data.
Show the R code
tail_share <- (1 - winsor_level) / 2 # 0.005 in each tail
q_lo <- stats::quantile(y, tail_share, names = FALSE) # bounds from the whole sample
q_hi <- stats::quantile(y, 1 - tail_share, names = FALSE)
crisis <- (dat$date >= gfc[1] & dat$date <= gfc[2]) | covid_flag # GFC or COVID quarter
y_est <- y # winsorised series: used to ESTIMATE
y_est[crisis] <- pmin(pmax(y[crisis], q_lo), q_hi) # clip only in the crisis windows
iR <- which(dat$date >= eval_start)[1] # position of R
est_i <- 1:iR
y0 <- y_est[est_i] # estimation sample
T0 <- length(y0)Quarters 166 1985Q1–2026Q2
Mean growth 0.65% per quarter, sd 1.04
Estimation sample 100 up to R = 2009Q4
Clipped quarters 2 bounds [-3.26, 2.80]
Show the R code
nice_par()
plot(dat$date, dat$y, type = "h", lwd = 2, col = ifelse(dat$y >= 0, navy, red), xlab = "",
ylab = expression(y[t] == 100 %*% Delta * log(Y[t]) ~ "(%)"))
for (w in list(gfc, covid)) rect(w[1] - 45, par("usr")[3], w[2] + 45, par("usr")[4], col = "grey92", border = NA)
lines(dat$date, dat$y, type = "h", lwd = 2, col = ifelse(dat$y >= 0, navy, red))
abline(h = mean(y), lty = 2, col = grey)
abline(h = c(q_lo, q_hi), lty = 2, lwd = 1.5, col = "black")
abline(v = dat$date[iR] + 45, col = red, lwd = 2)
mtext("estimation sample", side = 3, at = dat$date[round(iR / 2)], line = -1.2, cex = .8, col = navy)
mtext("evaluation sample", side = 3, at = dat$date[round((iR + Tn) / 2)], line = -1.2, cex = .8, col = red)
grid(col = "grey90")2.3 Cycles: autocorrelation and partial autocorrelation
The ACF measures the correlation between \(y_t\) and \(y_{t-k}\). The PACF measures the same correlation after removing the lags in between. A PACF that cuts off after lag \(p\) points to an AR(\(p\)).
Show the R code
par(mfrow = c(1, 2), mar = c(4, 4, 3, 1), las = 1)
stats::acf(y0, lag.max = 16, main = expression("ACF of " * y[t]), col = navy, lwd = 2, ylab = "")
stats::pacf(y0, lag.max = 16, main = expression("PACF of " * y[t]), col = navy, lwd = 2, ylab = "")
pacf_values <- stats::pacf(y0, lag.max = 4, plot = FALSE)$acf[, 1, 1]The partial autocorrelation is 0.43 at lag 1 and 0.24 at lag 2 (95% band \(\pm\) 0.20): growth has a short memory.
2.4 Stationarity: augmented Dickey–Fuller test
We regress \(\Delta x_t = a + \rho\, x_{t-1} + \sum_{i=1}^{k} b_i \Delta x_{t-i}\ (+\ \text{trend}) + u_t\) and test \(H_0: \rho = 0\) (unit root) with urca::ur.df(): constant and trend for the level, constant only for growth, 4 lagged differences. The ADF statistic is the \(t\)-statistic of \(\rho\) (tau); we reject a unit root when it is below the critical value.
Show the R code
log_level <- 100 * log(raw$value[raw$date >= est_start & raw$date <= dat$date[iR]]) # estimation sample
adf_lvl <- urca::ur.df(log_level, type = "trend", lags = adf_lags, selectlags = "Fixed") # level: constant + trend
adf_gr <- urca::ur.df(y[est_i], type = "drift", lags = adf_lags, selectlags = "Fixed") # growth: constant
adf_level <- adf_lvl@teststat[1, "tau3"] # ADF statistic, level
adf_growth <- adf_gr@teststat[1, "tau2"] # ADF statistic, growth
crit_trend <- adf_lvl@cval["tau3", ] # critical values 1%, 5%, 10%
crit_const <- adf_gr@cval["tau2", ]
summary(adf_gr) # full output of the test on growth
###############################################
# Augmented Dickey-Fuller Test Unit Root Test #
###############################################
Test regression drift
Call:
lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
Residuals:
Min 1Q Median 3Q Max
-2.53993 -0.32860 0.02761 0.33595 1.07169
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.31599 0.11891 2.657 0.00933 **
z.lag.1 -0.46946 0.14642 -3.206 0.00187 **
z.diff.lag1 -0.16983 0.15661 -1.084 0.28111
z.diff.lag2 0.07604 0.15130 0.503 0.61650
z.diff.lag3 -0.01897 0.15133 -0.125 0.90052
z.diff.lag4 0.04001 0.12496 0.320 0.74959
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.565 on 89 degrees of freedom
Multiple R-squared: 0.3287, Adjusted R-squared: 0.291
F-statistic: 8.715 on 5 and 89 DF, p-value: 9.314e-07
Value of test-statistic is: -3.2064 5.1786
Critical values for test statistics:
1pct 5pct 10pct
tau2 -3.51 -2.89 -2.58
phi1 6.70 4.71 3.86
Show the R code
tab(data.frame(
Series = c("$100\\log Y_t$ (level)", "$y_t = 100\\Delta\\log Y_t$ (growth)"),
Deterministics = c("constant + trend", "constant"),
`ADF statistic` = fmt(c(adf_level, adf_growth)),
`5% critical` = fmt(c(crit_trend[2], crit_const[2])),
`Unit root?` = c(ifelse(adf_level > crit_trend[2], "not rejected", "**rejected**"),
ifelse(adf_growth > crit_const[2], "not rejected", "**rejected**")),
check.names = FALSE), align = "llccc")| Series | Deterministics | ADF statistic | 5% critical | Unit root? |
|---|---|---|---|---|
| \(100\log Y_t\) (level) | constant + trend | -1.52 | -3.45 | not rejected |
| \(y_t = 100\Delta\log Y_t\) (growth) | constant | -3.21 | -2.89 | rejected |
The ADF test does not reject a unit root in the level and rejects it in growth. We therefore model growth.
3 Model specification, estimation and diagnostic checking
Everything in this step uses the estimation sample only (1985Q1–2009Q4).
3.1 Model specification: choosing the order \(p\)
For \(p = 0,\dots,\) 4 we regress \(y_t\) on a constant and \(p\) lags, on the same observations so that the criteria are comparable: \(\text{AIC} = n\log\hat\sigma^2 + 2k\) and \(\text{BIC} = n\log\hat\sigma^2 + k\log n\). Smaller is better.
Show the R code
ic <- as.data.frame(t(sapply(0:p_max, function(p) {
yy <- y0[(p_max + 1):T0]
X <- cbind(rep(1, length(yy)), if (p > 0) sapply(1:p, function(i) y0[(p_max + 1 - i):(T0 - i)]))
n <- length(yy)
e <- yy - X %*% solve(crossprod(X), crossprod(X, yy))
s2 <- sum(e^2) / n; k <- ncol(X) + 1
c(p = p, AIC = n * log(s2) + 2 * k, BIC = n * log(s2) + log(n) * k)
})))
best <- function(v) which(v == min(v))
bold <- function(v) ifelse(seq_along(v) == best(v), paste0("**", fmt(v), "**"), fmt(v))
tab(data.frame(`AR order p` = ic$p, AIC = bold(ic$AIC), BIC = bold(ic$BIC), check.names = FALSE), align = "ccc")| AR order p | AIC | BIC |
|---|---|---|
| 0 | -86.34 | -81.21 |
| 1 | -104.32 | -96.63 |
| 2 | -107.79 | -97.54 |
| 3 | -106.54 | -93.72 |
| 4 | -104.78 | -89.39 |
AIC selects \(p =\) 2 and BIC selects \(p =\) 2. We keep the AR(1): it is simple, transparent, and a standard benchmark that any richer model has to beat.
3.2 Estimation: OLS and maximum likelihood
Regress \(y_t\) on \((1, y_{t-1})\). This gives \(\hat\beta = (X'X)^{-1}X'y\), \(\hat\sigma^2 = \hat e'\hat e/(n-2)\) and \(\text{se} = \sqrt{\text{diag}((X'X)^{-1})\,\hat\sigma^2}\). As a cross-check we also estimate by maximum likelihood with stats::arima().
Show the R code
ar1_ols <- function(y) {
yy <- y[-1]; X <- cbind(1, y[-length(y)]); n <- length(yy)
XtXi <- solve(crossprod(X)); b <- XtXi %*% crossprod(X, yy)
e <- drop(yy - X %*% b); s2 <- sum(e^2) / (n - 2)
list(c = b[1], phi = b[2], mu = b[1] / (1 - b[2]), sigma2 = s2, se = sqrt(diag(XtXi) * s2), res = e, n = n)
}
fit <- ar1_ols(y0) # 1985Q1 - R
fit_full <- ar1_ols(y_est) # 1985Q1 - T (final forecast)
ml <- stats::arima(y0, order = c(1, 0, 0), method = "ML")
phi_ml <- unname(stats::coef(ml)[1]); mu_ml <- unname(stats::coef(ml)[2]) # arima reports the MEAN, not c
t_stat <- c(fit$c, fit$phi) / fit$se
p_val <- 2 * stats::pt(-abs(t_stat), fit$n - 2)
tab(data.frame(
Parameter = c("$c$ (intercept)", "$\\phi$ (AR coefficient)", "$\\sigma_\\varepsilon$"),
`OLS estimate` = fmt(c(fit$c, fit$phi, sqrt(fit$sigma2)), 3),
`Std. error` = c(fmt(fit$se, 3), ""),
`t-stat` = c(fmt(t_stat), ""),
`p-value` = c(fmt(p_val, 3), ""),
`ML (arima)` = fmt(c(mu_ml * (1 - phi_ml), phi_ml, sqrt(ml$sigma2)), 3),
check.names = FALSE), align = "lccccc")| Parameter | OLS estimate | Std. error | t-stat | p-value | ML (arima) |
|---|---|---|---|---|---|
| \(c\) (intercept) | 0.388 | 0.084 | 4.59 | 0.000 | 0.394 |
| \(\phi\) (AR coefficient) | 0.432 | 0.092 | 4.72 | 0.000 | 0.429 |
| \(\sigma_\varepsilon\) | 0.566 | 0.558 |
Persistence \(\hat\phi\) 0.432 s.e. 0.092
Mean growth \(\hat\mu\) 0.68% per quarter, ≈ 2.7% a year
Shock size \(\hat\sigma_\varepsilon\) 0.57 percentage points
On the estimation sample, winsorisation changes almost nothing: \(\hat\phi =\) 0.432 on the raw data, because the clipped quarters fall in COVID, after R. On the full sample it matters a lot: \(\hat\phi =\) 0.235 winsorised against -0.128 raw. 2 quarters out of 166 would otherwise rewrite the dynamics of the whole model.
3.3 Diagnostic checking
A well-specified model leaves residuals that are white noise. We check four properties: no autocorrelation (Ljung–Box), normality (Jarque–Bera), constant variance (Ljung–Box on \(\hat\varepsilon_t^2\)), and stationarity (\(\lvert\hat\phi\rvert < 1\)).
Show the R code
diag_tests <- function(res, phi) {
skew <- mean((res - mean(res))^3) / stats::sd(res)^3
kurt <- mean((res - mean(res))^4) / stats::sd(res)^4
jb <- length(res) / 6 * (skew^2 + (kurt - 3)^2 / 4)
list(lb4 = stats::Box.test(res, lag = 4, type = "Ljung-Box", fitdf = 1)$p.value,
lb8 = stats::Box.test(res, lag = 8, type = "Ljung-Box", fitdf = 1)$p.value,
jb = stats::pchisq(jb, 2, lower.tail = FALSE),
arch = stats::Box.test(res^2, lag = 4, type = "Ljung-Box")$p.value,
sk = skew, ku = kurt, phi = abs(phi))
}
d_est <- diag_tests(fit$res, fit$phi)
d_full <- diag_tests(fit_full$res, fit_full$phi)
reading <- function(p) ifelse(p > .05, "OK", "**rejected**")
pv <- function(p) fmt(p, 3)
tab(data.frame(
Property = c("No autocorrelation", "No autocorrelation", "Normality", "Constant variance", "Stationarity"),
Test = c("Ljung-Box, 4 lags", "Ljung-Box, 8 lags", "Jarque-Bera", "Ljung-Box on $\\hat\\varepsilon_t^2$", "$\\lvert\\hat\\phi\\rvert$ (should be < 1)"),
`p (est.)` = c(pv(d_est$lb4), pv(d_est$lb8), pv(d_est$jb), pv(d_est$arch), fmt(d_est$phi)),
`p (full)` = c(pv(d_full$lb4), pv(d_full$lb8), pv(d_full$jb), pv(d_full$arch), fmt(d_full$phi)),
`Reading (est.)` = c(reading(d_est$lb4), reading(d_est$lb8), reading(d_est$jb), reading(d_est$arch),
ifelse(d_est$phi < 1, "OK", "**rejected**")),
check.names = FALSE), align = "llccl")| Property | Test | p (est.) | p (full) | Reading (est.) |
|---|---|---|---|---|
| No autocorrelation | Ljung-Box, 4 lags | 0.040 | 0.744 | rejected |
| No autocorrelation | Ljung-Box, 8 lags | 0.095 | 0.923 | OK |
| Normality | Jarque-Bera | 0.000 | 0.000 | rejected |
| Constant variance | Ljung-Box on \(\hat\varepsilon_t^2\) | 0.561 | 0.000 | OK |
| Stationarity | \(\lvert\hat\phi\rvert\) (should be < 1) | 0.43 | 0.24 | OK |
Show the R code
res_dates <- dat$date[est_i][-1]
par(mfrow = c(1, 3), mar = c(4, 3.8, 3, 1), las = 1, cex.main = .95)
plot(res_dates, fit$res, type = "h", col = navy, lwd = 2, xlab = "", ylab = "", main = expression("Residuals " * hat(epsilon)[t]))
abline(h = 0, col = grey)
stats::acf(fit$res, lag.max = 16, main = expression("ACF of " * hat(epsilon)[t]), col = navy, lwd = 2, ylab = "")
qqnorm(fit$res, main = "Normal Q-Q plot", pch = 16, col = navy, xlab = "", ylab = "")
qqline(fit$res, col = red, lwd = 2)Residual skewness is -0.84 and kurtosis is 5.17 (3 for a normal distribution). Heavy tails come from the 2008–09 recession.
4 Point and density forecasts: the iterated approach
For an AR(1) the forecasts and their uncertainty have closed forms:
\[ \hat y_{T+h|T} = \mu + \phi^h (y_T - \mu), \qquad \text{Var}(e_{T+h|T}) = \sigma^2 \sum_{j=0}^{h-1}\phi^{2j}, \qquad y_{T+h}\mid\Omega_T \sim N\!\big(\hat y_{T+h|T},\ \text{Var}(e_{T+h|T})\big). \]
The forecast reverts to the mean \(\mu\) at speed \(\phi\), and the bands widen with \(h\) towards the unconditional variance.
Show the R code
ar1_forecast <- function(t, H) { # forecast made at origin t
f <- ar1_ols(y_est[1:t]); h <- 1:H
data.frame(h = h, yhat = f$mu + f$phi^h * (y_est[t] - f$mu),
sd = sqrt(f$sigma2 * sapply(h, function(k) sum(f$phi^(2 * (0:(k - 1)))))))
}
fc <- ar1_forecast(Tn, H) # model estimated on 1985Q1 - T
fdates <- as.Date(sapply(1:H, function(k) add_q(max(dat$date), k)), origin = "1970-01-01")
tab(data.frame(Quarter = qlab(fdates), h = fc$h, Forecast = fmt(fc$yhat), `Std. dev.` = fmt(fc$sd),
`80% interval` = paste0("[", fmt(fc$yhat - stats::qnorm(.9) * fc$sd), ", ", fmt(fc$yhat + stats::qnorm(.9) * fc$sd), "]"),
`95% interval` = paste0("[", fmt(fc$yhat - stats::qnorm(.975) * fc$sd), ", ", fmt(fc$yhat + stats::qnorm(.975) * fc$sd), "]"),
`Annualised` = paste0(fmt(4 * fc$yhat, 1), "%"), # annualised log growth: 4 x quarterly
check.names = FALSE), align = "ccccccc",
caption = paste0("Forecasts made at T = ", qlab(max(dat$date)), " (growth in % per quarter)"))| Quarter | h | Forecast | Std. dev. | 80% interval | 95% interval | Annualised |
|---|---|---|---|---|---|---|
| 2026Q3 | 1 | 0.62 | 0.65 | [-0.21, 1.46] | [-0.65, 1.90] | 2.5% |
| 2026Q4 | 2 | 0.64 | 0.67 | [-0.22, 1.50] | [-0.67, 1.95] | 2.6% |
| 2027Q1 | 3 | 0.65 | 0.67 | [-0.21, 1.51] | [-0.67, 1.96] | 2.6% |
| 2027Q2 | 4 | 0.65 | 0.67 | [-0.21, 1.51] | [-0.67, 1.96] | 2.6% |
Last observation \(y_T\) 0.55% 2026Q2
Forecast \(\hat y_{T+1|T}\) 0.62% 2026Q3, ± 1.28 (95%)
Forecast \(\hat y_{T+4|T}\) 0.65% 2027Q2, ± 1.32 (95%)
Show the R code
nice_par()
hist_d <- utils::tail(dat$date, 16); hist_y <- utils::tail(y, 16)
ylim <- range(c(hist_y, fc$yhat + 2 * fc$sd, fc$yhat - 2 * fc$sd))
plot(c(hist_d, fdates), rep(NA, 16 + H), ylim = ylim, xlab = "", ylab = expression(y[t] ~ "(%)"))
for (level in c(0.95, 0.80, 0.50)) {
z <- stats::qnorm(1 - (1 - level) / 2); xs <- c(max(hist_d), fdates)
lo <- c(utils::tail(y, 1), fc$yhat - z * fc$sd); hi <- c(utils::tail(y, 1), fc$yhat + z * fc$sd)
polygon(c(xs, rev(xs)), c(lo, rev(hi)), border = NA,
col = adjustcolor(navy, alpha.f = c("0.95" = .15, "0.8" = .25, "0.5" = .40)[as.character(level)]))
}
abline(h = fit$mu, lty = 3, col = grey)
lines(hist_d, hist_y, lwd = 2, col = "black")
lines(c(max(hist_d), fdates), c(utils::tail(y, 1), fc$yhat), lwd = 2.5, col = red)
points(fdates, fc$yhat, pch = 16, col = red)
abline(v = max(hist_d), lty = 2, col = grey)
grid(col = "grey90")
legend("topleft", bty = "n", cex = .8, ncol = 2, lwd = c(2, 2.5, 8, 8, 8),
col = c("black", red, adjustcolor(navy, .40), adjustcolor(navy, .25), adjustcolor(navy, .15)),
legend = c("Data", expression(hat(y)[T + h * "|" * T]), "50% interval", "80% interval", "95% interval"))5 Pseudo out-of-sample forecasting exercise
How good would this forecast have been in the past? For every origin \(t = R, \dots, T-1\) we:
- re-estimate the AR(1) on \(y_1,\dots,y_t\) only (an expanding window);
- forecast \(h = 1,\dots,H\) quarters ahead;
- compare with what actually happened, using the raw data, never winsorised.
It is pseudo real time because we use today’s revised data, not the data available at the time.
Show the R code
oos <- do.call(rbind, lapply(iR:(Tn - 1), function(t) {
f <- ar1_forecast(t, min(H, Tn - t))
data.frame(origin = dat$date[t], h = f$h, target = dat$date[t + f$h], yhat = f$yhat, sd = f$sd,
actual = y[t + f$h], covid = covid_flag[t] | covid_flag[t + f$h])
}))
oos$err <- oos$actual - oos$yhat # e_{t+h|t}
oos$pit <- stats::pnorm(oos$actual, oos$yhat, oos$sd) # PIT
oos$logs <- stats::dnorm(oos$actual, oos$yhat, oos$sd, log = TRUE) # log score
oos$in80 <- abs(oos$err) <= stats::qnorm(.90) * oos$sd
oos$in95 <- abs(oos$err) <= stats::qnorm(.975) * oos$sd
e1 <- oos$err[oos$h == 1]
lb_e1 <- stats::Box.test(e1, lag = 4, type = "Ljung-Box")$p.value
p_mean0 <- stats::t.test(e1)$p.value
show <- utils::head(oos[oos$h == 1, c("origin", "target", "yhat", "sd", "actual", "err", "pit")], 6)
show$origin <- qlab(show$origin); show$target <- qlab(show$target)
show[, 3:7] <- lapply(show[, 3:7], fmt)
tab(show, caption = "First one-step-ahead forecasts (all forecasts are kept in the object `oos`)", align = "ccccccc")| origin | target | yhat | sd | actual | err | pit |
|---|---|---|---|---|---|---|
| 2009Q4 | 2010Q1 | 0.85 | 0.57 | 0.48 | -0.37 | 0.26 |
| 2010Q1 | 2010Q2 | 0.59 | 0.56 | 0.96 | 0.37 | 0.74 |
| 2010Q2 | 2010Q3 | 0.80 | 0.56 | 0.77 | -0.03 | 0.48 |
| 2010Q3 | 2010Q4 | 0.72 | 0.56 | 0.52 | -0.20 | 0.36 |
| 2010Q4 | 2011Q1 | 0.61 | 0.56 | -0.24 | -0.85 | 0.06 |
| 2011Q1 | 2011Q2 | 0.28 | 0.56 | 0.67 | 0.40 | 0.76 |
There are 66 origins and 258 forecasts in total. The mean one-step error is -0.027 (p-value of a zero mean: 0.890), and the Ljung–Box p-value of the one-step errors is 0.040.
6 Point and density forecast evaluation
For each horizon we report the bias (mean error), the RMSFE (with and without COVID), the empirical coverage of the 80% and 95% intervals, and the average log score (higher is better).
Show the R code
ev <- do.call(rbind, lapply(1:H, function(k) {
s <- oos[oos$h == k, ]; sx <- s[!s$covid, ]
data.frame(h = k, n = nrow(s), ME = mean(s$err), RMSFE = sqrt(mean(s$err^2)),
RMSFE_xc = sqrt(mean(sx$err^2)), cov80 = 100 * mean(s$in80),
cov95 = 100 * mean(s$in95), LS = mean(s$logs))
}))
tab(data.frame(`$h$` = ev$h, `$N_h$` = ev$n, `Mean error` = fmt(ev$ME), RMSFE = fmt(ev$RMSFE),
`RMSFE ex-COVID` = fmt(ev$RMSFE_xc), `80% cov.` = paste0(fmt(ev$cov80, 0), "%"),
`95% cov.` = paste0(fmt(ev$cov95, 0), "%"), `Log score` = fmt(ev$LS), check.names = FALSE),
align = "cccccccc")| \(h\) | \(N_h\) | Mean error | RMSFE | RMSFE ex-COVID | 80% cov. | 95% cov. | Log score |
|---|---|---|---|---|---|---|---|
| 1 | 66 | -0.03 | 1.56 | 0.39 | 85% | 94% | -4.00 |
| 2 | 65 | -0.04 | 1.50 | 0.37 | 86% | 94% | -3.84 |
| 3 | 64 | -0.06 | 1.49 | 0.37 | 86% | 94% | -3.83 |
| 4 | 63 | -0.06 | 1.50 | 0.37 | 86% | 94% | -3.85 |
At \(h=1\) the RMSFE is 1.56 pp, but only 0.39 pp without the COVID window: a few quarters dominate squared-error measures. The 80% and 95% intervals contain 85% and 94% of the outcomes at \(h = 1\), against nominal coverage of 80% and 95%.
Show the R code
par(mfrow = c(1, 2), mar = c(3, 4, 2, 1), las = 1)
for (k in c(1, 4)) {
s <- oos[oos$h == k, ]
plot(s$target, s$actual, type = "l", lwd = 2, col = "black", xlab = "", ylab = "",
main = bquote(hat(y)[t + .(k) * "|" * t] ~ "vs realised"), ylim = c(-3.5, 3.5))
polygon(c(s$target, rev(s$target)), c(s$yhat - 1.645 * s$sd, rev(s$yhat + 1.645 * s$sd)),
col = adjustcolor(navy, .2), border = NA)
lines(s$target, s$yhat, col = red, lwd = 2)
grid(col = "grey90")
}Show the R code
nice_par()
plot(ev$h, ev$RMSFE, type = "b", pch = 16, lwd = 2, col = navy, ylim = c(0, max(ev$RMSFE) * 1.05),
xlab = "Horizon h (quarters)", ylab = "RMSFE (pp)", xaxt = "n")
axis(1, at = 1:H)
lines(ev$h, ev$RMSFE_xc, type = "b", pch = 17, lwd = 2, col = red)
grid(col = "grey90")
legend("center", bty = "n", cex = .85, pch = c(16, 17), lwd = 2, col = c(navy, red), legend = c("All", "Ex-COVID"))Show the R code
par(mfrow = c(1, 3), mar = c(3.5, 3.5, 2, 1), las = 1)
for (k in c(1, 2, 4)) {
hist(oos$pit[oos$h == k], breaks = seq(0, 1, 0.2), freq = FALSE, col = lightnavy, border = "white",
main = bquote("PIT, h = " * .(k)), xlab = "", ylab = "", ylim = c(0, 3))
abline(h = 1, col = red, lwd = 2, lty = 2)
}The PIT of an outcome is the forecast CDF evaluated at that outcome: \(z = F(y)\). Suppose the forecast is \(N(0.68, 0.57^2)\) and the realised value is \(1.20\). Then \(z = F(1.20) =\) 0.82. If the forecast densities are correct, the \(z\)’s are uniform on \([0,1]\), so \(P(z \le u) = u\).
Show the R code
m_ill <- 0.68; s_ill <- 0.57; y_ill <- 1.20; z_ill <- stats::pnorm(y_ill, m_ill, s_ill)
grid_y <- seq(m_ill - 3.3 * s_ill, m_ill + 3.3 * s_ill, length.out = 300)
par(mfrow = c(1, 2), mar = c(4, 3.5, 2, 1), las = 1)
plot(grid_y, stats::dnorm(grid_y, m_ill, s_ill), type = "l", lwd = 2, col = navy, xlab = "y", ylab = "", yaxt = "n",
main = "Density f: area up to y = z")
left <- grid_y[grid_y <= y_ill]
polygon(c(left, rev(left)), c(stats::dnorm(left, m_ill, s_ill), rep(0, length(left))), col = lightnavy, border = NA)
lines(grid_y, stats::dnorm(grid_y, m_ill, s_ill), lwd = 2, col = navy); abline(v = y_ill, col = red, lwd = 2)
plot(grid_y, stats::pnorm(grid_y, m_ill, s_ill), type = "l", lwd = 2, col = navy, xlab = "y", ylab = "", ylim = c(0, 1),
main = "CDF F: height at y = z")
segments(min(grid_y), z_ill, y_ill, z_ill, col = red, lty = 2, lwd = 2)
abline(v = y_ill, col = red, lwd = 2); points(y_ill, z_ill, pch = 16, col = red)
text(min(grid_y), z_ill + .06, bquote("z = F(y) = " * .(round(z_ill, 2))), col = red, adj = 0, cex = .85)7 Communication: the vintage table
This is how forecasting divisions track their own forecasts. Each column is a vintage \(t\) (the model re-estimated with data up to \(t\)), each row is a target quarter, and each cell is the forecast of that target made at that vintage. Every coloured diagonal is one horizon \(h\). Reading a row from left to right shows how the forecast of one quarter was revised as new data came in.
Show the R code
vintages <- utils::tail(dat$date, n_origins)
targets <- as.Date(sapply(seq_len(n_origins + H - 1), function(k) add_q(vintages[1], k)), origin = "1970-01-01")
fbv <- lapply(vintages, function(v) ar1_forecast(which(dat$date == v), H)$yhat)
cell <- function(r, i) { h <- r - i + 1
if (h < 1 || h > H) "<td></td>" else sprintf("<td class='hz%d'>%s</td>", min(h, 4), fmt(fbv[[i]][h])) }
rows <- vapply(seq_along(targets), function(r) {
real <- if (targets[r] <= max(dat$date)) fmt(y[match(targets[r], dat$date)]) else ""
paste0("<tr><td style='text-align:left'><b>", qlab(targets[r]), "</b></td><td><b>", real, "</b></td>",
paste(vapply(seq_len(n_origins), function(i) cell(r, i), ""), collapse = ""), "</tr>")
}, "")
cat("<table class='vintage'><thead><tr><th style='text-align:left'>Target (rows) / vintage t (columns)</th><th>Realised y</th>",
paste0("<th>", qlab(vintages), "</th>", collapse = ""), "</tr></thead><tbody>",
paste(rows, collapse = ""), "</tbody></table>",
"<p style='text-align:center;font-size:.85rem;color:#5a6475'>Shading: <span style='background:#dbe4f5;padding:0 .4rem'>h = 1</span> ",
"<span style='background:#e8eefa;padding:0 .4rem'>h = 2</span> <span style='background:#f3dcd9;padding:0 .4rem'>h = 3</span> ",
"<span style='background:#f9ecea;padding:0 .4rem'>h = 4</span></p>", sep = "")| Target (rows) / vintage t (columns) | Realised y | 2025Q1 | 2025Q2 | 2025Q3 | 2025Q4 | 2026Q1 | 2026Q2 |
|---|---|---|---|---|---|---|---|
| 2025Q2 | 0.99 | 0.50 | |||||
| 2025Q3 | 0.95 | 0.61 | 0.73 | ||||
| 2025Q4 | 0.05 | 0.64 | 0.67 | 0.72 | |||
| 2026Q1 | 0.62 | 0.64 | 0.66 | 0.67 | 0.51 | ||
| 2026Q2 | 0.55 | 0.65 | 0.66 | 0.61 | 0.64 | ||
| 2026Q3 | 0.65 | 0.64 | 0.65 | 0.62 | |||
| 2026Q4 | 0.65 | 0.65 | 0.64 | ||||
| 2027Q1 | 0.65 | 0.65 | |||||
| 2027Q2 | 0.65 |
Shading: h = 1 h = 2 h = 3 h = 4
Practical use for decision making: where judgement comes in
The AR(1) is a benchmark, not the final forecast. In a central bank or a research institute, the model output is the starting point of a discussion:
- Mean reversion. With \(\hat\phi =\) 0.24 the forecast is back near the mean \(\hat\mu =\) 0.65% within a couple of quarters. The model knows nothing about the business cycle, policy or news.
- Exceptional periods. Winsorising the crisis quarters is one way to keep the GFC and COVID from distorting the model. Others are common in practice:
- dummy variables (one per extreme quarter) in the regression, so those quarters do not move \(\hat\phi\) or \(\hat\sigma\);
- a COVID factor, as in Stock and Watson (2025), which captures the pandemic-specific comovements on top of the usual factors;
- simply not assessing the model in that period: drop the COVID quarters from the evaluation (the “ex-COVID” RMSFE above);
- fat-tailed or time-varying errors (Student-\(t\) shocks, stochastic volatility, outlier-adjusted models), so the density forecasts widen when volatility jumps.
- Real time. GDP is revised. A true real-time exercise would use the data as published at each date (
fredr’srealtime_start/realtime_endor ALFRED), which usually makes past forecasts look worse. - Judgement and conditioning. Forecasters overlay information the model ignores (surveys, high-frequency indicators, known shocks) and document every deviation from the model in the vintage table.
- Beating the benchmark. Any richer model (AR(\(p\)), VAR, factor model, nowcast) should be compared with this one on the same pseudo out-of-sample design and the same metrics.
Rendered on 13 October 2026 at 17:08 with R version 4.3.3 (2024-02-29). Data: FRED (GDPC1), last observation 2026Q2.
R version 4.3.3 (2024-02-29)
References. Grobéty, M., Economic Forecasting for Decision Making, lecture slides. Vermandel, G., Building a Sample for Time Series Analysis. Hamilton, J. (1994), Time Series Analysis, Princeton UP. Boysel, S. and Vaughan, D., fredr: an R client for the FRED API.