# Step 1: Install and Load the Package
# install.packages("vars") # Run this line once if you don't have the package
library(vars)
# We might also use other packages for diagnostics or plotting
# install.packages("tseries")
# install.packages("urca")
# Step 2: Load and Prepare Data
# The `Canada` dataset comes with the `vars` package.
data(Canada)
# Let's inspect the data
head(Canada)
plot(Canada) # Plot all the series
# For a real-world project, you would rigorously test each series for stationarity
# using tests like the Augmented Dickey-Fuller (ADF) test.
# If a series is non-stationary, you would typically difference it.
# For example: adf.test(Canada[, "e"])
# For this demonstration, we will assume the data is ready for modeling.
# The `Canada` dataset is treated as stationary in many examples.
# Step 3: Select the Lag Order (p)
# This is a critical step. We use VARselect() to get recommendations.
# It calculates criteria like AIC, BIC for different lag lengths.
lag_selection <- VARselect(Canada, lag.max = 8, type = "const")
lag_selection$selection
# Output will look something like this:
# AIC(n) HQ(n) SC(n) FPE(n)
# 3 2 2 3
# Here, AIC and FPE suggest p=3, while HQ and BIC (SC) suggest p=2.
# BIC tends to choose a more parsimonious (simpler) model. Let's choose p=2.
p <- 2
# Step 4: Estimate the VAR Model
# We'll use the lag order p=2 we selected.
# type="const" includes an intercept in the equations.
var_model <- VAR(Canada, p = p, type = "const")
# You can get a detailed summary of the model
summary(var_model)
# This output shows the regression results for each variable (e, prod, rw, U)
# as a dependent variable. You'll see coefficients, standard errors, t-values, and R-squared.
# Step 5: Check Model Diagnostics
# A good model should not have serially correlated residuals.
# We use the Portmanteau test for this.
serial_test <- serial.test(var_model, lags.pt = 10, type = "PT.asymptotic")
print(serial_test)
# A high p-value (e.g., > 0.05) is good! It means we fail to reject the null
# hypothesis of no serial correlation.
# Step 6: Analyze the Results (Forecasting, IRF, etc.)
# a) Forecasting
# Let's forecast the next 10 periods
forecasts <- predict(var_model, n.ahead = 10)
# Plot the forecasts with confidence intervals
plot(forecasts)
# You can also access the forecast values directly
# print(forecasts$fcst$e) # Forecasts for the 'e' variable
# b) Impulse Response Functions (IRF)
# How does a shock to one variable affect others?
# Let's trace the effect of a shock in the real wage (`rw`) on all other variables.
# The `boot=TRUE` option provides confidence bands through bootstrapping.
irf_rw <- irf(var_model, impulse = "rw", response = c("e", "prod", "U"), n.ahead = 20, boot = TRUE)
# Plot the IRF
plot(irf_rw)
# This plot shows how employment, productivity, and unemployment react over 20 periods
# to a one-time, one-standard-deviation shock in real wages.
# c) Forecast Error Variance Decomposition (FEVD)
# How much of the forecast error in one variable is explained by shocks to other variables?
fevd_results <- fevd(var_model, n.ahead = 10)
# Print the results (it's a list)
print(fevd_results)
# Plot the FEVD for the unemployment rate `U`
plot(fevd_results, "U")
# d) Granger Causality Test
# Does the past of variable X help predict variable Y?
# H0: 'rw' does not Granger-cause 'e'
causality_test1 <- causality(var_model, cause = "rw")
print(causality_test1$Granger)
# A low p-value (< 0.05) would suggest we reject the null, meaning 'rw' does Granger-cause
# the other variables (or at least one of them).
# Test if 'e' Granger-causes 'U'
causality_test2 <- causality(var_model, cause = "e", vcov. = vcov(var_model))
print(causality_test2$Granger)