Final Paper: IRFs by OLS

Set work directory

setwd("C:/Users/victo/Downloads/ECON 4310/Paper")

Loading Packages

We are using various R packages that need to be installed and added. You might have to uncomment/run the chunk below. Alternatively, you may be able to install the missing packages through RStudio, selecting “packages” and “install”, entering the names.

#install.packages("invgamma")
library(invgamma)

#install.packages("matlib")
library(matlib)

#install.packages("MASS")
library(MASS)

#install.packages("MCMCpack")
library(MCMCpack) #for Inverse Wishart
## Warning: package 'MCMCpack' was built under R version 4.2.3
## Loading required package: coda
## Warning: package 'coda' was built under R version 4.2.3
## ##
## ## Markov Chain Monte Carlo Package (MCMCpack)
## ## Copyright (C) 2003-2023 Andrew D. Martin, Kevin M. Quinn, and Jong Hee Park
## ##
## ## Support provided by the U.S. National Science Foundation
## ## (Grants SES-0350646 and SES-0350613)
## ##
## 
## Attaching package: 'MCMCpack'
## The following objects are masked from 'package:invgamma':
## 
##     dinvgamma, rinvgamma
#install.packages("mvtnorm")
library(mvtnorm) #for multivariate Normal

#install.packages("HypergeoMat")
library(HypergeoMat) #for multivariate gamma function (VAR-MDD calculation)
## Warning: package 'HypergeoMat' was built under R version 4.2.3
#install.packages("vars")
library(vars)
## Warning: package 'vars' was built under R version 4.2.3
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.2.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 4.2.3
## Loading required package: urca
## Warning: package 'urca' was built under R version 4.2.3
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.2.3

Load Data

Load monthly data and trim to 1997, Q2 (max MPR data):

data  <- read.csv("Chile_1986_Monthly.csv", header=TRUE)
# head(data)
#na.omit(as.numeric(data$CPI))
#na.omit(data$CPI)
UE <- ts(data$UE, frequency=12,start=c(1986,1))
CPI <- ts(data$CPI, frequency=12, start=c(1986,1))
UE <- window(UE,start=c(1997,2),end=c(2022,12))
CPI <- window(CPI,start=c(1997,2),end=c(2022,12))

Convert monthly to quarterly data

MonthlyToQuarterly <- function(MonthlyData,start,namevec){
  ## Assume that first obs is first month of quarter
  ## Assume that last obs is last month of quarter
  nobsMonth <- length(MonthlyData)
  nobsQuart <- nobsMonth/3
  firstyear <- start[1]
  firstquarter <- ((start[2]-1) %/% 3)+1
  QuarterlyData <- matrix(0,nrow=nobsQuart,ncol=1)
  for(i in 1:nobsQuart){
     QuarterlyData[i] <- mean(MonthlyData[(1+3*(i-1)):(3*i)],na.rm=FALSE)
  }
  QuarterlyData <- ts(QuarterlyData,frequency=4,start=c(firstyear,firstquarter),
                      names=namevec)
  return(QuarterlyData)
}

UEq <- MonthlyToQuarterly(UE,c(1996,1),"UE")
UEq <- window(UEq,start=c(1997,2))
#print(UEq)

CPIq <- MonthlyToQuarterly(CPI,c(1996,1),"CPI")
CPIq <- window(CPIq,start=c(1997,2))
#print(CPIq)

Load Monetary Policy Rate data (MPR)

data <- read.csv("Chile_MPR_monthly_F1997.csv", header = T)
MPR <- ts(data$MPR, frequency = 12, start = c(1997, 2))
MPRq <- MonthlyToQuarterly(MPR, c(1997,2), "MPR")
MPRq <- window(MPRq, start=c(1997,2), end=c(2021,3))
#print(MPRq)

Load the GDP data (unnecessary for monetary policy shock)

data     <- read.csv("Chile_1996_Quarterly.csv", header=TRUE)


