# R code for VAR series install.packages("vars") library(vars) T<-140 U1<-ts(rnorm(T), frequency=4, start=c(1970,1)) U2<-ts(rnorm(T), frequency=4, start=c(1970,1)) ts.plot(U1,ylab="Random walks", col=c("blue")) lines(U2,ylab="Random walks", col=c("red")) X1<-0*c(1:T);X2<-0*c(1:T) for (t in 1:(T-1)) { X1[t+1]<-0.45*X1[t]+0.45*X2[t]+U1[t] X2[t+1]<-0.25*X1[t]+0.55*X2[t]+U2[t] } ts.plot(X1,ylab="vars", col=c("blue")) lines(X2, col=c("red")) U<-cbind(U1,U2) X<-cbind(X1,X2) #to look for the best order VARselect(X) #Then call VAR with this best order p VAR(X,p=1) #or alternatively choose a criteria and let VAR choose the best order accordingly. res<-VAR(X,ic="AIC",lag.max=5,type="none") #type defines the deterministic model (none, const, trend, both) summary(res) #Validation: autocorrelation autocor.test<-serial.test(res) autocor.test plot(autocor.test,names="X1") #Validation: heteroscedasticity arch.test(res) #Validation: normality normality.test(res) #Causality causality(res,cause="X1")