# ============================================================
# VAR/VECM INFLASI, BI RATE, DAN KURS
# ============================================================

# 1. Package
library(readxl)
## Warning: package 'readxl' was built under R version 4.5.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.3
library(tseries)
## Warning: package 'tseries' was built under R version 4.5.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(urca)
## Warning: package 'urca' was built under R version 4.5.3
library(vars)
## Warning: package 'vars' was built under R version 4.5.3
## Loading required package: MASS
## Warning: package 'MASS' was built under R version 4.5.2
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.5.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.5.3
## 
## 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.5.3
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.5.3
# 2. Import data
data <- read_excel("D:/Folder Farras/SEMESTER 4/ANALISIS RUNTUN WAKTU/uas/dataUASno2.xlsx")

# 3. Persiapan data
data$bulan <- as.Date(paste0(data$bulan, "-01"))
data <- data %>% arrange(bulan)

# 4. Time series
ts_data <- ts(
  data[, c("inflasi", "birate", "kursrp")],
  start = c(2011, 1),
  frequency = 12
)

# ============================================================
# 5. PLOT DATA
# ============================================================

par(mfrow = c(3,1))

plot(ts_data[, "inflasi"],
     main = "Inflasi",
     ylab = "%")

plot(ts_data[, "birate"],
     main = "BI Rate",
     ylab = "%")

plot(ts_data[, "kursrp"],
     main = "Kurs Rupiah/USD",
     ylab = "Rp/USD")

par(mfrow = c(1,1))


# ============================================================
# 6. UJI ADF
# ============================================================

# Inflasi
adf_inflasi <- ur.df(
  ts_data[, "inflasi"],
  type = "drift",
  selectlags = "AIC"
)

summary(adf_inflasi)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.12169 -0.22325 -0.06314  0.17417  2.59120 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.29647    0.03991   7.429 4.18e-12 ***
## z.lag.1     -0.96025    0.08345 -11.507  < 2e-16 ***
## z.diff.lag   0.34093    0.06942   4.911 2.02e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4146 on 181 degrees of freedom
## Multiple R-squared:  0.4333, Adjusted R-squared:  0.4271 
## F-statistic:  69.2 on 2 and 181 DF,  p-value: < 2.2e-16
## 
## 
## Value of test-statistic is: -11.5071 66.2076 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
# BI Rate
adf_birate <- ur.df(
  ts_data[, "birate"],
  type = "drift",
  selectlags = "AIC"
)

summary(adf_birate)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.23048 -0.03737  0.00530  0.03848  0.48633 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.10373    0.05703   1.819   0.0706 .  
## z.lag.1     -0.01896    0.00997  -1.902   0.0588 .  
## z.diff.lag   0.48321    0.06617   7.302 8.66e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1658 on 181 degrees of freedom
## Multiple R-squared:  0.2341, Adjusted R-squared:  0.2257 
## F-statistic: 27.67 on 2 and 181 DF,  p-value: 3.28e-11
## 
## 
## Value of test-statistic is: -1.9018 1.8247 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
# Kurs
ln_kurs <- log(ts_data[, "kursrp"])

adf_kurs <- ur.df(
  ln_kurs,
  type = "drift",
  selectlags = "AIC"
)

summary(adf_kurs)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.073824 -0.007673 -0.000246  0.009766  0.098683 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.100956   0.067723   1.491  0.13777   
## z.lag.1     -0.010325   0.007133  -1.448  0.14948   
## z.diff.lag   0.235053   0.071832   3.272  0.00128 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.01797 on 181 degrees of freedom
## Multiple R-squared:  0.06619,    Adjusted R-squared:  0.05587 
## F-statistic: 6.415 on 2 and 181 DF,  p-value: 0.002034
## 
## 
## Value of test-statistic is: -1.4475 3.4278 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
# First difference BI Rate
d_birate <- diff(ts_data[, "birate"])

adf_d_birate <- ur.df(
  d_birate,
  type = "drift",
  selectlags = "AIC"
)

