In [3]:
###data load
install.packages("WDI")
library(WDI)
gdp<-WDI(country = c("US", "CA", "GB", "CN", "JP", "SG", "IL", "KR"),
         indicator=c("NY.GDP.PCAP.CD", "NY.GDP.MKTP.CD"),
         start=1960, end=2016
)

names(gdp)<-c("iso2c", "Country", "Year", "PerCapGDP", "GDP")


Installing package into ‘/home/junhwan/R/x86_64-pc-linux-gnu-library/3.3’
(as ‘lib’ is unspecified)

In [6]:
##data reshape
# install.packages("reshape2")
library(reshape2)
gdpCast<-dcast(Year~Country,
               data=gdp[ , c("Country","Year", "PerCapGDP" )],
               value.var="PerCapGDP")

# gdpCast

In [7]:
## check correlation
install.packages("psych")
library(psych)
pairs.panels(gdpCast)


Installing package into ‘/home/junhwan/R/x86_64-pc-linux-gnu-library/3.3’
(as ‘lib’ is unspecified)

In [9]:
##convert into time series
gdpTs<-ts(data=gdpCast[,-1]
          ,start=min(gdpCast$Year , end = max(start=min(gdpCast$Year))))

## check data
plot(gdpTs)
plot(gdpTs, plot.type="single", col=1:9)

legend("topleft", legend=colnames(gdpTs), lty=1, col=1:9, ncol=3)



In [11]:
## check best diff
install.packages("forecast")
library(forecast)

numDiffs<-ndiffs(gdpTs)
numDiffs
gdpdiffed<-(diff(gdpTs, 1))

plot(diff(gdpdiffed, 1), plot.type="single", col=1:9)


Installing package into ‘/home/junhwan/R/x86_64-pc-linux-gnu-library/3.3’
(as ‘lib’ is unspecified)
also installing the dependencies ‘TTR’, ‘quadprog’, ‘quantmod’, ‘tseries’, ‘fracdiff’, ‘lmtest’, ‘timeDate’, ‘RcppArmadillo’

1

In [12]:
##load and create VAR model
install.packages("vars")
library(vars)
gdpvar<-VAR(gdpdiffed)


Installing package into ‘/home/junhwan/R/x86_64-pc-linux-gnu-library/3.3’
(as ‘lib’ is unspecified)
also installing the dependencies ‘strucchange’, ‘urca’, ‘sandwich’

Loading required package: MASS
Loading required package: strucchange
Loading required package: zoo

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Loading required package: sandwich
Loading required package: urca
Loading required package: lmtest

In [14]:
## Var object
names(gdpvar)
names(gdpvar$varresult)
names(gdpvar$varresult$Korea..Rep.)
class(gdpvar$varresult$Korea..Rep.)


  1. 'varresult'
  2. 'datamat'
  3. 'y'
  4. 'type'
  5. 'p'
  6. 'K'
  7. 'obs'
  8. 'totobs'
  9. 'restrictions'
  10. 'call'
  1. 'Canada'
  2. 'China'
  3. 'Israel'
  4. 'Japan'
  5. 'Korea..Rep.'
  6. 'Singapore'
  7. 'United.Kingdom'
  8. 'United.States'
  1. 'coefficients'
  2. 'residuals'
  3. 'effects'
  4. 'rank'
  5. 'fitted.values'
  6. 'assign'
  7. 'qr'
  8. 'df.residual'
  9. 'xlevels'
  10. 'call'
  11. 'terms'
  12. 'model'
'lm'

In [15]:
## predict
predict(gdpvar, n.ahead=5) 
plot(predict(gdpvar, n.ahead=5) )


$Canada
           fcst     lower    upper       CI
[1,] -1429.1540 -4982.004 2123.696 3552.850
[2,]   946.1759 -3264.360 5156.712 4210.536
[3,]   594.8704 -3771.074 4960.815 4365.945
[4,]   380.5554 -4074.238 4835.349 4454.793
[5,]   529.4867 -3962.129 5021.103 4491.616

$China
          fcst     lower    upper       CI
[1,] -45.39394 -249.7784 158.9906 204.3845
[2,]  34.52345 -277.8040 346.8509 312.3275
[3,]  64.83698 -320.3226 449.9965 385.1596
[4,]  57.94822 -372.7479 488.6444 430.6961
[5,]  54.12335 -408.2497 516.4964 462.3731

$Israel
           fcst     lower    upper       CI
[1,]  -13.39499 -2420.712 2393.922 2407.317
[2,] 1383.81472 -1383.319 4150.949 2767.134
[3,]  957.57223 -1867.009 3782.153 2824.581
[4,]  587.35058 -2260.003 3434.704 2847.353
[5,]  495.92674 -2358.567 3350.421 2854.494

$Japan
         fcst     lower    upper       CI
[1,] 579.8032 -4499.577 5659.183 5079.380
[2,] 762.7656 -4710.074 6235.605 5472.839
[3,] 641.0166 -4900.229 6182.262 5541.246
[4,] 909.6320 -4650.838 6470.102 5560.470
[5,] 952.7613 -4619.078 6524.601 5571.839

$Korea..Rep.
          fcst     lower    upper       CI
[1,] 1362.1895 -1032.053 3756.432 2394.243
[2,] 1077.8573 -1524.344 3680.058 2602.201
[3,]  192.9797 -2457.941 2843.900 2650.921
[4,]  141.1238 -2522.513 2804.760 2663.637
[5,]  393.1720 -2287.218 3073.562 2680.390

$Singapore
          fcst     lower    upper       CI
[1,] 1221.6760 -2082.242 4525.594 3303.918
[2,] 2095.5831 -1823.829 6014.995 3919.412
[3,]  996.6005 -3130.818 5124.019 4127.418
[4,]  521.5653 -3681.781 4724.912 4203.347
[5,]  703.1948 -3525.943 4932.333 4229.138

$United.Kingdom
           fcst     lower    upper       CI
[1,] -1753.5798 -5459.632 1952.473 3706.052
[2,]  1300.7449 -3190.394 5791.884 4491.139
[3,]  1069.2729 -3546.508 5685.054 4615.781
[4,]   447.0615 -4249.716 5143.839 4696.777
[5,]   432.0207 -4307.949 5171.991 4739.970

$United.States
          fcst     lower    upper       CI
[1,]  846.3377 -161.5872 1854.263 1007.925
[2,] 1117.6019 -171.7000 2406.904 1289.302
[3,] 1011.6792 -329.8371 2353.196 1341.516
[4,]  915.8978 -442.0860 2273.881 1357.984
[5,]  899.7851 -467.2967 2266.867 1367.082

In [2]:



Installing package into ‘/home/junhwan/R/x86_64-pc-linux-gnu-library/3.3’
(as ‘lib’ is unspecified)
also installing the dependency ‘RJSONIO’

Loading required package: RJSONIO
Installing package into ‘/home/junhwan/R/x86_64-pc-linux-gnu-library/3.3’
(as ‘lib’ is unspecified)
Error in View(gdpCast): ‘View()’ not yet supported in the Jupyter R kernel
Traceback:

1. View(gdpCast)
2. stop(sQuote("View()"), " not yet supported in the Jupyter R kernel")