Analisis Regresi Linier Berganda

#install.packages("readxl")
library(readxl) #Memasukkan Data ke R (Excel)
Data <- read_excel("D:/STATISTIKA 2021/SEMESTER 5/DATA MINING/Pertumbuhan Ekonomi SS.xlsx")
View(Data)
str(Data)
## tibble [32 × 5] (S3: tbl_df/tbl/data.frame)
##  $ PertumbuhanEkonomi   : num [1:32] 24407 25836 26565 27807 26831 ...
##  $ Ekspor               : num [1:32] 15444 18961 19065 19400 20312 ...
##  $ Investasi            : num [1:32] 6070 5997 6695 7165 4577 ...
##  $ KonsumsiRT           : num [1:32] 11920 12058 12361 12394 12415 ...
##  $ PengeluaranPemerintah: num [1:32] 1388 2083 2268 3244 1265 ...
dim(Data)
## [1] 32  5

#Histogram Peubah Respons dan Peubah Penjelas

#install.packages("RColorBrewer")
library(RColorBrewer) #Warna Histogram
display.brewer.all()

hist(Data$PertumbuhanEkonomi, las=1, breaks=8,
     main = "Histogram Pertumbuhan Ekonomi",
     xlab = "Miliar",
     ylab = "Frekuensi",
     border = "black",
     col = brewer.pal(n = 10, name = "RdBu"))

hist(Data$Ekspor, las=1, breaks=8,
     main = "Histogram Ekspor",
     xlab = "Miliar",
     ylab = "Frekuensi",
     border = "black",
     col = brewer.pal(n = 10, name = "Set3"))

hist(Data$Investasi, las=1, breaks=8,
     main = "Histogram Investasi",
     xlab = "Miliar",
     ylab = "Frekuensi",
     border = "black",
     col = brewer.pal(n = 10, name = "Paired"))

hist(Data$KonsumsiRT, las=1, breaks=8,
     main = "Histogram Konsumsi Rumah Tangga",
     xlab = "Miliar",
     ylab = "Frekuensi",
     border = "black",
     col = brewer.pal(n = 10, name = "RdBu"))

hist(Data$PengeluaranPemerintah, las=1, breaks=8,
     main = "Histogram Pengeluaran Pemerintah",
     xlab = "Miliar",
     ylab = "Frekuensi",
     border = "black",
     col = brewer.pal(n = 10, name = "PRGn"))

boxplot(Data, las=1,
        ylab="Miliar", xlab="Peubah", 
        main = "Boxplot Peubah Respons dan Peubah Penjelas")

# Plot Peubah Respons dengan Peubah Penjelas 
plot(Data$Ekspor, Data$PertumbuhanEkonomi, type="p",
     main = "Plot antara Pertumbuhan Ekonomi dan Ekspor (Miliar)")

plot(Data$Investasi, Data$PertumbuhanEkonomi, type="p",
     main = "Plot antara Pertumbuhan Ekonomi dan Investasi (Miliar)")

plot(Data$KonsumsiRT, Data$PertumbuhanEkonomi, type="p",
     main = "Plot antara Pertumbuhan Ekonomi dan Konsumsi Rumah Tangga (Miliar)")

plot(Data$PengeluaranPemerintah, Data$PertumbuhanEkonomi, type="p",
     main = "Plot antara Pertumbuhan Ekonomi dan Pengeluaran Pemerintah (Miliar)")