summary(adf_d_birate)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.21116  0.00062  0.00062  0.03884  0.50062 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.0006207  0.0122743  -0.051   0.9597    
## z.lag.1     -0.4345393  0.0786943  -5.522 1.16e-07 ***
## z.diff.lag  -0.1528745  0.0753032  -2.030   0.0438 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1658 on 180 degrees of freedom
## Multiple R-squared:  0.2671, Adjusted R-squared:  0.2589 
## F-statistic: 32.79 on 2 and 180 DF,  p-value: 7.177e-13
## 
## 
## Value of test-statistic is: -5.5219 15.2806 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
# First difference log Kurs
d_lnkurs <- diff(ln_kurs)

adf_d_kurs <- ur.df(
  d_lnkurs,
  type = "drift",
  selectlags = "AIC"
)

summary(adf_d_kurs)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.052530 -0.007926 -0.001255  0.008252  0.091833 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.003788   0.001323   2.863 0.004699 ** 
## z.lag.1     -0.981540   0.088767 -11.058  < 2e-16 ***
## z.diff.lag   0.275815   0.071622   3.851 0.000163 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.01737 on 180 degrees of freedom
## Multiple R-squared:  0.4322, Adjusted R-squared:  0.4259 
## F-statistic: 68.51 on 2 and 180 DF,  p-value: < 2.2e-16
## 
## 
## Value of test-statistic is: -11.0575 61.1539 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
# ============================================================
# 7. UJI KOINTEGRASI JOHANSEN
#    Hanya variabel I(1)
# ============================================================

data_i1 <- cbind(
  birate = ts_data[, "birate"],
  lnkurs = log(ts_data[, "kursrp"])
)

# Pemilihan lag
VARselect(
  data_i1,
  lag.max = 12,
  type = "const"
)
## $selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      3      3      3      3 
## 
## $criteria
##                    1             2             3             4             5
## AIC(n) -1.129870e+01 -1.159321e+01 -1.170587e+01 -1.166379e+01 -1.162666e+01
## HQ(n)  -1.125451e+01 -1.151956e+01 -1.160276e+01 -1.153122e+01 -1.146463e+01
## SC(n)  -1.118976e+01 -1.141165e+01 -1.145169e+01 -1.133699e+01 -1.122724e+01
## FPE(n)  1.238914e-05  9.228865e-06  8.245989e-06  8.601250e-06  8.927991e-06
##                    6             7             8             9            10
## AIC(n) -1.160775e+01 -1.160177e+01 -1.159910e+01 -1.156837e+01 -1.154016e+01
## HQ(n)  -1.141626e+01 -1.138082e+01 -1.134869e+01 -1.128850e+01 -1.123083e+01
## SC(n)  -1.113571e+01 -1.105710e+01 -1.098181e+01 -1.087847e+01 -1.077762e+01
## FPE(n)  9.100394e-06  9.157775e-06  9.185835e-06  9.477165e-06  9.754412e-06
##                   11            12
## AIC(n) -1.154408e+01 -1.152291e+01
## HQ(n)  -1.120529e+01 -1.115466e+01
## SC(n)  -1.070892e+01 -1.061514e+01
## FPE(n)  9.723517e-06  9.940360e-06
# Johansen trace test
johansen <- ca.jo(
  data_i1,
  type = "trace",
  ecdet = "const",
  K = 2,
  spec = "transitory"
)

summary(johansen)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1]  4.813719e-02  1.953844e-02 -1.318390e-16
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  3.63  7.52  9.24 12.97
## r = 0  | 12.71 17.85 19.96 24.60
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##           birate.l1  lnkurs.l1   constant
## birate.l1  1.000000   1.000000      1.000
## lnkurs.l1 -7.003972   3.605564   1466.484
## constant  63.645189 -40.147167 -13744.823
## 
## Weights W:
## (This is the loading matrix)
## 
##              birate.l1   lnkurs.l1     constant
## birate.d -0.0051730906 -0.01650336 7.885043e-18
## lnkurs.d  0.0009525254 -0.00122101 1.772166e-18
# ============================================================
# 8. MEMBENTUK DATA STASIONER UNTUK VAR
# ============================================================

