library(readxl)
library(vars)
## Warning: package 'vars' was built under R version 4.6.1
## Loading required package: MASS
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.6.1
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 4.6.1
## Loading required package: urca
## Loading required package: lmtest
library(urca)

# 1. IMPORT DATA & PERSIAPAN OBJEK TIME SERIES
dataVAR <- read_excel("C:/Dhinar/ARW/Data No. 2 ARW UAS.xlsx")
dataVAR <- data.frame(dataVAR)

# Standarisasi nama kolom agar tidak merusak fungsi VAR/IRF
colnames(dataVAR) <- make.names(colnames(dataVAR))

# Membentuk time series
tsdataVAR <- ts(dataVAR[, c("Inflasi", "BI..Rate", "USD")], start = c(2011, 1), frequency = 12)

# Definisi data_pakai (MEMPERBAIKI ERROR 'data_pakai not found')
data_pakai <- tsdataVAR

# Plot deret waktu
plot(data_pakai, main = "Plot Waktu: Inflasi, BI-Rate, dan USD/IDR")

# 2. UJI STASIONERITAS (ADF TEST - LEVEL)
inf <- data_pakai[, "Inflasi"]
bir <- data_pakai[, "BI..Rate"]
usd <- data_pakai[, "USD"]

adf_inf_drift <- ur.df(inf, type = "drift", selectlags = "AIC")
summary(adf_inf_drift)
## 
## ############################################### 
## # 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 
## -64.706 -16.229   0.107  17.924  68.822 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 43.18817    4.44546   9.715  < 2e-16 ***
## z.lag.1     -0.91081    0.08517 -10.694  < 2e-16 ***
## z.diff.lag   0.27106    0.07094   3.821 0.000182 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 25.23 on 183 degrees of freedom
## Multiple R-squared:  0.4055, Adjusted R-squared:  0.399 
## F-statistic: 62.41 on 2 and 183 DF,  p-value: < 2.2e-16
## 
## 
## Value of test-statistic is: -10.6936 57.1772 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
adf_bir_drift <- ur.df(bir, type = "drift", selectlags = "AIC")
summary(adf_bir_drift)
## 
## ############################################### 
## # 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 
## -4.9169 -0.1438  0.0264  0.1588  1.9508 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.162680   0.105133   1.547   0.1235    
## z.lag.1     -0.018909   0.009968  -1.897   0.0594 .  
## z.diff.lag   0.465032   0.064863   7.169  1.8e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.663 on 183 degrees of freedom
## Multiple R-squared:  0.2259, Adjusted R-squared:  0.2175 
## F-statistic:  26.7 on 2 and 183 DF,  p-value: 6.671e-11
## 
## 
## Value of test-statistic is: -1.8969 1.8415 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
adf_usd_drift <- ur.df(usd, type = "drift", selectlags = "AIC")
summary(adf_usd_drift)
## 
## ############################################### 
## # 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 
## -1063.70  -124.79   -16.86   155.68  2133.65 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept) 215.50202  143.39140   1.503    0.135
## z.lag.1      -0.01210    0.01044  -1.159    0.248
## z.diff.lag   -0.07677    0.07378  -1.040    0.299
## 
## Residual standard error: 328.5 on 183 degrees of freedom
## Multiple R-squared:  0.01409,    Adjusted R-squared:  0.003315 
## F-statistic: 1.308 on 2 and 183 DF,  p-value: 0.273
## 
## 
## Value of test-statistic is: -1.1587 2.9312 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
# 3. PENENTUAN LAG OPTIMUM VAR
lag_opt <- VARselect(data_pakai, lag.max = 10, type = "const")
print(lag_opt$selection)
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      4      2      2      4
# Memilih lag optimum berdasarkan kriteria AIC
p_opt <- lag_opt$selection["AIC(n)"] 

