Fitting a vector autoregression (VAR) to two related series
Today let's learn how to forecast two related series together, using a model called a vector autoregression.
Let's say you follow the Canadian economy. For every quarter from 1980Q1 to 2000Q4 you have two numbers: the unemployment rate in percent, called U, and an employment index called e. That is 84 quarters of each.
The index e is 100 times the natural log of employment, and it climbs from 929.6 to 961.8 over these years. U starts at 7.53% in 1980Q1, peaks at 12.77% in 1982Q4 and ends at 6.87% in 2000Q4.
Now, you want to forecast U. The usual approach is to use the past of U itself. But employment moves at the same time as unemployment. So here is the question for this lesson: does the past of e add anything to a forecast of U, beyond what U's own past already gives?
The line above is U, quarter by quarter, from 7.53 in 1980Q1 through the peak of 12.77 in 1982Q4 to 6.87 in 2000Q4. This is the series we will forecast, first on its own and then with help from e.
How to predict a series from its own past with an AR(2)
Before adding employment, let's build the simplest forecast of U: one that uses only U's own past. The code below loads the Canada data from the vars package, keeps the two series and plots them.
plot() draws one panel per series, with e on top and U below. The output confirms 84 rows, from the first quarter of 1980 to the fourth quarter of 2000.
To forecast U from its own past we need its lags. A lag is the same series shifted back in time. The lag 1 of U is U one quarter earlier, and the lag 2 is U two quarters earlier.
Regressing a series on its own lags is called an autoregression, and the number of lags it uses is its order. So an AR(2) predicts U this quarter from a constant, U one quarter back and U two quarters back.
The first two quarters do not have both lags, so 2 rows are lost and 84 - 2 = 82 rows are usable. The code below builds the lag columns for both series into a data frame called L, then fits one AR(2) per series with lm(). Each row of L is one quarter from 1980Q3 on: e and U are that quarter, U.l1 and U.l2 are U one and two quarters earlier, and e.l1 and e.l2 are the same for e.
The U fit predicts U this quarter as 0.515, plus 1.542 times last quarter's U, minus 0.597 times U from two quarters back. The e fit has the same form, with its own three coefficients.
The last line prints the residual standard error of each fit. It is the typical size of a residual, the gap between an actual quarter and its fitted value. It is in the units of the series: 0.354 for U and 0.410 for e. We will keep these two numbers as the baseline.
Each fit uses only its own series. The U fit has no e term in it, and the e fit has no U term.
How does last quarter's change in employment relate to the change in unemployment?
The AR(2) fits leave out the lags of the other series. To see whether that could matter, let's compare U with last quarter's employment.
Employment trends upward through the sample, from 929.6 in 1980Q1 to 961.8 in 2000Q4. So a correlation between the levels of e and U would reflect that trend, not quarter-to-quarter movement. To look at that movement we use changes.
Last quarter's change in e is e(t-1) minus e(t-2), and this quarter's change in U is U(t) minus U(t-1). One unit of e is about 1% employment growth, because e is 100 times a natural log.
The chart below plots the 82 pairs: last quarter's change in e on the x axis, this quarter's change in U on the y axis.
The chart shows r = -0.66, where r is the correlation between the two changes. A negative r means that when e rose more than usual last quarter, U tended to fall this quarter. The histogram button shows how last quarter's changes in e are spread.
How does that compare with what U's own past offers? The code below builds a data frame D of four changes. dU and de are this quarter's changes in U and e, and dU.l1 and de.l1 are the same changes one quarter earlier. It then correlates this quarter's change in U with the previous change in e and with the previous change in U.
The correlation with last quarter's change in e is -0.660, and with U's own last change it is 0.566. So the previous change in e tracks the change in U more closely than U's own previous change does.
How does last quarter's change in unemployment relate to the change in employment?
Now the other direction: does last quarter's change in U relate to this quarter's change in e? The chart below plots the same 82 quarters the other way around: last quarter's change in U on the x axis, this quarter's change in e on the y axis.
The chart shows r = -0.62, which is -0.618 to 3 decimals. The code below prints that correlation next to two more. One is the change in e against its own previous change, and the other is the two changes within the same quarter.
The change in e correlates -0.618 with U's previous change and 0.725 with e's own previous change. So e's own past is the closer of the two, but both correlations are large. Whether the past of U adds anything once e's own lags are in the equation is a different question, and the coefficients of the fitted VAR show it.
The third number, -0.857, is the correlation between the changes in U and in e within the same quarter. When employment growth is high, unemployment tends to fall in that same quarter. We will need this number again when we interpret the fitted coefficients.
Quick check: what does an AR(2) for unemployment not use?
Think about which series the AR(2) for U has as predictors.
A VAR(2) as one regression per series on the lags of every series
The two series move together, so a vector autoregression gives each series its own equation. Every equation has the lags of both series in it. It is called a vector autoregression because, in each quarter, the two values are stacked into one vector, (e, U). The system is a regression of that vector on its own past.
A VAR(p) for k series regresses every series on p lags of all k series, plus a constant. Every series is endogenous: it is explained inside the system, as the outcome of its own equation. None is treated as a fixed input. For our 2 series with 2 lags that gives two equations, one for e and one for U:
\[ e_t = c_1 + a_{11} e_{t-1} + a_{12} U_{t-1} + b_{11} e_{t-2} + b_{12} U_{t-2} + \varepsilon_{1,t} \]
\[ U_t = c_2 + a_{21} e_{t-1} + a_{22} U_{t-1} + b_{21} e_{t-2} + b_{22} U_{t-2} + \varepsilon_{2,t} \]
In a coefficient such as \(a_{12}\), the first subscript says which equation it belongs to (1 is e, 2 is U). The second says which series the lag belongs to. The a's multiply the lag 1 values and the b's the lag 2 values. The error term is written with epsilon because e already names employment.
Stacking the two equations gives the compact form of a VAR(2). Here \(\mathbf{y}_t\) holds \(e_t\) and \(U_t\), \(\mathbf{c}\) holds the two constants, \(A_1\) holds the a coefficients and \(A_2\) holds the b coefficients:
\[ \mathbf{y}_t = \mathbf{c} + A_1 \mathbf{y}_{t-1} + A_2 \mathbf{y}_{t-2} + \boldsymbol{\varepsilon}_t \]
Each equation has 5 coefficients: a constant, and 2 lags of each of the 2 series. So the two equations have 10 coefficients between them, which in general is k(kp + 1) for k series and p lags.
That count grows quickly: 4 series with 2 lags have 36 coefficients, and 4 series with 4 lags have 68. Here, each equation's 5 coefficients are estimated from 82 rows.
Each equation is fitted by ordinary least squares (OLS), the same method lm() uses, one equation at a time. The code below fits the VAR(2) with VAR() from the vars package, then pulls out the coefficients of the U equation. Here type = "const" puts a constant in each equation.
coef(fit)$U is a table with one row per coefficient, and we keep its Estimate column. The five numbers are the coefficients of the U equation: 12.581 for the constant, -0.644 on e.l1, 0.741 on U.l1, 0.632 on e.l2 and 0.153 on U.l2. The names match the columns of L.
To check that each equation is an ordinary regression, the code below refits both equations with lm() on the same five regressors. It then compares the coefficients.
For both equations the largest difference between the two sets of coefficients is below 1e-10, which is numerical noise. So a VAR needs no new estimation method: it is two regressions, each with the lags of both series as predictors.
How a fitted VAR(2) produces a forecast one or several quarters ahead
With the coefficients in hand, a forecast is arithmetic. To forecast 2001Q1, we put the last two observed quarters into each equation. 2000Q4 (e 961.766, U 6.87) is lag 1 and 2000Q3 (e 961.029, U 6.93) is lag 2.
The code below does that by hand for the e equation with a small function. It then compares the result with predict() from the vars package.
Both routes give 962.333 for e in 2001Q1. The function uses the stored coefficients, not the rounded ones we print. A coefficient rounded to 3 decimals can be off by 0.0005. Multiplied by an e near 960, that is an error of up to 0.48 for each coefficient.
The horizon h is the number of quarters ahead. A forecast for h = 2 needs e and U for 2001Q1, but those are not observed. So the 2001Q1 forecasts of both series replace the observed row as lag 1, and 2000Q4 moves to lag 2.
The code below forecasts e for 2001Q2 that way. It takes the 2001Q1 forecasts of e and U from predict(), then prints the four-quarter forecasts for both series.
The hand value, 962.743, is the second e forecast from predict(). So predict() feeds each forecast back in as a lag, one quarter at a time, until it reaches the horizon you ask for.
An AR(2) forecast is the same computation with one series: its own two lags in one equation.
How to read the coefficient block: own-lag and cross-lag coefficients
The table below is the coefficient block: the coefficients of both equations side by side, with the t value of each.
The code below builds the same table from the fitted VAR, so you can reproduce it.
- An own-lag coefficient multiplies a lag of the equation's own series: U.l1 and U.l2 in the U equation, e.l1 and e.l2 in the e equation. A cross-lag coefficient multiplies a lag of the other series: e.l1 and e.l2 in the U equation, U.l1 and U.l2 in the e equation.
- A t value is the estimate divided by its standard error, which measures how much the estimate would vary from sample to sample. When its size is above about 2, the estimate can be told apart from 0.
- The two lags of a series belong together, so we interpret them as a pair.
Start with the U equation. The cross-lag coefficients on e are -0.644 and 0.632, with t values of -5.57 and 5.51. Both are far above 2 in size. Their sum is -0.012.
So raising both e lags by 1 moves the U forecast by almost nothing, while raising only last quarter's e by 1 moves it by -0.644. This is because -0.644 * e.l1 + 0.632 * e.l2 equals -0.644 * (e.l1 - e.l2) - 0.012 * e.l2. The U forecast therefore changes by about -0.64 points for each unit of employment growth e(t-1) - e(t-2), with e(t-2) held fixed. One unit is about 1%.
The own-lag coefficients of U changed too. In the AR(2) they were 1.542 and -0.597, and in the VAR they are 0.741 and 0.153. So U's own lags have smaller coefficients once the lags of e are in the equation.
Now the e equation. The cross-lag coefficients on U are 0.156 and -0.084, with t values of 0.73 and -0.41, both below 1 in size. So the U lags give no measurable gain to the e equation.
The e equation depends on e's own lags, 1.821 and -0.817 (t = 11.73 and -5.30). They add up to 1.004, so raising both e lags by 1 raises the e forecast by 1.004.
But the correlation of -0.618 from earlier showed that U's previous change is strongly related to e's change. Here the U lags have no measurable effect. Why? The code below splits that correlation into two.
If U's previous change mattered for e only through e's own previous change, the correlation would be the product of these two, -0.621. The actual value is -0.618, very close to it. So e's own previous change accounts for the raw correlation. Once e's own lags are in the equation, the U lags add nothing measurable.
The residual standard errors show the same pattern. The code below puts the AR(2) and VAR(2) values next to each other.
For U the residual standard error falls from 0.354 to 0.300 once the e lags are added. For e it falls only from 0.410 to 0.402.
One caution: e trends upward through the sample, so the usual t distribution is only an approximation here. Treat the t values as a guide to which coefficients matter, not as exact tests.
Does a VAR forecast better than one AR(2) per series?
A better fit inside the sample does not guarantee a better forecast. So we test the forecasts on quarters that were not used to fit them, with a rolling-origin comparison.
We pick a target quarter, fit the models on the rows up to the forecast origin, forecast the target and record the error. The origin is h quarters before the target, so every forecast uses only earlier rows. We repeat this for the 24 target quarters from 1995Q1 to 2000Q4. At every origin we refit the VAR(2) and, for each series, an AR(2).
The AR(2) is fitted with ar.ols(), using aic = FALSE and order.max = 2. It gives the same forecasts as the lm() regression on lags we built earlier.
The errors are summarised by the RMSE, the root mean squared error. Square each forecast error, average the squares over the 24 targets and take the square root. It is in the units of the series, and lower is better. The function below returns the RMSE by model and series for a given horizon.
Inside the loop, train holds the rows up to the origin. predict() returns forecasts for horizons 1 to h, with earlier forecasts fed back in as lags, as we did by hand. We keep the h-th one.
For U the VAR has the lower RMSE at both horizons: 0.226 against 0.263 one quarter ahead, and 0.704 against 0.910 four quarters ahead. For e it is a little higher: 0.320 against 0.305, and 1.444 against 1.375. Both models make larger errors four quarters ahead than one quarter ahead, since forecasting further out is harder.
These numbers support a narrow claim. Against an AR(2) on the same levels, in this one 24-quarter window, the VAR forecasts U better and e slightly worse. It is not a claim that a VAR beats every single-series model. We compared it with one AR(2) per series and nothing else.
Quick check: why is the VAR forecast better for unemployment than for employment?
The RMSE table shows a lower error for U and a higher one for e. Which statement accounts for that?
lm() on the same regressors, as the comparison showed.Your turn: rebuild the 2001Q1 forecast of unemployment from its equation
The predict() value for U in 2001Q1 was 6.698. Let's rebuild it from the U equation, the same computation we did by hand for e.
The code below refits the VAR and stores b_U, the five coefficients of the U equation. Their names are const, e.l1, U.l1, e.l2 and U.l2. It also stores the last two observed quarters: e_lag1 and U_lag1 for 2000Q4, and e_lag2 and U_lag2 for 2000Q3. Replace the NA with the forecast, using each coefficient by name, then run the block.
Show answer
# Multiply each coefficient by its lag value and add the constant
forecast_U <- b_U["const"] + b_U["e.l1"] * e_lag1 + b_U["U.l1"] * U_lag1 +
b_U["e.l2"] * e_lag2 + b_U["U.l2"] * U_lag2
round(unname(forecast_U), 3)
#> [1] 6.698
round(predict(fit, n.ahead = 1)$fcst$U[1, "fcst"], 3)
#> [1] 6.698The forecast for 2001Q1 is 6.698, a fall of about 0.17 points from the 6.87 of 2000Q4. It matches predict().
References
- VAR, SVAR and SVEC Models: Implementation Within R Package vars - Pfaff (2008), Journal of Statistical Software 27(4). The paper behind the
varspackage used in this lesson, coveringVAR(),predict()and the other tools built around them. - New Introduction to Multiple Time Series Analysis - Luetkepohl (2005), Springer. The standard textbook treatment of VAR estimation, forecasting and inference.
- Macroeconomics and Reality - Sims (1980), Econometrica 48(1), 1-48. The paper that brought the VAR into empirical macroeconomics.
- Forecasting: Principles and Practice, vector autoregressions - Hyndman and Athanasopoulos, 3rd edition, OTexts. A short introduction to VAR forecasting with worked examples.
- Applied Time Series Econometrics - Luetkepohl and Kraetzig (2004), Cambridge University Press. The source of the Canada data used here.
Quick recap
You fitted a vector autoregression to Canada's employment and unemployment, interpreted its coefficient block, and tested its forecasts. To summarize:
- A VAR(p) is k regressions, one per series, each on p lags of all k series plus a constant. Ours has 2 equations with 5 coefficients each, fitted by ordinary least squares, and their coefficients match
lm()to numerical precision. - An own-lag coefficient multiplies a lag of the equation's own series, and a cross-lag coefficient multiplies a lag of the other series. In the U equation the e lags are -0.644 and 0.632 (t = -5.57 and 5.51). In the e equation the U lags are 0.156 and -0.084 (t = 0.73 and -0.41).
- A forecast puts the last p rows into each equation. A multi-quarter forecast feeds the earlier forecasts back in as lags.
- Over the 24 quarters from 1995Q1 to 2000Q4, the one-quarter-ahead RMSE for U was 0.226 with the VAR against 0.263 with the AR(2), and for e it was 0.320 against 0.305. That is one window and one comparison, so it does not say a VAR always wins.
So whenever someone asks what a VAR adds, here is the answer. It lets the past of each series enter the forecast of the others, and the coefficient block shows whether that helps. In this sample it helped for unemployment and not for employment.