GDP <- gsub(",","",data$GDP)
GDP <- as.numeric(GDP)
GDP      <- ts(GDP, frequency=4, start=c(1997,2))
#head(GDP)
GDPlog <- log(GDP)
GDPlogdiff <- diff(GDPlog)
GDPgrowth <- 400*GDPlogdiff
GDPgrowth <- ts(GDPgrowth, frequency = 4,start=c(1997,2))

VAR Estimation and IRFs

Tall     <- length(UEq) # total number of observations, starting 1997:Q2
T1       <- (1+4) # estimation sample will start in T1
p        <- 4 # number of lags
n        <- 3 # number of variables
yyall <- cbind(MPRq, UEq, CPIq)

yy <- yyall[T1:Tall,]
xx     <- matrix(1,nrow=(Tall-T1+1), ncol=(n*p+2))
xx[,2] <- 1:(Tall-T1+1)

for(i in 1:p){
  # first column of xx is vector of 1s
  # second column is time trend
  # followed by y(t-1), y(t-2), ..., y(t-p)
  xx[,(2+(i-1)*n+1):(2+i*n)] <- yyall[(T1-i):(Tall-i),]
}

OLS/ML Estimation

Phi_hat   <- solve(t(xx)%*%xx,t(xx)%*%yy)
Sigma_hat <- (t(yy)%*%yy - t(yy)%*%xx%*%Phi_hat)/(Tall-T1+1)

Sigma_hat_tr <- t(chol(Sigma_hat))

print(Phi_hat)
##              MPRq          UEq        CPIq
##  [1,]  3.31589094  0.730720198 -0.29238156
##  [2,] -0.01510097 -0.001359154  0.00275034
##  [3,]  1.10667395  0.052127195 -0.05019767
##  [4,]  0.06648655  1.293715152 -0.03237839
##  [5,]  0.20931955 -0.151294608  0.27415532
##  [6,] -0.36181610 -0.094764932  0.14551312
##  [7,] -0.12095907 -0.494031308  0.01203095
##  [8,] -0.06197169  0.310062429  0.06839104
##  [9,] -0.04258664  0.047179841 -0.13794882
## [10,]  0.09925800  0.112450548 -0.04829436
## [11,] -0.27752248  0.029187200 -0.10705629
## [12,]  0.02657578  0.023185119  0.05200609
## [13,] -0.26468293 -0.021576992  0.10151634
## [14,]  0.88898678  0.217565970  0.31373781

Impulse Responses

Hmax  <- 36
shind <- 1  # Monetary Policy Rate shock is first

IRFmat           <- matrix(0,nrow=(Hmax+1),ncol=n)
# response at impact is shind column of Sigma_hat_tr
IRFmat[1,]      <- t(Sigma_hat_tr[,shind])

xxIRF            <- matrix(0,nrow=1,ncol=p*n)  
xxIRF[1,1:n]     <- IRFmat[1,]
Phi_hat_no_const <- Phi_hat[3:(n*p+2),] 

for(h in 1:Hmax){
  IRFmat[(1+h),] <- xxIRF%*%Phi_hat_no_const
  if(p > 1){
    xxIRF[1,(n+1):(n*p)] <- xxIRF[1,1:(n*(p-1))]
  }
  xxIRF[1,1:n]     <- IRFmat[(1+h),]
}

#par(mfrow=c(1,2),mar=c(2.2,2.2,2.2,2.2))
plot(0:Hmax,100*IRFmat[,1],ylim=c(-10,80),type="o",
     col="blue",lwd=2,ylab="",xlab="")
title(main="MPR [%]")
abline(h=0,col="red")

plot(0:Hmax,100*IRFmat[,2],ylim=c(-1,10),type="o",
     col="blue",lwd=2,ylab="",xlab="")
title(main="Unemployment Rate [%]")
abline(h=0,col="red")

plot(0:Hmax,100*IRFmat[,3],ylim=c(-5,10),type="o",
     col="blue",lwd=2,ylab="",xlab="")
title(main="Inflation, CPI [%]")
abline(h=0,col="red")