var_data <- cbind(
  inflasi = ts_data[-1, "inflasi"],
  d_birate = diff(ts_data[, "birate"]),
  d_lnkurs = diff(log(ts_data[, "kursrp"]))
)

head(var_data)
##          inflasi d_birate      d_lnkurs
## Feb 2011    0.13     0.25 -0.0130377826
## Mar 2011   -0.32     0.00 -0.0177490006
## Apr 2011   -0.31     0.00 -0.0127598504
## May 2011    0.12     0.00 -0.0105000147
## Jun 2011    0.55     0.00  0.0004514997
## Jul 2011    0.67     0.00 -0.0038278868
# ============================================================
# 9. PEMILIHAN LAG VAR
# ============================================================

lag_selection <- VARselect(
  var_data,
  lag.max = 12,
  type = "const"
)

lag_selection$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      2      2      2      2
# ============================================================
# 10. ESTIMASI VAR
# ============================================================

model_var <- VAR(
  var_data,
  p = 2,
  type = "const"
)

summary(model_var)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: inflasi, d_birate, d_lnkurs 
## Deterministic variables: const 
## Sample size: 183 
## Log Likelihood: 477.638 
## Roots of the characteristic polynomial:
## 0.6806 0.6462 0.6462 0.4845 0.4845 0.3228
## Call:
## VAR(y = var_data, p = 2, type = "const")
## 
## 
## Estimation results for equation inflasi: 
## ======================================== 
## inflasi = inflasi.l1 + d_birate.l1 + d_lnkurs.l1 + inflasi.l2 + d_birate.l2 + d_lnkurs.l2 + const 
## 
##             Estimate Std. Error t value Pr(>|t|)    
## inflasi.l1   0.34355    0.07172   4.790 3.52e-06 ***
## d_birate.l1  0.34646    0.19361   1.790   0.0753 .  
## d_lnkurs.l1  1.25289    1.73257   0.723   0.4706    
## inflasi.l2  -0.34018    0.07238  -4.700 5.23e-06 ***
## d_birate.l2 -0.13502    0.19363  -0.697   0.4865    
## d_lnkurs.l2  1.50424    1.73704   0.866   0.3877    
## const        0.30126    0.04147   7.264 1.17e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.4125 on 176 degrees of freedom
## Multiple R-Squared: 0.2112,  Adjusted R-squared: 0.1844 
## F-statistic: 7.856 on 6 and 176 DF,  p-value: 1.657e-07 
## 
## 
## Estimation results for equation d_birate: 
## ========================================= 
## d_birate = inflasi.l1 + d_birate.l1 + d_lnkurs.l1 + inflasi.l2 + d_birate.l2 + d_lnkurs.l2 + const 
## 
##              Estimate Std. Error t value Pr(>|t|)    
## inflasi.l1   0.007871   0.028172   0.279  0.78028    
## d_birate.l1  0.375371   0.076049   4.936 1.84e-06 ***
## d_lnkurs.l1  1.853583   0.680548   2.724  0.00711 ** 
## inflasi.l2  -0.071144   0.028430  -2.502  0.01325 *  
## d_birate.l2  0.196247   0.076056   2.580  0.01069 *  
## d_lnkurs.l2  0.073145   0.682305   0.107  0.91475    
## const        0.011723   0.016290   0.720  0.47271    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.162 on 176 degrees of freedom
## Multiple R-Squared: 0.2884,  Adjusted R-squared: 0.2642 
## F-statistic: 11.89 on 6 and 176 DF,  p-value: 3.491e-11 
## 
## 
## Estimation results for equation d_lnkurs: 
## ========================================= 
## d_lnkurs = inflasi.l1 + d_birate.l1 + d_lnkurs.l1 + inflasi.l2 + d_birate.l2 + d_lnkurs.l2 + const 
## 
##              Estimate Std. Error t value Pr(>|t|)    
## inflasi.l1   0.004146   0.002963   1.399 0.163499    
## d_birate.l1  0.006895   0.007999   0.862 0.389865    
## d_lnkurs.l1  0.257069   0.071585   3.591 0.000427 ***
## inflasi.l2   0.004520   0.002990   1.511 0.132465    
## d_birate.l2  0.006280   0.008000   0.785 0.433551    
## d_lnkurs.l2 -0.306843   0.071770  -4.275 3.12e-05 ***
## const        0.001496   0.001714   0.873 0.383821    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.01704 on 176 degrees of freedom
## Multiple R-Squared: 0.1765,  Adjusted R-squared: 0.1484 
## F-statistic: 6.287 on 6 and 176 DF,  p-value: 5.242e-06 
## 
## 
## 
## Covariance matrix of residuals:
##          inflasi  d_birate  d_lnkurs
## inflasi  0.17019 0.0129879 0.0005700
## d_birate 0.01299 0.0262582 0.0003963
## d_lnkurs 0.00057 0.0003963 0.0002905
## 
## Correlation matrix of residuals:
##          inflasi d_birate d_lnkurs
## inflasi  1.00000   0.1943  0.08106
## d_birate 0.19429   1.0000  0.14347
## d_lnkurs 0.08106   0.1435  1.00000
# ============================================================
# 11. UJI STABILITAS
# ============================================================

