Authors:
Aulia Rahman Al Madani, Sandrina Najwa, Budi Nurani Ruchjana
Indonesia’s economic growth has undergone significant fluctuations in recent years, driven by global shocks such as the 2020 COVID-19 pandemic, the 2013 taper tantrum, and the 2022 global energy crisis. These events underscore the urgent need for more accurate and robust forecasting models to support economic stability and policymaking. This study applies the Principal Component Analysis-Vector Autoregressive Integrated (PCA-VARI) model to forecast economic growth in Indonesia. PCA reduces seven economic variables into two principal components for ten years (2012-2022). The results show that the first component (PC1) shows the highest correlation with the variables of Money Supply, BI Rate, and Foreign Exchange Reserves, which reflect monetary policy and financial stability. Meanwhile, the second component (PC2) is highly correlated to the GDP Index, Exchange Rate, and Inflation variables, which reflect macroeconomic conditions. VARI, as a non-stationary multivariate time series model, is used to model the relationship between these components, with the third-order lag selected as the optimal lag based on the Akaike Information Criterion (AIC), Hannan-Quinn Criterion (HQ), and Final Prediction Error (FPE) values. The results show that the PCA-VARI(3) model is able to provide highly accurate forecasting with a MAPE of 1.21% for PC1 and 1.34% for PC2, and has met all the necessary model assumptions.
This section contains the libraries used in the analysis. These libraries include important functions such as data manipulation, time series analysis, statistical tests, and visualization of results.
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.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(padr)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(MLmetrics)
##
## Attaching package: 'MLmetrics'
## The following object is masked from 'package:base':
##
## Recall
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(vars)
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: strucchange
## Loading required package: sandwich
## Loading required package: urca
library(tseries)
library(psych)
##
## Attaching package: 'psych'
## The following object is masked from 'package:MLmetrics':
##
## AUC
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(stats)
library(MVN)
library(tseries)
library(ppcor)
library(TSA)
## Registered S3 methods overwritten by 'TSA':
## method from
## fitted.Arima forecast
## plot.Arima forecast
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
library(MASS)
data <- read.csv(file.choose(),header=T,sep=",",dec=".",fileEncoding = 'UTF-8-BOM')
data <- data[,-1]
str(data)
## 'data.frame': 132 obs. of 7 variables:
## $ M2 : num 2854978 2849796 2911920 2927259 2992057 ...
## $ BI_RATE : num 6 5.75 5.75 5.75 5.75 5.75 5.75 5.75 5.75 5.75 ...
## $ GDP : num 100 100 100 100 100 ...
## $ EXCHANGE_RATE : num 9061 9014 9148 9166 9305 ...
## $ INFLASI : num 3.65 3.56 3.97 4.5 4.45 4.53 4.56 4.58 4.31 4.61 ...
## $ CADANGAN_DEVISA : num 111990 112220 110493 116413 111528 ...
## $ TRANSAKSI_INDEKS_SAHAM: int 86754 106226 85421 97741 105236 84717 91229 71033 90730 99053 ...
This section presents descriptive statistics of the dataset used. The
summary(data) function is used to display summary
statistics such as minimum, maximum, mean, median, and quartile values
of each variable in the dataset. In addition, the cor(data)
function is used to calculate the correlation matrix between variables
in the dataset, which is useful for understanding the relationship
between variables before conducting further analysis.
summary(data)
## M2 BI_RATE GDP EXCHANGE_RATE
## Min. :2849796 Min. :3.500 Min. : 95.38 Min. : 9014
## 1st Qu.:4020903 1st Qu.:4.250 1st Qu.: 99.75 1st Qu.:12094
## Median :5198863 Median :5.625 Median : 99.98 Median :13542
## Mean :5243964 Mean :5.489 Mean : 99.82 Mean :13071
## 3rd Qu.:6277136 3rd Qu.:6.750 3rd Qu.:100.28 3rd Qu.:14319
## Max. :8528022 Max. :7.750 Max. :101.73 Max. :15805
## INFLASI CADANGAN_DEVISA TRANSAKSI_INDEKS_SAHAM
## Min. :1.320 Min. : 92671 Min. : 71033
## 1st Qu.:2.975 1st Qu.:109589 1st Qu.:117416
## Median :3.590 Median :119076 Median :153322
## Mean :4.129 Mean :119633 Mean :171006
## 3rd Qu.:5.032 3rd Qu.:130469 3rd Qu.:187737
## Max. :8.790 Max. :146870 Max. :410267
cor(data)
## M2 BI_RATE GDP EXCHANGE_RATE
## M2 1.0000000 -0.7278313 -0.3252223 0.8530724
## BI_RATE -0.7278313 1.0000000 0.3777291 -0.4344904
## GDP -0.3252223 0.3777291 1.0000000 -0.2569790
## EXCHANGE_RATE 0.8530724 -0.4344904 -0.2569790 1.0000000
## INFLASI -0.5824984 0.7535297 0.3405650 -0.4667095
## CADANGAN_DEVISA 0.8749369 -0.8586216 -0.3643905 0.6432833
## TRANSAKSI_INDEKS_SAHAM 0.8079704 -0.6766584 -0.2573730 0.5606953
## INFLASI CADANGAN_DEVISA TRANSAKSI_INDEKS_SAHAM
## M2 -0.5824984 0.8749369 0.8079704
## BI_RATE 0.7535297 -0.8586216 -0.6766584
## GDP 0.3405650 -0.3643905 -0.2573730
## EXCHANGE_RATE -0.4667095 0.6432833 0.5606953
## INFLASI 1.0000000 -0.7485919 -0.4555245
## CADANGAN_DEVISA -0.7485919 1.0000000 0.7324551
## TRANSAKSI_INDEKS_SAHAM -0.4555245 0.7324551 1.0000000
The normality test is conducted to determine whether the data has a
normal distribution, which is an important assumption in many
statistical methods. The test was conducted using the Mardia’s
test method through the mvn() function to test for
multivariate normality. In addition, jarque.bera.test() was
used to test the normality of individual variables in the dataset. If
the test results show that the data is not normally distributed, it is
necessary to transform the data.
normalitas = mvn(data=data, mvnTest="mardia")
normalitas
## $multivariateNormality
## Test Statistic p value Result
## 1 Mardia Skewness 445.566110793576 1.18327364333092e-50 NO
## 2 Mardia Kurtosis 0.8197797887952 0.412341654905377 YES
## 3 MVN <NA> <NA> NO
##
## $univariateNormality
## Test Variable Statistic p value Normality
## 1 Anderson-Darling M2 1.1237 0.0059 NO
## 2 Anderson-Darling BI_RATE 3.0812 <0.001 NO
## 3 Anderson-Darling GDP 6.9709 <0.001 NO
## 4 Anderson-Darling EXCHANGE_RATE 6.8190 <0.001 NO
## 5 Anderson-Darling INFLASI 2.8688 <0.001 NO
## 6 Anderson-Darling CADANGAN_DEVISA 1.1942 0.0039 NO
## 7 Anderson-Darling TRANSAKSI_INDEKS_SAHAM 5.3014 <0.001 NO
##
## $Descriptives
## n Mean Std.Dev Median Min
## M2 132 5.243964e+06 1.503460e+06 5198863.190 2849795.50
## BI_RATE 132 5.488636e+00 1.392408e+00 5.625 3.50
## GDP 132 9.981924e+01 1.111452e+00 99.985 95.38
## EXCHANGE_RATE 132 1.307091e+04 1.758641e+03 13542.070 9013.90
## INFLASI 132 4.128561e+00 1.928132e+00 3.590 1.32
## CADANGAN_DEVISA 132 1.196334e+05 1.330493e+04 119075.635 92671.00
## TRANSAKSI_INDEKS_SAHAM 132 1.710061e+05 7.340751e+04 153321.500 71033.00
## Max 25th 75th Skew
## M2 8528022.31 4020903.3175 6277136.2000 0.29665622
## BI_RATE 7.75 4.2500 6.7500 0.14426561
## GDP 101.73 99.7475 100.2800 -1.36436733
## EXCHANGE_RATE 15805.19 12093.9900 14318.5975 -0.99016659
## INFLASI 8.79 2.9750 5.0325 0.68909616
## CADANGAN_DEVISA 146869.99 109589.1375 130469.3400 0.09697604
## TRANSAKSI_INDEKS_SAHAM 410267.00 117415.5000 187737.2500 1.18889439
## Kurtosis
## M2 -0.9217547
## BI_RATE -1.2366593
## GDP 2.8359974
## EXCHANGE_RATE -0.1308265
## INFLASI -0.3051824
## CADANGAN_DEVISA -0.9924214
## TRANSAKSI_INDEKS_SAHAM 0.7448006
jarque.bera.test(data$M2)
##
## Jarque Bera Test
##
## data: data$M2
## X-squared = 6.3364, df = 2, p-value = 0.04208
jarque.bera.test(data$BI_RATE)
##
## Jarque Bera Test
##
## data: data$BI_RATE
## X-squared = 8.5161, df = 2, p-value = 0.01415
jarque.bera.test(data$GDP)
##
## Jarque Bera Test
##
## data: data$GDP
## X-squared = 88.968, df = 2, p-value < 2.2e-16
jarque.bera.test(data$EXCHANGE_RATE)
##
## Jarque Bera Test
##
## data: data$EXCHANGE_RATE
## X-squared = 22.109, df = 2, p-value = 1.582e-05
jarque.bera.test(data$INFLASI)
##
## Jarque Bera Test
##
## data: data$INFLASI
## X-squared = 11.071, df = 2, p-value = 0.003945
jarque.bera.test(data$CADANGAN_DEVISA)
##
## Jarque Bera Test
##
## data: data$CADANGAN_DEVISA
## X-squared = 5.298, df = 2, p-value = 0.07072
jarque.bera.test(data$TRANSAKSI_INDEKS_SAHAM)
##
## Jarque Bera Test
##
## data: data$TRANSAKSI_INDEKS_SAHAM
## X-squared = 35.353, df = 2, p-value = 2.104e-08
Since many statistical methods require normally distributed data, a
natural logarithm transformation (log()) is used on each
variable to reduce skewness and make the distribution closer to normal.
The result of this transformation is stored in a new variable, and the
transformed dataset is then displayed with
summary(datatr).
LnM2 = log(data$M2)
LnBI_RATE =log(data$BI_RATE)
LnGDP= log(data$GDP)
LnEXCHANGE_RATE = log(data$EXCHANGE_RATE)
LnINFLASI = log(data$INFLASI)
LnCADANGAN_DEVISA = log(data$CADANGAN_DEVISA)
LnTRANSAKSI_INDEKS_SAHAM = log(data$TRANSAKSI_INDEKS_SAHAM)
datatr = cbind(LnM2, LnBI_RATE, LnGDP, LnEXCHANGE_RATE, LnINFLASI,LnCADANGAN_DEVISA, LnTRANSAKSI_INDEKS_SAHAM)
datatr= data.frame(datatr)
summary(datatr)
## LnM2 LnBI_RATE LnGDP LnEXCHANGE_RATE
## Min. :14.86 Min. :1.253 Min. :4.558 Min. :9.107
## 1st Qu.:15.21 1st Qu.:1.447 1st Qu.:4.603 1st Qu.:9.400
## Median :15.46 Median :1.727 Median :4.605 Median :9.514
## Mean :15.43 Mean :1.670 Mean :4.603 Mean :9.468
## 3rd Qu.:15.65 3rd Qu.:1.910 3rd Qu.:4.608 3rd Qu.:9.569
## Max. :15.96 Max. :2.048 Max. :4.622 Max. :9.668
## LnINFLASI LnCADANGAN_DEVISA LnTRANSAKSI_INDEKS_SAHAM
## Min. :0.2776 Min. :11.44 Min. :11.17
## 1st Qu.:1.0902 1st Qu.:11.60 1st Qu.:11.67
## Median :1.2781 Median :11.69 Median :11.94
## Mean :1.3055 Mean :11.69 Mean :11.97
## 3rd Qu.:1.6154 3rd Qu.:11.78 3rd Qu.:12.14
## Max. :2.1736 Max. :11.90 Max. :12.92
After the data was transformed, normality testing was conducted again using the same methods as before, namely Mardia’s test and Jarque-Bera test. The purpose of this test is to evaluate whether the transformation has succeeded in making the data closer to a normal distribution.
normalitas = mvn(data=datatr, mvnTest="mardia")
normalitas
## $multivariateNormality
## Test Statistic p value Result
## 1 Mardia Skewness 382.154295115798 1.33504636302696e-39 NO
## 2 Mardia Kurtosis -0.174988977102764 0.861088294944947 YES
## 3 MVN <NA> <NA> NO
##
## $univariateNormality
## Test Variable Statistic p value Normality
## 1 Anderson-Darling LnM2 0.8428 0.0292 NO
## 2 Anderson-Darling LnBI_RATE 2.7965 <0.001 NO
## 3 Anderson-Darling LnGDP 7.1430 <0.001 NO
## 4 Anderson-Darling LnEXCHANGE_RATE 8.8384 <0.001 NO
## 5 Anderson-Darling LnINFLASI 1.3252 0.0019 NO
## 6 Anderson-Darling LnCADANGAN_DEVISA 1.0882 0.0072 NO
## 7 Anderson-Darling LnTRANSAKSI_INDEKS_SAHAM 1.3462 0.0017 NO
##
## $Descriptives
## n Mean Std.Dev Median Min
## LnM2 132 15.430730 0.29340124 15.463943 14.8627578
## LnBI_RATE 132 1.669644 0.26067780 1.726974 1.2527630
## LnGDP 132 4.603299 0.01122317 4.605020 4.5578689
## LnEXCHANGE_RATE 132 9.468065 0.14678497 9.513556 9.1065231
## LnINFLASI 132 1.305479 0.48911743 1.278148 0.2776317
## LnCADANGAN_DEVISA 132 11.686017 0.11172363 11.687494 11.4368109
## LnTRANSAKSI_INDEKS_SAHAM 132 11.969648 0.39107536 11.940222 11.1708998
## Max 25th 75th Skew Kurtosis
## LnM2 15.958868 15.207016 15.652367 -0.13794950 -0.9984697
## LnBI_RATE 2.047693 1.446919 1.909543 -0.16069466 -1.1894145
## LnGDP 4.622322 4.602642 4.607966 -1.41462316 2.9940582
## LnEXCHANGE_RATE 9.668094 9.400462 9.569314 -1.17623869 0.1939062
## LnINFLASI 2.173615 1.090240 1.615422 -0.27658828 -0.5127536
## LnCADANGAN_DEVISA 11.897303 11.604489 11.778893 -0.07625938 -0.9522666
## LnTRANSAKSI_INDEKS_SAHAM 12.924563 11.673437 12.142781 0.45683116 -0.5363734
jarque.bera.test(datatr$LnM2)
##
## Jarque Bera Test
##
## data: datatr$LnM2
## X-squared = 5.5798, df = 2, p-value = 0.06143
jarque.bera.test(datatr$LnBI_RATE)
##
## Jarque Bera Test
##
## data: datatr$LnBI_RATE
## X-squared = 8.0033, df = 2, p-value = 0.01829
jarque.bera.test(datatr$LnGDP)
##
## Jarque Bera Test
##
## data: datatr$LnGDP
## X-squared = 97.417, df = 2, p-value < 2.2e-16
jarque.bera.test(datatr$LnEXCHANGE_RATE)
##
## Jarque Bera Test
##
## data: datatr$LnEXCHANGE_RATE
## X-squared = 31.465, df = 2, p-value = 1.471e-07
jarque.bera.test(datatr$LnINFLASI)
##
## Jarque Bera Test
##
## data: datatr$LnINFLASI
## X-squared = 2.9609, df = 2, p-value = 0.2275
jarque.bera.test(datatr$LnCADANGAN_DEVISA)
##
## Jarque Bera Test
##
## data: datatr$LnCADANGAN_DEVISA
## X-squared = 4.795, df = 2, p-value = 0.09094
jarque.bera.test(datatr$LnTRANSAKSI_INDEKS_SAHAM)
##
## Jarque Bera Test
##
## data: datatr$LnTRANSAKSI_INDEKS_SAHAM
## X-squared = 6.0646, df = 2, p-value = 0.0482
Principal Component Analysis (PCA) was performed to reduce the dimensionality of the data without losing too much information. KMO test and Bartlett’s test are conducted to assess the feasibility of using PCA. The results of PCA will be visualized with bar plot and scree plot to determine the number of significant principal components (PC). The results of PCA are eigenvalues, loading factors, and scores that will be used in further analysis.
KMO(datatr)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = datatr)
## Overall MSA = 0.77
## MSA for each item =
## LnM2 LnBI_RATE LnGDP
## 0.68 0.82 0.83
## LnEXCHANGE_RATE LnINFLASI LnCADANGAN_DEVISA
## 0.61 0.86 0.85
## LnTRANSAKSI_INDEKS_SAHAM
## 0.86
bartlett.test(datatr)
##
## Bartlett test of homogeneity of variances
##
## data: datatr
## Bartlett's K-squared = 1073.7, df = 6, p-value < 2.2e-16
a = cov(datatr)
b = cor(datatr)
eigen(a)
## eigen() decomposition
## $values
## [1] 4.384268e-01 9.511086e-02 2.733768e-02 1.662167e-02 1.893812e-03
## [6] 8.859710e-04 9.015183e-05
##
## $vectors
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.386885085 -0.314302864 0.5912404376 -0.265245546 -0.21227519
## [2,] -0.347120538 -0.006567846 0.4125629047 0.787369816 -0.29064792
## [3,] -0.007480407 -0.008117968 0.0002364499 0.006380789 -0.04907048
## [4,] 0.149190052 -0.152419822 0.5667444944 -0.000225559 0.48427166
## [5,] -0.662440449 -0.691459866 -0.0355412620 -0.283066279 -0.01780679
## [6,] 0.153741433 -0.021081141 0.0181381940 -0.190010413 -0.79482227
## [7,] 0.495025109 -0.631911156 -0.3967808482 0.439798059 0.03842601
## [,6] [,7]
## [1,] 0.5352426310 0.008231420
## [2,] 0.0659052330 0.022025087
## [3,] -0.0005611996 -0.998713745
## [4,] -0.6310741309 -0.023185200
## [5,] -0.0354373510 0.009660083
## [6,] -0.5534996698 0.037173705
## [7,] -0.0574392048 0.002288881
eigen(b)
## eigen() decomposition
## $values
## [1] 4.71612452 0.98112069 0.63911223 0.36730142 0.18107106 0.09385515 0.02141493
##
## $vectors
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.4309224 -0.28695408 -0.1969924 -0.00464444 -0.15110894 0.04653075
## [2,] -0.4037830 -0.19455204 -0.4255218 -0.15465445 0.42026558 -0.62738633
## [3,] -0.2224136 -0.74159735 0.5528230 -0.28813653 0.04668605 0.09837876
## [4,] 0.3503654 -0.40620484 -0.5887405 -0.27487952 -0.04840874 0.21394270
## [5,] -0.3766986 -0.32618424 -0.2097266 0.61785617 -0.56416654 -0.06010744
## [6,] 0.4303839 -0.01475317 0.2550512 -0.11364977 -0.39703473 -0.73333348
## [7,] 0.3897555 -0.24129658 0.1373323 0.65024341 0.56577334 -0.08591002
## [,7]
## [1,] 0.817396226
## [2,] 0.154548583
## [3,] -0.008466677
## [4,] -0.491886845
## [5,] -0.063825529
## [6,] -0.202904259
## [7,] -0.143909540
fit_pca = princomp(datatr, cor=TRUE)
summary(fit_pca)
## Importance of components:
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5
## Standard deviation 2.1716640 0.9905154 0.79944495 0.60605397 0.42552445
## Proportion of Variance 0.6737321 0.1401601 0.09130175 0.05247163 0.02586729
## Cumulative Proportion 0.6737321 0.8138922 0.90519392 0.95766555 0.98353285
## Comp.6 Comp.7
## Standard deviation 0.30635788 0.146338413
## Proportion of Variance 0.01340788 0.003059276
## Cumulative Proportion 0.99694072 1.000000000
plot(fit_pca, type="barplot", main="Bar Plot", col='cadetblue')
plot(fit_pca, type="lines", main="Scree Plot", col='red')
fit_pca$scores
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5
## [1,] -2.77747406 -1.583520684 -1.463808587 0.445603690 0.019233900
## [2,] -2.50067590 -1.509480049 -1.652465541 0.111335593 -0.231073022
## [3,] -2.79966412 -1.498034068 -1.428905665 0.354413655 0.171603309
## [4,] -2.54965493 -1.306761146 -1.535843279 0.030145761 0.310135577
## [5,] -2.56841326 -1.197532016 -1.402834684 -0.088930268 0.052460689
## [6,] -2.91521555 -1.267914607 -1.143244676 0.230216555 0.238226310
## [7,] -2.84327974 -1.208671977 -1.170464878 0.101940953 0.140658958
## [8,] -2.98007089 -1.331410353 -1.102256178 0.547668350 0.597181036
## [9,] -2.61374752 -1.189598693 -1.205482930 0.239563207 0.218095329
## [10,] -2.54518295 -1.065493544 -1.183095459 0.018056209 0.180121802
## [11,] -2.44909069 -1.097080602 -1.208929719 0.136747935 0.164471829
## [12,] -2.31887493 -1.047810919 -1.223669980 0.116256717 0.185221848
## [13,] -2.47528693 -0.992451927 -1.133337483 -0.069385044 0.057786522
## [14,] -2.57356685 -0.799992692 -1.021757043 -0.510337024 -0.079582106
## [15,] -2.40759574 -0.571931101 -1.027820272 -1.037757532 -0.304043768
## [16,] -2.34133608 -0.654525701 -1.054377198 -0.788029182 -0.148277598
## [17,] -2.09388655 -0.479865235 -1.083576343 -1.238733153 -0.626187521
## [18,] -2.57320113 -0.451207232 -0.727698218 -1.169212241 -0.687656385
## [19,] -3.43281809 -0.265582405 -0.096023511 -1.109471255 -0.112619884
## [20,] -3.55543028 -0.144585432 0.266121615 -0.799662978 -0.005789211
## [21,] -2.99308313 0.224033847 0.453141475 -1.008933799 -0.364024339
## [22,] -3.16022502 0.055224351 0.478439152 -0.640687024 -0.016666729
## [23,] -3.28013461 0.056403536 0.675821527 -0.386333355 0.130741720
## [24,] -3.08589316 0.168493752 0.858479261 -0.178139399 0.341379656
## [25,] -2.99216061 0.162287454 0.810462668 -0.224980399 0.281921713
## [26,] -2.75395244 0.151079432 0.633502987 -0.417038391 0.064457070
## [27,] -2.51944319 0.171595381 0.383265596 -0.873269184 -0.410335559
## [28,] -2.44100279 0.128524893 0.355286828 -0.725393262 -0.210467578
## [29,] -2.54975317 0.029749220 0.461472203 -0.370414524 0.161700340
## [30,] -2.41338347 0.024527273 0.578051330 -0.097006798 0.190704668
## [31,] -1.80196183 -0.150131928 0.221923933 0.004273774 -0.502341403
## [32,] -1.80557144 -0.331712700 0.228855718 0.400465865 -0.424172385
## [33,] -1.69411879 -0.118848442 0.331460528 0.044064210 -0.444790728
## [34,] -1.64850950 -0.025361773 0.422871859 -0.024717387 -0.356808689
## [35,] -2.18168587 -0.008715663 0.742190460 0.146375085 0.287734382
## [36,] -2.04459461 0.417092870 0.894097553 -0.581578579 0.321408894
## [37,] -1.72404126 0.353108313 0.787464949 -0.435283812 0.084813442
## [38,] -1.53979415 0.275050608 0.760857523 -0.215455441 0.137023517
## [39,] -1.53606083 0.388373614 0.929649988 -0.361505011 -0.074091883
## [40,] -1.55530875 0.434017589 0.924088432 -0.573559438 -0.112797416
## [41,] -1.82819436 0.334799010 1.099065018 -0.175680000 0.335779212
## [42,] -1.97732410 0.329751313 1.277083976 -0.021969581 0.427785105
## [43,] -2.16529695 0.229462735 1.373391827 0.293330370 0.686037588
## [44,] -1.96718672 0.424886190 1.472928069 0.026732286 0.338599772
## [45,] -2.04982131 0.452566741 1.763028491 0.340133478 0.360447238
## [46,] -1.70381495 0.537509591 1.434926806 -0.342720209 -0.419124439
## [47,] -1.87871322 0.154006784 1.413259050 0.490915905 -0.250589198
## [48,] -1.32691828 -0.052527323 1.168966446 1.046422848 -0.473316930
## [49,] -1.60294998 0.049151349 1.304907048 0.758795804 -0.281567086
## [50,] -1.39761063 0.105480540 1.063226115 0.354159017 -0.311844238
## [51,] -1.19332183 0.086725740 0.798484065 0.140911097 -0.291329651
## [52,] -1.07611682 -0.085704914 0.733445993 0.517278277 -0.438362256
## [53,] -1.22708448 -0.148820633 0.900901359 0.799517538 -0.496154034
## [54,] -0.72514123 0.004447015 0.651140679 0.428583543 -0.485315685
## [55,] -0.71388266 -0.121080495 0.523026938 0.588579846 -0.448754571
## [56,] 0.16455291 -0.133867323 -0.043346391 0.078666249 -0.708187884
## [57,] 0.08051414 -0.205457987 -0.084873841 0.190022054 -0.238189678
## [58,] 0.02163144 -0.233163739 -0.139002040 0.128766544 -0.013923984
## [59,] 0.77870928 0.436649899 -0.232277771 -1.386017375 -1.263196384
## [60,] 0.30701438 -0.121941100 -0.080161803 0.261579039 -0.098882257
## [61,] -0.08493449 -0.206825116 0.035880921 0.522148552 0.462491933
## [62,] 0.11625625 -0.019571561 -0.065046004 0.124000506 0.391925939
## [63,] 0.41909753 0.089372181 -0.191068505 -0.070030877 0.130888035
## [64,] 0.36057247 0.204143138 -0.180646254 -0.264311814 0.319677134
## [65,] 0.35461238 0.239250191 -0.174139355 -0.197649880 0.498903573
## [66,] -0.09500337 0.028377317 -0.001474862 0.453703292 1.049918174
## [67,] 0.31094888 0.085489981 -0.212558384 0.349688805 0.774115507
## [68,] 0.49241615 0.100940179 -0.359633685 0.270130875 0.800359026
## [69,] 0.52002950 0.007805881 -0.466497571 0.441269890 1.019925490
## [70,] 0.79661906 0.238724199 -0.484333300 0.019789707 0.477332853
## [71,] 0.94512324 0.278771581 -0.562008896 -0.042692670 0.220096094
## [72,] 0.96324989 0.362412670 -0.578125687 0.001468341 0.542337926
## [73,] 1.16432755 0.349245536 -0.778927863 -0.073917919 0.275664525
## [74,] 0.94563671 0.315768219 -0.632107414 0.206050787 0.356328852
## [75,] 1.11653173 0.591388050 -0.622337135 -0.278749544 0.016501025
## [76,] 0.66663002 0.388256160 -0.469618513 0.417402373 0.585513782
## [77,] 0.72951518 0.652014022 -0.305366109 0.217856519 -0.023133896
## [78,] 0.10737392 0.499090465 0.010875085 0.973601860 0.272434537
## [79,] 0.35256997 0.777775104 0.045739799 0.573562962 -0.120073189
## [80,] 0.43487945 0.992071919 0.090121891 0.371653849 -0.420488205
## [81,] 0.08372939 0.863625927 0.351559193 1.089060655 -0.235579525
## [82,] 0.22772158 1.135887647 0.389691298 0.777980353 -0.329793357
## [83,] 0.29852060 1.244045157 0.212400529 0.463040939 -0.572523284
## [84,] 0.37903520 1.238133082 0.059618868 0.578057236 -0.461492914
## [85,] 0.61880723 1.301604656 -0.210184688 0.220926385 -1.020058864
## [86,] 0.48535504 1.093722200 -0.288786027 0.819838392 -0.640406972
## [87,] 0.55614994 1.151332638 -0.302460750 0.962979761 -0.598169209
## [88,] 0.69650003 1.451922199 -0.390926745 0.349857496 -0.854955019
## [89,] 0.29261901 1.542952892 -0.134550640 0.513384590 -0.470631554
## [90,] 0.21105065 1.465077830 -0.213108271 0.854377525 -0.130150378
## [91,] 0.50682414 1.595410969 -0.485410842 0.462381642 -0.307749660
## [92,] 0.51534476 1.651227811 -0.514504090 0.510360354 -0.085371246
## [93,] 0.48668510 1.603573922 -0.586539472 0.624703285 0.012543797
## [94,] 0.82525642 1.642640152 -0.836293240 0.462732268 -0.180692901
## [95,] 0.58501545 1.472092550 -0.792091813 0.959221880 0.153303647
## [96,] 0.73770560 1.428581904 -0.900829572 1.107261152 0.115652319
## [97,] 0.65204804 1.288002390 -1.005713619 1.278674779 0.301661077
## [98,] 0.57736231 1.269502113 -0.965712339 1.203629717 0.574224600
## [99,] 1.54852717 -0.410832344 1.024601101 0.084751704 0.236375875
## [100,] 2.17214261 -1.896282632 2.101595923 0.041650045 0.612943439
## [101,] 2.62451539 -2.844629486 2.265943416 -0.240263314 0.369761514
## [102,] 2.76814935 -2.587841085 1.535380806 -0.372961385 0.045569376
## [103,] 3.01986129 -2.367387821 1.146885202 0.312821705 0.115070723
## [104,] 3.16407657 -2.033391209 0.824350417 0.655401049 -0.047200632
## [105,] 2.99172594 -1.723336218 0.742834696 0.679534304 -0.005304778
## [106,] 2.80061791 -1.518201698 0.589813913 0.827561198 0.037841529
## [107,] 3.21302590 -1.012441893 0.023908888 -0.204635453 -0.551140922
## [108,] 3.45651606 -0.774101302 -0.141458290 -0.640806397 -0.768624377
## [109,] 3.68050235 -0.724463418 -0.336775916 -0.784327385 -1.051879207
## [110,] 3.59111521 -1.067301615 -0.377110973 -0.136157839 -0.576441379
## [111,] 3.51538669 -1.110775393 -0.174520478 0.091695615 -0.437049573
## [112,] 3.32249572 -1.314670760 0.031932143 0.511881412 0.071718308
## [113,] 3.05430025 -1.340573864 0.150575322 0.326557852 0.291886690
## [114,] 3.60427270 -1.308479129 -0.032328409 0.101547815 -0.400884770
## [115,] 3.47832916 -1.190096563 0.069075373 0.058358386 -0.155244989
## [116,] 3.74538207 -0.997025731 -0.168667981 -0.143004449 -0.109536320
## [117,] 3.75134730 -0.854425508 -0.343528512 -0.107005101 -0.066049558
## [118,] 3.86612126 -0.507584301 -0.514685269 -0.447057667 -0.358711848
## [119,] 3.64989482 -0.371963239 -0.541404995 -0.161102441 -0.055050772
## [120,] 3.53088860 -0.163519513 -0.546550676 -0.077062415 0.102241441
## [121,] 3.18027608 -0.007663311 -0.513241943 -0.156109489 0.251040905
## [122,] 3.18419035 0.025845602 -0.577549441 0.001779499 0.230673173
## [123,] 3.34505066 0.519178702 -0.614104808 -0.994331836 -0.134891417
## [124,] 2.99729302 0.726655101 -0.430490025 -1.259013205 0.172153527
## [125,] 2.74963166 0.664986203 -0.318408386 -0.856363139 0.535283499
## [126,] 2.83883634 1.005006746 -0.321028014 -1.429119526 0.486669924
## [127,] 2.29073735 0.986212874 -0.047337869 -0.981325067 1.025007579
## [128,] 2.41621608 1.212154846 -0.137748643 -1.244646584 0.513026504
## [129,] 2.10454898 1.677585307 0.116105283 -1.600824139 0.391402682
## [130,] 1.78932649 1.825082059 0.420371038 -0.987378514 0.509872198
## [131,] 1.84836106 2.061224486 0.461377407 -0.840553697 0.322993636
## [132,] 1.87025528 2.225005315 0.417269791 -0.802255372 0.340681401
## Comp.6 Comp.7
## [1,] -0.3447268809 0.016781658
## [2,] -0.3031131224 -0.071471027
## [3,] -0.1394977269 0.033383816
## [4,] -0.5240562376 -0.119679588
## [5,] -0.2292092912 -0.057022269
## [6,] 0.1439352202 0.111327327
## [7,] 0.1248382123 0.085163800
## [8,] 0.0407089258 0.148354292
## [9,] -0.0655104306 0.057400442
## [10,] -0.0918215396 0.030255489
## [11,] -0.1342557646 0.058614104
## [12,] -0.2202127763 0.105376194
## [13,] 0.0003747422 0.113024830
## [14,] 0.1798232251 0.102933519
## [15,] 0.1436630812 0.036770092
## [16,] 0.0208523069 0.062106987
## [17,] 0.1006212810 0.044381034
## [18,] 0.4884636883 0.174312946
## [19,] 0.7222511787 0.402888449
## [20,] 0.6132240235 0.317509910
## [21,] 0.3929036974 0.022993227
## [22,] 0.3405042830 0.092519821
## [23,] 0.3160290674 0.125958993
## [24,] 0.2374058907 0.034711365
## [25,] 0.1444169861 -0.072677289
## [26,] -0.0433426100 -0.110354344
## [27,] -0.1408390367 -0.064853548
## [28,] -0.3133125756 -0.033288398
## [29,] -0.3481726443 0.029868807
## [30,] -0.3162146998 0.006759703
## [31,] -0.5171365447 0.003002943
## [32,] -0.5097377413 0.049205693
## [33,] -0.5272613478 0.019368989
## [34,] -0.5607120474 -0.058552082
## [35,] -0.5542189107 0.073158213
## [36,] -0.6480623394 -0.083874275
## [37,] -0.7691113439 -0.153404154
## [38,] -0.7178354505 -0.187356919
## [39,] -0.4722483550 -0.215982055
## [40,] -0.4678413020 -0.183578414
## [41,] -0.3920793234 -0.124181031
## [42,] -0.1819856023 -0.048348044
## [43,] -0.1045764052 0.022617233
## [44,] 0.0359919421 -0.087226863
## [45,] 0.3663707612 -0.049748648
## [46,] 0.2909894913 -0.091896348
## [47,] 0.4113122930 0.106121088
## [48,] 0.1089891877 0.091556444
## [49,] 0.4139859196 0.070773816
## [50,] 0.2635562244 0.039343395
## [51,] 0.1050672012 0.045262362
## [52,] 0.1371044554 0.101214809
## [53,] 0.4566519741 0.182791134
## [54,] 0.1131186309 0.054679020
## [55,] 0.0100188364 0.115635098
## [56,] 0.3473722894 -0.161005901
## [57,] 0.3591902151 -0.177164226
## [58,] 0.5111328636 -0.137177466
## [59,] 0.5586833430 -0.433868601
## [60,] 0.4930227230 -0.131035426
## [61,] 0.4970895891 -0.085400071
## [62,] 0.2781142185 -0.199990602
## [63,] 0.1467499865 -0.247880444
## [64,] 0.0461726290 -0.274461913
## [65,] -0.0295639205 -0.238929360
## [66,] 0.1605645791 -0.005165463
## [67,] -0.1050794255 -0.159548112
## [68,] -0.0322033552 -0.200275312
## [69,] 0.1001061569 -0.177910642
## [70,] 0.2159233962 -0.280409483
## [71,] 0.2375711540 -0.278068820
## [72,] 0.0341873595 -0.285173537
## [73,] -0.0874663925 -0.333958215
## [74,] 0.1723249745 -0.273616505
## [75,] 0.2396446592 -0.366670253
## [76,] 0.4005467125 -0.203011675
## [77,] 0.2238036057 -0.224563706
## [78,] 0.2443548257 0.096393886
## [79,] 0.3154330549 -0.088302402
## [80,] 0.2125471856 -0.138653188
## [81,] 0.3996389987 0.037774038
## [82,] 0.3753514016 -0.077245472
## [83,] 0.0751306533 -0.043503410
## [84,] -0.1155182389 0.007849421
## [85,] -0.1618430495 -0.052345813
## [86,] -0.2723132077 0.061712557
## [87,] -0.3035342331 0.056681283
## [88,] -0.3654197327 -0.047411490
## [89,] -0.0924905191 0.066311212
## [90,] -0.2448880572 0.133520700
## [91,] -0.3141880842 0.059514090
## [92,] -0.2034905689 -0.009877696
## [93,] 0.0376751792 0.145313122
## [94,] 0.0101743543 -0.022335233
## [95,] 0.0751363411 0.118110472
## [96,] -0.0445887156 0.134653510
## [97,] -0.1790712284 0.164961901
## [98,] 0.0099449115 0.188538616
## [99,] 0.4609875252 0.045939842
## [100,] 0.0065176326 -0.197905991
## [101,] -0.3071415668 0.072833959
## [102,] -0.2750981871 0.093435432
## [103,] -0.1446002732 0.065170003
## [104,] -0.1562708327 0.078092107
## [105,] -0.0265207268 0.078423049
## [106,] 0.0812871093 0.149049527
## [107,] 0.0950250674 0.026968699
## [108,] -0.0788146847 -0.042231840
## [109,] -0.2125114211 -0.151123125
## [110,] 0.0018424765 -0.046655208
## [111,] 0.1419304493 -0.027854252
## [112,] 0.1228199556 0.041874264
## [113,] 0.1997413203 0.143532903
## [114,] 0.1281849515 0.094436720
## [115,] 0.1328440963 0.067270654
## [116,] -0.2470893970 -0.040006754
## [117,] -0.3363612668 -0.002652537
## [118,] -0.3013687748 0.018341537
## [119,] -0.2569374927 0.076223265
## [120,] -0.1748634520 0.193877095
## [121,] -0.0061300110 0.158640483
## [122,] 0.0173073997 0.185504173
## [123,] 0.0140762245 0.074991651
## [124,] 0.1695534866 0.130658429
## [125,] 0.2470102459 0.151609132
## [126,] 0.1529281137 0.022880838
## [127,] 0.4564873732 0.109083709
## [128,] 0.2421029512 0.121441748
## [129,] -0.0121414286 0.134500008
## [130,] -0.1292997186 0.291525791
## [131,] -0.5265419142 0.261594117
## [132,] -0.7909645396 0.329524459
fit_pca$loadings
##
## Loadings:
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7
## LnM2 0.431 0.287 0.197 0.151 0.817
## LnBI_RATE -0.404 0.195 0.426 0.155 -0.420 -0.627 0.155
## LnGDP -0.222 0.742 -0.553 0.288
## LnEXCHANGE_RATE 0.350 0.406 0.589 0.275 0.214 -0.492
## LnINFLASI -0.377 0.326 0.210 -0.618 0.564
## LnCADANGAN_DEVISA 0.430 -0.255 0.114 0.397 -0.733 -0.203
## LnTRANSAKSI_INDEKS_SAHAM 0.390 0.241 -0.137 -0.650 -0.566 -0.144
##
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7
## SS loadings 1.000 1.000 1.000 1.000 1.000 1.000 1.000
## Proportion Var 0.143 0.143 0.143 0.143 0.143 0.143 0.143
## Cumulative Var 0.143 0.286 0.429 0.571 0.714 0.857 1.000
After PCA is performed, the two Principal Components (PC1 and PC2) that have the greatest contribution to the variability of the data are constructed as linear combinations of the original variables. The coefficients of each variable are determined by the PCA results. These two principal components will then be used in the VAR analysis.
PC1 = 0.431*datatr$LnM2 -0.404*datatr$LnBI_RATE -0.222*datatr$LnGDP + 0.350*datatr$LnEXCHANGE_RATE -0.377*datatr$LnINFLASI + 0.430*datatr$LnCADANGAN_DEVISA + 0.390*datatr$LnTRANSAKSI_INDEKS_SAHAM
PC2 = 0.287*datatr$LnM2 +0.195*datatr$LnBI_RATE +0.742*datatr$LnGDP + 0.406*datatr$LnEXCHANGE_RATE +0.326*datatr$LnINFLASI + 0.241*datatr$LnTRANSAKSI_INDEKS_SAHAM
Stationarity testing is done to ensure that the data meets the basic assumptions in time series analysis. Stationarity is tested in terms of variance using Box-Cox Transformation and in terms of average using Augmented Dickey-Fuller (ADF) test.
Data that has been converted into time series form is then tested for stationarity with Box-Cox Transformation. If the data is not stationary, then differencing is performed to remove unwanted trends or variability.
datavar = data.frame(PC1,PC2)
lambda_PC1 = BoxCox.lambda(PC1)
lambda_PC1
## [1] -0.9999242
lambda_PC2 = BoxCox.lambda(PC2)
lambda_PC2
## [1] -0.9999242
transformed_PC1 <- ifelse(lambda_PC1 == 0, log(PC1), (PC1^lambda_PC1 - 1) / lambda_PC1)
transformed_PC1
## [1] 0.9405164
transformed_PC2 <- ifelse(lambda_PC2 == 0, log(PC2), (PC1^lambda_PC2 - 1) / lambda_PC2)
transformed_PC2
## [1] 0.9405164
#If Lambda = 1 (stasioner)
PC1 <- ts(datavar$PC1, start = c(2012, 1), freq = 12)
PC1
## Jan Feb Mar Apr May Jun Jul Aug
## 2012 16.79484 16.89865 16.78031 16.81095 16.84022 16.74234 16.76968 16.68688
## 2013 16.86845 16.85237 16.90907 16.91078 17.02496 16.90805 16.60627 16.53948
## 2014 16.60194 16.68463 16.80095 16.79801 16.73009 16.76138 17.00735 17.00491
## 2015 16.91278 16.96145 16.98316 16.98079 16.86298 16.81864 16.74701 16.82920
## 2016 17.03275 17.08998 17.14669 17.20538 17.18368 17.30443 17.31046 17.59811
## 2017 17.42165 17.46871 17.57354 17.52992 17.50883 17.34936 17.47853 17.53289
## 2018 17.77932 17.72317 17.79722 17.63363 17.69497 17.50665 17.60224 17.64507
## 2019 17.76224 17.69981 17.71653 17.77139 17.63017 17.57867 17.68010 17.66442
## 2020 17.70709 17.66435 17.81116 17.82230 17.93801 18.05678 18.15891 18.24353
## 2021 18.53295 18.48337 18.44728 18.33767 18.24073 18.46071 18.39534 18.45240
## 2022 18.31694 18.32937 18.39584 18.26793 18.16853 18.18162 17.98909 18.07020
## Sep Oct Nov Dec
## 2012 16.81720 16.83318 16.86275 16.89414
## 2013 16.69285 16.61581 16.56169 16.57767
## 2014 17.02455 17.01703 16.79851 16.80346
## 2015 16.81016 16.98664 16.94801 17.12488
## 2016 17.53008 17.50137 17.80882 17.58530
## 2017 17.53241 17.65969 17.73100 17.69525
## 2018 17.54434 17.58182 17.62010 17.63438
## 2019 17.66976 17.78376 17.69822 17.74641
## 2020 18.20858 18.17570 18.36356 18.44494
## 2021 18.45979 18.53165 18.45547 18.42051
## 2022 17.97374 17.87381 17.89279 17.89290
PC2 <- ts(datavar$PC2, start = c(2012, 1), freq = 12)
PC2
## Jan Feb Mar Apr May Jun Jul Aug
## 2012 14.89586 14.92573 14.92109 14.99676 15.02348 14.98828 15.00890 14.95546
## 2013 15.07115 15.15481 15.25033 15.21350 15.28008 15.28979 15.36931 15.38062
## 2014 15.41652 15.42587 15.45927 15.44447 15.40592 15.38026 15.30326 15.22948
## 2015 15.50454 15.46353 15.49867 15.53178 15.49043 15.48137 15.43921 15.49505
## 2016 15.30814 15.35349 15.36373 15.28142 15.23844 15.30059 15.25661 15.25682
## 2017 15.22479 15.29891 15.32708 15.37728 15.38254 15.29350 15.29755 15.29672
## 2018 15.32976 15.29420 15.38470 15.28888 15.35381 15.26950 15.34842 15.40271
## 2019 15.43676 15.33795 15.32876 15.43701 15.45180 15.40478 15.44896 15.44945
## 2020 15.27046 15.28331 15.35822 15.28101 15.21556 15.19505 15.10054 15.07028
## 2021 15.31082 15.18299 15.16416 15.11415 15.14917 15.15350 15.18987 15.23621
## 2022 15.34598 15.32333 15.50848 15.59027 15.54464 15.66684 15.63174 15.68289
## Sep Oct Nov Dec
## 2012 15.00063 15.04896 15.02923 15.04342
## 2013 15.47169 15.41378 15.40251 15.41574
## 2014 15.31576 15.34838 15.37281 15.54353
## 2015 15.47326 15.52789 15.36645 15.25148
## 2016 15.24103 15.24271 15.48986 15.24719
## 2017 15.25496 15.32171 15.32187 15.34252
## 2018 15.31301 15.39216 15.43688 15.41886
## 2019 15.41990 15.41949 15.34272 15.31226
## 2020 15.09800 15.09061 15.23049 15.30664
## 2021 15.24081 15.31046 15.29577 15.31665
## 2022 15.81828 15.79206 15.81602 15.84058
Stationarity is tested using the Augmented Dickey-Fuller (ADF) test. If the test results show that the data is not stationary (p-value> 0.05), then differencing is performed to make the data stationary. ACF and PACF plots are used to determine the optimal number of lags in the VAR model.
adf.test(PC1)
##
## Augmented Dickey-Fuller Test
##
## data: PC1
## Dickey-Fuller = -2.1213, Lag order = 5, p-value = 0.5261
## alternative hypothesis: stationary
adf.test(PC2)
##
## Augmented Dickey-Fuller Test
##
## data: PC2
## Dickey-Fuller = -1.7135, Lag order = 5, p-value = 0.6956
## alternative hypothesis: stationary
#H0 : Data is Not Stationary
#H1 : Data Stationary
#alpha 0.05
#Test Criteria: Reject H0 if pvalue < alpha
## Differencing
df.PC1=diff(PC1, 1)
df.PC2=diff(PC2, 1)
# Average Stationary Check
adf.test(df.PC1)
##
## Augmented Dickey-Fuller Test
##
## data: df.PC1
## Dickey-Fuller = -3.9833, Lag order = 5, p-value = 0.01228
## alternative hypothesis: stationary
adf.test(df.PC2)
## Warning in adf.test(df.PC2): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: df.PC2
## Dickey-Fuller = -4.0431, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
#ACF Plot
Acf(df.PC1, main = "Plot ACF PC1", lag.max = 50)
Acf(df.PC2, main = "Plot ACF PC2", lag.max = 50)
#PACF Plot
Pacf(df.PC1, main = "Plot PACF PC1", lag.max = 50)
Pacf(df.PC2, main = "Plot PACF PC2", lag.max = 50)
The Granger Causality test is conducted to determine whether one variable can be used to predict another variable. The test is conducted in both directions (PC1 against PC2 and vice versa). If the p-value < 0.05, then there is a causal relationship between the two variables.
# Granger Causality Test
grangertest(x = df.PC1, y = df.PC2)
## Granger causality test
##
## Model 1: df.PC2 ~ Lags(df.PC2, 1:1) + Lags(df.PC1, 1:1)
## Model 2: df.PC2 ~ Lags(df.PC2, 1:1)
## Res.Df Df F Pr(>F)
## 1 127
## 2 128 -1 16.382 8.94e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
grangertest(x = df.PC2, y = df.PC1)
## Granger causality test
##
## Model 1: df.PC1 ~ Lags(df.PC1, 1:1) + Lags(df.PC2, 1:1)
## Model 2: df.PC1 ~ Lags(df.PC1, 1:1)
## Res.Df Df F Pr(>F)
## 1 127
## 2 128 -1 8.3255 0.004595 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The VARI model is built using the training dataset
(train_data) with 80% of the data used for training and the
rest for testing (test_data). Optimal lag selection is done
using the VARselect() function, and the VARI model is built
using the VAR() function.
data_ts <- ts(datavar[,c("PC1", "PC2")], start = c(2012,1), end = c(2022,12), frequency = 12)
data_ts %>% autoplot(facets = TRUE)
n_train <- as.integer(0.80*nrow(data_ts))
train_data <- head(data_ts, n_train)
nrow(train_data)
## [1] 105
test_data <- tail(data_ts, -n_train)
nrow(test_data)
## [1] 27
VARselect(train_data, lag.max = 5, season = 12)
## $selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 3 3 2 3
##
## $criteria
## 1 2 3 4 5
## AIC(n) -9.813635e+00 -1.006689e+01 -1.013213e+01 -1.007102e+01 -1.003388e+01
## HQ(n) -9.518414e+00 -9.729496e+00 -9.752565e+00 -9.649280e+00 -9.569965e+00
## SC(n) -9.084187e+00 -9.233237e+00 -9.194273e+00 -9.028955e+00 -8.887608e+00
## FPE(n) 5.490355e-05 4.269861e-05 4.009723e-05 4.275206e-05 4.453396e-05
model <- VAR(train_data, p=3, season=12)
Model evaluation is performed by making predictions using the
forecast() function, and the results are compared with the
actual data. MAPE (Mean Absolute Percentage Error) is calculated to
evaluate the accuracy of the model. If the MAPE value is low, the model
has good performance.
prediction <- forecast(model, h = nrow(test_data))
prediction
## PC1
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Oct 2020 18.23707 18.10979 18.36436 18.04241 18.43173
## Nov 2020 18.23913 18.06728 18.41097 17.97631 18.50194
## Dec 2020 18.23724 18.03209 18.44239 17.92350 18.55099
## Jan 2021 18.23772 17.99999 18.47544 17.87415 18.60129
## Feb 2021 18.24166 17.97857 18.50475 17.83930 18.64403
## Mar 2021 18.31154 18.02582 18.59727 17.87457 18.74852
## Apr 2021 18.29722 17.98991 18.60453 17.82723 18.76721
## May 2021 18.28816 17.96127 18.61504 17.78823 18.78808
## Jun 2021 18.24499 17.89985 18.59013 17.71715 18.77283
## Jul 2021 18.27759 17.91515 18.64003 17.72328 18.83190
## Aug 2021 18.31713 17.93840 18.69587 17.73790 18.89636
## Sep 2021 18.32434 17.93012 18.71855 17.72143 18.92724
## Oct 2021 18.36412 17.95511 18.77312 17.73859 18.98964
## Nov 2021 18.36700 17.94385 18.79015 17.71985 19.01416
## Dec 2021 18.36769 17.93096 18.80442 17.69977 19.03561
## Jan 2022 18.36764 17.91784 18.81744 17.67973 19.05555
## Feb 2022 18.37177 17.90937 18.83418 17.66459 19.07896
## Mar 2022 18.44279 17.96822 18.91737 17.71699 19.16860
## Apr 2022 18.42885 17.94248 18.91522 17.68502 19.17269
## May 2022 18.42015 17.92235 18.91795 17.65883 19.18147
## Jun 2022 18.37745 17.86856 18.88635 17.59916 19.15574
## Jul 2022 18.41032 17.89064 18.93000 17.61554 19.20511
## Aug 2022 18.45009 17.91991 18.98027 17.63925 19.26093
## Sep 2022 18.45751 17.91710 18.99791 17.63102 19.28399
## Oct 2022 18.49742 17.94704 19.04781 17.65568 19.33916
## Nov 2022 18.50040 17.94028 19.06052 17.64377 19.35703
## Dec 2022 18.50115 17.93152 19.07078 17.62998 19.37233
##
## PC2
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Oct 2020 15.15508 15.08538 15.22479 15.04847 15.26169
## Nov 2020 15.17613 15.08416 15.26810 15.03548 15.31678
## Dec 2020 15.15912 15.04960 15.26865 14.99162 15.32663
## Jan 2021 15.16614 15.04407 15.28821 14.97945 15.35283
## Feb 2021 15.18055 15.04977 15.31133 14.98053 15.38057
## Mar 2021 15.23213 15.09424 15.37001 15.02125 15.44300
## Apr 2021 15.22210 15.07842 15.36578 15.00236 15.44184
## May 2021 15.22166 15.07329 15.37003 14.99475 15.44857
## Jun 2021 15.19984 15.04756 15.35211 14.96695 15.43272
## Jul 2021 15.19725 15.04172 15.35279 14.95939 15.43512
## Aug 2021 15.19474 15.03648 15.35300 14.95270 15.43678
## Sep 2021 15.20255 15.04198 15.36312 14.95698 15.44813
## Oct 2021 15.23509 15.07255 15.39762 14.98651 15.48366
## Nov 2021 15.24397 15.07975 15.40818 14.99282 15.49511
## Dec 2021 15.22259 15.05694 15.38825 14.96924 15.47594
## Jan 2022 15.22303 15.05613 15.38993 14.96777 15.47829
## Feb 2022 15.23122 15.06324 15.39921 14.97431 15.48814
## Mar 2022 15.27780 15.10887 15.44673 15.01944 15.53616
## Apr 2022 15.26273 15.09296 15.43249 15.00309 15.52236
## May 2022 15.25773 15.08723 15.42823 14.99697 15.51848
## Jun 2022 15.23192 15.06076 15.40307 14.97016 15.49367
## Jul 2022 15.22560 15.05387 15.39734 14.96295 15.48825
## Aug 2022 15.21969 15.04743 15.39195 14.95624 15.48314
## Sep 2022 15.22445 15.05172 15.39718 14.96028 15.48862
## Oct 2022 15.25418 15.08102 15.42735 14.98935 15.51901
## Nov 2022 15.26051 15.08695 15.43407 14.99508 15.52594
## Dec 2022 15.23682 15.06290 15.41074 14.97084 15.50281
# Visualization of forecast values with testing data
# plot PC1
train_data[,"PC1"] %>%
autoplot(main = "PC1") +
autolayer(test_data[,"PC1"], series = "Test Data") +
autolayer(prediction$forecast$PC1$mean, series = "Forecast")
# Plot PC2
train_data[,"PC2"] %>% autoplot(main = "PC2") +
autolayer(test_data[,"PC2"], series = "Test Data") +
autolayer(prediction$forecast$PC2$mean, series = "Forecast")
mape_PC1 <- MAPE(prediction$forecast$PC1$mean, test_data[,"PC1"])
mape_PC2 <- MAPE(prediction$forecast$PC2$mean, test_data[,"PC2"])
paste("MAPE untuk PC1: ", mape_PC1)
## [1] "MAPE untuk PC1: 0.0121222160568113"
paste("MAPE untuk PC2: ", mape_PC2)
## [1] "MAPE untuk PC2: 0.0134520674380905"
Model diagnostics are performed to evaluate whether the VAR model is appropriate. The residuals of the model are tested using the Kolmogorov-Smirnov test to see if the residuals are normally distributed. In addition, the Ljung-Box test is conducted to test whether the residuals are white noise or still contain certain patterns. If the test results show that the residuals are normally distributed and have no autocorrelation, then the model is considered valid.
residuals_model <- residuals(model)
# model PC1
r1 <- residuals_model[, "PC1"]
n1=length(r1)
mean1=mean(r1)
sd1=sd(r1)
res1=rnorm(n1,mean1,sd1)
ks.test(r1, res1)
##
## Asymptotic two-sample Kolmogorov-Smirnov test
##
## data: r1 and res1
## D = 0.13725, p-value = 0.2919
## alternative hypothesis: two-sided
#model PC2
r2 <- residuals_model[, "PC2"]
n2=length(r2)
mean2=mean(r2)
sd2=sd(r2)
res2=rnorm(n2,mean2,sd2)
ks.test(r2, res2)
##
## Asymptotic two-sample Kolmogorov-Smirnov test
##
## data: r2 and res2
## D = 0.098039, p-value = 0.711
## alternative hypothesis: two-sided
#=====White Noise-Autocorrelation Test=====
residuals_pc1 <- residuals_model[, "PC1"]
residuals_pc2 <- residuals_model[, "PC2"]
# Ljung-Box test
#model PC1
ljung_box_pc1 <- Box.test(residuals_pc1, lag = 3, type = "Ljung-Box"); ljung_box_pc1
##
## Box-Ljung test
##
## data: residuals_pc1
## X-squared = 0.86093, df = 3, p-value = 0.8348
#model PC2
ljung_box_pc2 <- Box.test(residuals_pc2, lag = 3, type = "Ljung-Box"); ljung_box_pc2
##
## Box-Ljung test
##
## data: residuals_pc2
## X-squared = 0.30573, df = 3, p-value = 0.9589
Once a suitable VARI model is found, forecasting of PC1 and PC2
values for the next 12-month period is performed using the
predict() function. The plot of the forecasting results is
compared with the historical data to see how the model captures the data
pattern.
final_model <- VAR(data_ts, p = 3, season = 12)
forecasting <- forecast(final_model, h = nrow(test_data))
predictions <- predict(final_model, n.ahead = 12, ci = 0.95)
plot(predictions, names = "PC1")
plot(predictions, names = "PC2")
plot(forecast(model, h=27, interval="approximate"))
The forecasting results are compared with the historical data using a plot, where the predicted values are displayed alongside the actual data. If the model is able to follow the data pattern well, then the model is considered suitable for use in analysis and decision making.
# Plot PC1
autoplot(train_data[, "PC1"]) +
autolayer(test_data[, "PC1"], series = "Test Data") +
autolayer(prediction$forecast$PC1$mean, series = "Forecast") +
ggtitle("PC1 Forecast vs. Training Data")
# Plot PC2
autoplot(train_data[, "PC2"]) +
autolayer(test_data[, "PC2"], series = "Test Data") +
autolayer(prediction$forecast$PC2$mean, series = "Forecast") +
ggtitle("PC2 Forecast vs. Training Data")