Data Cleaning
df_wb <- read_csv("world-bank.csv")
#glimpse(df_wb)
# Filter for Brazil, Russia, India, China, and South Africa
df_brics <- df_wb %>% filter(grepl("BRA|CHN|IND|RUS|ZAF", `Country Code`))
variables_to_include <- "NY.GDP.PCAP.KD.ZG|FS.AST.CGOV.GD.ZS|FP.CPI.TOTL|NY.GDP.MKTP.KD.ZG|MS.MIL.XPND.GD.ZS|SE.PRM.AGES|DT.DOD.DSTC.ZS|SP.POP.TOTL"
df_brics <- df_brics %>% filter(grepl(variables_to_include, `Series Code`))
#glimpse(df_brics)
df_brics_trim <- df_brics_wide %>% dplyr::select(-c(country, country_code, var, var_code))
#transpose(df_brics_trim)
# first remember the names
n <- df_brics_trim$country_var
# transpose all but the first column (name)
df_brics_t <- as.data.frame(t(df_brics_trim[,-1]))
colnames(df_brics_t) <- n
df_brics_t <- df_brics_t %>% rename_with(~ gsub("\\((.*?)\\)", "", .x))
df_brics_t <- df_brics_t %>% rename_with(~ gsub(" ", "_", .x))
df_brics_t <- df_brics_t %>% rename_with(~ gsub(",", "", .x))
df_brics_t <- df_brics_t %>% rename_with(~ gsub("\\.", "", .x))
df_brics_t <- df_brics_t %>% rename_with(~ gsub("\\_$", "", .x))
df_brics_t <- df_brics_t %>% rename_with(~ tolower(gsub("-", "_", .x)))
df_brics_t <- df_brics_t %>% rename_with(~ gsub("n_federation", "", .x))
df_brics_t <- df_brics_t %>% rename_with(~ gsub("_etc", "", .x))
#head(df_brics_t)
df_brics_t[df_brics_t==".."] <- NA
# Visualize NA counts for each column
df_brics_t %>%
summarise_all(list(~is.na(.)))%>%
pivot_longer(everything(),
names_to = "variables", values_to="missing") %>%
count(variables, missing) %>%
ggplot(aes(y=variables,x=n,fill=missing))+
geom_col()

df_bics <- df_brics_t %>% dplyr::select(!starts_with("Russia"))
#head(df_bics, 10)
dim(df_bics)
## [1] 50 32
df_bics <- df_bics[10:49, ]
#head(df_bics)
#write.csv(df_bics,'bics-raw.csv')

## brazil_claims_on_central_government brazil_consumer_price_index
## 1981 -7.94880285752566 7.20570021177796E-10
## 1982 -6.71335644028969 1.44505532582099E-09
## 1983 -6.19744689646999 3.39628003922508E-09
## 1984 -10.7005674289363 9.92127211260219E-09
## 1985 -11.5883704305964 3.2342321744633E-08
## 1986 -10.7005674289363 7.99317280305453E-08
## brazil_military_expenditure brazil_primary_school_starting_age
## 1981 1.14314876818876 7
## 1982 1.30353436107498 7
## 1983 1.25458032300474 7
## 1984 1.19954104516533 7
## 1985 1.42952312218953 7
## 1986 1.22038872750142 7
## brazil_short_term_debt brazil_population_total brazil_gdp_growth
## 1981 18.742 123570327 -4.39335720034877
## 1982 18.5149 126498322 0.58024555339469
## 1983 14.4029 129448815 -3.40979347371636
## 1984 10.557 132383569 5.26914314976867
## 1985 9.0027 135274083 7.94586174871235
## 1986 8.5999 138108915 7.98829510486276
## brazil_gdp_per_capita_growth china_claims_on_central_government
## 1981 -6.618768651953 -0.502448134235561
## 1982 -1.74783636439638 -0.104218036382346
## 1983 -5.611348800618 0.0979916023075936
## 1984 2.93547711198904 1.30383966370855
## 1985 5.63929261361262 -1.02539326218941
## 1986 5.77172078314929 0.56475643516567
## china_consumer_price_index china_military_expenditure
## 1981 100 1.63265067284541
## 1982 100 1.63265067284541
## 1983 100 1.63265067284541
## 1984 100 1.63265067284541
## 1985 100 1.63265067284541
## 1986 26.0483197849659 1.63265067284541
## china_primary_school_starting_age china_short_term_debt
## 1981 7 0
## 1982 7 27.512
## 1983 7 41.4494
## 1984 7 46.3233
## 1985 7 38.4389
## 1986 7 25.6162
## china_population_total china_gdp_growth china_gdp_per_capita_growth
## 1981 993885000 5.11276109058912 3.77490366463344
## 1982 1008630000 9.01711391382203 7.4234102319374
## 1983 1023310000 10.7702025065715 9.18113704957753
## 1984 1036825000 15.1915400236134 13.6900198409219
## 1985 1051040000 13.4306775204064 11.8965617103967
## 1986 1066790000 8.94996176438008 7.34143347128678
## india_claims_on_central_government india_consumer_price_index
## 1981 16.8047994946957 11.0142316437707
## 1982 18.2038937927274 11.8833363334912
## 1983 17.929345684808 13.2936603505448
## 1984 19.3789001267168 14.3995476077688
## 1985 21.0818577843934 15.1996475603979
## 1986 22.7738813575351 16.5265343439129
## india_military_expenditure india_primary_school_starting_age
## 1981 3.18624812218328 6
## 1982 3.34511766555139 6
## 1983 3.3230686597074 6
## 1984 3.41687559436328 6
## 1985 3.5690302776083 6
## 1986 4.10646868066223 6
## india_short_term_debt india_population_total india_gdp_growth
## 1981 7.104 715384997 6.00620362381754
## 1982 8.8966 732239498 3.47573324031229
## 1983 10.8908 749428958 7.28889290124583
## 1984 11.2074 766833411 3.82073785597366
## 1985 11.193 784360012 5.25429922330882
## 1986 11.0132 801975250 4.77656417048958
## india_gdp_per_capita_growth south_africa_claims_on_central_government
## 1981 3.57127570913673 13.5019268465057
## 1982 1.09395534641537 12.238911136425
## 1983 4.82803505303571 12.1758907485368
## 1984 1.46436797625853 11.6768036177522
## 1985 2.90238163725327 13.2436434885628
## 1986 2.47516632225742 12.3469592777191
## south_africa_consumer_price_index south_africa_military_expenditure
## 1981 7.46987031120577 3.63812916270383
## 1982 8.56338740977399 3.61409329641932
## 1983 9.61695870311206 3.60464820498906
## 1984 10.7254555673885 3.69560695941547
## 1985 12.4730855520057 3.69833382968385
## 1986 14.7999305407644 3.82057632450885
## south_africa_primary_school_starting_age south_africa_short_term_debt
## 1981 6 16.2724
## 1982 6 16.2724
## 1983 6 16.2724
## 1984 6 16.2724
## 1985 6 16.2724
## 1986 6 16.2724
## south_africa_population_total south_africa_gdp_growth
## 1981 29333095 5.36073741903375
## 1982 30150448 -0.383390768760975
## 1983 30993762 -1.84654447698229
## 1984 31841588 5.09911490972956
## 1985 32678876 -1.21148371912611
## 1986 33495956 0.01783477808317
## south_africa_gdp_per_capita_growth
## 1981 2.57228058840973
## 1982 -3.08391231341534
## 1983 -4.51721682682282
## 1984 2.30070667087364
## 1985 -3.74261236687336
## 1986 -2.42193951707283

# Visualize correlation between variables
corrplot(cor(df_bics_num %>% dplyr::select(starts_with("Brazil"))), method="shade", shade.col=NA, tl.col="black", tl.srt=45)

Brazil
Plot Series (overlayed)