roots(model_var)
## [1] 0.6805548 0.6462041 0.6462041 0.4845371 0.4845371 0.3228033
# ============================================================
# 12. UJI AUTOKORELASI
# ============================================================

serial.test(
  model_var,
  lags.pt = 12,
  type = "PT.asymptotic"
)
## 
##  Portmanteau Test (asymptotic)
## 
## data:  Residuals of VAR object model_var
## Chi-squared = 89.488, df = 90, p-value = 0.4954
# ============================================================
# 13. UJI ARCH
# ============================================================

arch.test(
  model_var,
  lags.multi = 12
)
## 
##  ARCH (multivariate)
## 
## data:  Residuals of VAR object model_var
## Chi-squared = 344.08, df = 432, p-value = 0.9993
# ============================================================
# 14. UJI NORMALITAS
# ============================================================

normality.test(model_var)
## $JB
## 
##  JB-Test (multivariate)
## 
## data:  Residuals of VAR object model_var
## Chi-squared = 2550.3, df = 6, p-value < 2.2e-16
## 
## 
## $Skewness
## 
##  Skewness only (multivariate)
## 
## data:  Residuals of VAR object model_var
## Chi-squared = 205.7, df = 3, p-value < 2.2e-16
## 
## 
## $Kurtosis
## 
##  Kurtosis only (multivariate)
## 
## data:  Residuals of VAR object model_var
## Chi-squared = 2344.6, df = 3, p-value < 2.2e-16
# ============================================================
# 15. IRF
# ============================================================

# Shock BI Rate
irf_bi <- irf(
  model_var,
  impulse = "d_birate",
  response = c("inflasi", "d_birate", "d_lnkurs"),
  n.ahead = 24,
  ortho = TRUE,
  boot = TRUE,
  runs = 1000
)

plot(irf_bi)

# Shock Kurs
irf_kurs <- irf(
  model_var,
  impulse = "d_lnkurs",
  response = c("inflasi", "d_birate", "d_lnkurs"),
  n.ahead = 24,
  ortho = TRUE,
  boot = TRUE,
  runs = 1000
)

plot(irf_kurs)

# Shock Inflasi
irf_inflasi <- irf(
  model_var,
  impulse = "inflasi",
  response = c("d_birate", "d_lnkurs"),
  n.ahead = 24,
  ortho = TRUE,
  boot = TRUE,
  runs = 1000
)

plot(irf_inflasi)

# Semua IRF
irf_all <- irf(
  model_var,
  n.ahead = 24,
  ortho = TRUE,
  boot = TRUE,
  runs = 1000
)

plot(irf_all)