library(readxl)
library(vars)
## Warning: package 'vars' was built under R version 4.3.2
## Loading required package: MASS
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.3.2
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 4.3.2
## Loading required package: urca
## Loading required package: lmtest
library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
VAR <- read_excel("VAR.xlsx")
View(VAR)
a<- ts(VAR$CAL,start = c(1996,1),frequency = 4)
b<- ts(VAR$GAP,start = c(1996,1),frequency = 4)
c<- ts(VAR$INF,start = c(1996,1),frequency = 4)
plot(a)

plot(b)

plot(c)

var<-cbind(a,b,c)
var
##                 a           b          c
## 1996 Q1 11.043333   71.505067  3.6369846
## 1996 Q2  6.006667  -30.048434  4.7605128
## 1996 Q3  7.970000   77.408244  4.7713509
## 1996 Q4  4.756667   37.664292  5.3507406
## 1997 Q1  4.093333  -38.474883  5.3055819
## 1997 Q2  5.446667  -74.133110  3.5518349
## 1997 Q3  6.863333  -11.157709  4.1376381
## 1997 Q4 15.716667    6.786568  4.5355412
## 1998 Q1  6.633333   12.686512  5.5446127
## 1998 Q2  7.340000   31.143836  6.5087800
## 1998 Q3  8.250000   13.910702  6.6223299
## 1998 Q4  9.130000   47.122040  5.0824974
## 1999 Q1  8.293333   88.721789  3.2832057
## 1999 Q2  9.086667   39.957129  2.6760660
## 1999 Q3  8.920000   59.085591  3.1170591
## 1999 Q4  9.190000  128.779293  4.2212600
## 2000 Q1  8.450000   75.065992  6.4643616
## 2000 Q2 10.383333   69.555047  6.3655111
## 2000 Q3  9.036667    8.072402  7.8648241
## 2000 Q4  8.726667  -44.053543  7.8197767
## 2001 Q1  7.586667   19.480837  5.4364657
## 2001 Q2  7.143333    7.100767  5.0536403
## 2001 Q3  7.150000   74.572032  2.5289704
## 2001 Q4  6.776667   15.410352  1.5534259
## 2002 Q1  6.506667  -32.282337  1.8286290
## 2002 Q2  5.740000 -101.895347  3.2192867
## 2002 Q3  5.586667 -188.923147  3.2677210
## 2002 Q4  5.743333 -149.302569  5.1877380
## 2003 Q1  4.883333 -126.842715  6.1694828
## 2003 Q2  4.743333  -35.460772  4.5194408
## 2003 Q3  4.473333   91.370955  5.4330260
## 2003 Q4  4.376667 -107.088267  5.8045661
## 2004 Q1  4.313333  -91.794073  5.4222942
## 2004 Q2  4.390000  -61.799043  7.9780312
## 2004 Q3  5.176667 -155.562996  7.1914332
## 2004 Q4  4.733333 -109.479120  4.7895205
## 2005 Q1  4.953333  -48.066747  4.4652360
## 2005 Q2  5.030000  -64.460653  3.0734005
## 2005 Q3  5.636667  -60.912045  3.3532355
## 2005 Q4  6.786667  -19.836877  4.3493685
## 2006 Q1  5.630000  -33.008869  5.9391406
## 2006 Q2  6.083333  -17.796316  6.8686732
## 2006 Q3  7.356667    6.960248  6.8716209
## 2006 Q4  9.803333   73.202139  6.6624260
## 2007 Q1  5.903333   20.150324  5.3978638
## 2007 Q2  4.483333   17.869967  3.9483555
## 2007 Q3  6.836667  111.999390  3.6450114
## 2007 Q4  7.040000   98.811584  5.9779897
## 2008 Q1  6.893333  121.819729  8.9813519
## 2008 Q2  9.473333  -16.697318 11.0153588
## 2008 Q3  7.853333  -88.520214  8.6643819
## 2008 Q4  4.220000 -364.816567  3.7106254
## 2009 Q1  3.290000  -96.943198  0.7574150
## 2009 Q2  3.226667   67.530229  0.5442643
## 2009 Q3  3.253333  -63.019873  4.5546095
## 2009 Q4  3.356667  -29.341427  9.5657672
## 2010 Q1  4.226667   73.947757 10.5389592
## 2010 Q2  5.456667  135.733031  9.2790449
## 2010 Q3  6.716667  140.872563  8.9103007
## 2010 Q4  6.833333  220.388498  9.5644651
## 2011 Q1  7.093333  199.583603  9.6051057
## 2011 Q2  7.901117  166.716879  9.7139999
## 2011 Q3  8.626013  170.610441  9.0214486
## 2011 Q4  8.966653  138.053085  7.4948317
## 2012 Q1  8.342905   19.297015  7.5392493
## 2012 Q2  7.984571   -6.119468  7.8648146
## 2012 Q3  8.028535   -8.650348  7.2928018
## 2012 Q4  7.901884  -32.843774  6.7478069
## 2013 Q1  7.352411  -89.950937  4.8358339
## 2013 Q2  9.209200  -54.970566  6.6312034
## 2013 Q3  8.547881 -121.250644  7.0540523
## 2013 Q4  8.259121 -128.027955  5.3790957
## 2014 Q1  8.148136  -23.465428  5.7961296
## 2014 Q2  8.014256  -31.948640  3.8824963
plot(var)