Test for Stationary
##
## Phillips-Perron Unit Root Test
##
## data: braz_claims
## Dickey-Fuller Z(alpha) = -12.713, Truncation lag parameter = 3, p-value
## = 0.3263
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: braz_cpi
## Dickey-Fuller Z(alpha) = -5.1403, Truncation lag parameter = 3, p-value
## = 0.8077
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: braz_milt
## Dickey-Fuller Z(alpha) = -10.854, Truncation lag parameter = 3, p-value
## = 0.4445
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: braz_st_debt
## Dickey-Fuller Z(alpha) = -13.388, Truncation lag parameter = 3, p-value
## = 0.2833
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(braz_claims)
## Dickey-Fuller Z(alpha) = -27.709, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(braz_cpi)
## Dickey-Fuller Z(alpha) = -19.766, Truncation lag parameter = 3, p-value
## = 0.0405
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(braz_milt)
## Dickey-Fuller Z(alpha) = -26.987, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(braz_st_debt)
## Dickey-Fuller Z(alpha) = -40.166, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
Create vectors of time-series objects for modeling
Train, test split
## [1] 175
## [1] 20
VAR - Vector AutoRegressive model
Find optimal lag length
## AIC(n) HQ(n) SC(n) FPE(n)
## 6 6 6 6
Build VAR model
##
## VAR Estimation Results:
## =========================
## Endogenous variables: braz_gdp_diff, braz_claims_diff, braz_cpi_diff, braz_milt_diff, braz_st_debt_diff
## Deterministic variables: const
## Sample size: 36
## Log Likelihood: -327.56
## Roots of the characteristic polynomial:
## 0.8827 0.8827 0.8191 0.8191 0.7884 0.7884 0.7879 0.7879 0.7841 0.7841 0.7255 0.7255 0.705 0.4959 0.4959
## Call:
## VAR(y = braz_vec, p = 3, type = "const", exogen = NULL)
##
##
## Estimation results for equation braz_gdp_diff:
## ==============================================
## braz_gdp_diff = braz_gdp_diff.l1 + braz_claims_diff.l1 + braz_cpi_diff.l1 + braz_milt_diff.l1 + braz_st_debt_diff.l1 + braz_gdp_diff.l2 + braz_claims_diff.l2 + braz_cpi_diff.l2 + braz_milt_diff.l2 + braz_st_debt_diff.l2 + braz_gdp_diff.l3 + braz_claims_diff.l3 + braz_cpi_diff.l3 + braz_milt_diff.l3 + braz_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## braz_gdp_diff.l1 -0.27697 0.16388 -1.690 0.1065
## braz_claims_diff.l1 -0.06445 0.07305 -0.882 0.3881
## braz_cpi_diff.l1 -0.22589 0.17930 -1.260 0.2222
## braz_milt_diff.l1 -3.31817 2.89994 -1.144 0.2660
## braz_st_debt_diff.l1 -0.16201 0.19225 -0.843 0.4094
## braz_gdp_diff.l2 -0.29844 0.19771 -1.509 0.1468
## braz_claims_diff.l2 -0.05707 0.07121 -0.801 0.4323
## braz_cpi_diff.l2 0.16686 0.19381 0.861 0.3995
## braz_milt_diff.l2 4.76902 3.50461 1.361 0.1887
## braz_st_debt_diff.l2 -0.49525 0.18881 -2.623 0.0163 *
## braz_gdp_diff.l3 -0.42808 0.18054 -2.371 0.0279 *
## braz_claims_diff.l3 0.08780 0.06208 1.414 0.1727
## braz_cpi_diff.l3 -0.10800 0.14853 -0.727 0.4756
## braz_milt_diff.l3 -8.34983 3.05270 -2.735 0.0128 *
## braz_st_debt_diff.l3 0.56808 0.18529 3.066 0.0061 **
## const 0.78194 0.91324 0.856 0.4020
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 2.795 on 20 degrees of freedom
## Multiple R-Squared: 0.6105, Adjusted R-squared: 0.3184
## F-statistic: 2.09 on 15 and 20 DF, p-value: 0.06215
##
##
## Estimation results for equation braz_claims_diff:
## =================================================
## braz_claims_diff = braz_gdp_diff.l1 + braz_claims_diff.l1 + braz_cpi_diff.l1 + braz_milt_diff.l1 + braz_st_debt_diff.l1 + braz_gdp_diff.l2 + braz_claims_diff.l2 + braz_cpi_diff.l2 + braz_milt_diff.l2 + braz_st_debt_diff.l2 + braz_gdp_diff.l3 + braz_claims_diff.l3 + braz_cpi_diff.l3 + braz_milt_diff.l3 + braz_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## braz_gdp_diff.l1 -1.43714 0.53543 -2.684 0.01427 *
## braz_claims_diff.l1 0.19014 0.23867 0.797 0.43501
## braz_cpi_diff.l1 0.40854 0.58581 0.697 0.49359
## braz_milt_diff.l1 18.48338 9.47493 1.951 0.06524 .
## braz_st_debt_diff.l1 -0.45305 0.62815 -0.721 0.47911
## braz_gdp_diff.l2 -0.38023 0.64598 -0.589 0.56272
## braz_claims_diff.l2 -0.40522 0.23268 -1.742 0.09695 .
## braz_cpi_diff.l2 -0.84527 0.63323 -1.335 0.19692
## braz_milt_diff.l2 -14.98878 11.45055 -1.309 0.20538
## braz_st_debt_diff.l2 0.64267 0.61688 1.042 0.30993
## braz_gdp_diff.l3 0.41874 0.58988 0.710 0.48599
## braz_claims_diff.l3 -0.01777 0.20283 -0.088 0.93107
## braz_cpi_diff.l3 0.76820 0.48529 1.583 0.12911
## braz_milt_diff.l3 20.18082 9.97404 2.023 0.05661 .
## braz_st_debt_diff.l3 -1.76583 0.60541 -2.917 0.00853 **
## const 0.42716 2.98382 0.143 0.88760
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 9.131 on 20 degrees of freedom
## Multiple R-Squared: 0.599, Adjusted R-squared: 0.2983
## F-statistic: 1.992 on 15 and 20 DF, p-value: 0.07512
##
##
## Estimation results for equation braz_cpi_diff:
## ==============================================
## braz_cpi_diff = braz_gdp_diff.l1 + braz_claims_diff.l1 + braz_cpi_diff.l1 + braz_milt_diff.l1 + braz_st_debt_diff.l1 + braz_gdp_diff.l2 + braz_claims_diff.l2 + braz_cpi_diff.l2 + braz_milt_diff.l2 + braz_st_debt_diff.l2 + braz_gdp_diff.l3 + braz_claims_diff.l3 + braz_cpi_diff.l3 + braz_milt_diff.l3 + braz_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## braz_gdp_diff.l1 0.04426 0.20611 0.215 0.8322
## braz_claims_diff.l1 -0.08434 0.09187 -0.918 0.3695
## braz_cpi_diff.l1 0.59160 0.22551 2.623 0.0163 *
## braz_milt_diff.l1 4.33417 3.64733 1.188 0.2486
## braz_st_debt_diff.l1 0.18344 0.24180 0.759 0.4569
## braz_gdp_diff.l2 -0.18522 0.24867 -0.745 0.4650
## braz_claims_diff.l2 0.05403 0.08957 0.603 0.5531
## braz_cpi_diff.l2 0.05523 0.24376 0.227 0.8231
## braz_milt_diff.l2 -5.81363 4.40783 -1.319 0.2021
## braz_st_debt_diff.l2 0.06052 0.23747 0.255 0.8014
## braz_gdp_diff.l3 0.17121 0.22707 0.754 0.4596
## braz_claims_diff.l3 0.07052 0.07808 0.903 0.3772
## braz_cpi_diff.l3 -0.12309 0.18681 -0.659 0.5175
## braz_milt_diff.l3 -9.76891 3.83945 -2.544 0.0193 *
## braz_st_debt_diff.l3 0.11291 0.23305 0.484 0.6333
## const 2.38492 1.14861 2.076 0.0510 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 3.515 on 20 degrees of freedom
## Multiple R-Squared: 0.6585, Adjusted R-squared: 0.4023
## F-statistic: 2.571 on 15 and 20 DF, p-value: 0.0251
##
##
## Estimation results for equation braz_milt_diff:
## ===============================================
## braz_milt_diff = braz_gdp_diff.l1 + braz_claims_diff.l1 + braz_cpi_diff.l1 + braz_milt_diff.l1 + braz_st_debt_diff.l1 + braz_gdp_diff.l2 + braz_claims_diff.l2 + braz_cpi_diff.l2 + braz_milt_diff.l2 + braz_st_debt_diff.l2 + braz_gdp_diff.l3 + braz_claims_diff.l3 + braz_cpi_diff.l3 + braz_milt_diff.l3 + braz_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## braz_gdp_diff.l1 -0.010932 0.011094 -0.985 0.3362
## braz_claims_diff.l1 0.010542 0.004945 2.132 0.0456 *
## braz_cpi_diff.l1 -0.007885 0.012138 -0.650 0.5234
## braz_milt_diff.l1 -0.181804 0.196318 -0.926 0.3655
## braz_st_debt_diff.l1 0.003345 0.013015 0.257 0.7998
## braz_gdp_diff.l2 0.004405 0.013385 0.329 0.7455
## braz_claims_diff.l2 0.003223 0.004821 0.669 0.5114
## braz_cpi_diff.l2 0.002150 0.013120 0.164 0.8715
## braz_milt_diff.l2 -0.293499 0.237252 -1.237 0.2304
## braz_st_debt_diff.l2 0.010843 0.012782 0.848 0.4063
## braz_gdp_diff.l3 0.016990 0.012222 1.390 0.1798
## braz_claims_diff.l3 -0.004858 0.004203 -1.156 0.2613
## braz_cpi_diff.l3 -0.013738 0.010055 -1.366 0.1870
## braz_milt_diff.l3 -0.297043 0.206659 -1.437 0.1661
## braz_st_debt_diff.l3 -0.029836 0.012544 -2.378 0.0275 *
## const 0.077239 0.061824 1.249 0.2260
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.1892 on 20 degrees of freedom
## Multiple R-Squared: 0.6058, Adjusted R-squared: 0.3102
## F-statistic: 2.049 on 15 and 20 DF, p-value: 0.06722
##
##
## Estimation results for equation braz_st_debt_diff:
## ==================================================
## braz_st_debt_diff = braz_gdp_diff.l1 + braz_claims_diff.l1 + braz_cpi_diff.l1 + braz_milt_diff.l1 + braz_st_debt_diff.l1 + braz_gdp_diff.l2 + braz_claims_diff.l2 + braz_cpi_diff.l2 + braz_milt_diff.l2 + braz_st_debt_diff.l2 + braz_gdp_diff.l3 + braz_claims_diff.l3 + braz_cpi_diff.l3 + braz_milt_diff.l3 + braz_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## braz_gdp_diff.l1 -0.2076535 0.1792832 -1.158 0.2604
## braz_claims_diff.l1 0.0717143 0.0799155 0.897 0.3802
## braz_cpi_diff.l1 -0.1467401 0.1961520 -0.748 0.4631
## braz_milt_diff.l1 -3.0305923 3.1725540 -0.955 0.3509
## braz_st_debt_diff.l1 -0.0255204 0.2103269 -0.121 0.9046
## braz_gdp_diff.l2 -0.0026629 0.2162990 -0.012 0.9903
## braz_claims_diff.l2 0.1155129 0.0779088 1.483 0.1537
## braz_cpi_diff.l2 0.2524389 0.2120278 1.191 0.2478
## braz_milt_diff.l2 1.5788641 3.8340616 0.412 0.6849
## braz_st_debt_diff.l2 0.0105448 0.2065545 0.051 0.9598
## braz_gdp_diff.l3 0.0846668 0.1975150 0.429 0.6728
## braz_claims_diff.l3 0.0004023 0.0679160 0.006 0.9953
## braz_cpi_diff.l3 -0.3069409 0.1624933 -1.889 0.0735 .
## braz_milt_diff.l3 -3.5325895 3.3396720 -1.058 0.3028
## braz_st_debt_diff.l3 0.0329716 0.2027134 0.163 0.8724
## const 0.7180177 0.9990932 0.719 0.4807
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 3.057 on 20 degrees of freedom
## Multiple R-Squared: 0.4002, Adjusted R-squared: -0.04958
## F-statistic: 0.8898 on 15 and 20 DF, p-value: 0.5849
##
##
##
## Covariance matrix of residuals:
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_milt_diff
## braz_gdp_diff 7.8094 4.7378 -1.1956 0.16641
## braz_claims_diff 4.7378 83.3668 -17.1844 0.77849
## braz_cpi_diff -1.1956 -17.1844 12.3535 -0.20468
## braz_milt_diff 0.1664 0.7785 -0.2047 0.03579
## braz_st_debt_diff 2.9262 -7.1107 0.5278 0.02999
## braz_st_debt_diff
## braz_gdp_diff 2.92624
## braz_claims_diff -7.11072
## braz_cpi_diff 0.52782
## braz_milt_diff 0.02999
## braz_st_debt_diff 9.34671
##
## Correlation matrix of residuals:
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_milt_diff
## braz_gdp_diff 1.0000 0.1857 -0.12173 0.31477
## braz_claims_diff 0.1857 1.0000 -0.53548 0.45069
## braz_cpi_diff -0.1217 -0.5355 1.00000 -0.30782
## braz_milt_diff 0.3148 0.4507 -0.30782 1.00000
## braz_st_debt_diff 0.3425 -0.2547 0.04912 0.05185
## braz_st_debt_diff
## braz_gdp_diff 0.34251
## braz_claims_diff -0.25473
## braz_cpi_diff 0.04912
## braz_milt_diff 0.05185
## braz_st_debt_diff 1.00000
Stargazer Output
##
## ==========================================================================
## Dependent variable:
## --------------------------------------------
## y
## (1) (2) (3) (4) (5)
## --------------------------------------------------------------------------
## braz_gdp_diff.l1 -0.277 -1.437** 0.044 -0.011 -0.208
## (0.164) (0.535) (0.206) (0.011) (0.179)
##
## braz_claims_diff.l1 -0.064 0.190 -0.084 0.011** 0.072
## (0.073) (0.239) (0.092) (0.005) (0.080)
##
## braz_cpi_diff.l1 -0.226 0.409 0.592** -0.008 -0.147
## (0.179) (0.586) (0.226) (0.012) (0.196)
##
## braz_milt_diff.l1 -3.318 18.483* 4.334 -0.182 -3.031
## (2.900) (9.475) (3.647) (0.196) (3.173)
##
## braz_st_debt_diff.l1 -0.162 -0.453 0.183 0.003 -0.026
## (0.192) (0.628) (0.242) (0.013) (0.210)
##
## braz_gdp_diff.l2 -0.298 -0.380 -0.185 0.004 -0.003
## (0.198) (0.646) (0.249) (0.013) (0.216)
##
## braz_claims_diff.l2 -0.057 -0.405* 0.054 0.003 0.116
## (0.071) (0.233) (0.090) (0.005) (0.078)
##
## braz_cpi_diff.l2 0.167 -0.845 0.055 0.002 0.252
## (0.194) (0.633) (0.244) (0.013) (0.212)
##
## braz_milt_diff.l2 4.769 -14.989 -5.814 -0.293 1.579
## (3.505) (11.451) (4.408) (0.237) (3.834)
##
## braz_st_debt_diff.l2 -0.495** 0.643 0.061 0.011 0.011
## (0.189) (0.617) (0.237) (0.013) (0.207)
##
## braz_gdp_diff.l3 -0.428** 0.419 0.171 0.017 0.085
## (0.181) (0.590) (0.227) (0.012) (0.198)
##
## braz_claims_diff.l3 0.088 -0.018 0.071 -0.005 0.0004
## (0.062) (0.203) (0.078) (0.004) (0.068)
##
## braz_cpi_diff.l3 -0.108 0.768 -0.123 -0.014 -0.307*
## (0.149) (0.485) (0.187) (0.010) (0.162)
##
## braz_milt_diff.l3 -8.350** 20.181* -9.769** -0.297 -3.533
## (3.053) (9.974) (3.839) (0.207) (3.340)
##
## braz_st_debt_diff.l3 0.568*** -1.766*** 0.113 -0.030** 0.033
## (0.185) (0.605) (0.233) (0.013) (0.203)
##
## const 0.782 0.427 2.385* 0.077 0.718
## (0.913) (2.984) (1.149) (0.062) (0.999)
##
## --------------------------------------------------------------------------
## Observations 36 36 36 36 36
## R2 0.611 0.599 0.658 0.606 0.400
## Adjusted R2 0.318 0.298 0.402 0.310 -0.050
## Residual Std. Error (df = 20) 2.795 9.131 3.515 0.189 3.057
## F Statistic (df = 15; 20) 2.090* 1.992* 2.571** 2.049* 0.890
## ==========================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
Check for Serial Correlation
braz_serial <- serial.test(braz_model, type = "PT.asymptotic")
braz_serial
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object braz_model
## Chi-squared = 284.43, df = 325, p-value = 0.9491
Test for Heteroscedasticity
braz_arch <- vars::arch.test(braz_model, lags.multi = 15, multivariate.only = TRUE)
braz_arch
##
## ARCH (multivariate)
##
## data: Residuals of VAR object braz_model
## Chi-squared = 315, df = 3375, p-value = 1
Test for Normality of Residuals
braz_norm <- normality.test(braz_model, multivariate.only = TRUE)
braz_norm
## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object braz_model
## Chi-squared = 14.032, df = 10, p-value = 0.1715
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object braz_model
## Chi-squared = 7.1553, df = 5, p-value = 0.2093
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object braz_model
## Chi-squared = 6.8766, df = 5, p-value = 0.23
Check for Stability of model
braz_stability <- stability(braz_model, type = "OLS-CUSUM")
plot(braz_stability)

