# NOT RUN {
oldpar <- par(no.readonly =TRUE)
# First example, a US monetary policy shock, quarterly data
library(BGVAR)
data(eerData)
model.ssvs.eer<-bgvar(Data=eerData,W=W.trade0012,saves=100,burns=100,plag=1,prior="SSVS",
eigen=TRUE)
# US monetary policy shock
shocks<-list();shocks$var="stir";shocks$cN<-"US";shocks$ident="chol";shocks$scal=-100
irf.chol.us.mp<-IRF(obj=model.ssvs.eer,shock=shocks,nhor=24)
# plots an impulse response function
plot(irf.chol.us.mp,resp="US")
# calculates generalized impulse response functions for the same shock as above
shocks$ident="girf"
irf.girf.ssvs<-IRF(obj=model.ssvs.eer,shock=shocks,nhor=24)
plot(irf.girf.ssvs,resp="US.y")
# Shock to first ordered variable yields same responses of Cholesky and GIRF
shocks<-list();shocks$var="y";shocks$cN<-"US";shocks$ident="chol";shocks$scal<--100
irf.chol<-IRF(model.ssvs.eer,shock=shocks,nhor=24)
shocks$ident<-"girf"
irf.girf<-IRF(model.ssvs.eer,shock=shocks,nhor=24)
matplot(cbind(irf.chol$posterior["US.y",,1,"median"],
irf.girf$posterior["US.y",,1,"median"]),
type="l",ylab="")
matplot(cbind(irf.chol$posterior["US.Dp",,1,"median"],
irf.girf$posterior["US.Dp",,1,"median"]),
type="l",ylab="")
matplot(cbind(irf.chol$posterior["EA.y",,1,"median"],
irf.girf$posterior["EA.y",,1,"median"]),
type="l",ylab="")
sign.constr<-list()
# the variable to shock, can be imposed for more than one country
sign.constr$shock1$shock<-c("US.stir")
# but should be the same variable for all of them
sign.constr$shock1$restrictions$res1<-c("US.y")
sign.constr$shock1$restrictions$res2<-c("US.Dp")
# first entry is for the shock, following entries for the restrictions
# (ltir should go up, y and p go down)
sign.constr$shock1$sign<-c(">","<","<")
# nr. of time periods restrictions are imposed, first entry is for the shock,
# following entries for the restrictions
sign.constr$shock1$rest.horz<-c(1,1,1)
# are constraints binding for all (1) countries specified for
# at least 50\% of the countries (0.5), or 75\% (0.75)
sign.constr$shock1$constr<-c(1,1,1)
# a minus 100 bp shock to long-term interest rates (on average)
sign.constr$shock1$scal=+100
sign.constr$MaxTries<-200
irf.sign.us.mp<-IRF(obj=model.ssvs.eer,sign.constr=sign.constr,nhor=24)
plot(irf.sign.us.mp,resp=c("US"))
# second example, cross-country restrictions, multiple shocks and ECB country model
data(monthlyData);monthlyData$OC<-NULL
OE.weights <- list(EB=EA.weights)
model.ssvs<-bgvar(Data=monthlyData,W=W,saves=100,burns=100,plag=1,prior="SSVS",
thin=1,eigen=TRUE,OE.weights=OE.weights)
EA_countries <- c("AT", "BE", "DE","ES", "FI","FR", "IE", "IT", "NL", "PT","GR","SK")
# A simultaneous Cholesky shock to long-term interest rates in the euro area countries,
# scaled to amount to -100 basis points (on average over the EA countries).
# Note that the ordering of the variables influences the response, the ordering is exactly as
# in the country models, to use a different order you have re-estimate the model (by bgvar)
shocks<-list();shocks$var="ltir";shocks$cN<-EA_countries;shocks$ident="chol";shocks$scal=-100
irf.chol.ssvs<-IRF(obj=model.ssvs,shock=shocks,nhor=48)
plot(irf.chol.ssvs,resp=c("AT"))
# imposes sign restrictions on the cross-section and for a global shock (long-term interest rates)
sign.constr<-list()
# the variable to shock, can be imposed for more than one country
sign.constr$shock1$shock<-c(paste(EA_countries[-c(3,12)],".ltir",sep=""))
# but should be the same variable for all of them
# restrictions (industrial production should decrease for selected countries)
sign.constr$shock1$restrictions$res1<-paste(EA_countries,".y",sep="")
# another set of restrictions (inflation should decrease for selected countries)
sign.constr$shock1$restrictions$res2<-paste(EA_countries,".p",sep="")
# first entry is for the shock, following entries for the restrictions
# (ltir should go up, y and p go down)
sign.constr$shock1$sign<-c(">","<","<")
# nr. of time periods restrictions are imposed, first entry is for the shock,
# following entries for the restrictions
sign.constr$shock1$rest.horz<-c(1,1,1)
# are constraints binding for all (1) countries specified or for
# at least 50\% of the countries (0.5), or 75\% (0.75)
sign.constr$shock1$constr<-c(1,0.5,0.5)
# a minus 100 bp shock to long-term interest rates (on average)
sign.constr$shock1$scal=-100
sign.constr$MaxTries<-200
irf.sign.ssvs<-IRF(obj=model.ssvs,nhor=48,sign.constr=sign.constr)
plot(irf.sign.ssvs,resp=c("AT"))
# Same example but using a local (German) shock and cross-country restrictions.
# Note that the ordering of the variables influences the response,
# the ordering is exactly as in the country models, to use a different order you have re-estimate
# the model (by bgvar)
shocks<-list();shocks$var="ltir";shocks$cN<-EA_countries;shocks$ident="chol";shocks$scal=-100
irf.chol.ssvs<-IRF(obj=model.ssvs,shock=shocks,nhor=24)
# imposes sign restrictions on the cross-section and for a global shock (long-term interest rates)
sign.constr<-list()
sign.constr$shock1$shock<-c("DE.ltir") # the variable to shock,
# can be imposed for more than one country
#but should be the same variable for all of them
# restrictions (industrial production should decrease for selected countries)
sign.constr$shock1$restrictions$res1<-paste(EA_countries,".y",sep="")
# another set of restrictions (inflation should decrease for selected countries)
sign.constr$shock1$restrictions$res2<-paste(EA_countries,".p",sep="")
# first entry is for the shock, following entries for the restrictions
# (ltir should go up, y and p go down)
sign.constr$shock1$sign<-c(">","<","<")
# nr. of time periods restrictions are imposed,
# first entry is for the shock, following entries for the restrictions
sign.constr$shock1$rest.horz<-c(2,2,1)
# are constraints binding for all (1) countries specified or for
# at least 50\% of the countries (0.5), or 75\% (0.75)
sign.constr$shock1$constr<-c(1,0.5,0.5)
# a minus 100 bp shock to long-term interest rates (on average)
sign.constr$shock1$scal=-100
sign.constr$MaxTries<-200
irf.sign.ssvs<-IRF(obj=model.ssvs,nhor=24,sign.constr=sign.constr)
# Example with zero restriction (Arias et al., 2018) and
# rationality conditions (D'Amico and King, 2017).
data("eerDataspf")
model.ssvs.eer<-bgvar(Data=eerDataspf,W=W.trade0012.spf,saves=300,burns=300,
plag=1,prior="SSVS",thin=1,eigen=TRUE)
sign.constr<-list()
sign.constr$shock1$shock<-"US.stir_t+4"
sign.constr$shock1$restrictions$res1<-"US.Dp_t+4"
sign.constr$shock1$restrictions$res2<-"US.stir"
sign.constr$shock1$restrictions$res3<-"US.y_t+4"
# rationality condition: US.stir_t+4 on impact is equal to average of
# IRF of US.stir between horizon 1 and 4 (defined with rest.horz, but as period 5!)
sign.constr$shock1$restrictions$res4<-"US.stir_t+4"
# rationality condition: US.Dp_t+4 on impact is equal to H-step ahead IRF of US.Dp in
# horizon 4 (defined with rest.horz, but as period 5!)
sign.constr$shock1$restrictions$res5<-"US.Dp_t+4"
# rationality condition: US.y_t+4 on impact is equal to H-step ahead IRF of US.y in
# horizon 4 (defined with rest.horz, but as period 5!)
sign.constr$shock1$restrictions$res6<-"US.y_t+4"
sign.constr$shock1$sign<-c(">","<","0","<","ratio.avg","ratio.H","ratio.H")
sign.constr$shock1$rest.horz<-c(1,1,1,1,5,5,5)
sign.constr$shock1$constr<-c(1,1,1,1,1,1,1)
sign.constr$shock1$scal=0.1
sign.constr$MaxTries<-100
irf.sign<-IRF(obj=model.ssvs.eer,nhor=20,sign.constr=sign.constr)
par(mfrow=c(4,1),mar=c(5.1,4.1,4.1,2.1))
# rationality condition: US.stir_t+4 on impact is equal to average of IRF of
# US.stir between horizon 1 and 4
matplot(cbind(irf.sign$posterior["US.stir_t+4",,1,"median"],
irf.sign$posterior["US.stir",,1,"median"]),
type="l",ylab="",main="stir")
abline(h=mean(irf.sign$posterior["US.stir",2:5,1,"median"]))
abline(v=c(1,5),lty=3,col="grey")
# rationality condition: US.y_t+4 on impact is equal to H-step ahead IRF of US.y in horizon 4
matplot(cbind(irf.sign$posterior["US.y_t+4",,1,"median"],
irf.sign$posterior["US.y",,1,"median"]),
type="l",ylab="",main="y")
abline(h=irf.sign$posterior["US.y_t+4",1,1,"median"])
abline(v=5,lty=3,col="grey")
# rationality condition: US.Dp_t+4 on impact is equal to H-step ahead IRF of US.Dp in horizon 4
matplot(cbind(irf.sign$posterior["US.Dp_t+4",,1,"median"],
irf.sign$posterior["US.Dp",,1,"median"]),
type="l",ylab="",main="Dp")
abline(h=irf.sign$posterior["US.Dp_t+4",1,1,"median"])
abline(v=5,lty=3,col="grey")
par(mar=rep(0,4))
plot("1",type="n",axes=FALSE)
legend("center",c("expectation","actual"),lty=1:2,col=c("black","red"),bty="n",ncol=2)
par(oldpar)
# }
Run the code above in your browser using DataLab