Regresi Linear Berganda adalah model regresi linear dengan melibatkan lebih dari satu variable bebas atau predictor. Dalam bahasa inggris, istilah ini disebut dengan multiple linear regression.
library(readxl)
## Warning: package 'readxl' was built under R version 4.1.2
dataisoman <- read_excel(path = "D:/1. PERKULIAHAN/Semester 2/Linier Algebra/data/IsolasiMandiriSeptember2020.xlsx")
dataisoman
## # A tibble: 30 x 8
## Tanggal `Self Isolation` retail_and_recreation_~ grocery_and_pha~
## <dttm> <dbl> <dbl> <dbl>
## 1 2020-09-01 00:00:00 5423 -24 -2
## 2 2020-09-02 00:00:00 5757 -25 -6
## 3 2020-09-03 00:00:00 6323 -25 -6
## 4 2020-09-04 00:00:00 6257 -22 -5
## 5 2020-09-05 00:00:00 6501 -25 -4
## 6 2020-09-06 00:00:00 6582 -30 -10
## 7 2020-09-07 00:00:00 6706 -25 -8
## 8 2020-09-08 00:00:00 6598 -26 -9
## 9 2020-09-09 00:00:00 6691 -26 -10
## 10 2020-09-10 00:00:00 6968 -26 -10
## # ... with 20 more rows, and 4 more variables:
## # parks_percent_change_from_baseline <dbl>,
## # transit_stations_percent_change_from_baseline <dbl>,
## # workplaces_percent_change_from_baseline <dbl>,
## # residential_percent_change_from_baseline <dbl>
summary(dataisoman)
## Tanggal Self Isolation
## Min. :2020-09-01 00:00:00 Min. : 5423
## 1st Qu.:2020-09-08 06:00:00 1st Qu.: 6695
## Median :2020-09-15 12:00:00 Median : 7849
## Mean :2020-09-15 12:00:00 Mean : 8126
## 3rd Qu.:2020-09-22 18:00:00 3rd Qu.: 9553
## Max. :2020-09-30 00:00:00 Max. :10915
## retail_and_recreation_percent_change_from_baseline
## Min. :-53.00
## 1st Qu.:-41.75
## Median :-38.50
## Mean :-35.00
## 3rd Qu.:-26.00
## Max. :-22.00
## grocery_and_pharmacy_percent_change_from_baseline
## Min. :-30.00
## 1st Qu.:-18.75
## Median :-12.00
## Mean :-13.47
## 3rd Qu.: -8.00
## Max. : -2.00
## parks_percent_change_from_baseline
## Min. :-76.00
## 1st Qu.:-65.00
## Median :-61.50
## Mean :-61.83
## 3rd Qu.:-59.00
## Max. :-51.00
## transit_stations_percent_change_from_baseline
## Min. :-55.0
## 1st Qu.:-52.0
## Median :-47.0
## Mean :-45.6
## 3rd Qu.:-41.0
## Max. :-34.0
## workplaces_percent_change_from_baseline
## Min. :-39.00
## 1st Qu.:-37.00
## Median :-32.00
## Mean :-30.27
## 3rd Qu.:-23.25
## Max. :-13.00
## residential_percent_change_from_baseline
## Min. : 8.0
## 1st Qu.:13.0
## Median :14.0
## Mean :14.9
## 3rd Qu.:18.0
## Max. :19.0
pairs(dataisoman)
pairs(dataisoman, lower.panel = NULL)
plot(dataisoman$`Self Isolation` ~ dataisoman$retail_and_recreation_percent_change_from_baseline, data=dataisoman)
plot(dataisoman$`Self Isolation` ~ dataisoman$
retail_and_recreation_percent_change_from_baseline+dataisoman$
grocery_and_pharmacy_percent_change_from_baseline+dataisoman$
parks_percent_change_from_baseline+dataisoman$
transit_stations_percent_change_from_baseline+dataisoman$
workplaces_percent_change_from_baseline+dataisoman$
residential_percent_change_from_baseline, data = dataisoman)
### 1.6. Membuat Korelasi Antar Variable Korelasi merupakan keterhubungan antar variabel. Untuk mengukur seberapa jauh hubungan antara satu variabel dengan variabel yang lain kita dapat menggunakan fungsi cor().
cor(dataisoman$`Self Isolation`,dataisoman$
retail_and_recreation_percent_change_from_baseline)
## [1] -0.8063893
cor(dataisoman$`Self Isolation`,dataisoman$
grocery_and_pharmacy_percent_change_from_baseline)
## [1] -0.4679635
cor(dataisoman$`Self Isolation`,dataisoman$
parks_percent_change_from_baseline)
## [1] -0.4943694
cor(dataisoman$`Self Isolation`,dataisoman$
transit_stations_percent_change_from_baseline)
## [1] -0.5797651
cor(dataisoman$`Self Isolation`,dataisoman$
workplaces_percent_change_from_baseline)
## [1] -0.1650708
cor(dataisoman$`Self Isolation`,dataisoman$
residential_percent_change_from_baseline)
## [1] 0.48826
Dari hasil seluruh output di atas dapat disimpulkan bahwa untuk tingkat keterhubungan antara variabel y dengan x1, x2, x3, x4, dan x5 memiliki hubungan karena nilai yang dihasilkan berjumlah lebih dari 0.3. Sedangkan untuk tingkat keterhubungan antara variabel y dan x6 tidak sangat terhubung karena nilai yang dihasilkan berjumlah kurang dari 0.
Berikut adalah cara melakukan permodelan Regresi Linier Berganda.
model <- lm(dataisoman$`Self Isolation` ~ dataisoman$Tanggal, data = dataisoman)
Selanjutnya dengan model yang telah dibuat di atas, kita akan menggunakan fungsi summary() untuk menjelaskan atau mereview hasil dari model tersebut. Dan dengan ringkasan summary(model) kita dapat melihat informasi terperinci tentang kinerja dan koefisian model.
summary(model)
##
## Call:
## lm(formula = dataisoman$`Self Isolation` ~ dataisoman$Tanggal,
## data = dataisoman)
##
## Residuals:
## Min 1Q Median 3Q Max
## -647.63 -164.61 56.74 124.48 697.25
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.360e+06 1.168e+05 -28.76 <2e-16 ***
## dataisoman$Tanggal 2.105e-03 7.302e-05 28.83 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 299.1 on 28 degrees of freedom
## Multiple R-squared: 0.9674, Adjusted R-squared: 0.9662
## F-statistic: 831.1 on 1 and 28 DF, p-value: < 2.2e-16
Di atas merupakan rincian dari model yang telah dibuat.
setelah menjalankan fungsi summary() maka akan didapat 5 nilai residual. Residual adalah perbedaan antara nilai nyata dan nilai prediksi. Yang mana semakin kecil nilai residual maka semakin baik atau benar model yang kita buat. Berikut nilai-nilai residual yang dihasilkan:
Nilai Minimum = -647.63 Nilai Maximum = 697.25 Nilai Median = 56.74 Nilai Quartil 1 = -164.61 Nilai Quartil 3 = 124.48
Dari nilai-nilai tersebut dapat kita lihat bahwa dalam konteks ini berupa nilai minimum, maximum, median, quartil 1 dan quartil 3. Dapat kita simpulkan bahwa model yang telah kita buat belum bisa dikatakan baik atau benar karena nilai-nilai yang dihasilkan tidak mendekati nol.
Di bawah nilai residual terdapat koefisien, yang mana dalam koefisien tersebut terdapat nilai intersep, dan tanggal. Selain itu juga terdapat nilai-p dari koefisien
Selanjutnya terdapat dua R2 yaitu:
Multiple R-squared: 0.9674. hal ini menunjukkan bahwa 0.009997% variasi variabel respon, y, dapat dijelaskan oleh variabel prediktor x. Multiple R-squared tidak dapat berkurang saat kita menambahkan lebih banyak variabel independen ke model yang kita buat.
Adjusted R-squared: 0.9662 Adjusted R-squared lebih baik ada penambahan variabel. Jadi jika kita menambahkan lebih dari satu variabel ke model, itu hanya meningkat jika itu mengurangi kesalahan prediksi secara keseluruhan.
ANOVA (analysis of variance) adalah pengujian yang dilakukan dengan membandingkan varians. Dengan membandingkan varians tersebut, dapat diketahui ada tidaknya perbedaan rata-rata dari tiga atau lebih kelompok. Asumsi normalitas pada ANOVA adalah pada residual yaitu selisih antara Y Prediksi dengan Y Aktual. Tepatnya residual dapat dihitung sebagai berikut: Y Aktual – Y Prediksi. Dimana Y Aktual adalah Y sesungguhnya atau kenyataan. Sedangkan Y prediksi adalah Y hasil persamaan ANOVA.
anova(model)
## Analysis of Variance Table
##
## Response: dataisoman$`Self Isolation`
## Df Sum Sq Mean Sq F value Pr(>F)
## dataisoman$Tanggal 1 74343214 74343214 831.08 < 2.2e-16 ***
## Residuals 28 2504711 89454
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(dataisoman$`Self Isolation` ~ dataisoman$
Tanggal,
data = dataisoman, col = "dodgerblue", pch = 20, cex = 1.5,
main = "Data Covid POSITIF di DKI Jakarta dan Google Mobility Index Bulan September")
abline(model)
Dari Plot di atas perlu kita ketahui bahwa titik-titik hijau yang ada pada grafik tersebut adalah data real dan garis hitam di dalam kotak adalah data prediksi.
plot(cooks.distance(model), pch = 16, col = "dodgerblue")
plot(model)
AIC dan BIC banyak digunakan dalam kriteria pemilihan model. AIC berarti Kriteria Informasi Akaike dan BIC berarti Kriteria Informasi Bayesian. Meskipun kedua istilah ini membahas pemilihan model, keduanya tidak sama. Seseorang dapat menemukan perbedaan antara dua pendekatan pemilihan model.
AIC(model)
## [1] 431.1109
BIC(model)
## [1] 435.3145
head(predict(model), n = 15)
## 1 2 3 4 5 6 7 8
## 5489.026 5670.900 5852.774 6034.648 6216.522 6398.396 6580.270 6762.144
## 9 10 11 12 13 14 15
## 6944.018 7125.893 7307.767 7489.641 7671.515 7853.389 8035.263
plot(head(predict(model), n = 20))
head(resid(model), n = 11)
## 1 2 3 4 5 6 7
## -66.02581 86.10011 470.22603 222.35195 284.47786 183.60378 125.72970
## 8 9 10 11
## -164.14438 -253.01846 -157.89255 -164.76663
coef(model)
## (Intercept) dataisoman$Tanggal
## -3.360273e+06 2.105024e-03
Tabel di bawah merupakan semua proses yang telah dilakukan, sehingga terbuat tabel yang ada nilai residuals dan nilai protected.
dataisoman$residuals <- model$residuals
dataisoman$predicted <- model$fitted.values
dataisoman
## # A tibble: 30 x 10
## Tanggal `Self Isolation` retail_and_recreation_~ grocery_and_pha~
## <dttm> <dbl> <dbl> <dbl>
## 1 2020-09-01 00:00:00 5423 -24 -2
## 2 2020-09-02 00:00:00 5757 -25 -6
## 3 2020-09-03 00:00:00 6323 -25 -6
## 4 2020-09-04 00:00:00 6257 -22 -5
## 5 2020-09-05 00:00:00 6501 -25 -4
## 6 2020-09-06 00:00:00 6582 -30 -10
## 7 2020-09-07 00:00:00 6706 -25 -8
## 8 2020-09-08 00:00:00 6598 -26 -9
## 9 2020-09-09 00:00:00 6691 -26 -10
## 10 2020-09-10 00:00:00 6968 -26 -10
## # ... with 20 more rows, and 6 more variables:
## # parks_percent_change_from_baseline <dbl>,
## # transit_stations_percent_change_from_baseline <dbl>,
## # workplaces_percent_change_from_baseline <dbl>,
## # residential_percent_change_from_baseline <dbl>, residuals <dbl>,
## # predicted <dbl>
scatter.smooth(x=dataisoman$Tanggal, y=dataisoman$`Self Isolation`,
main="Tanggal ~ Self Isolation")
boxplot(dataisoman$`Self Isolation`, main="Self Isolation",
boxplot.stats(dataisoman$`Self Isolation`)$out)
plot(density(dataisoman$`Self Isolation`), main="Google Mobility Index: Self Isolation",
ylab="Frequency")
coefs <- coef(model)
plot(`Self Isolation` ~ Tanggal, data = dataisoman)
abline(coefs)
text(x = 12, y = 10, paste('expression = ', round(coefs[1], 2), '+',
round(coefs[2], 2), '*Self Isolation'))
Adanya korelasi antar variabel dapat dilakukan melalui visualisasi menggunakan scatterplot dan perhitungan matematis menggunakan metode Pearson untuk metode parametrik dan metode rangking Spearman dan Kendall untuk metode non-parametrik. Pada R uji korelasi dapat dilakukan dengan menggunakan fungsi cor.test(). Format fungsi tersebut adalah sebagai berikut:
a. Uji Korelasi Varible Y dengan X1
cor.test(dataisoman$
retail_and_recreation_percent_change_from_baseline,
dataisoman$`Self Isolation`)
##
## Pearson's product-moment correlation
##
## data: dataisoman$retail_and_recreation_percent_change_from_baseline and dataisoman$`Self Isolation`
## t = -7.2153, df = 28, p-value = 7.461e-08
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.9040240 -0.6287962
## sample estimates:
## cor
## -0.8063893
b. Uji Korelasi Varible Y dengan X2
cor.test(dataisoman$
grocery_and_pharmacy_percent_change_from_baseline,
dataisoman$`Self Isolation`)
##
## Pearson's product-moment correlation
##
## data: dataisoman$grocery_and_pharmacy_percent_change_from_baseline and dataisoman$`Self Isolation`
## t = -2.802, df = 28, p-value = 0.009112
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.7087436 -0.1295325
## sample estimates:
## cor
## -0.4679635
c. Uji Korelasi Varible Y dengan X3
cor.test(dataisoman$
parks_percent_change_from_baseline,
dataisoman$`Self Isolation`)
##
## Pearson's product-moment correlation
##
## data: dataisoman$parks_percent_change_from_baseline and dataisoman$`Self Isolation`
## t = -3.0094, df = 28, p-value = 0.005488
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.7254344 -0.1631600
## sample estimates:
## cor
## -0.4943694
d. Uji Korelasi Varible Y dengan X4
cor.test(dataisoman$
transit_stations_percent_change_from_baseline,
dataisoman$`Self Isolation`)
##
## Pearson's product-moment correlation
##
## data: dataisoman$transit_stations_percent_change_from_baseline and dataisoman$`Self Isolation`
## t = -3.7652, df = 28, p-value = 0.0007857
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.7776131 -0.2774466
## sample estimates:
## cor
## -0.5797651
e. Uji Korelasi Varible Y dengan X5
cor.test(dataisoman$
workplaces_percent_change_from_baseline,
dataisoman$`Self Isolation`)
##
## Pearson's product-moment correlation
##
## data: dataisoman$workplaces_percent_change_from_baseline and dataisoman$`Self Isolation`
## t = -0.88562, df = 28, p-value = 0.3834
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.4958518 0.2075409
## sample estimates:
## cor
## -0.1650708
d. Uji Korelasi Varible Y dengan X6
cor.test(dataisoman$
residential_percent_change_from_baseline,
dataisoman$`Self Isolation`)
##
## Pearson's product-moment correlation
##
## data: dataisoman$residential_percent_change_from_baseline and dataisoman$`Self Isolation`
## t = 2.9605, df = 28, p-value = 0.006192
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.1553107 0.7215967
## sample estimates:
## cor
## 0.48826
Berdasarkan seluruh output yang dihasilkan, metode Pearson menghasilkan output berupa nilai t uji, derajat kebebasan, nilai p-value, rentang estimasi nilai korelasi berdasarkan tingkat kepercayaan, dan estimasi nilai korelasi.