#Libraries
library(fpp2) #Forecasting and Plotting tools
library(vars) #Vector Autoregressions
library(fredr) #Access FRED Data
library(knitr) #Use knitr to make tables
library(kableExtra) #Extra options for tables
library(gridExtra) #Graph Display
library(tidyverse) #Data manipulation
VAR
in library vars
ts
object containing \(m\) time series and choice of order \(p\)
type=trend
) and seasonal dummies at frequency f (season=f
) in each equation##Obtain and transform NIPA Data (cf Lecture 06)
fredr_set_key("8782f247febb41f291821950cf9118b6") #Key I obtained for this class
#US GDP components
GDP<-fredr(series_id = "GDP",
observation_start = as.Date("1947-01-01"),
observation_end=as.Date("2021-03-02"),
vintage_dates = as.Date("2021-03-02")) #Gross Domestic Product
#US GDP components from NIPA tables (cf http://www.bea.gov/national/pdf/nipaguid.pdf)
PCEC<-fredr(series_id = "PCEC",
observation_start = as.Date("1947-01-01"),
observation_end=as.Date("2021-03-02"),
vintage_dates = as.Date("2021-03-02")) #Personal consumption expenditures
FPI<-fredr(series_id = "FPI",
observation_start = as.Date("1947-01-01"),
observation_end=as.Date("2021-03-02"),
vintage_dates = as.Date("2021-03-02")) #Fixed Private Investment
CBI<-fredr(series_id = "CBI",
observation_start = as.Date("1947-01-01"),
observation_end=as.Date("2021-03-02"),
vintage_dates = as.Date("2021-03-02")) #Change in Private Inventories
NETEXP<-fredr(series_id = "NETEXP",
observation_start = as.Date("1947-01-01"),
observation_end=as.Date("2021-03-02"),
vintage_dates = as.Date("2021-03-02")) #Net Exports of Goods and Services
GCE<-fredr(series_id = "GCE",
observation_start = as.Date("1947-01-01"),
observation_end=as.Date("2021-03-02"),
vintage_dates = as.Date("2021-03-02")) #Government Consumption Expenditures and Gross Investment
#Format the series as quarterly time series objects, starting at the first date
gdp<-ts(GDP$value,frequency = 4,start=c(1947,1),names="Gross Domestic Product")
pcec<-ts(PCEC$value,frequency = 4,start=c(1947,1),names="Consumption")
fpi<-ts(FPI$value,frequency = 4,start=c(1947,1),names="Fixed Investment")
cbi<-ts(CBI$value,frequency = 4,start=c(1947,1),names="Inventory Growth")
invest<-fpi+cbi #Private Investment
netexp<-ts(NETEXP$value,frequency = 4,start=c(1947,1),names="Net Exports")
gce<-ts(GCE$value,frequency = 4,start=c(1947,1),names="Government Spending")
#Convert to log differences to ensure stationarity and collect in frame
NIPAdata<-ts(data.frame(diff(log(gdp)),diff(log(pcec)),diff(log(invest)),diff(log(gce))),frequency = 4,start=c(1947,2))
NIPAVAR<-VAR(NIPAdata,p=1) #VAR(1) in Y=log.diff.gdp, C=log.diff.pcec, I=log.diff.invest, G=log.diff.gce
NIPAfcst<-forecast(NIPAVAR) #Produce forecasts of all series
#Collect Coefficients into a Data Frame
var1table<-data.frame(
GDP<-NIPAVAR$varresult$diff.log.gdp..$coefficients,
Consumption<-NIPAVAR$varresult$diff.log.pcec..$coefficients,
Investment<-NIPAVAR$varresult$diff.log.invest..$coefficients,
Government<-NIPAVAR$varresult$diff.log.gce..$coefficients
)
rownames(var1table)<-c("Lagged Change log Y","Lagged Change log C",
"Lagged Change log I","Lagged Change log G",
"Constant")
#Make Table of Estimated Coefficients
kable(var1table,
col.names=c("GDP","Consumption","Investment","Government"),
caption="Estimated VAR Coefficients") %>%
kable_styling(bootstrap_options = "striped", font_size = 16)
GDP | Consumption | Investment | Government | |
---|---|---|---|---|
Lagged Change log Y | -0.6160785 | -0.2169527 | -4.1159018 | 0.0806111 |
Lagged Change log C | 0.5118518 | 0.0369735 | 3.6546627 | -0.0089679 |
Lagged Change log I | 0.1389572 | 0.0887646 | 0.6619958 | 0.0499548 |
Lagged Change log G | 0.1860983 | 0.1035326 | 0.1360237 | 0.6141333 |
Constant | 0.0116165 | 0.0150637 | 0.0098481 | 0.0041253 |
autoplot(NIPAfcst)+
labs(title = "VAR(1) Forecasts of GDP and and NIPA Component Growth")
forecast
library, use a different and faster methodPredict \(\widehat{y}_{T+3}=\widehat{\beta}_0+\widehat{\beta}_1\widehat{y}_{T+2}+\widehat{\beta}_2\widehat{y}_{T+1}\) \[=\widehat{\beta}_0+\widehat{\beta}_1((\widehat{\beta}_0+\widehat{\beta}_1\widehat{\beta}_0)+(\widehat{\beta}_1^2+\widehat{\beta}_2)y_{T}+\widehat{\beta}_1\widehat{\beta}_2y_{T-1})+\widehat{\beta}_2(\widehat{\beta}_0+\widehat{\beta}_1y_{T}+\widehat{\beta}_2y_{T-1})\] \[=(\widehat{\beta}_0+\widehat{\beta}_1\widehat{\beta}_0+\widehat{\beta}_1^2\widehat{\beta}_0+\widehat{\beta}_2\widehat{\beta}_0)+(\widehat{\beta}_1^3+2\widehat{\beta}_1\widehat{\beta}_2)y_{T}+(\widehat{\beta}_1^2\widehat{\beta}_2+\widehat{\beta}_2^2)y_{T-1}\]
VAR
command in R (and all other VAR software I know of) produces multi-period forecasts this waycheckresiduals
to inspect and compare various measures of distancecheckresiduals(NIPAVAR$varresult$diff.log.gdp..) #GDP Growth forecast residual diagnostic
##
## Breusch-Godfrey test for serial correlation of order up to 10
##
## data: Residuals
## LM test = 26.209, df = 10, p-value = 0.003469