0% found this document useful (0 votes)
40 views

R Code

The document discusses analyzing time series data of GDP and CPI in R. It loads libraries, imports CSV data, plots the time series, performs unit root tests and cointegration tests. It then fits Bayesian VAR and MSBVAR models to the data and generates forecasts.

Uploaded by

bhaskkar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

R Code

The document discusses analyzing time series data of GDP and CPI in R. It loads libraries, imports CSV data, plots the time series, performs unit root tests and cointegration tests. It then fits Bayesian VAR and MSBVAR models to the data and generates forecasts.

Uploaded by

bhaskkar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

R-CODE

setwd("~/Sinha")

# Library locations #

library("MTS", lib.loc="~/R/win-library/3.3")
library("MSBVAR", lib.loc="~/R/win-library/3.3")
library("bvarsv", lib.loc="~/R/win-library/3.3")
library("urca", lib.loc="~/R/win-library/3.3")
library("uroot", lib.loc="~/R/win-library/3.3")
library("vars", lib.loc="~/R/win-library/3.3")
library("tsDyn", lib.loc="~/R/win-library/3.3")
library("TSA", lib.loc="~/R/win-library/3.3")

# Data generating Process for Baysean VAR in csv #

Gendata1 = read.csv("BVARDAT.csv", header = True)


View(Gendata1)

gdp = Gendata1[,2]
cpi = Gendata1[,3]

Yt = ts(gdp, frequency = 4, start = 2010)


Xt = ts(cpi, frequency = 4, start = 2010)

Xt

win.graph(width=7.5, height=4.5,pointsize=12)
par(mfrow=c(1,1))
plot(Yt, ylab="GDP of India", xlab= "Time ", type = "l",
main = "Fig. 4.1 Time plot of quarterly GDP 2010-2017")
plot(Xt, ylab="Consumer's Price Index (CPI)", xlab= "Time", type = "l",
main = "Fig. 4.2 : Time plot of Quarterly Consumers Price Index, 2010-2017")

## IDENTIFYING TRENDS AND SEASONS

##ACF AND PACF

win.graph(width = 8, height = 6.5, pointsize = 12)


par(mfrow=c(2,2))
acf(Yt, lag.max = 72, plot = TRUE, main = "ACF plot of GDP")
pacf(Yt, lag.max = 72, plot = TRUE, main = "PACF plot of GDP")

acf(Xt, lag.max = 56, plot = TRUE, main = "ACF plot of India Consumer Price Index")
pacf(Xt, lag.max = 56, plot = TRUE, main = "PACF plot of India Consumer rice
Index")

x = ur.df(Yt, type = "none", selectlags = "AIC")


xt = ur.df(Yt, type = "trend", selectlags = "AIC")
xd = ur.df(Yt, type = "drift", selectlags = "AIC")

x_1 = ur.df(diff(Yt, lag = 1), type = "none", selectlags = "AIC")


xt_1 = ur.df(diff(Yt, lag = 1), type = "trend", selectlags = "AIC")
xd_1 = ur.df(diff(Yt, lag = 1), type = "drift", selectlags = "AIC")

x_2 = ur.df(diff(diff(Yt), lag = 1), type = "none", selectlags = "AIC")


xt_2 = ur.df(diff(diff(Yt), lag = 1), type = "trend", selectlags = "AIC")
xd_2 = ur.df(diff(diff(Yt), lag = 1), type = "drift", selectlags = "AIC")

summary(xd); summary(xd_1); summary(xd_2)


summary(xt); summary(xt_1); summary(xt_2)

y = ur.df(Xt, type = "none", selectlags = "AIC")


yt = ur.df(Xt, type = "trend", selectlags = "AIC")
yd = ur.df(Xt, type = "drift", selectlags = "AIC")

y_1 = ur.df(diff(Xt, lag = 1), type = "none", selectlags = "AIC")


yt_1 = ur.df(diff(Xt, lag = 1), type = "trend", selectlags = "AIC")
yd_1 = ur.df(diff(Xt, lag = 1), type = "drift", selectlags = "AIC")

y_2 = ur.df(diff(diff(Xt), lag = 1), type = "none", selectlags = "AIC")


yt_2 = ur.df(diff(diff(Xt), lag = 1), type = "trend", selectlags = "AIC")
yd_2 = ur.df(diff(diff(Xt), lag = 1), type = "drift", selectlags = "AIC")

summary(x); x@cval; x@lags; x@teststat


summary(y); y@cval; y@lags; y@teststat

summary(x_1); x_1@cval; x_1@lags; x_1@teststat


summary(y_1); y_1@cval; y_1@lags; y_1@teststat

summary(xt); xt@cval; xt@lags; xt@teststat


summary(yt); yt@cval; yt@lags; yt@teststat

summary(xt_1); xt_1@cval; xt_1@lags; xt_1@teststat


summary(yt_1); yt_1@cval; yt_1@lags; yt_1@teststat

summary(xd); xd@cval; xd@lags; xd@teststat


summary(yd); yd@cval; yd@lags; yd@teststat

summary(xd_1); xd_1@cval; xd_1@lags; xd_1@teststat


summary(yd_1); yd_1@cval; yd_1@lags; yd_1@teststat

### Cointegraion

lg.reg <-lm(Yt~Xt)
error<- lg.reg$residuals
RES = ur.df(error, type = "drift", selectlags = "AIC")
summary(RES)

co.data = cbind(gdp,cpi)
VARselect(co.data, type = "const")$selection
cointest = ca.jo(co.data, 10, type = "trace", ecdet = "const",
spec = "transitory")
cointest
cointest@cval
cointest@teststat[2]
cointest@teststat[1]

gdp = gdp
cpi = cpi

d.gdp = diff(gdp)
d.cpi = diff(cpi)
adf.test(gdp); adf.test(cpi)

ur.df(gdp, type = "drift", selectlags = "AIC")@cval


modelvar = VAR(cbind(gdp,cpi), type = "const", lag.max = 2, ic = "AIC")

modelvar$varresult
summary(modelvar)

data("mts-examples",package="MTS")
z=log(co.data[,1:2])
zt=diffM(z)*100
C=0.1*diag(rep(1,3))
V0=diag(rep(1,2))
model = BVAR(zt,p=1,C,V0)
summary(model)
model$residuals

model$residuals
forecast(model, nsteps = 29)

### MSBVAR forecasts

# Fit model
m1 <- msbvar(Y.sample1, p=1, h=2, lambda0=0.8, lambda1=0.2,
lambda3=1, lambda4=0.2, lambda5=0, mu5=0, mu6=0,
qm=12, prior=0)

# Gibbs sampling

m1id <- gibbs.msbvar(m1, N1=1000, N2=10000, permute=FALSE, Sigma.idx=1)

# Forecast density estimation


msforc <- forecast(m1id, nsteps=nrow(Y.sample2), N1=1000, N2=10000)

# Summarize forecasts
apply(msforc$forecasts, c(2,3), mean)

You might also like