# 4. PEMODELAN VAR LEVEL
model_var <- VAR(data_pakai, p = p_opt, type = "const")
summary(model_var)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: Inflasi, BI..Rate, USD 
## Deterministic variables: const 
## Sample size: 184 
## Log Likelihood: -2325.887 
## Roots of the characteristic polynomial:
## 0.9894 0.9168 0.755 0.755 0.734 0.5916 0.5916 0.5262 0.5262 0.4796 0.3803 0.3798
## Call:
## VAR(y = data_pakai, p = p_opt, type = "const")
## 
## 
## Estimation results for equation Inflasi: 
## ======================================== 
## Inflasi = Inflasi.l1 + BI..Rate.l1 + USD.l1 + Inflasi.l2 + BI..Rate.l2 + USD.l2 + Inflasi.l3 + BI..Rate.l3 + USD.l3 + Inflasi.l4 + BI..Rate.l4 + USD.l4 + const 
## 
##              Estimate Std. Error t value Pr(>|t|)    
## Inflasi.l1   0.226182   0.077219   2.929  0.00386 ** 
## BI..Rate.l1  2.257679   2.988937   0.755  0.45108    
## USD.l1       0.003148   0.005790   0.544  0.58736    
## Inflasi.l2  -0.259354   0.078793  -3.292  0.00121 ** 
## BI..Rate.l2 -2.306287   4.881780  -0.472  0.63722    
## USD.l2       0.003769   0.007540   0.500  0.61780    
## Inflasi.l3  -0.124059   0.079299  -1.564  0.11956    
## BI..Rate.l3 -2.355827   4.924690  -0.478  0.63300    
## USD.l3      -0.002770   0.007542  -0.367  0.71392    
## Inflasi.l4  -0.137577   0.075984  -1.811  0.07196 .  
## BI..Rate.l4  2.575653   2.942114   0.875  0.38256    
## USD.l4      -0.006980   0.005877  -1.188  0.23664    
## const       97.406336  17.051673   5.712 4.84e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 24.61 on 171 degrees of freedom
## Multiple R-Squared: 0.2339,  Adjusted R-squared: 0.1801 
## F-statistic:  4.35 on 12 and 171 DF,  p-value: 4.958e-06 
## 
## 
## Estimation results for equation BI..Rate: 
## ========================================= 
## BI..Rate = Inflasi.l1 + BI..Rate.l1 + USD.l1 + Inflasi.l2 + BI..Rate.l2 + USD.l2 + Inflasi.l3 + BI..Rate.l3 + USD.l3 + Inflasi.l4 + BI..Rate.l4 + USD.l4 + const 
## 
##               Estimate Std. Error t value Pr(>|t|)    
## Inflasi.l1   2.509e-04  2.013e-03   0.125   0.9010    
## BI..Rate.l1  1.318e+00  7.793e-02  16.914   <2e-16 ***
## USD.l1       2.708e-04  1.510e-04   1.794   0.0746 .  
## Inflasi.l2  -3.704e-03  2.054e-03  -1.803   0.0731 .  
## BI..Rate.l2 -1.630e-01  1.273e-01  -1.281   0.2021    
## USD.l2       3.561e-05  1.966e-04   0.181   0.8565    
## Inflasi.l3  -9.880e-04  2.068e-03  -0.478   0.6334    
## BI..Rate.l3 -1.379e-01  1.284e-01  -1.074   0.2843    
## USD.l3      -3.000e-04  1.967e-04  -1.526   0.1289    
## Inflasi.l4   3.781e-03  1.981e-03   1.909   0.0580 .  
## BI..Rate.l4 -4.439e-02  7.671e-02  -0.579   0.5635    
## USD.l4      -2.812e-05  1.532e-04  -0.184   0.8546    
## const        5.376e-01  4.446e-01   1.209   0.2282    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.6416 on 171 degrees of freedom
## Multiple R-Squared: 0.9839,  Adjusted R-squared: 0.9828 
## F-statistic: 872.2 on 12 and 171 DF,  p-value: < 2.2e-16 
## 
## 
## Estimation results for equation USD: 
## ==================================== 
## USD = Inflasi.l1 + BI..Rate.l1 + USD.l1 + Inflasi.l2 + BI..Rate.l2 + USD.l2 + Inflasi.l3 + BI..Rate.l3 + USD.l3 + Inflasi.l4 + BI..Rate.l4 + USD.l4 + const 
## 
##              Estimate Std. Error t value Pr(>|t|)    
## Inflasi.l1    0.89353    1.00989   0.885   0.3775    
## BI..Rate.l1  86.65131   39.09003   2.217   0.0280 *  
## USD.l1        0.82988    0.07573  10.959   <2e-16 ***
## Inflasi.l2    0.30806    1.03047   0.299   0.7653    
## BI..Rate.l2 -64.58268   63.84508  -1.012   0.3132    
## USD.l2       -0.03392    0.09861  -0.344   0.7313    
## Inflasi.l3    2.41646    1.03709   2.330   0.0210 *  
## BI..Rate.l3 -83.21548   64.40627  -1.292   0.1981    
## USD.l3        0.04715    0.09864   0.478   0.6333    
## Inflasi.l4    0.39089    0.99373   0.393   0.6945    
## BI..Rate.l4  61.16653   38.47767   1.590   0.1138    
## USD.l4        0.15056    0.07686   1.959   0.0518 .  
## const       -28.51611  223.00584  -0.128   0.8984    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 321.8 on 171 degrees of freedom
## Multiple R-Squared: 0.9811,  Adjusted R-squared: 0.9798 
## F-statistic: 741.6 on 12 and 171 DF,  p-value: < 2.2e-16 
## 
## 
## 
## Covariance matrix of residuals:
##          Inflasi BI..Rate       USD
## Inflasi  605.495   3.8329    450.86
## BI..Rate   3.833   0.4116     27.07
## USD      450.862  27.0659 103564.09
## 
## Correlation matrix of residuals:
##          Inflasi BI..Rate     USD
## Inflasi  1.00000   0.2428 0.05694
## BI..Rate 0.24279   1.0000 0.13109
## USD      0.05694   0.1311 1.00000
# 5. IMPULSE RESPONSE FUNCTION (IRF) DAN PLOT
irf_inf_bir <- irf(model_var, impulse = "Inflasi", response = "BI..Rate", n.ahead = 12, boot = TRUE)
irf_inf_usd <- irf(model_var, impulse = "Inflasi", response = "USD", n.ahead = 12, boot = TRUE)
irf_bir_inf <- irf(model_var, impulse = "BI..Rate", response = "Inflasi", n.ahead = 12, boot = TRUE)
irf_usd_inf <- irf(model_var, impulse = "USD", response = "Inflasi", n.ahead = 12, boot = TRUE)

# Reset device grafik jika terjadi error "pin" / margin
if(!is.null(dev.list())) dev.off() 
## null device 
##           1
# Tampilkan grafik IRF dalam satu layout 2x2
par(mfrow = c(2, 2))
plot(irf_inf_bir, main = "Shock Inflasi -> BI-Rate")
plot(irf_inf_usd, main = "Shock Inflasi -> USD")
plot(irf_bir_inf, main = "Shock BI-Rate -> Inflasi")
plot(irf_usd_inf, main = "Shock USD -> Inflasi")