summary(Data)
##  PertumbuhanEkonomi     Ekspor        Investasi       KonsumsiRT   
##  Min.   :24407      Min.   :15444   Min.   : 4577   Min.   :11920  
##  1st Qu.:29102      1st Qu.:20191   1st Qu.: 6549   1st Qu.:12937  
##  Median :31706      Median :22804   Median : 7291   Median :14045  
##  Mean   :31895      Mean   :22845   Mean   : 7249   Mean   :14168  
##  3rd Qu.:34815      3rd Qu.:25492   3rd Qu.: 7957   3rd Qu.:15278  
##  Max.   :38420      Max.   :29358   Max.   :10047   Max.   :16632  
##  PengeluaranPemerintah
##  Min.   :1265         
##  1st Qu.:1735         
##  Median :2311         
##  Mean   :2502         
##  3rd Qu.:3192         
##  Max.   :4059
cor(Data)
##                       PertumbuhanEkonomi    Ekspor Investasi KonsumsiRT
## PertumbuhanEkonomi             1.0000000 0.9468991 0.5139441  0.9898244
## Ekspor                         0.9468991 1.0000000 0.3243939  0.9450304
## Investasi                      0.5139441 0.3243939 1.0000000  0.5045237
## KonsumsiRT                     0.9898244 0.9450304 0.5045237  1.0000000
## PengeluaranPemerintah          0.3854982 0.2415247 0.7759085  0.3446699
##                       PengeluaranPemerintah
## PertumbuhanEkonomi                0.3854982
## Ekspor                            0.2415247
## Investasi                         0.7759085
## KonsumsiRT                        0.3446699
## PengeluaranPemerintah             1.0000000
# Korelasi Peubah Respons dengan Peubah Penjelas 
cor.test (Data$Ekspor, Data$PertumbuhanEkonomi)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$Ekspor and Data$PertumbuhanEkonomi
## t = 16.13, df = 30, p-value = 2.504e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.8930807 0.9739998
## sample estimates:
##       cor 
## 0.9468991
cor.test (Data$Investasi, Data$PertumbuhanEkonomi)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$Investasi and Data$PertumbuhanEkonomi
## t = 3.2815, df = 30, p-value = 0.002622
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.2013304 0.7315395
## sample estimates:
##       cor 
## 0.5139441
cor.test (Data$KonsumsiRT, Data$PertumbuhanEkonomi)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$KonsumsiRT and Data$PertumbuhanEkonomi
## t = 38.1, df = 30, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.9790429 0.9950731
## sample estimates:
##       cor 
## 0.9898244
cor.test (Data$PengeluaranPemerintah, Data$PertumbuhanEkonomi)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$PengeluaranPemerintah and Data$PertumbuhanEkonomi
## t = 2.2883, df = 30, p-value = 0.02934
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.04251973 0.64719554
## sample estimates:
##       cor 
## 0.3854982
# Korelasi Antar Peubah Penjelas 
cor(as.matrix(Data[,c("Ekspor","Investasi", "KonsumsiRT","PengeluaranPemerintah")]))
##                          Ekspor Investasi KonsumsiRT PengeluaranPemerintah
## Ekspor                1.0000000 0.3243939  0.9450304             0.2415247
## Investasi             0.3243939 1.0000000  0.5045237             0.7759085
## KonsumsiRT            0.9450304 0.5045237  1.0000000             0.3446699
## PengeluaranPemerintah 0.2415247 0.7759085  0.3446699             1.0000000
# Analisis Regresi Berganda
Model_ARLB <- lm (PertumbuhanEkonomi ~ Ekspor + Investasi + KonsumsiRT + PengeluaranPemerintah, data=Data)
summary (Model_ARLB)
## 
## Call:
## lm(formula = PertumbuhanEkonomi ~ Ekspor + Investasi + KonsumsiRT + 
##     PengeluaranPemerintah, data = Data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1152.85  -204.86    50.33   316.81   858.18 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -2.990e+03  1.312e+03  -2.279   0.0308 *  
## Ekspor                 1.594e-01  9.108e-02   1.750   0.0914 .  
## Investasi             -9.160e-03  1.668e-01  -0.055   0.9566    
## KonsumsiRT             2.161e+00  2.589e-01   8.347 5.89e-09 ***
## PengeluaranPemerintah  2.763e-01  1.612e-01   1.714   0.0980 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 496.4 on 27 degrees of freedom
## Multiple R-squared:  0.9845, Adjusted R-squared:  0.9822 
## F-statistic: 428.3 on 4 and 27 DF,  p-value: < 2.2e-16
# Uji Asumsi Klasik
#install.packages("performance")
library(performance)
# Uji Normalitas 
check_normality(Model_ARLB)
## OK: residuals appear as normally distributed (p = 0.255).
# Uji Heteroskedastisitas
check_heteroscedasticity(Model_ARLB)
## Warning: Heteroscedasticity (non-constant error variance) detected (p = 0.010).
# Uji Multikolinieritas
multicollinearity(Model_ARLB)
## # Check for Multicollinearity
## 
## Low Correlation
## 
##                   Term  VIF     VIF 95% CI Increased SE Tolerance
##              Investasi 4.11 [ 2.67,  6.82]         2.03      0.24
##  PengeluaranPemerintah 2.67 [ 1.83,  4.36]         1.63      0.38
##  Tolerance 95% CI
##      [0.15, 0.38]
##      [0.23, 0.55]
## 
## High Correlation
## 
##        Term   VIF     VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##      Ekspor 13.90 [ 8.37, 23.57]         3.73      0.07     [0.04, 0.12]
##  KonsumsiRT 16.80 [10.07, 28.55]         4.10      0.06     [0.04, 0.10]
# Uji Autokorelasi
check_autocorrelation(Model_ARLB)
## Warning: Autocorrelated residuals detected (p < .001).
# Cek Performa/Kinerja Model
model_performance(Model_ARLB,metrics = "all")
## # Indices of model performance
## 
## AIC     |    AICc |     BIC |    R2 | R2 (adj.) |    RMSE |   Sigma
## -------------------------------------------------------------------
## 494.643 | 498.003 | 503.438 | 0.984 |     0.982 | 455.939 | 496.364
# Hasil Prediksi Model (y_dugaARLB)
y_dugaARLB <- predict(Model_ARLB)
y_dugaARLB
##        1        2        3        4        5        6        7        8 
## 25559.95 26612.30 27327.46 27719.54 27386.12 27826.94 28248.08 28627.45 
##        9       10       11       12       13       14       15       16 
## 28625.26 29074.28 29813.50 30256.38 30187.60 30561.75 31348.18 31667.47 
##       17       18       19       20       21       22       23       24 
## 31415.20 31921.87 32823.21 33344.30 32878.19 33748.02 34958.60 35064.54 
##       25       26       27       28       29       30       31       32 
## 34537.17 35607.88 36457.24 37139.89 36140.98 37152.21 38010.90 38603.36
# Korelasi Antara Pertumbuhan Ekonomi dan Y Duga
cor.test (Data$PertumbuhanEkonomi, y_dugaARLB)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$PertumbuhanEkonomi and y_dugaARLB
## t = 43.63, df = 30, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.9839397 0.9962314
## sample estimates:
##       cor 
## 0.9922119