Check for Granger Causality
granger_gdp <- causality(braz_model, cause = "braz_gdp_diff")
granger_gdp
## $Granger
##
## Granger causality H0: braz_gdp_diff do not Granger-cause
## braz_claims_diff braz_cpi_diff braz_milt_diff braz_st_debt_diff
##
## data: VAR object braz_model
## F-Test = 1.8468, df1 = 12, df2 = 100, p-value = 0.05051
##
##
## $Instant
##
## H0: No instantaneous causality between: braz_gdp_diff and
## braz_claims_diff braz_cpi_diff braz_milt_diff braz_st_debt_diff
##
## data: VAR object braz_model
## Chi-squared = 6.7767, df = 4, p-value = 0.1482
granger_claims <- causality(braz_model, cause = "braz_claims_diff")
granger_claims
## $Granger
##
## Granger causality H0: braz_claims_diff do not Granger-cause
## braz_gdp_diff braz_cpi_diff braz_milt_diff braz_st_debt_diff
##
## data: VAR object braz_model
## F-Test = 2.1644, df1 = 12, df2 = 100, p-value = 0.01905
##
##
## $Instant
##
## H0: No instantaneous causality between: braz_claims_diff and
## braz_gdp_diff braz_cpi_diff braz_milt_diff braz_st_debt_diff
##
## data: VAR object braz_model
## Chi-squared = 11.284, df = 4, p-value = 0.02355
granger_cpi <- causality(braz_model, cause = "braz_cpi_diff")
granger_cpi
## $Granger
##
## Granger causality H0: braz_cpi_diff do not Granger-cause braz_gdp_diff
## braz_claims_diff braz_milt_diff braz_st_debt_diff
##
## data: VAR object braz_model
## F-Test = 1.1201, df1 = 12, df2 = 100, p-value = 0.3526
##
##
## $Instant
##
## H0: No instantaneous causality between: braz_cpi_diff and
## braz_gdp_diff braz_claims_diff braz_milt_diff braz_st_debt_diff
##
## data: VAR object braz_model
## Chi-squared = 8.2878, df = 4, p-value = 0.08159
granger_milt <- causality(braz_model, cause = "braz_milt_diff")
granger_milt
## $Granger
##
## Granger causality H0: braz_milt_diff do not Granger-cause
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_st_debt_diff
##
## data: VAR object braz_model
## F-Test = 3.9203, df1 = 12, df2 = 100, p-value = 6.169e-05
##
##
## $Instant
##
## H0: No instantaneous causality between: braz_milt_diff and
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_st_debt_diff
##
## data: VAR object braz_model
## Chi-squared = 7.6411, df = 4, p-value = 0.1056
granger_debt <- causality(braz_model, cause = "braz_st_debt_diff")
granger_debt
## $Granger
##
## Granger causality H0: braz_st_debt_diff do not Granger-cause
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_milt_diff
##
## data: VAR object braz_model
## F-Test = 3.3854, df1 = 12, df2 = 100, p-value = 0.000358
##
##
## $Instant
##
## H0: No instantaneous causality between: braz_st_debt_diff and
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_milt_diff
##
## data: VAR object braz_model
## Chi-squared = 6.8949, df = 4, p-value = 0.1415
Create Impulse Response Functions
# Black line outside of confidence interval means the response is significant!
irf1 <- irf(braz_model, impulse = "braz_cpi_diff", response = "braz_gdp_diff", n.ahead = 4, boot = TRUE, run = 200, ci = 0.95)
plot(irf1)

Review Variance Decomposition
vd1 <- fevd(braz_model, n.ahead = 4)
plot(vd1)

Generate Predictions
forecast <- predict(braz_model, n.ahead = 4, ci = 0.95)
fanchart(forecast, names = "braz_gdp_diff", colors = c("seagreen","lightgreen"), main = "Forecasts", xlab = "Year", ylab = "GDP Growth %")

View Actuals vs Predicted
index_pred <- append(index(train_brazil[,1]), 2017:2019)
all_actuals <- append(train_brazil[,1], test_brazil[1:3,1])
all_preds <- append(train_brazil[,1], forecast$fcst$braz_gdp_diff[,1])
all_actuals_dediff <- diffinv(all_actuals, xi = braz_gdp[1])
all_preds_dediff <- diffinv(all_preds, xi = braz_gdp[1])
all_preds_dediff <- all_preds_dediff[-length(all_preds_dediff)]
all_actuals_dediff
## [1] -4.3933572 0.5802456 -3.4097935 5.2691431 7.9458617 7.9882951
## [7] 3.5996295 -0.1026727 3.2794589 -3.1023559 1.5119372 -0.4669132
## [13] 4.6651690 5.3345517 4.4167314 2.2075355 3.3948460 0.3380979
## [19] 0.4679376 4.3879494 1.3898964 3.0534619 1.1408290 5.7599646
## [25] 3.2021314 3.9619887 6.0698706 5.0941954 -0.1258120 7.5282258
## [31] 3.9744254 1.9211503 3.0048463 0.5039557 -3.5457634 -3.2759169
## [37] 1.3228691 1.7836668 1.4111530
all_preds_dediff
## [1] -4.3933572 0.5802456 -3.4097935 5.2691431 7.9458617 7.9882951
## [7] 3.5996295 -0.1026727 3.2794589 -3.1023559 1.5119372 -0.4669132
## [13] 4.6651690 5.3345517 4.4167314 2.2075355 3.3948460 0.3380979
## [19] 0.4679376 4.3879494 1.3898964 3.0534619 1.1408290 5.7599646
## [25] 3.2021314 3.9619887 6.0698706 5.0941954 -0.1258120 7.5282258
## [31] 3.9744254 1.9211503 3.0048463 0.5039557 -3.5457634 -3.2759169
## [37] -3.2244152 -0.3268554 -0.7071444
length(index_pred)
## [1] 38
length(all_actuals_dediff)
## [1] 39
length(all_preds_dediff[-1])
## [1] 38
plot(index_pred,
all_preds_dediff[-1],
type = "l",
ylab="Actuals and Predicted",
xlab="Year",
col = "red")
lines(index_pred,
all_actuals_dediff[-1],
type = "l",
col = "green")
legend("topleft",
c("Predicted","Actuals"),
fill=c("red","green"))

