# Leading Economic Indicator # __________________________ #1. VAR Model with 2 Lags #________________________ # mydata <- read.csv("K:/AIMS South Africa 2019/Data/Macro Data SA.csv", header=TRUE) # mydata <- read.csv("/media/guest/Lexar/AIMS South Africa 2019/Data/Macro Data SA.csv",header=TRUE) mydata <- read.csv("../data/Macro Data SA.csv", header=TRUE) head(mydata) # Shows the top of the data file. attach(mydata) # Variables in the datafile can be directly accessed # using their names. GDPts <- ts(GDP,frequency=4,start=c(1960,1)) SPIts <- ts(SPI,frequency=4,start=c(1960,1)) CPIts <- ts(CPI,frequency=4,start=c(1960,1)) plot(GDPts, type = "l") plot(SPIts, type = "l") plot(CPIts, type = "l") GDPlnd <- diff(log(GDPts)) SPIlnd <- diff(log(SPIts)) CPIlnd <- diff(log(CPIts)) plot(GDPlnd, type = "l") abline(0,0) plot(SPIlnd, type = "l") abline(0,0) plot(CPIlnd, type = "l") abline(0,0) newdata <- data.frame(GDPlnd, SPIlnd) head(newdata) library(vars) varmodel <- VAR(newdata,2) summary(varmodel) par(cex.axis=1.2, cex.lab = 1.2, cex.main=1.3, cex.sub=1) impulse.SPIGDP <- irf(varmodel, impulse = "SPIlnd", response = "GDPlnd", cumulative = "true") plot(impulse.SPIGDP) impulse.GDPSPI <- irf(varmodel, impulse = "GDPlnd", response = "SPIlnd", cumulative = "true") plot(impulse.GDPSPI) # 2. VAR Model with Optimal Lag Length # ____________________________________ # Remove all ojects from the workspace and all plots. # mydata <- read.csv("K:/AIMS South Africa 2019/Data/Macro Data SA.csv", header=TRUE) # mydata <- read.csv("/media/guest/Lexar/AIMS South Africa 2019/Data/Macro Data SA.csv",header=TRUE) mydata <- read.csv("../data/Macro Data SA.csv", header=TRUE) head(mydata) # Shows the top of the data file. attach(mydata) # Variables in the datafile can be directly accessed # using their names. GDPts <- ts(GDP,frequency=4,start=c(1960,1)) SPIts <- ts(SPI,frequency=4,start=c(1960,1)) CPIts <- ts(CPI,frequency=4,start=c(1960,1)) plot(GDPts, type = "l") plot(SPIts, type = "l") plot(CPIts, type = "l") GDPlnd <- diff(log(GDPts)) SPIlnd <- diff(log(SPIts)) CPIlnd <- diff(log(CPIts)) plot(GDPlnd, type = "l") abline(0,0) plot(SPIlnd, type = "l") abline(0,0) plot(CPIlnd, type = "l") abline(0,0) newdata <- data.frame(GDPlnd, SPIlnd) head(newdata) library(vars) varmodel.AIC <- VAR(newdata, lag.max=5, ic = "AIC") summary(varmodel.AIC) impulse.SPIGDP <- irf(varmodel.AIC, impulse = "SPIlnd", response = "GDPlnd", cumulative = "true") plot(impulse.SPIGDP) impulse.GDPSPI <- irf(varmodel.AIC, impulse = "GDPlnd", response = "SPIlnd", cumulative = "true") plot(impulse.GDPSPI)