Analisis Komponen Utama

x <- data.matrix(Data[, c('Ekspor', 'Investasi', 'KonsumsiRT','PengeluaranPemerintah')])
head(x)
##        Ekspor Investasi KonsumsiRT PengeluaranPemerintah
## [1,] 15443.83   6070.14   11919.53               1388.04
## [2,] 18961.48   5996.57   12057.78               2083.34
## [3,] 19064.53   6694.59   12360.51               2267.53
## [4,] 19400.01   7164.88   12394.40               3243.64
## [5,] 20312.28   4576.75   12414.77               1265.14
## [6,] 20089.67   5706.60   12528.48               2137.20
y <- Data$PertumbuhanEkonomi
head(y)
## [1] 24407.10 25835.93 26564.60 27807.45 26831.47 28066.35
model_coba <- lm (y~x)
summary (model_coba)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1152.85  -204.86    50.33   316.81   858.18 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)            -2.990e+03  1.312e+03  -2.279   0.0308 *  
## xEkspor                 1.594e-01  9.108e-02   1.750   0.0914 .  
## xInvestasi             -9.160e-03  1.668e-01  -0.055   0.9566    
## xKonsumsiRT             2.161e+00  2.589e-01   8.347 5.89e-09 ***
## xPengeluaranPemerintah  2.763e-01  1.612e-01   1.714   0.0980 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 496.4 on 27 degrees of freedom
## Multiple R-squared:  0.9845, Adjusted R-squared:  0.9822 
## F-statistic: 428.3 on 4 and 27 DF,  p-value: < 2.2e-16

Pembentukan Komponen Utama