VAR - Evaluation Metrics
# Root Mean Squared Error
print("Root Mean Squared Error")
## [1] "Root Mean Squared Error"
rmse(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 3.142148
# Mean Absolute Error
print("Mean Absolute Error")
## [1] "Mean Absolute Error"
mae(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 2.925368
# Mean Absolute Percent Error
print("Mean Absolute Percent Error")
## [1] "Mean Absolute Percent Error"
mape(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 2.0406
SVAR - Structural Vector AutoRegressive model
SVAR - Build SVAR model
##
## SVAR Estimation Results:
## ========================
##
## Call:
## SVAR(x = braz_model, estmethod = c("scoring", "direct"), Amat = amat,
## Bmat = NULL, hessian = TRUE)
##
## Type: A-model
## Sample size: 36
## Log Likelihood: -255.409
## Method: c
## Method: scoring
## Method: direct
## Number of iterations: 99
##
## LR overidentification test:
##
## LR overidentification
##
## data: braz_vec
## Chi^2 = -250, df = 5, p-value = 1
##
##
## Estimated A matrix:
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_milt_diff
## braz_gdp_diff 1.00000 0.00000 0.0000 0.000
## braz_claims_diff -0.60667 1.00000 0.0000 0.000
## braz_cpi_diff 0.05615 0.15980 1.0000 0.000
## braz_milt_diff 0.00946 -0.05412 -0.0135 1.000
## braz_st_debt_diff -0.03282 -0.36163 -0.1395 -6.751
## braz_st_debt_diff
## braz_gdp_diff 0
## braz_claims_diff 0
## braz_cpi_diff 0
## braz_milt_diff 0
## braz_st_debt_diff 1
##
## Estimated standard errors for A matrix:
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_milt_diff
## braz_gdp_diff 0.0000 0.0000 0.0000 0.0000
## braz_claims_diff 0.1667 0.0000 0.0000 0.0000
## braz_cpi_diff 0.1949 0.1667 0.0000 0.0000
## braz_milt_diff 0.1952 0.1688 0.1667 0.0000
## braz_st_debt_diff 0.1952 0.1690 0.1667 0.1667
## braz_st_debt_diff
## braz_gdp_diff 0
## braz_claims_diff 0
## braz_cpi_diff 0
## braz_milt_diff 0
## braz_st_debt_diff 0
##
## Estimated B matrix:
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_milt_diff
## braz_gdp_diff 1 0 0 0
## braz_claims_diff 0 1 0 0
## braz_cpi_diff 0 0 1 0
## braz_milt_diff 0 0 0 1
## braz_st_debt_diff 0 0 0 0
## braz_st_debt_diff
## braz_gdp_diff 0
## braz_claims_diff 0
## braz_cpi_diff 0
## braz_milt_diff 0
## braz_st_debt_diff 1
##
## Covariance matrix of reduced form residuals (*100):
## braz_gdp_diff braz_claims_diff braz_cpi_diff braz_milt_diff
## braz_gdp_diff 100.000 60.667 -15.3098 2.1309
## braz_claims_diff 60.667 136.805 -25.2685 6.4894
## braz_cpi_diff -15.310 -25.268 104.8976 0.1937
## braz_milt_diff 2.131 6.489 0.1937 100.3337
## braz_st_debt_diff 37.471 91.748 6.3057 679.8101
## braz_st_debt_diff
## braz_gdp_diff 37.471
## braz_claims_diff 91.748
## braz_cpi_diff 6.306
## braz_milt_diff 679.810
## braz_st_debt_diff 4724.778
SVAR - Review Variance Decomposition
vd1 <- fevd(braz_svar_model, n.ahead = 4)
plot(vd1)

SVAR - Generate predictions
irfvc <- irf(braz_svar_model, n.ahead = 3, cumulative=TRUE)
summary(irfvc)
## Length Class Mode
## irf 5 -none- list
## Lower 5 -none- list
## Upper 5 -none- list
## response 5 -none- character
## impulse 5 -none- character
## ortho 1 -none- logical
## cumulative 1 -none- logical
## runs 1 -none- numeric
## ci 1 -none- numeric
## boot 1 -none- logical
## model 1 -none- character
SVAR - View Actuals vs Predicted
index_pred <- append(index(train_brazil[,1]), 2017:2019)
all_actuals_svar <- append(train_brazil[,1], test_brazil[1:3,1])
all_preds_svar <- append(train_brazil[,1], irfvc$irf$braz_gdp_diff[,1])
all_actuals_dediff_svar <- diffinv(all_actuals_svar, xi = braz_gdp[1])
all_preds_dediff_svar <- diffinv(all_preds_svar, xi = braz_gdp[1])
all_preds_dediff_svar <- all_preds_dediff_svar[-length(all_preds_svar)]
plot(index_pred,
all_preds_dediff_svar[-1],
type = "l",
ylab="Actuals and Predicted",
xlab="Year",
col = "red")
lines(index_pred,
all_actuals_dediff_svar[-1],
type = "l",
col = "green")
legend("topleft",
c("Predicted","Actuals"),
fill=c("red","green"))

SVAR - Evaluation Metrics
# Root Mean Squared Error
print("Root Mean Squared Error")
## [1] "Root Mean Squared Error"
rmse(all_actuals_dediff_svar[37:39], all_preds_dediff_svar[37:39])
## [1] 3.172766
# Mean Absolute Error
print("Mean Absolute Error")
## [1] "Mean Absolute Error"
mae(all_actuals_dediff_svar[37:39], all_preds_dediff_svar[37:39])
## [1] 3.116472
# Mean Absolute Percent Error
print("Mean Absolute Percent Error")
## [1] "Mean Absolute Percent Error"
mape(all_actuals_dediff_svar[37:39], all_preds_dediff_svar[37:39])
## [1] 2.093884
India
Plot Series (overlayed)

Test for Stationary
##
## Phillips-Perron Unit Root Test
##
## data: india_claims
## Dickey-Fuller Z(alpha) = -12.965, Truncation lag parameter = 3, p-value
## = 0.3103
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: india_cpi
## Dickey-Fuller Z(alpha) = -0.0087098, Truncation lag parameter = 3,
## p-value = 0.99
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: india_milt
## Dickey-Fuller Z(alpha) = -11.144, Truncation lag parameter = 3, p-value
## = 0.426
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: india_st_debt
## Dickey-Fuller Z(alpha) = -4.852, Truncation lag parameter = 3, p-value
## = 0.826
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(india_claims)
## Dickey-Fuller Z(alpha) = -21.314, Truncation lag parameter = 3, p-value
## = 0.02442
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(india_cpi)
## Dickey-Fuller Z(alpha) = -14.206, Truncation lag parameter = 3, p-value
## = 0.2288
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(india_milt)
## Dickey-Fuller Z(alpha) = -24.659, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(india_st_debt)
## Dickey-Fuller Z(alpha) = -21.898, Truncation lag parameter = 3, p-value
## = 0.02147
## alternative hypothesis: stationary
Create vectors of time-series objects for modeling
Train, test split
## [1] 175
## [1] 20
VAR - Vector AutoRegressive model
Find optimal lag length
## AIC(n) HQ(n) SC(n) FPE(n)
## 6 6 6 5
Build VAR model
##
## VAR Estimation Results:
## =========================
## Endogenous variables: india_gdp_diff, india_claims_diff, india_cpi_diff, india_milt_diff, india_st_debt_diff
## Deterministic variables: const
## Sample size: 36
## Log Likelihood: -234.736
## Roots of the characteristic polynomial:
## 0.9147 0.8247 0.8247 0.7818 0.7818 0.7215 0.7215 0.7097 0.6683 0.6683 0.5966 0.5966 0.3822 0.2951 0.2951
## Call:
## VAR(y = india_vec, p = 3, type = "const", exogen = NULL)
##
##
## Estimation results for equation india_gdp_diff:
## ===============================================
## india_gdp_diff = india_gdp_diff.l1 + india_claims_diff.l1 + india_cpi_diff.l1 + india_milt_diff.l1 + india_st_debt_diff.l1 + india_gdp_diff.l2 + india_claims_diff.l2 + india_cpi_diff.l2 + india_milt_diff.l2 + india_st_debt_diff.l2 + india_gdp_diff.l3 + india_claims_diff.l3 + india_cpi_diff.l3 + india_milt_diff.l3 + india_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## india_gdp_diff.l1 -0.44016 0.34478 -1.277 0.216
## india_claims_diff.l1 -0.46747 0.60453 -0.773 0.448
## india_cpi_diff.l1 0.10087 0.58443 0.173 0.865
## india_milt_diff.l1 4.57903 4.06294 1.127 0.273
## india_st_debt_diff.l1 0.29115 0.43361 0.671 0.510
## india_gdp_diff.l2 -0.22141 0.39000 -0.568 0.577
## india_claims_diff.l2 0.44593 0.59409 0.751 0.462
## india_cpi_diff.l2 -0.54334 0.59012 -0.921 0.368
## india_milt_diff.l2 -1.95852 4.42764 -0.442 0.663
## india_st_debt_diff.l2 -0.40980 0.43636 -0.939 0.359
## india_gdp_diff.l3 -0.07845 0.32902 -0.238 0.814
## india_claims_diff.l3 0.66260 0.56995 1.163 0.259
## india_cpi_diff.l3 0.31025 0.49578 0.626 0.539
## india_milt_diff.l3 -1.63020 4.68263 -0.348 0.731
## india_st_debt_diff.l3 0.29594 0.45192 0.655 0.520
## const 0.14909 1.08651 0.137 0.892
##
##
## Residual standard error: 3.606 on 20 degrees of freedom
## Multiple R-Squared: 0.291, Adjusted R-squared: -0.2407
## F-statistic: 0.5473 on 15 and 20 DF, p-value: 0.8812
##
##
## Estimation results for equation india_claims_diff:
## ==================================================
## india_claims_diff = india_gdp_diff.l1 + india_claims_diff.l1 + india_cpi_diff.l1 + india_milt_diff.l1 + india_st_debt_diff.l1 + india_gdp_diff.l2 + india_claims_diff.l2 + india_cpi_diff.l2 + india_milt_diff.l2 + india_st_debt_diff.l2 + india_gdp_diff.l3 + india_claims_diff.l3 + india_cpi_diff.l3 + india_milt_diff.l3 + india_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## india_gdp_diff.l1 -0.1588118 0.1695411 -0.937 0.360
## india_claims_diff.l1 0.3960240 0.2972683 1.332 0.198
## india_cpi_diff.l1 -0.2532939 0.2873839 -0.881 0.389
## india_milt_diff.l1 -2.8574389 1.9978830 -1.430 0.168
## india_st_debt_diff.l1 -0.2739241 0.2132222 -1.285 0.214
## india_gdp_diff.l2 -0.1448707 0.1917753 -0.755 0.459
## india_claims_diff.l2 -0.3115059 0.2921319 -1.066 0.299
## india_cpi_diff.l2 0.2839799 0.2901833 0.979 0.339
## india_milt_diff.l2 3.3006135 2.1772183 1.516 0.145
## india_st_debt_diff.l2 0.2200605 0.2145751 1.026 0.317
## india_gdp_diff.l3 -0.1072810 0.1617924 -0.663 0.515
## india_claims_diff.l3 -0.2766458 0.2802624 -0.987 0.335
## india_cpi_diff.l3 -0.0001505 0.2437917 -0.001 1.000
## india_milt_diff.l3 2.0102685 2.3026037 0.873 0.393
## india_st_debt_diff.l3 0.2233061 0.2222231 1.005 0.327
## const 0.1786234 0.5342755 0.334 0.742
##
##
## Residual standard error: 1.773 on 20 degrees of freedom
## Multiple R-Squared: 0.4512, Adjusted R-squared: 0.03962
## F-statistic: 1.096 on 15 and 20 DF, p-value: 0.4164
##
##
## Estimation results for equation india_cpi_diff:
## ===============================================
## india_cpi_diff = india_gdp_diff.l1 + india_claims_diff.l1 + india_cpi_diff.l1 + india_milt_diff.l1 + india_st_debt_diff.l1 + india_gdp_diff.l2 + india_claims_diff.l2 + india_cpi_diff.l2 + india_milt_diff.l2 + india_st_debt_diff.l2 + india_gdp_diff.l3 + india_claims_diff.l3 + india_cpi_diff.l3 + india_milt_diff.l3 + india_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## india_gdp_diff.l1 -0.14982 0.16107 -0.930 0.3634
## india_claims_diff.l1 0.19158 0.28241 0.678 0.5053
## india_cpi_diff.l1 0.66482 0.27302 2.435 0.0244 *
## india_milt_diff.l1 -0.13929 1.89802 -0.073 0.9422
## india_st_debt_diff.l1 -0.02926 0.20256 -0.144 0.8866
## india_gdp_diff.l2 -0.06975 0.18219 -0.383 0.7059
## india_claims_diff.l2 0.06150 0.27753 0.222 0.8269
## india_cpi_diff.l2 0.21800 0.27568 0.791 0.4383
## india_milt_diff.l2 0.11878 2.06839 0.057 0.9548
## india_st_debt_diff.l2 0.32969 0.20385 1.617 0.1215
## india_gdp_diff.l3 0.05608 0.15371 0.365 0.7190
## india_claims_diff.l3 -0.35802 0.26625 -1.345 0.1938
## india_cpi_diff.l3 0.04806 0.23161 0.208 0.8377
## india_milt_diff.l3 0.14824 2.18751 0.068 0.9466
## india_st_debt_diff.l3 -0.02419 0.21112 -0.115 0.9099
## const 0.56986 0.50757 1.123 0.2749
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 1.684 on 20 degrees of freedom
## Multiple R-Squared: 0.8513, Adjusted R-squared: 0.7399
## F-statistic: 7.636 on 15 and 20 DF, p-value: 2.596e-05
##
##
## Estimation results for equation india_milt_diff:
## ================================================
## india_milt_diff = india_gdp_diff.l1 + india_claims_diff.l1 + india_cpi_diff.l1 + india_milt_diff.l1 + india_st_debt_diff.l1 + india_gdp_diff.l2 + india_claims_diff.l2 + india_cpi_diff.l2 + india_milt_diff.l2 + india_st_debt_diff.l2 + india_gdp_diff.l3 + india_claims_diff.l3 + india_cpi_diff.l3 + india_milt_diff.l3 + india_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## india_gdp_diff.l1 0.012637 0.020346 0.621 0.5415
## india_claims_diff.l1 0.018405 0.035674 0.516 0.6116
## india_cpi_diff.l1 -0.053267 0.034488 -1.545 0.1381
## india_milt_diff.l1 0.190001 0.239757 0.792 0.4374
## india_st_debt_diff.l1 0.020271 0.025588 0.792 0.4375
## india_gdp_diff.l2 -0.016870 0.023014 -0.733 0.4720
## india_claims_diff.l2 -0.025081 0.035057 -0.715 0.4826
## india_cpi_diff.l2 0.012111 0.034824 0.348 0.7316
## india_milt_diff.l2 -0.121481 0.261279 -0.465 0.6470
## india_st_debt_diff.l2 -0.006877 0.025750 -0.267 0.7921
## india_gdp_diff.l3 -0.016122 0.019416 -0.830 0.4161
## india_claims_diff.l3 -0.015648 0.033633 -0.465 0.6468
## india_cpi_diff.l3 0.041097 0.029256 1.405 0.1754
## india_milt_diff.l3 -0.007734 0.276326 -0.028 0.9779
## india_st_debt_diff.l3 0.062475 0.026668 2.343 0.0296 *
## const -0.018035 0.064116 -0.281 0.7814
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.2128 on 20 degrees of freedom
## Multiple R-Squared: 0.4548, Adjusted R-squared: 0.04589
## F-statistic: 1.112 on 15 and 20 DF, p-value: 0.4049
##
##
## Estimation results for equation india_st_debt_diff:
## ===================================================
## india_st_debt_diff = india_gdp_diff.l1 + india_claims_diff.l1 + india_cpi_diff.l1 + india_milt_diff.l1 + india_st_debt_diff.l1 + india_gdp_diff.l2 + india_claims_diff.l2 + india_cpi_diff.l2 + india_milt_diff.l2 + india_st_debt_diff.l2 + india_gdp_diff.l3 + india_claims_diff.l3 + india_cpi_diff.l3 + india_milt_diff.l3 + india_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## india_gdp_diff.l1 -0.01630 0.19056 -0.086 0.933
## india_claims_diff.l1 -0.42935 0.33412 -1.285 0.213
## india_cpi_diff.l1 0.37070 0.32301 1.148 0.265
## india_milt_diff.l1 2.02386 2.24555 0.901 0.378
## india_st_debt_diff.l1 0.16388 0.23965 0.684 0.502
## india_gdp_diff.l2 0.22198 0.21555 1.030 0.315
## india_claims_diff.l2 -0.17450 0.32835 -0.531 0.601
## india_cpi_diff.l2 -0.20178 0.32616 -0.619 0.543
## india_milt_diff.l2 3.14840 2.44711 1.287 0.213
## india_st_debt_diff.l2 0.01677 0.24117 0.070 0.945
## india_gdp_diff.l3 0.16860 0.18185 0.927 0.365
## india_claims_diff.l3 -0.05833 0.31500 -0.185 0.855
## india_cpi_diff.l3 -0.18922 0.27401 -0.691 0.498
## india_milt_diff.l3 1.11168 2.58804 0.430 0.672
## india_st_debt_diff.l3 -0.19633 0.24977 -0.786 0.441
## const 0.45038 0.60051 0.750 0.462
##
##
## Residual standard error: 1.993 on 20 degrees of freedom
## Multiple R-Squared: 0.4597, Adjusted R-squared: 0.0545
## F-statistic: 1.134 on 15 and 20 DF, p-value: 0.3892
##
##
##
## Covariance matrix of residuals:
## india_gdp_diff india_claims_diff india_cpi_diff
## india_gdp_diff 13.0004 -4.1810 -1.76309
## india_claims_diff -4.1810 3.1435 0.77992
## india_cpi_diff -1.7631 0.7799 2.83711
## india_milt_diff -0.3034 0.1829 -0.04894
## india_st_debt_diff 0.6451 -0.2499 1.13315
## india_milt_diff india_st_debt_diff
## india_gdp_diff -0.30338 0.6451
## india_claims_diff 0.18293 -0.2499
## india_cpi_diff -0.04894 1.1332
## india_milt_diff 0.04527 -0.1077
## india_st_debt_diff -0.10774 3.9712
##
## Correlation matrix of residuals:
## india_gdp_diff india_claims_diff india_cpi_diff
## india_gdp_diff 1.00000 -0.65402 -0.2903
## india_claims_diff -0.65402 1.00000 0.2612
## india_cpi_diff -0.29031 0.26116 1.0000
## india_milt_diff -0.39545 0.48492 -0.1366
## india_st_debt_diff 0.08978 -0.07073 0.3376
## india_milt_diff india_st_debt_diff
## india_gdp_diff -0.3955 0.08978
## india_claims_diff 0.4849 -0.07073
## india_cpi_diff -0.1366 0.33759
## india_milt_diff 1.0000 -0.25410
## india_st_debt_diff -0.2541 1.00000
Stargazer Output
##
## ======================================================================
## Dependent variable:
## ----------------------------------------
## y
## (1) (2) (3) (4) (5)
## ----------------------------------------------------------------------
## india_gdp_diff.l1 -0.440 -0.159 -0.150 0.013 -0.016
## (0.345) (0.170) (0.161) (0.020) (0.191)
##
## india_claims_diff.l1 -0.467 0.396 0.192 0.018 -0.429
## (0.605) (0.297) (0.282) (0.036) (0.334)
##
## india_cpi_diff.l1 0.101 -0.253 0.665** -0.053 0.371
## (0.584) (0.287) (0.273) (0.034) (0.323)
##
## india_milt_diff.l1 4.579 -2.857 -0.139 0.190 2.024
## (4.063) (1.998) (1.898) (0.240) (2.246)
##
## india_st_debt_diff.l1 0.291 -0.274 -0.029 0.020 0.164
## (0.434) (0.213) (0.203) (0.026) (0.240)
##
## india_gdp_diff.l2 -0.221 -0.145 -0.070 -0.017 0.222
## (0.390) (0.192) (0.182) (0.023) (0.216)
##
## india_claims_diff.l2 0.446 -0.312 0.061 -0.025 -0.175
## (0.594) (0.292) (0.278) (0.035) (0.328)
##
## india_cpi_diff.l2 -0.543 0.284 0.218 0.012 -0.202
## (0.590) (0.290) (0.276) (0.035) (0.326)
##
## india_milt_diff.l2 -1.959 3.301 0.119 -0.121 3.148
## (4.428) (2.177) (2.068) (0.261) (2.447)
##
## india_st_debt_diff.l2 -0.410 0.220 0.330 -0.007 0.017
## (0.436) (0.215) (0.204) (0.026) (0.241)
##
## india_gdp_diff.l3 -0.078 -0.107 0.056 -0.016 0.169
## (0.329) (0.162) (0.154) (0.019) (0.182)
##
## india_claims_diff.l3 0.663 -0.277 -0.358 -0.016 -0.058
## (0.570) (0.280) (0.266) (0.034) (0.315)
##
## india_cpi_diff.l3 0.310 -0.0002 0.048 0.041 -0.189
## (0.496) (0.244) (0.232) (0.029) (0.274)
##
## india_milt_diff.l3 -1.630 2.010 0.148 -0.008 1.112
## (4.683) (2.303) (2.188) (0.276) (2.588)
##
## india_st_debt_diff.l3 0.296 0.223 -0.024 0.062** -0.196
## (0.452) (0.222) (0.211) (0.027) (0.250)
##
## const 0.149 0.179 0.570 -0.018 0.450
## (1.087) (0.534) (0.508) (0.064) (0.601)
##
## ----------------------------------------------------------------------
## Observations 36 36 36 36 36
## R2 0.291 0.451 0.851 0.455 0.460
## Adjusted R2 -0.241 0.040 0.740 0.046 0.055
## Residual Std. Error (df = 20) 3.606 1.773 1.684 0.213 1.993
## F Statistic (df = 15; 20) 0.547 1.096 7.636*** 1.112 1.134
## ======================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
Check for Serial Correlation
india_serial <- serial.test(india_model, type = "PT.asymptotic")
india_serial
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object india_model
## Chi-squared = 273.49, df = 325, p-value = 0.9826
Test for Heteroscedasticity
india_arch <- vars::arch.test(india_model, lags.multi = 15, multivariate.only = TRUE)
india_arch
##
## ARCH (multivariate)
##
## data: Residuals of VAR object india_model
## Chi-squared = 315, df = 3375, p-value = 1
Test for Normality of Residuals
india_norm <- normality.test(india_model, multivariate.only = TRUE)
india_norm
## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object india_model
## Chi-squared = 35.948, df = 10, p-value = 8.593e-05
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object india_model
## Chi-squared = 18.903, df = 5, p-value = 0.002004
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object india_model
## Chi-squared = 17.045, df = 5, p-value = 0.004415
Check for Stability of model
india_stability <- stability(india_model, type = "OLS-CUSUM")
plot(india_stability)

Check for Granger Causality
granger_gdp <- causality(india_model, cause = "india_gdp_diff")
granger_gdp
## $Granger
##
## Granger causality H0: india_gdp_diff do not Granger-cause
## india_claims_diff india_cpi_diff india_milt_diff india_st_debt_diff
##
## data: VAR object india_model
## F-Test = 0.53342, df1 = 12, df2 = 100, p-value = 0.8882
##
##
## $Instant
##
## H0: No instantaneous causality between: india_gdp_diff and
## india_claims_diff india_cpi_diff india_milt_diff india_st_debt_diff
##
## data: VAR object india_model
## Chi-squared = 11.461, df = 4, p-value = 0.02184
granger_claims <- causality(india_model, cause = "india_claims_diff")
granger_claims
## $Granger
##
## Granger causality H0: india_claims_diff do not Granger-cause
## india_gdp_diff india_cpi_diff india_milt_diff india_st_debt_diff
##
## data: VAR object india_model
## F-Test = 0.81258, df1 = 12, df2 = 100, p-value = 0.6369
##
##
## $Instant
##
## H0: No instantaneous causality between: india_claims_diff and
## india_gdp_diff india_cpi_diff india_milt_diff india_st_debt_diff
##
## data: VAR object india_model
## Chi-squared = 12.18, df = 4, p-value = 0.01606
granger_cpi <- causality(india_model, cause = "india_cpi_diff")
granger_cpi
## $Granger
##
## Granger causality H0: india_cpi_diff do not Granger-cause
## india_gdp_diff india_claims_diff india_milt_diff india_st_debt_diff
##
## data: VAR object india_model
## F-Test = 0.57237, df1 = 12, df2 = 100, p-value = 0.8596
##
##
## $Instant
##
## H0: No instantaneous causality between: india_cpi_diff and
## india_gdp_diff india_claims_diff india_milt_diff india_st_debt_diff
##
## data: VAR object india_model
## Chi-squared = 7.9751, df = 4, p-value = 0.0925
granger_milt <- causality(india_model, cause = "india_milt_diff")
granger_milt
## $Granger
##
## Granger causality H0: india_milt_diff do not Granger-cause
## india_gdp_diff india_claims_diff india_cpi_diff india_st_debt_diff
##
## data: VAR object india_model
## F-Test = 1.1356, df1 = 12, df2 = 100, p-value = 0.3406
##
##
## $Instant
##
## H0: No instantaneous causality between: india_milt_diff and
## india_gdp_diff india_claims_diff india_cpi_diff india_st_debt_diff
##
## data: VAR object india_model
## Chi-squared = 9.218, df = 4, p-value = 0.05588
granger_debt <- causality(india_model, cause = "india_st_debt_diff")
granger_debt
## $Granger
##
## Granger causality H0: india_st_debt_diff do not Granger-cause
## india_gdp_diff india_claims_diff india_cpi_diff india_milt_diff
##
## data: VAR object india_model
## F-Test = 1.599, df1 = 12, df2 = 100, p-value = 0.1038
##
##
## $Instant
##
## H0: No instantaneous causality between: india_st_debt_diff and
## india_gdp_diff india_claims_diff india_cpi_diff india_milt_diff
##
## data: VAR object india_model
## Chi-squared = 5.2493, df = 4, p-value = 0.2627
Create Impulse Response Functions
# Black line outside of confidence interval means the response is significant!
irf1 <- irf(india_model, impulse = "india_cpi_diff", response = "india_gdp_diff", n.ahead = 4, boot = TRUE, run = 200, ci = 0.95)
plot(irf1)

Review Variance Decomposition
vd1 <- fevd(india_model, n.ahead = 4)
plot(vd1)

Generate Predictions
forecast <- predict(india_model, n.ahead = 4, ci = 0.95)
fanchart(forecast, names = "india_gdp_diff", colors = c("seagreen","lightgreen"), main = "Forecasts", xlab = "Year", ylab = "GDP Growth %")

View Actuals vs Predicted
index_pred <- append(index(train_india[,1]), 2017:2019)
all_actuals <- append(train_india[,1], test_india[1:3,1])
all_preds <- append(train_india[,1], forecast$fcst$india_gdp_diff[,1])
all_actuals_dediff <- diffinv(all_actuals, xi = india_gdp[1])
all_preds_dediff <- diffinv(all_preds, xi = india_gdp[1])
all_preds_dediff <- all_preds_dediff[-length(all_preds_dediff)]
all_actuals_dediff
## [1] 6.006204 3.475733 7.288893 3.820738 5.254299 4.776564 3.965356 9.627783
## [9] 5.947343 5.533455 1.056831 5.482396 4.750776 6.658924 7.574492 7.549522
## [17] 4.049821 6.184416 8.845756 3.840991 4.823966 3.803975 7.860381 7.922937
## [25] 7.923431 8.060733 7.660815 3.086698 7.861889 8.497585 5.241315 5.456389
## [33] 6.386106 7.410228 7.996254 8.256306 6.795383 6.532989 4.041554
all_preds_dediff
## [1] 6.006204 3.475733 7.288893 3.820738 5.254299 4.776564 3.965356
## [8] 9.627783 5.947343 5.533455 1.056831 5.482396 4.750776 6.658924
## [15] 7.574492 7.549522 4.049821 6.184416 8.845756 3.840991 4.823966
## [22] 3.803975 7.860381 7.922937 7.923431 8.060733 7.660815 3.086698
## [29] 7.861889 8.497585 5.241315 5.456389 6.386106 7.410228 7.996254
## [36] 8.256306 13.404243 11.692164 11.906787
length(index_pred)
## [1] 38
length(all_actuals_dediff)
## [1] 39
length(all_preds_dediff[-1])
## [1] 38
plot(index_pred,
all_preds_dediff[-1],
type = "l",
ylab="Actuals and Predicted",
xlab="Year",
col = "red")
lines(index_pred,
all_actuals_dediff[-1],
type = "l",
col = "green")
legend("topleft",
c("Predicted","Actuals"),
fill=c("red","green"))

VAR - Evaluation Metrics
# Root Mean Squared Error
print("Root Mean Squared Error")
## [1] "Root Mean Squared Error"
rmse(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 6.637168
# Mean Absolute Error
print("Mean Absolute Error")
## [1] "Mean Absolute Error"
mae(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 6.544423
# Mean Absolute Percent Error
print("Mean Absolute Percent Error")
## [1] "Mean Absolute Percent Error"
mape(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 1.236118
China
Plot Series (overlayed)

Test for Stationary
##
## Phillips-Perron Unit Root Test
##
## data: china_claims
## Dickey-Fuller Z(alpha) = 0.53956, Truncation lag parameter = 3, p-value
## = 0.99
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: china_cpi
## Dickey-Fuller Z(alpha) = -8.4768, Truncation lag parameter = 3, p-value
## = 0.5956
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: china_milt
## Dickey-Fuller Z(alpha) = -14.073, Truncation lag parameter = 3, p-value
## = 0.2398
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: china_st_debt
## Dickey-Fuller Z(alpha) = -10.18, Truncation lag parameter = 3, p-value
## = 0.4873
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(china_claims)
## Dickey-Fuller Z(alpha) = -29.998, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(china_cpi)
## Dickey-Fuller Z(alpha) = -35.581, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(china_milt)
## Dickey-Fuller Z(alpha) = -37.705, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(china_st_debt)
## Dickey-Fuller Z(alpha) = -22.13, Truncation lag parameter = 3, p-value
## = 0.02029
## alternative hypothesis: stationary
Create vectors of time-series objects for modeling
Train, test split
## [1] 175
## [1] 20
VAR - Vector AutoRegressive model
Find optimal lag length
## AIC(n) HQ(n) SC(n) FPE(n)
## 6 6 6 5
Build VAR model
##
## VAR Estimation Results:
## =========================
## Endogenous variables: china_gdp_diff, china_claims_diff, china_cpi_diff, china_milt_diff, china_st_debt_diff
## Deterministic variables: const
## Sample size: 36
## Log Likelihood: -327.092
## Roots of the characteristic polynomial:
## 0.8647 0.8647 0.8408 0.8408 0.8363 0.8242 0.8242 0.8157 0.8157 0.7663 0.7663 0.7311 0.7311 0.4112 0.4112
## Call:
## VAR(y = china_vec, p = 3, type = "const", exogen = NULL)
##
##
## Estimation results for equation china_gdp_diff:
## ===============================================
## china_gdp_diff = china_gdp_diff.l1 + china_claims_diff.l1 + china_cpi_diff.l1 + china_milt_diff.l1 + china_st_debt_diff.l1 + china_gdp_diff.l2 + china_claims_diff.l2 + china_cpi_diff.l2 + china_milt_diff.l2 + china_st_debt_diff.l2 + china_gdp_diff.l3 + china_claims_diff.l3 + china_cpi_diff.l3 + china_milt_diff.l3 + china_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## china_gdp_diff.l1 0.26033 0.18922 1.376 0.1841
## china_claims_diff.l1 -0.14343 0.15388 -0.932 0.3624
## china_cpi_diff.l1 -0.04945 0.02633 -1.878 0.0750 .
## china_milt_diff.l1 1.57891 1.76781 0.893 0.3824
## china_st_debt_diff.l1 0.02330 0.05385 0.433 0.6699
## china_gdp_diff.l2 -0.32671 0.16944 -1.928 0.0682 .
## china_claims_diff.l2 0.04194 0.16470 0.255 0.8016
## china_cpi_diff.l2 0.04157 0.02714 1.532 0.1413
## china_milt_diff.l2 2.57682 1.76125 1.463 0.1590
## china_st_debt_diff.l2 -0.01246 0.05576 -0.223 0.8255
## china_gdp_diff.l3 0.19315 0.17394 1.110 0.2800
## china_claims_diff.l3 -0.15270 0.15934 -0.958 0.3493
## china_cpi_diff.l3 0.07026 0.02546 2.760 0.0121 *
## china_milt_diff.l3 4.30342 1.81184 2.375 0.0277 *
## china_st_debt_diff.l3 -0.06286 0.04935 -1.274 0.2173
## const -0.10790 0.35406 -0.305 0.7637
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 1.69 on 20 degrees of freedom
## Multiple R-Squared: 0.693, Adjusted R-squared: 0.4627
## F-statistic: 3.009 on 15 and 20 DF, p-value: 0.01146
##
##
## Estimation results for equation china_claims_diff:
## ==================================================
## china_claims_diff = china_gdp_diff.l1 + china_claims_diff.l1 + china_cpi_diff.l1 + china_milt_diff.l1 + china_st_debt_diff.l1 + china_gdp_diff.l2 + china_claims_diff.l2 + china_cpi_diff.l2 + china_milt_diff.l2 + china_st_debt_diff.l2 + china_gdp_diff.l3 + china_claims_diff.l3 + china_cpi_diff.l3 + china_milt_diff.l3 + china_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## china_gdp_diff.l1 -0.26065 0.23512 -1.109 0.2808
## china_claims_diff.l1 0.30308 0.19120 1.585 0.1286
## china_cpi_diff.l1 0.01350 0.03271 0.413 0.6843
## china_milt_diff.l1 -0.93290 2.19656 -0.425 0.6756
## china_st_debt_diff.l1 -0.11313 0.06691 -1.691 0.1064
## china_gdp_diff.l2 0.25840 0.21053 1.227 0.2339
## china_claims_diff.l2 -0.19405 0.20464 -0.948 0.3543
## china_cpi_diff.l2 -0.01479 0.03373 -0.438 0.6657
## china_milt_diff.l2 1.04915 2.18840 0.479 0.6368
## china_st_debt_diff.l2 -0.01870 0.06928 -0.270 0.7899
## china_gdp_diff.l3 -0.41516 0.21612 -1.921 0.0691 .
## china_claims_diff.l3 0.34331 0.19798 1.734 0.0983 .
## china_cpi_diff.l3 0.06378 0.03163 2.016 0.0574 .
## china_milt_diff.l3 0.54377 2.25125 0.242 0.8116
## china_st_debt_diff.l3 -0.02369 0.06132 -0.386 0.7033
## const 0.62795 0.43993 1.427 0.1689
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 2.1 on 20 degrees of freedom
## Multiple R-Squared: 0.4608, Adjusted R-squared: 0.05632
## F-statistic: 1.139 on 15 and 20 DF, p-value: 0.3859
##
##
## Estimation results for equation china_cpi_diff:
## ===============================================
## china_cpi_diff = china_gdp_diff.l1 + china_claims_diff.l1 + china_cpi_diff.l1 + china_milt_diff.l1 + china_st_debt_diff.l1 + china_gdp_diff.l2 + china_claims_diff.l2 + china_cpi_diff.l2 + china_milt_diff.l2 + china_st_debt_diff.l2 + china_gdp_diff.l3 + china_claims_diff.l3 + china_cpi_diff.l3 + china_milt_diff.l3 + china_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## china_gdp_diff.l1 1.710565 1.575575 1.086 0.291
## china_claims_diff.l1 1.142323 1.281256 0.892 0.383
## china_cpi_diff.l1 0.002128 0.219223 0.010 0.992
## china_milt_diff.l1 -6.619452 14.719666 -0.450 0.658
## china_st_debt_diff.l1 0.363080 0.448412 0.810 0.428
## china_gdp_diff.l2 -2.340690 1.410812 -1.659 0.113
## china_claims_diff.l2 -0.562229 1.371365 -0.410 0.686
## china_cpi_diff.l2 0.172800 0.226004 0.765 0.453
## china_milt_diff.l2 -16.377102 14.665021 -1.117 0.277
## china_st_debt_diff.l2 0.073032 0.464256 0.157 0.877
## china_gdp_diff.l3 1.475848 1.448282 1.019 0.320
## china_claims_diff.l3 -0.137292 1.326723 -0.103 0.919
## china_cpi_diff.l3 -0.153392 0.211965 -0.724 0.478
## china_milt_diff.l3 -4.379035 15.086215 -0.290 0.775
## china_st_debt_diff.l3 -0.616888 0.410899 -1.501 0.149
## const 1.246959 2.948063 0.423 0.677
##
##
## Residual standard error: 14.07 on 20 degrees of freedom
## Multiple R-Squared: 0.3448, Adjusted R-squared: -0.1466
## F-statistic: 0.7017 on 15 and 20 DF, p-value: 0.7556
##
##
## Estimation results for equation china_milt_diff:
## ================================================
## china_milt_diff = china_gdp_diff.l1 + china_claims_diff.l1 + china_cpi_diff.l1 + china_milt_diff.l1 + china_st_debt_diff.l1 + china_gdp_diff.l2 + china_claims_diff.l2 + china_cpi_diff.l2 + china_milt_diff.l2 + china_st_debt_diff.l2 + china_gdp_diff.l3 + china_claims_diff.l3 + china_cpi_diff.l3 + china_milt_diff.l3 + china_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## china_gdp_diff.l1 -0.0397196 0.0120468 -3.297 0.0036 **
## china_claims_diff.l1 0.0085279 0.0097965 0.871 0.3944
## china_cpi_diff.l1 0.0010809 0.0016762 0.645 0.5264
## china_milt_diff.l1 -0.2152673 0.1125465 -1.913 0.0702 .
## china_st_debt_diff.l1 -0.0024059 0.0034286 -0.702 0.4909
## china_gdp_diff.l2 -0.0221559 0.0107871 -2.054 0.0533 .
## china_claims_diff.l2 0.0024403 0.0104855 0.233 0.8183
## china_cpi_diff.l2 -0.0004071 0.0017280 -0.236 0.8162
## china_milt_diff.l2 -0.1595581 0.1121287 -1.423 0.1702
## china_st_debt_diff.l2 0.0032700 0.0035497 0.921 0.3679
## china_gdp_diff.l3 -0.0307122 0.0110736 -2.773 0.0117 *
## china_claims_diff.l3 -0.0013873 0.0101441 -0.137 0.8926
## china_cpi_diff.l3 -0.0104750 0.0016207 -6.463 2.66e-06 ***
## china_milt_diff.l3 0.1920668 0.1153492 1.665 0.1115
## china_st_debt_diff.l3 0.0041480 0.0031417 1.320 0.2017
## const -0.0133222 0.0225409 -0.591 0.5611
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.1076 on 20 degrees of freedom
## Multiple R-Squared: 0.8062, Adjusted R-squared: 0.6609
## F-statistic: 5.548 on 15 and 20 DF, p-value: 0.0002689
##
##
## Estimation results for equation china_st_debt_diff:
## ===================================================
## china_st_debt_diff = china_gdp_diff.l1 + china_claims_diff.l1 + china_cpi_diff.l1 + china_milt_diff.l1 + china_st_debt_diff.l1 + china_gdp_diff.l2 + china_claims_diff.l2 + china_cpi_diff.l2 + china_milt_diff.l2 + china_st_debt_diff.l2 + china_gdp_diff.l3 + china_claims_diff.l3 + china_cpi_diff.l3 + china_milt_diff.l3 + china_st_debt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## china_gdp_diff.l1 0.45192 0.80626 0.561 0.581
## china_claims_diff.l1 -0.50788 0.65565 -0.775 0.448
## china_cpi_diff.l1 -0.01371 0.11218 -0.122 0.904
## china_milt_diff.l1 2.80283 7.53243 0.372 0.714
## china_st_debt_diff.l1 0.26566 0.22946 1.158 0.261
## china_gdp_diff.l2 -0.78267 0.72195 -1.084 0.291
## china_claims_diff.l2 0.25397 0.70176 0.362 0.721
## china_cpi_diff.l2 0.08481 0.11565 0.733 0.472
## china_milt_diff.l2 0.36323 7.50446 0.048 0.962
## china_st_debt_diff.l2 -0.01525 0.23757 -0.064 0.949
## china_gdp_diff.l3 0.81228 0.74112 1.096 0.286
## china_claims_diff.l3 0.27567 0.67892 0.406 0.689
## china_cpi_diff.l3 0.02782 0.10847 0.256 0.800
## china_milt_diff.l3 1.05503 7.72000 0.137 0.893
## china_st_debt_diff.l3 -0.29854 0.21027 -1.420 0.171
## const 0.50561 1.50860 0.335 0.741
##
##
## Residual standard error: 7.2 on 20 degrees of freedom
## Multiple R-Squared: 0.2976, Adjusted R-squared: -0.2292
## F-statistic: 0.5649 on 15 and 20 DF, p-value: 0.8686
##
##
##
## Covariance matrix of residuals:
## china_gdp_diff china_claims_diff china_cpi_diff
## china_gdp_diff 2.85535 -0.39414 7.5502
## china_claims_diff -0.39414 4.40829 -2.4949
## china_cpi_diff 7.55020 -2.49492 197.9622
## china_milt_diff -0.04195 0.01581 -0.2308
## china_st_debt_diff 4.48282 -2.48236 19.5316
## china_milt_diff china_st_debt_diff
## china_gdp_diff -0.04195 4.48282
## china_claims_diff 0.01581 -2.48236
## china_cpi_diff -0.23076 19.53164
## china_milt_diff 0.01157 0.04923
## china_st_debt_diff 0.04923 51.83894
##
## Correlation matrix of residuals:
## china_gdp_diff china_claims_diff china_cpi_diff
## china_gdp_diff 1.0000 -0.11109 0.31757
## china_claims_diff -0.1111 1.00000 -0.08446
## china_cpi_diff 0.3176 -0.08446 1.00000
## china_milt_diff -0.2307 0.06999 -0.15246
## china_st_debt_diff 0.3685 -0.16421 0.19281
## china_milt_diff china_st_debt_diff
## china_gdp_diff -0.23074 0.36846
## china_claims_diff 0.06999 -0.16421
## china_cpi_diff -0.15246 0.19281
## china_milt_diff 1.00000 0.06356
## china_st_debt_diff 0.06356 1.00000
Stargazer Output
##
## ========================================================================
## Dependent variable:
## ------------------------------------------
## y
## (1) (2) (3) (4) (5)
## ------------------------------------------------------------------------
## china_gdp_diff.l1 0.260 -0.261 1.711 -0.040*** 0.452
## (0.189) (0.235) (1.576) (0.012) (0.806)
##
## china_claims_diff.l1 -0.143 0.303 1.142 0.009 -0.508
## (0.154) (0.191) (1.281) (0.010) (0.656)
##
## china_cpi_diff.l1 -0.049* 0.013 0.002 0.001 -0.014
## (0.026) (0.033) (0.219) (0.002) (0.112)
##
## china_milt_diff.l1 1.579 -0.933 -6.619 -0.215* 2.803
## (1.768) (2.197) (14.720) (0.113) (7.532)
##
## china_st_debt_diff.l1 0.023 -0.113 0.363 -0.002 0.266
## (0.054) (0.067) (0.448) (0.003) (0.229)
##
## china_gdp_diff.l2 -0.327* 0.258 -2.341 -0.022* -0.783
## (0.169) (0.211) (1.411) (0.011) (0.722)
##
## china_claims_diff.l2 0.042 -0.194 -0.562 0.002 0.254
## (0.165) (0.205) (1.371) (0.010) (0.702)
##
## china_cpi_diff.l2 0.042 -0.015 0.173 -0.0004 0.085
## (0.027) (0.034) (0.226) (0.002) (0.116)
##
## china_milt_diff.l2 2.577 1.049 -16.377 -0.160 0.363
## (1.761) (2.188) (14.665) (0.112) (7.504)
##
## china_st_debt_diff.l2 -0.012 -0.019 0.073 0.003 -0.015
## (0.056) (0.069) (0.464) (0.004) (0.238)
##
## china_gdp_diff.l3 0.193 -0.415* 1.476 -0.031** 0.812
## (0.174) (0.216) (1.448) (0.011) (0.741)
##
## china_claims_diff.l3 -0.153 0.343* -0.137 -0.001 0.276
## (0.159) (0.198) (1.327) (0.010) (0.679)
##
## china_cpi_diff.l3 0.070** 0.064* -0.153 -0.010*** 0.028
## (0.025) (0.032) (0.212) (0.002) (0.108)
##
## china_milt_diff.l3 4.303** 0.544 -4.379 0.192 1.055
## (1.812) (2.251) (15.086) (0.115) (7.720)
##
## china_st_debt_diff.l3 -0.063 -0.024 -0.617 0.004 -0.299
## (0.049) (0.061) (0.411) (0.003) (0.210)
##
## const -0.108 0.628 1.247 -0.013 0.506
## (0.354) (0.440) (2.948) (0.023) (1.509)
##
## ------------------------------------------------------------------------
## Observations 36 36 36 36 36
## R2 0.693 0.461 0.345 0.806 0.298
## Adjusted R2 0.463 0.056 -0.147 0.661 -0.229
## Residual Std. Error (df = 20) 1.690 2.100 14.070 0.108 7.200
## F Statistic (df = 15; 20) 3.009** 1.139 0.702 5.548*** 0.565
## ========================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
Check for Serial Correlation
china_serial <- serial.test(china_model, type = "PT.asymptotic")
china_serial
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object china_model
## Chi-squared = 279.89, df = 325, p-value = 0.9665
Test for Heteroscedasticity
china_arch <- vars::arch.test(china_model, lags.multi = 15, multivariate.only = TRUE)
china_arch
##
## ARCH (multivariate)
##
## data: Residuals of VAR object china_model
## Chi-squared = 315, df = 3375, p-value = 1
Test for Normality of Residuals
china_norm <- normality.test(china_model, multivariate.only = TRUE)
china_norm
## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object china_model
## Chi-squared = 175.45, df = 10, p-value < 2.2e-16
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object china_model
## Chi-squared = 47.274, df = 5, p-value = 4.995e-09
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object china_model
## Chi-squared = 128.18, df = 5, p-value < 2.2e-16
Check for Stability of model
china_stability <- stability(china_model, type = "OLS-CUSUM")
plot(china_stability)

Check for Granger Causality
granger_gdp <- causality(china_model, cause = "china_gdp_diff")
granger_gdp
## $Granger
##
## Granger causality H0: china_gdp_diff do not Granger-cause
## china_claims_diff china_cpi_diff china_milt_diff china_st_debt_diff
##
## data: VAR object china_model
## F-Test = 2.6551, df1 = 12, df2 = 100, p-value = 0.003949
##
##
## $Instant
##
## H0: No instantaneous causality between: china_gdp_diff and
## china_claims_diff china_cpi_diff china_milt_diff china_st_debt_diff
##
## data: VAR object china_model
## Chi-squared = 7.1029, df = 4, p-value = 0.1306
granger_claims <- causality(china_model, cause = "china_claims_diff")
granger_claims
## $Granger
##
## Granger causality H0: china_claims_diff do not Granger-cause
## china_gdp_diff china_cpi_diff china_milt_diff china_st_debt_diff
##
## data: VAR object china_model
## F-Test = 0.48816, df1 = 12, df2 = 100, p-value = 0.9174
##
##
## $Instant
##
## H0: No instantaneous causality between: china_claims_diff and
## china_gdp_diff china_cpi_diff china_milt_diff china_st_debt_diff
##
## data: VAR object china_model
## Chi-squared = 1.2421, df = 4, p-value = 0.8711
granger_cpi <- causality(china_model, cause = "china_cpi_diff")
granger_cpi
## $Granger
##
## Granger causality H0: china_cpi_diff do not Granger-cause
## china_gdp_diff china_claims_diff china_milt_diff china_st_debt_diff
##
## data: VAR object china_model
## F-Test = 4.8394, df1 = 12, df2 = 100, p-value = 3.169e-06
##
##
## $Instant
##
## H0: No instantaneous causality between: china_cpi_diff and
## china_gdp_diff china_claims_diff china_milt_diff china_st_debt_diff
##
## data: VAR object china_model
## Chi-squared = 3.7932, df = 4, p-value = 0.4347
granger_milt <- causality(china_model, cause = "china_milt_diff")
granger_milt
## $Granger
##
## Granger causality H0: china_milt_diff do not Granger-cause
## china_gdp_diff china_claims_diff china_cpi_diff china_st_debt_diff
##
## data: VAR object china_model
## F-Test = 1.1636, df1 = 12, df2 = 100, p-value = 0.3196
##
##
## $Instant
##
## H0: No instantaneous causality between: china_milt_diff and
## china_gdp_diff china_claims_diff china_cpi_diff china_st_debt_diff
##
## data: VAR object china_model
## Chi-squared = 3.0399, df = 4, p-value = 0.5512
granger_debt <- causality(china_model, cause = "china_st_debt_diff")
granger_debt
## $Granger
##
## Granger causality H0: china_st_debt_diff do not Granger-cause
## china_gdp_diff china_claims_diff china_cpi_diff china_milt_diff
##
## data: VAR object china_model
## F-Test = 0.87293, df1 = 12, df2 = 100, p-value = 0.5765
##
##
## $Instant
##
## H0: No instantaneous causality between: china_st_debt_diff and
## china_gdp_diff china_claims_diff china_cpi_diff china_milt_diff
##
## data: VAR object china_model
## Chi-squared = 5.5916, df = 4, p-value = 0.2318
Create Impulse Response Functions
# Black line outside of confidence interval means the response is significant!
irf1 <- irf(china_model, impulse = "china_cpi_diff", response = "china_gdp_diff", n.ahead = 4, boot = TRUE, run = 200, ci = 0.95)
plot(irf1)

Review Variance Decomposition
vd1 <- fevd(china_model, n.ahead = 4)
plot(vd1)

Generate Predictions (differenced data)
forecast <- predict(china_model, n.ahead = 4, ci = 0.95)
fanchart(forecast, names = "china_gdp_diff", colors = c("seagreen","lightgreen"), main = "Forecasts", xlab = "Year", ylab = "GDP Growth %")

View Actuals vs Predicted
index_pred <- append(index(train_china[,1]), 2017:2019)
all_actuals <- append(train_china[,1], test_china[1:3,1])
all_preds <- append(train_china[,1], forecast$fcst$china_gdp_diff[,1])
all_actuals_dediff <- diffinv(all_actuals, xi = china_gdp[1])
all_preds_dediff <- diffinv(all_preds, xi = china_gdp[1])
all_preds_dediff <- all_preds_dediff[-length(all_preds_dediff)]
all_actuals_dediff
## [1] 5.112761 9.017114 10.770203 15.191540 13.430678 8.949962 11.657428
## [8] 11.222595 4.206334 3.920251 9.262786 14.224530 13.883729 13.036807
## [15] 10.953954 9.922557 9.236780 7.845952 7.661652 8.490093 8.335733
## [22] 9.133631 10.038030 10.113621 11.394592 12.720956 14.230861 9.650679
## [29] 9.398726 10.635871 9.550832 7.863736 7.766150 7.425764 7.041329
## [36] 6.848762 6.947201 6.749774 5.949714
all_preds_dediff
## [1] 5.112761 9.017114 10.770203 15.191540 13.430678 8.949962 11.657428
## [8] 11.222595 4.206334 3.920251 9.262786 14.224530 13.883729 13.036807
## [15] 10.953954 9.922557 9.236780 7.845952 7.661652 8.490093 8.335733
## [22] 9.133631 10.038030 10.113621 11.394592 12.720956 14.230861 9.650679
## [29] 9.398726 10.635871 9.550832 7.863736 7.766150 7.425764 7.041329
## [36] 6.848762 5.101999 5.976239 5.945940
plot(index_pred,
all_preds_dediff[-1],
type = "l",
ylab="Actuals and Predicted",
xlab="Year",
col = "red")
lines(index_pred,
all_actuals_dediff[-1],
type = "l",
col = "green")
legend("topleft",
c("Predicted","Actuals"),
fill=c("red","green"))

VAR - Evaluation Metrics
# Root Mean Squared Error
print("Root Mean Squared Error")
## [1] "Root Mean Squared Error"
rmse(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 1.155154
# Mean Absolute Error
print("Mean Absolute Error")
## [1] "Mean Absolute Error"
mae(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 0.8741703
# Mean Absolute Percent Error
print("Mean Absolute Percent Error")
## [1] "Mean Absolute Percent Error"
mape(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 0.1269465
South Africa
Plot Series (overlayed)

Test for Stationary
##
## Phillips-Perron Unit Root Test
##
## data: so_af_claims
## Dickey-Fuller Z(alpha) = -10.917, Truncation lag parameter = 3, p-value
## = 0.4404
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: so_af_cpi
## Dickey-Fuller Z(alpha) = -0.15058, Truncation lag parameter = 3,
## p-value = 0.99
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: so_af_milt
## Dickey-Fuller Z(alpha) = -4.9933, Truncation lag parameter = 3, p-value
## = 0.817
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: so_af_st_debt
## Dickey-Fuller Z(alpha) = -6.7321, Truncation lag parameter = 3, p-value
## = 0.7065
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(so_af_claims)
## Dickey-Fuller Z(alpha) = -38.575, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(so_af_cpi)
## Dickey-Fuller Z(alpha) = -19.589, Truncation lag parameter = 3, p-value
## = 0.04242
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(so_af_milt)
## Dickey-Fuller Z(alpha) = -15.715, Truncation lag parameter = 3, p-value
## = 0.1325
## alternative hypothesis: stationary
##
## Phillips-Perron Unit Root Test
##
## data: diff(so_af_st_debt)
## Dickey-Fuller Z(alpha) = -32.044, Truncation lag parameter = 3, p-value
## = 0.01
## alternative hypothesis: stationary
Create vectors of time-series objects for modeling
Train, test split
## [1] 140
## [1] 16
VAR - Vector AutoRegressive model
Find optimal lag length
## AIC(n) HQ(n) SC(n) FPE(n)
## 7 7 7 8
Build VAR model
##
## VAR Estimation Results:
## =========================
## Endogenous variables: so_af_gdp_diff, so_af_claims_diff, so_af_cpi_diff, so_af_milt_diff
## Deterministic variables: const
## Sample size: 36
## Log Likelihood: -214.687
## Roots of the characteristic polynomial:
## 0.9504 0.8205 0.8205 0.7305 0.7159 0.7159 0.701 0.701 0.6904 0.6432 0.6432 0.4619
## Call:
## VAR(y = so_af_vec, p = 3, type = "const", exogen = NULL)
##
##
## Estimation results for equation so_af_gdp_diff:
## ===============================================
## so_af_gdp_diff = so_af_gdp_diff.l1 + so_af_claims_diff.l1 + so_af_cpi_diff.l1 + so_af_milt_diff.l1 + so_af_gdp_diff.l2 + so_af_claims_diff.l2 + so_af_cpi_diff.l2 + so_af_milt_diff.l2 + so_af_gdp_diff.l3 + so_af_claims_diff.l3 + so_af_cpi_diff.l3 + so_af_milt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## so_af_gdp_diff.l1 -0.59453 0.24082 -2.469 0.0214 *
## so_af_claims_diff.l1 0.10945 0.09692 1.129 0.2704
## so_af_cpi_diff.l1 -0.53963 0.29903 -1.805 0.0842 .
## so_af_milt_diff.l1 1.13822 2.56603 0.444 0.6615
## so_af_gdp_diff.l2 -0.42825 0.24315 -1.761 0.0915 .
## so_af_claims_diff.l2 0.05772 0.09880 0.584 0.5648
## so_af_cpi_diff.l2 0.12319 0.39664 0.311 0.7589
## so_af_milt_diff.l2 1.41006 3.13872 0.449 0.6575
## so_af_gdp_diff.l3 -0.11519 0.18810 -0.612 0.5463
## so_af_claims_diff.l3 0.10526 0.10177 1.034 0.3117
## so_af_cpi_diff.l3 0.04693 0.29566 0.159 0.8753
## so_af_milt_diff.l3 -4.16362 2.48908 -1.673 0.1079
## const 1.08150 1.05474 1.025 0.3159
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 2.284 on 23 degrees of freedom
## Multiple R-Squared: 0.3983, Adjusted R-squared: 0.08444
## F-statistic: 1.269 on 12 and 23 DF, p-value: 0.2999
##
##
## Estimation results for equation so_af_claims_diff:
## ==================================================
## so_af_claims_diff = so_af_gdp_diff.l1 + so_af_claims_diff.l1 + so_af_cpi_diff.l1 + so_af_milt_diff.l1 + so_af_gdp_diff.l2 + so_af_claims_diff.l2 + so_af_cpi_diff.l2 + so_af_milt_diff.l2 + so_af_gdp_diff.l3 + so_af_claims_diff.l3 + so_af_cpi_diff.l3 + so_af_milt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## so_af_gdp_diff.l1 0.27149 0.67230 0.404 0.690
## so_af_claims_diff.l1 -0.13041 0.27057 -0.482 0.634
## so_af_cpi_diff.l1 0.60454 0.83479 0.724 0.476
## so_af_milt_diff.l1 -3.85354 7.16354 -0.538 0.596
## so_af_gdp_diff.l2 0.23883 0.67880 0.352 0.728
## so_af_claims_diff.l2 -0.05408 0.27582 -0.196 0.846
## so_af_cpi_diff.l2 0.14751 1.10730 0.133 0.895
## so_af_milt_diff.l2 2.88770 8.76229 0.330 0.745
## so_af_gdp_diff.l3 -0.51500 0.52511 -0.981 0.337
## so_af_claims_diff.l3 -0.08310 0.28410 -0.293 0.773
## so_af_cpi_diff.l3 -0.88746 0.82538 -1.075 0.293
## so_af_milt_diff.l3 -7.93201 6.94873 -1.142 0.265
## const -0.67808 2.94450 -0.230 0.820
##
##
## Residual standard error: 6.377 on 23 degrees of freedom
## Multiple R-Squared: 0.2216, Adjusted R-squared: -0.1845
## F-statistic: 0.5457 on 12 and 23 DF, p-value: 0.8618
##
##
## Estimation results for equation so_af_cpi_diff:
## ===============================================
## so_af_cpi_diff = so_af_gdp_diff.l1 + so_af_claims_diff.l1 + so_af_cpi_diff.l1 + so_af_milt_diff.l1 + so_af_gdp_diff.l2 + so_af_claims_diff.l2 + so_af_cpi_diff.l2 + so_af_milt_diff.l2 + so_af_gdp_diff.l3 + so_af_claims_diff.l3 + so_af_cpi_diff.l3 + so_af_milt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## so_af_gdp_diff.l1 0.24417 0.13406 1.821 0.081589 .
## so_af_claims_diff.l1 -0.04052 0.05395 -0.751 0.460286
## so_af_cpi_diff.l1 0.66605 0.16646 4.001 0.000561 ***
## so_af_milt_diff.l1 0.10659 1.42844 0.075 0.941164
## so_af_gdp_diff.l2 0.29083 0.13536 2.149 0.042422 *
## so_af_claims_diff.l2 -0.10768 0.05500 -1.958 0.062491 .
## so_af_cpi_diff.l2 0.02628 0.22080 0.119 0.906295
## so_af_milt_diff.l2 -1.28602 1.74724 -0.736 0.469153
## so_af_gdp_diff.l3 0.20416 0.10471 1.950 0.063493 .
## so_af_claims_diff.l3 -0.19325 0.05665 -3.411 0.002392 **
## so_af_cpi_diff.l3 0.21422 0.16458 1.302 0.205939
## so_af_milt_diff.l3 1.44243 1.38560 1.041 0.308689
## const 0.66295 0.58715 1.129 0.270493
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 1.272 on 23 degrees of freedom
## Multiple R-Squared: 0.7418, Adjusted R-squared: 0.6071
## F-statistic: 5.507 on 12 and 23 DF, p-value: 0.0002335
##
##
## Estimation results for equation so_af_milt_diff:
## ================================================
## so_af_milt_diff = so_af_gdp_diff.l1 + so_af_claims_diff.l1 + so_af_cpi_diff.l1 + so_af_milt_diff.l1 + so_af_gdp_diff.l2 + so_af_claims_diff.l2 + so_af_cpi_diff.l2 + so_af_milt_diff.l2 + so_af_gdp_diff.l3 + so_af_claims_diff.l3 + so_af_cpi_diff.l3 + so_af_milt_diff.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## so_af_gdp_diff.l1 -0.015664 0.020125 -0.778 0.44431
## so_af_claims_diff.l1 -0.005759 0.008100 -0.711 0.48419
## so_af_cpi_diff.l1 -0.006682 0.024989 -0.267 0.79154
## so_af_milt_diff.l1 0.762508 0.214438 3.556 0.00168 **
## so_af_gdp_diff.l2 -0.025574 0.020320 -1.259 0.22080
## so_af_claims_diff.l2 0.002694 0.008257 0.326 0.74712
## so_af_cpi_diff.l2 -0.007830 0.033147 -0.236 0.81536
## so_af_milt_diff.l2 -0.052735 0.262296 -0.201 0.84243
## so_af_gdp_diff.l3 -0.010674 0.015719 -0.679 0.50388
## so_af_claims_diff.l3 -0.008733 0.008504 -1.027 0.31514
## so_af_cpi_diff.l3 0.015169 0.024707 0.614 0.54527
## so_af_milt_diff.l3 -0.172210 0.208008 -0.828 0.41623
## const -0.029534 0.088143 -0.335 0.74061
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.1909 on 23 degrees of freedom
## Multiple R-Squared: 0.4958, Adjusted R-squared: 0.2327
## F-statistic: 1.884 on 12 and 23 DF, p-value: 0.09263
##
##
##
## Covariance matrix of residuals:
## so_af_gdp_diff so_af_claims_diff so_af_cpi_diff
## so_af_gdp_diff 5.21851 4.8621 0.22709
## so_af_claims_diff 4.86208 40.6703 1.38769
## so_af_cpi_diff 0.22709 1.3877 1.61713
## so_af_milt_diff 0.04142 0.0603 0.01005
## so_af_milt_diff
## so_af_gdp_diff 0.04142
## so_af_claims_diff 0.06030
## so_af_cpi_diff 0.01005
## so_af_milt_diff 0.03644
##
## Correlation matrix of residuals:
## so_af_gdp_diff so_af_claims_diff so_af_cpi_diff
## so_af_gdp_diff 1.00000 0.33374 0.07817
## so_af_claims_diff 0.33374 1.00000 0.17111
## so_af_cpi_diff 0.07817 0.17111 1.00000
## so_af_milt_diff 0.09497 0.04953 0.04140
## so_af_milt_diff
## so_af_gdp_diff 0.09497
## so_af_claims_diff 0.04953
## so_af_cpi_diff 0.04140
## so_af_milt_diff 1.00000
Stargazer Output
##
## =================================================================
## Dependent variable:
## -----------------------------------
## y
## (1) (2) (3) (4)
## -----------------------------------------------------------------
## so_af_gdp_diff.l1 -0.595** 0.271 0.244* -0.016
## (0.241) (0.672) (0.134) (0.020)
##
## so_af_claims_diff.l1 0.109 -0.130 -0.041 -0.006
## (0.097) (0.271) (0.054) (0.008)
##
## so_af_cpi_diff.l1 -0.540* 0.605 0.666*** -0.007
## (0.299) (0.835) (0.166) (0.025)
##
## so_af_milt_diff.l1 1.138 -3.854 0.107 0.763***
## (2.566) (7.164) (1.428) (0.214)
##
## so_af_gdp_diff.l2 -0.428* 0.239 0.291** -0.026
## (0.243) (0.679) (0.135) (0.020)
##
## so_af_claims_diff.l2 0.058 -0.054 -0.108* 0.003
## (0.099) (0.276) (0.055) (0.008)
##
## so_af_cpi_diff.l2 0.123 0.148 0.026 -0.008
## (0.397) (1.107) (0.221) (0.033)
##
## so_af_milt_diff.l2 1.410 2.888 -1.286 -0.053
## (3.139) (8.762) (1.747) (0.262)
##
## so_af_gdp_diff.l3 -0.115 -0.515 0.204* -0.011
## (0.188) (0.525) (0.105) (0.016)
##
## so_af_claims_diff.l3 0.105 -0.083 -0.193*** -0.009
## (0.102) (0.284) (0.057) (0.009)
##
## so_af_cpi_diff.l3 0.047 -0.887 0.214 0.015
## (0.296) (0.825) (0.165) (0.025)
##
## so_af_milt_diff.l3 -4.164 -7.932 1.442 -0.172
## (2.489) (6.949) (1.386) (0.208)
##
## const 1.082 -0.678 0.663 -0.030
## (1.055) (2.945) (0.587) (0.088)
##
## -----------------------------------------------------------------
## Observations 36 36 36 36
## R2 0.398 0.222 0.742 0.496
## Adjusted R2 0.084 -0.184 0.607 0.233
## Residual Std. Error (df = 23) 2.284 6.377 1.272 0.191
## F Statistic (df = 12; 23) 1.269 0.546 5.507*** 1.884*
## =================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
Check for Serial Correlation
so_af_serial <- serial.test(so_af_model, type = "PT.asymptotic")
so_af_serial
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object so_af_model
## Chi-squared = 163.72, df = 208, p-value = 0.9897
Test for Heteroscedasticity
so_af_arch <- vars::arch.test(so_af_model, lags.multi = 15, multivariate.only = TRUE)
so_af_arch
##
## ARCH (multivariate)
##
## data: Residuals of VAR object so_af_model
## Chi-squared = 210, df = 1500, p-value = 1
Check for Stability of model
so_af_stability <- stability(so_af_model, type = "OLS-CUSUM")
plot(so_af_stability)

Check for Granger Causality
granger_gdp <- causality(so_af_model, cause = "so_af_gdp_diff")
granger_gdp
## $Granger
##
## Granger causality H0: so_af_gdp_diff do not Granger-cause
## so_af_claims_diff so_af_cpi_diff so_af_milt_diff
##
## data: VAR object so_af_model
## F-Test = 1.1622, df1 = 9, df2 = 92, p-value = 0.3285
##
##
## $Instant
##
## H0: No instantaneous causality between: so_af_gdp_diff and
## so_af_claims_diff so_af_cpi_diff so_af_milt_diff
##
## data: VAR object so_af_model
## Chi-squared = 3.7969, df = 3, p-value = 0.2843
granger_claims <- causality(so_af_model, cause = "so_af_claims_diff")
granger_claims
## $Granger
##
## Granger causality H0: so_af_claims_diff do not Granger-cause
## so_af_gdp_diff so_af_cpi_diff so_af_milt_diff
##
## data: VAR object so_af_model
## F-Test = 2.2656, df1 = 9, df2 = 92, p-value = 0.02436
##
##
## $Instant
##
## H0: No instantaneous causality between: so_af_claims_diff and
## so_af_gdp_diff so_af_cpi_diff so_af_milt_diff
##
## data: VAR object so_af_model
## Chi-squared = 4.2179, df = 3, p-value = 0.2389
granger_cpi <- causality(so_af_model, cause = "so_af_cpi_diff")
granger_cpi
## $Granger
##
## Granger causality H0: so_af_cpi_diff do not Granger-cause
## so_af_gdp_diff so_af_claims_diff so_af_milt_diff
##
## data: VAR object so_af_model
## F-Test = 0.92002, df1 = 9, df2 = 92, p-value = 0.5116
##
##
## $Instant
##
## H0: No instantaneous causality between: so_af_cpi_diff and
## so_af_gdp_diff so_af_claims_diff so_af_milt_diff
##
## data: VAR object so_af_model
## Chi-squared = 1.0741, df = 3, p-value = 0.7833
granger_milt <- causality(so_af_model, cause = "so_af_milt_diff")
granger_milt
## $Granger
##
## Granger causality H0: so_af_milt_diff do not Granger-cause
## so_af_gdp_diff so_af_claims_diff so_af_cpi_diff
##
## data: VAR object so_af_model
## F-Test = 0.72209, df1 = 9, df2 = 92, p-value = 0.6875
##
##
## $Instant
##
## H0: No instantaneous causality between: so_af_milt_diff and
## so_af_gdp_diff so_af_claims_diff so_af_cpi_diff
##
## data: VAR object so_af_model
## Chi-squared = 0.36955, df = 3, p-value = 0.9465
Create Impulse Response Functions
# Black line outside of confidence interval means the response is significant!
irf1 <- irf(so_af_model, impulse = "so_af_cpi_diff", response = "so_af_gdp_diff", n.ahead = 4, boot = TRUE, run = 200, ci = 0.95)
plot(irf1)

Review Variance Decomposition
vd1 <- fevd(so_af_model, n.ahead = 4)
plot(vd1)

Generate Predictions
forecast <- predict(so_af_model, n.ahead = 4, ci = 0.95)
fanchart(forecast, names = "so_af_gdp_diff", colors = c("seagreen","lightgreen"), main = "Forecasts", xlab = "Year", ylab = "GDP Growth %")

View Actuals vs Predicted
index_pred <- append(index(train_so_af[,1]), 2017:2019)
all_actuals <- append(train_so_af[,1], test_so_af[1:3,1])
all_preds <- append(train_so_af[,1], forecast$fcst$so_af_gdp_diff[,1])
all_actuals_dediff <- diffinv(all_actuals, xi = so_af_gdp[1])
all_preds_dediff <- diffinv(all_preds, xi = so_af_gdp[1])
all_preds_dediff <- all_preds_dediff[-length(all_preds_dediff)]
all_actuals_dediff
## [1] 5.36073742 -0.38339077 -1.84654448 5.09911491 -1.21148372 0.01783478
## [7] 2.10073522 4.20013255 2.39478416 -0.31778568 -1.01821987 -2.13705689
## [13] 1.23351991 3.20000105 3.09999542 4.29999896 2.60000212 0.50000091
## [19] 2.39999624 4.20000348 2.69999457 3.70038235 2.94907914 4.55455274
## [25] 5.27705631 5.60379766 5.36047589 3.19104674 -1.53808933 3.03973081
## [31] 3.28416814 2.21335481 2.48520050 1.84699160 1.19373280 0.39908793
## [37] 1.41451263 0.78705557 0.15258332
all_preds_dediff
## [1] 5.36073742 -0.38339077 -1.84654448 5.09911491 -1.21148372 0.01783478
## [7] 2.10073522 4.20013255 2.39478416 -0.31778568 -1.01821987 -2.13705689
## [13] 1.23351991 3.20000105 3.09999542 4.29999896 2.60000212 0.50000091
## [19] 2.39999624 4.20000348 2.69999457 3.70038235 2.94907914 4.55455274
## [25] 5.27705631 5.60379766 5.36047589 3.19104674 -1.53808933 3.03973081
## [31] 3.28416814 2.21335481 2.48520050 1.84699160 1.19373280 0.39908793
## [37] 2.22792968 3.11383790 -0.84742425
length(index_pred)
## [1] 38
length(all_actuals_dediff)
## [1] 39
length(all_preds_dediff[-1])
## [1] 38
plot(index_pred,
all_preds_dediff[-1],
type = "l",
ylab="Actuals and Predicted",
xlab="Year",
col = "red")
lines(index_pred,
all_actuals_dediff[-1],
type = "l",
col = "green")
legend("topleft",
c("Predicted","Actuals"),
fill=c("red","green"))

VAR - Evaluation Metrics
# Root Mean Squared Error
print("Root Mean Squared Error")
## [1] "Root Mean Squared Error"
rmse(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 1.535749
# Mean Absolute Error
print("Mean Absolute Error")
## [1] "Mean Absolute Error"
mae(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 1.380069
# Mean Absolute Percent Error
print("Mean Absolute Percent Error")
## [1] "Mean Absolute Percent Error"
mape(all_actuals_dediff[37:39], all_preds_dediff[37:39])
## [1] 3.361737