d<-decompose(a)
plot(d)

e<-decompose(b)
plot(e)

f<-decompose(c)
plot(f)

g<-decompose(var)
plot(g)

adf1<-adf.test(a)
pp1<-pp.test(a)
## Warning in pp.test(a): p-value smaller than printed p-value
kp1<-kpss.test(a)
## Warning in kpss.test(a): p-value greater than printed p-value
adf2<-adf.test(diff(a))
## Warning in adf.test(diff(a)): p-value smaller than printed p-value
adf1
## 
##  Augmented Dickey-Fuller Test
## 
## data:  a
## Dickey-Fuller = -2.1983, Lag order = 4, p-value = 0.4946
## alternative hypothesis: stationary
pp1
## 
##  Phillips-Perron Unit Root Test
## 
## data:  a
## Dickey-Fuller Z(alpha) = -34.847, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
kp1
## 
##  KPSS Test for Level Stationarity
## 
## data:  a
## KPSS Level = 0.25492, Truncation lag parameter = 3, p-value = 0.1
adf2
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(a)
## Dickey-Fuller = -4.9581, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
adf4<-ur.df(a,type="trend",selectlags = "AIC")
summary(adf4)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.7570 -0.8660  0.0353  0.7220  9.0084 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  2.477966   0.896095   2.765  0.00731 **
## z.lag.1     -0.353398   0.109567  -3.225  0.00194 **
## tt          -0.001308   0.009865  -0.133  0.89487   
## z.diff.lag  -0.140011   0.114950  -1.218  0.22743   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.724 on 68 degrees of freedom
## Multiple R-squared:  0.2234, Adjusted R-squared:  0.1891 
## F-statistic:  6.52 on 3 and 68 DF,  p-value: 0.0006088
## 
## 
## Value of test-statistic is: -3.2254 3.4899 5.2319 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.04 -3.45 -3.15
## phi2  6.50  4.88  4.16
## phi3  8.73  6.49  5.47
vardata<-data.frame(a,b,c)
varlags<-VARselect(vardata,lag.max = 12)
varlags
## $selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      2      2      1      2 
## 
## $criteria
##                   1            2            3            4            5
## AIC(n)     9.573164     9.287395     9.360929     9.556351     9.583524
## HQ(n)      9.734809     9.570274     9.765042    10.081698    10.230104
## SC(n)      9.984868    10.007876    10.390187    10.894387    11.230337
## FPE(n) 14381.565959 10832.389197 11724.712062 14404.410274 15053.092408
##                   6            7            8            9          10
## AIC(n)     9.689666     9.826235     9.924289     9.977522    10.13196
## HQ(n)     10.457481    10.715283    10.934572    11.109038    11.38471
## SC(n)     11.645257    12.090604    12.497436    12.859446    13.32267
## FPE(n) 17166.302716 20394.385667 23621.327100 26587.380690 33785.96464
##                 11          12
## AIC(n)     9.99266    10.05215
## HQ(n)     11.36664    11.54737
## SC(n)     13.49214    13.86041
## FPE(n) 32819.17628 40144.29893
varmodel<-VAR(vardata,p=2)
summary(varmodel)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: a, b, c 
## Deterministic variables: const 
## Sample size: 72 
## Log Likelihood: -653.042 
## Roots of the characteristic polynomial:
## 0.7639 0.7639 0.7443 0.7443 0.2618 0.0142
## Call:
## VAR(y = vardata, p = 2)
## 
## 
## Estimation results for equation a: 
## ================================== 
## a = a.l1 + b.l1 + c.l1 + a.l2 + b.l2 + c.l2 + const 
## 
##         Estimate Std. Error t value Pr(>|t|)    
## a.l1   0.4273551  0.1218349   3.508 0.000826 ***
## b.l1   0.0036743  0.0028482   1.290 0.201612    
## c.l1   0.1024790  0.1446278   0.709 0.481125    
## a.l2   0.1597613  0.1189511   1.343 0.183916    
## b.l2   0.0007893  0.0030904   0.255 0.799223    
## c.l2  -0.0991802  0.1354889  -0.732 0.466790    
## const  2.8126305  1.0060344   2.796 0.006801 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 1.698 on 65 degrees of freedom
## Multiple R-Squared: 0.4057,  Adjusted R-squared: 0.3509 
## F-statistic: 7.396 on 6 and 65 DF,  p-value: 4.684e-06 
## 
## 
## Estimation results for equation b: 
## ================================== 
## b = a.l1 + b.l1 + c.l1 + a.l2 + b.l2 + c.l2 + const 
## 
##       Estimate Std. Error t value Pr(>|t|)    
## a.l1  -4.45429    5.34085  -0.834    0.407    
## b.l1   0.73665    0.12486   5.900 1.44e-07 ***
## c.l1  -6.93637    6.34002  -1.094    0.278    
## a.l2  -1.00487    5.21444  -0.193    0.848    
## b.l2  -0.01889    0.13548  -0.139    0.890    
## c.l2   7.95152    5.93940   1.339    0.185    
## const 31.44831   44.10133   0.713    0.478    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 74.43 on 65 degrees of freedom
## Multiple R-Squared: 0.4782,  Adjusted R-squared:  0.43 
## F-statistic: 9.928 on 6 and 65 DF,  p-value: 9.291e-08 
## 
## 
## Estimation results for equation c: 
## ================================== 
## c = a.l1 + b.l1 + c.l1 + a.l2 + b.l2 + c.l2 + const 
## 
##        Estimate Std. Error t value Pr(>|t|)    
## a.l1  -0.021333   0.085168  -0.250   0.8030    
## b.l1   0.004311   0.001991   2.165   0.0341 *  
## c.l1   1.076537   0.101101  10.648 6.91e-16 ***
## a.l2  -0.139424   0.083152  -1.677   0.0984 .  
## b.l2   0.001474   0.002160   0.682   0.4975    
## c.l2  -0.511176   0.094713  -5.397 1.02e-06 ***
## const  3.579180   0.703264   5.089 3.29e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 1.187 on 65 degrees of freedom
## Multiple R-Squared: 0.7603,  Adjusted R-squared: 0.7382 
## F-statistic: 34.37 on 6 and 65 DF,  p-value: < 2.2e-16 
## 
## 
## 
## Covariance matrix of residuals:
##          a       b        c
## a  2.88254   28.70  0.06917
## b 28.70491 5539.27 16.28261
## c  0.06917   16.28  1.40859
## 
## Correlation matrix of residuals:
##         a      b       c
## a 1.00000 0.2272 0.03432
## b 0.22717 1.0000 0.18433
## c 0.03432 0.1843 1.00000