# I follow the R Programming guide, pages 54-59 library(vars) # Use the vars package library(readxl) currencies <- read_excel("D:/Programming guide/data/currencies.xls") View(currencies) head(currencies) tail(currencies) # Log returns currencies$reur = c(NA ,100*diff(log(currencies$EUR))) currencies$rgbp = c(NA ,100*diff(log(currencies$GBP))) currencies$rjpy = c(NA ,100*diff(log(currencies$JPY))) currencies = currencies [-1,] # Remove the 1st missing value # OR # rEUR <- ts(diff(log(currencies$EUR))*100,start=c(1998,12,15),frequency=252) rGBP <- ts(diff(log(currencies$GBP))*100,start=c(1998,12,15),frequency=252) rJPY <- ts(diff(log(currencies$JPY))*100,start=c(1998,12,15),frequency=252) FX <- cbind(rEUR,rGBP,rJPY) ts.plot(FX,col=1:3, main="FX returns") ### # VAR estimation VAR(currencies[c("reur","rgbp","rjpy")],p = 2) # VAR(2) VARselect(currencies[c("reur","rgbp","rjpy")], lag.max = 10) var = VAR(currencies[c("reur","rgbp","rjpy")],p = 1) # VAR(1) # Granger causality causality(var, cause=c("reur","rgbp"))$Granger causality(var, cause=c("reur","rjpy"))$Granger causality(var, cause=c("rgbp","rjpy"))$Granger causality(var, cause=c("reur"))$Granger causality(var, cause=c("rgbp"))$Granger causality(var, cause=c("rjpy"))$Granger # Impulse-response functions ir = irf(var, n.ahead = 20) plot(ir) # Forecast error variance decomposition vd = fevd(var,n.ahead = 20) plot(vd) par(mar = c(1,1,1,1))# Changing area margins plot(vd) vd # Reversing the ordering in the VAR model var_reverse = VAR(currencies[c("rjpy","rgbp","reur")],p = 1) vd_reverse = fevd(var_reverse,n.ahead = 20) plot(vd_reverse) vd_reverse # Forecasting predict(var, n.ahead = 10) # Normality test normality.test(var) # Test for arch arch.test(var) # Test for serial correlation, autocorrelation serial.test(var) # Model summary summary(var)