[R-SIG-Finance] R: Use VAR model to predict response to change in values of certain variables

Matthieu Stigler matthieu.stigler at gmail.com
Mon Nov 23 14:46:44 CET 2009


Hi Karl

Please provide reproducible code when you ask questions, we don't have
access to your local drive!!
So use rather datasets in R, as for example:
data(Canada)
colnames(Canada)
 var.2c <- VAR(Canada, p = 1, type = "none")

You will note that there is a predict.varest function which does forecasts:
     pred<-predict(var.2c, n.ahead = 8, ci = 0.95)

This is not exactly what you want... you want to change some
forecasted values, and see the influence, right? This is not available
i the predict.varest. You will need to do some manipulations,
basically extracting the lm object and using predict(lm, newdata)


Construct data frame with forecasted values. It should include all
variables you see when in the output of var.2c. To keep simple, I used
the simplest model (1 lag, no intercept), so need only to give 4
values (the lag-1). For the example, I took last dat of data Canada,
change as you want:
:
yourPred<-as.data.frame(matrix(Canada[nrow(Canada),], nrow=1))
colnames(yourPred)<-colnames(model.frame(var.2c$varresult$e))[-1] #you
need to give same names as in var result!

predict(var.2c$varresult$e, newdata=yourPred)

Note that this is exactly the same as what does forecast.varest,
except you can now change it manually!

pred$fcst$e[1,1]

And now you can change the values in yourPred as you want.

Hope this helps

Mat

2009/11/23 Karl Schriek <kschriek at gmail.com>:
> Hi
>
> I've fitted a VECM model in R, and converted in to a VAR representation. I
> would like to use this model to predict the future value of a response
> variable based on different scenarios for the explanatory variables.
>
> Here is the code for the model:
>
> library(urca)
> library(vars)
>
> input <-read.csv("data.csv")
> ts <- ts(input[16:52,],c(2000,1),frequency=4)
> dat1 <- cbind(ts[,"dx"], ts[,"u"], ts[,"cci"],ts[,"bci"],ts[,"cpi"],ts[,"gdp"])
>
> args('ca.jo')
> vecm <- ca.jo(dat1, type = 'trace', K = 2, season =
> NULL,spec="longrun",dumvar=NULL)
> vecm.var <- vec2var(vecm,r=2)
>
> Now what I would like do is to predict "dx" into the future by varying the
> others. I am not sure if something like "predict dx if
> u=30,cpi=15,bci=50,gdp=..." in the next period would work. So what I have in
> mind is something along the lines of: increase "u" by 15% in the next period
> (which would obviously impact on all the other variables as well, including
> "dx") and predict the impact into the future.
>
> Also, I am not sure if the "vec2var" step is necessary, so please ignore it
> if you think it is redundant.
>
> Thanks
> Karl
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



More information about the R-SIG-Finance mailing list