PCA <- prcomp (x, center=TRUE)
PCA
## Standard deviations (1, .., p=4):
## [1] 3916.7451 1273.6103  500.4547  293.9267
## 
## Rotation (n x k) = (4 x 4):
##                              PC1        PC2        PC3        PC4
## Ekspor                0.92927908  0.1862035  0.1671404  0.2717218
## Investasi             0.10668145 -0.7563804 -0.4698159  0.4424712
## KonsumsiRT            0.34723229 -0.1483787 -0.4463423 -0.8112904
## PengeluaranPemerintah 0.06700139 -0.6092625  0.7430449 -0.2686902
summary (PCA)
## Importance of components:
##                              PC1       PC2       PC3       PC4
## Standard deviation     3916.7451 1.274e+03 500.45472 293.92666
## Proportion of Variance    0.8868 9.376e-02   0.01448   0.00499
## Cumulative Proportion     0.8868 9.805e-01   0.99501   1.00000
DataPCA <- PCA$x
View(DataPCA)

PC1 <- data.matrix(DataPCA[, c('PC1')])
head(PC1)
##           [,1]
## [1,] -7858.967
## [2,] -4503.346
## [3,] -4215.660
## [4,] -3776.565
## [5,] -3330.407
## [6,] -3318.826
#PC2 <- data.matrix(DataPCA[, c('PC2')])
#head(PC2)

Analisis Regresi Komponen Utama

Model_ARKU <- lm (y ~ PC1)
summary (Model_ARKU)
## 
## Call:
## lm(formula = y ~ PC1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2012.7  -668.1   232.3   864.5  1355.8 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 3.190e+04  1.757e+02   181.6   <2e-16 ***
## PC1         9.161e-01  4.557e-02    20.1   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 993.7 on 30 degrees of freedom
## Multiple R-squared:  0.9309, Adjusted R-squared:  0.9286 
## F-statistic: 404.2 on 1 and 30 DF,  p-value: < 2.2e-16
# Uji Asumsi Klasik
# Package "performance"
#install.packages("performance")
library(performance)

# Uji Normalitas 
check_normality(Model_ARKU)
## OK: residuals appear as normally distributed (p = 0.051).
# Uji Heteroskedastisitas
check_heteroscedasticity(Model_ARKU)
## OK: Error variance appears to be homoscedastic (p = 0.402).
# Uji Multikolinieritas
multicollinearity(Model_ARKU)
## Not enough model terms in the conditional part of the model to check for
##   multicollinearity.
## NULL
# Uji Autokorelasi
check_autocorrelation(Model_ARKU)
## OK: Residuals appear to be independent and not autocorrelated (p = 0.066).
# Cek Performa/Kinerja Model
model_performance(Model_ARKU,metrics = "all")
## # Indices of model performance
## 
## AIC     |    AICc |     BIC |    R2 | R2 (adj.) |    RMSE |   Sigma
## -------------------------------------------------------------------
## 536.442 | 537.299 | 540.839 | 0.931 |     0.929 | 962.190 | 993.746
# Hasil Prediksi Model (y_dugaARLB)
y_dugaARKU <- predict(Model_ARKU)
y_dugaARKU
##        1        2        3        4        5        6        7        8 
## 24695.58 27769.67 28033.22 28435.47 28844.20 28854.80 27330.41 27745.72 
##        9       10       11       12       13       14       15       16 
## 29334.87 29446.59 29029.97 29583.29 31667.93 31630.44 30889.32 30306.07 
##       17       18       19       20       21       22       23       24 
## 31654.30 31695.56 32065.26 32262.37 32721.70 33382.46 36078.74 34441.15 
##       25       26       27       28       29       30       31       32 
## 35001.92 34466.13 36818.18 38192.51 36411.66 36110.72 37676.03 38069.56
# Korelasi Antara Pertumbuhan Ekonomi dan Y Duga
cor.test (y, y_dugaARKU)
## 
##  Pearson's product-moment correlation
## 
## data:  y and y_dugaARKU
## t = 20.104, df = 30, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.9285209 0.9828608
## sample estimates:
##       cor 
## 0.9648317