Skip to content

Instantly share code, notes, and snippets.

@jpicerno1
Last active July 17, 2020 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jpicerno1/97a6df866488c069aa5a to your computer and use it in GitHub Desktop.
Save jpicerno1/97a6df866488c069aa5a to your computer and use it in GitHub Desktop.
# R code re: CapitalSpecator.com post on "shock" modeling with VAR analytics:
# "Modeling "What If?" Scenarios With Impulse Response Simulations"
# http://www.capitalspectator.com/modeling-what-if-scenarios-with-impulse-response-simulations/
# 19 Feb 2016
# By James Picerno
# http://www.capitalspectator.com/
# (c) 2016 by Beta Publishing LLC
#Load Packages
library(vars)
library(quantmod)
fred.tickers <-c("GDPC96","MCOILWTICO","OILPRICE")
getSymbols(fred.tickers,src="FRED")
# create oil price data set
oil.q.1 <-apply.quarterly(MCOILWTICO,mean)
oil.q.aa <-apply.quarterly(OILPRICE,mean)
oil.q.a <-oil.q.aa['1971-03-01/1985-12-01']
oil.q <-rbind(oil.q.1,oil.q.a)
# create gdp data set
gdp.1 <-to.period(GDPC96,indexAt='yearqtr',OHLC=FALSE)
gdp.2a <-tail(gdp.1,length(oil.q))
# combine oil and gdp data
oil.gdp.1 <-cbind(oil.q,as.numeric(gdp.2a))
colnames(oil.gdp.1) <-c("oil","gdp")
oil.gdp.2 <-oil.gdp.1['/2015-12-01']
# create stationary data
oil.gdp <-na.omit(diff(log(oil.gdp.2),lag=1))
# estimate VAR model
var.select.oil.gdp <-VARselect(oil.gdp)
var.model.oil.gdp <-VAR(oil.gdp,p=var.select.oil.gdp$selection[1])
# create matrix to run structural VAR analysis
amat.oil.gdp <- diag(2)
colnames(amat.oil.gdp) <-c(names(oil.gdp))
rownames(amat.oil.gdp) <-c(names(oil.gdp))
amat.oil.gdp[2, 1] <- NA
# estimate structural VAR
svar.oil.gdp <-SVAR(var.model.oil.gdp, estmethod = "direct",
Amat = amat.oil.gdp, Bmat = NULL,
hessian = TRUE, method="BFGS")
# generate impulse response function data
irf.oil.gdp <-irf(svar.oil.gdp, impulse = "oil",
response = "gdp", boot =TRUE,
n.ahead=11,cumulative=TRUE,ci=0.6827)
# generate "shock" data
shock.val= -10
shock.p.oil.gdp <- irf.oil.gdp$irf$oil*shock.val
shock.l.oil.gdp <-irf.oil.gdp$Lower$oil*shock.val
shock.u.oil.gdp <-irf.oil.gdp$Upper$oil*shock.val
shock.all.oil.gdp <-cbind(shock.p.oil.gdp,shock.l.oil.gdp,shock.u.oil.gdp)
colnames(shock.all.oil.gdp) <-c("point","lower","upper")
### END
@Ruthygg
Copy link

Ruthygg commented Oct 7, 2016

How about the plots? Do you have the code for those?

@scoro004
Copy link

Hi! Were you able to code the plots? @Ruthygg

@jpicerno1
Copy link
Author

Ruthygg,
For a quck but 'ugly' charting solution, simply use the plot() command with the irf output. For instance, plot(irf.oil.gdp). Plotting a 'pretty' chart is a bit more complicated. For details, please email me directly--contact info is available on my website: CapitalSpectator.com
--JP

@MatthewHosseini
Copy link

JP: Arrived here a bit late ... but if you could help, I'd appreciate it. Running the code on SVAR produces an error provided below. Is there something about R that has changed and therefore I need to incorporate? Please advise. thx

svar.oil.gdp <-SVAR(var.model.oil.gdp, estmethod = "direct",

  •                 Amat = amat.oil.gdp, Bmat = NULL,
    
  •                 hessian = TRUE, method="BFGS")
    

Error in SVAR(var.model.oil.gdp, estmethod = "direct", Amat = amat.oil.gdp, :

Please, provide an object of class 'varest',
generated by function 'VAR()' as input for 'x'.

@jpicerno1
Copy link
Author

MatthewHosseini,
Sorry for the delayed response--please email directly via contact info at CapitalSpectator.com for faster replies. Meantime, I just re-ran the code above and all works fine, which suggests there's an issue with your R installation and/or relevant packages.
--JP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment