Dosen Pengampu : Prof. Dr. Suhartono, M.Kom

Lembaga : Universitas Islam Negeri Maulana Malik Ibrahim Malang

Fakultas : Sains dan Teknologi

Jurusan : Teknik Informatika

Kelas : (C) Linear Algebra

NIM : 210605110102

Pengertian Regresi Linier

Regresi linear berganda merupakan model regresi yang melibatkan lebih dari satu variabel independen. Analisis regresi linear berganda dilakukan untuk mengetahui arah dan seberapa besar pengaruh variabel independen terhadap variabel dependen (Ghozali, 2018).

Memanggil Data dari Excel

Excel adalah salah satu jenis file eksternal yang sering digunakan untuk menyimpan data. Kita dapat menggunakan package {readxl} dengan fungsi read_excel() untuk import data dari file Excel. Argumen path = adalah lokasi dan nama file Excel yang akan kita gunakan.

library(readxl)
Sembuh <- read_excel((path = "DataSembuh.xlsx"))
Sembuh
## # A tibble: 31 x 8
##    Tanggal SEMBUH retail_and_recreation_perce~ grocery_and_pha~ parks_percent_c~
##      <dbl>  <dbl>                        <dbl>            <dbl>            <dbl>
##  1       1 326509                          -29               -5              -51
##  2       2 328526                          -29               -5              -52
##  3       3 331016                          -27               -5              -49
##  4       4 332758                          -29              -10              -50
##  5       5 334100                          -26               -6              -45
##  6       6 335617                          -26               -2              -45
##  7       7 337426                          -31               -9              -52
##  8       8 338931                          -28               -7              -48
##  9       9 340587                          -29               -9              -49
## 10      10 341398                          -25               -5              -46
## # ... with 21 more rows, and 3 more variables:
## #   transit_stations_percent_change_from_baseline <dbl>,
## #   workplaces_percent_change_from_baseline <dbl>,
## #   residential_percent_change_from_baseline <dbl>

Summary() dari Data

Nilai statistik yang dicari adalah minimum, Q1, median, mean, Q3, dan maximum.

summary(Sembuh)
##     Tanggal         SEMBUH      
##  Min.   : 1.0   Min.   :326509  
##  1st Qu.: 8.5   1st Qu.:339759  
##  Median :16.0   Median :350561  
##  Mean   :16.0   Mean   :349573  
##  3rd Qu.:23.5   3rd Qu.:361364  
##  Max.   :31.0   Max.   :369007  
##  retail_and_recreation_percent_change_from_baseline
##  Min.   :-31.00                                    
##  1st Qu.:-29.00                                    
##  Median :-27.00                                    
##  Mean   :-27.19                                    
##  3rd Qu.:-26.00                                    
##  Max.   :-21.00                                    
##  grocery_and_pharmacy_percent_change_from_baseline
##  Min.   :-11.000                                  
##  1st Qu.: -8.000                                  
##  Median : -6.000                                  
##  Mean   : -6.032                                  
##  3rd Qu.: -4.500                                  
##  Max.   :  0.000                                  
##  parks_percent_change_from_baseline
##  Min.   :-52.00                    
##  1st Qu.:-46.50                    
##  Median :-44.00                    
##  Mean   :-44.35                    
##  3rd Qu.:-42.00                    
##  Max.   :-34.00                    
##  transit_stations_percent_change_from_baseline
##  Min.   :-51.00                               
##  1st Qu.:-38.50                               
##  Median :-37.00                               
##  Mean   :-36.03                               
##  3rd Qu.:-32.00                               
##  Max.   :-28.00                               
##  workplaces_percent_change_from_baseline
##  Min.   :-66.00                         
##  1st Qu.:-33.00                         
##  Median :-31.00                         
##  Mean   :-29.06                         
##  3rd Qu.:-24.50                         
##  Max.   :-12.00                         
##  residential_percent_change_from_baseline
##  Min.   : 5.000                          
##  1st Qu.: 7.500                          
##  Median :10.000                          
##  Mean   : 9.032                          
##  3rd Qu.:10.000                          
##  Max.   :18.000

Membuat Matriks dengan Fungsi Pairs()

pairs(Sembuh)

pairs(Sembuh, lower.panel=NULL)

Visualisasi Data Menggunakan Fungsi Plot()

plot(Sembuh$SEMBUH ~ Sembuh$Tanggal, data = Sembuh)

Visualisasikan Data dengan Data Sembuh sebagai Variable Y dan Google Mobility Index sebagai Variable X

plot(Sembuh$SEMBUH, Sembuh$retail_and_recreation_percent_change_from_baseline+Sembuh$grocery_and_pharmacy_percent_change_from_baseline+Sembuh$parks_percent_change_from_baseline+Sembuh$transit_stations_percent_change_from_baseline+Sembuh$workplaces_percent_change_from_baseline+Sembuh$residential_percent_change_from_baseline, data = Sembuh)
## Warning in plot.window(...): "data" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "data" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "data" is not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "data" is not a
## graphical parameter
## Warning in box(...): "data" is not a graphical parameter
## Warning in title(...): "data" is not a graphical parameter

Korelasi AntarVariable

a.) Korelasi Variable Y dengan X1

cor(Sembuh$SEMBUH,Sembuh$
      retail_and_recreation_percent_change_from_baseline)
## [1] 0.4206678

b.) Korelasi Variable Y dengan X2

cor(Sembuh$SEMBUH,Sembuh$
      grocery_and_pharmacy_percent_change_from_baseline)
## [1] 0.2758551

c.) Korelasi Variable Y dengan X3

cor(Sembuh$SEMBUH,Sembuh$
      parks_percent_change_from_baseline)
## [1] 0.5933155

d.) Korelasi Variable Y dengan X4

cor(Sembuh$SEMBUH,Sembuh$
      transit_stations_percent_change_from_baseline)
## [1] 0.3463417

e.) Korelasi Variable Y dengan X5

cor(Sembuh$SEMBUH,Sembuh$
      workplaces_percent_change_from_baseline)
## [1] 0.2165227

f.) Korelasi Variable Y dengan X6

cor(Sembuh$SEMBUH,Sembuh$
      residential_percent_change_from_baseline)
## [1] -0.183123

Permodelan Regresi Linier Berganda

model <- lm(Sembuh$SEMBUH ~ Sembuh$Tanggal, data = Sembuh)
summary(model)
## 
## Call:
## lm(formula = Sembuh$SEMBUH ~ Sembuh$Tanggal, data = Sembuh)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2028.09  -469.32    67.81   640.58  1455.25 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    327134.67     325.15 1006.11   <2e-16 ***
## Sembuh$Tanggal   1402.42      17.74   79.06   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 883.4 on 29 degrees of freedom
## Multiple R-squared:  0.9954, Adjusted R-squared:  0.9952 
## F-statistic:  6251 on 1 and 29 DF,  p-value: < 2.2e-16

Rincian Model dengan Fungsi Summary()

Di atas merupakan rincian dari model yang telah dibuat.

Di posisi paling atas terdapat lm formula adalah Sembuh\(SEMBUH ~ Sembuh\)Tanggal, data = Sembuh

Lalu di bawahnya terdapat 5 nilai residual, sebelumnya kita perlu tahu bahwa Residual merupakan selisih dari nilai prediksi dan nilai sebenarnya (actual) atau ei =Yi - (a + b Xi ). Jika nilai pengamatan terletak dalam garis regresi maka nilai residunya sama dengan nol. Yang mana semakin kecil nilai residual maka semakin baik atau benar model yang kita buat. Berikut nilai-nilai residual yang dihasilkan:

Nilai minimum = -2028.09

Nilai maximum = 1455.25

Nilai median = 67.81

Nilai quartil 1 = -469.32

Nilai quartil 3 = 640.58

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, retail_and_recreation, grocery_and_pharmacy, parks, transit_stations, workplaces dan residential. Selain itu juga terdapat nilai-p dari koefisien

Fungsi Anova()

Uji Anova(Analysis of Variance Table) berfungsi untuk membandingkan rata-rata populasi untuk mengetahui perbedaan signifikan dari dua atau lebih kelompok data.

anova(model)
## Analysis of Variance Table
## 
## Response: Sembuh$SEMBUH
##                Df     Sum Sq    Mean Sq F value    Pr(>F)    
## Sembuh$Tanggal  1 4877617321 4877617321  6250.8 < 2.2e-16 ***
## Residuals      29   22629278     780320                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Plot() Model dari Data Real dengan Data Prediksi

plot(Sembuh$SEMBUH ~ Sembuh$Tanggal, data = Sembuh, col = "darkgreen", pch = 20, cex = 1.5, main = "Data Inflow Covid DKI Jakarta Maret 2021 dan Google Mobility Index")
abline(model)

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 = "darkgreen") #Plot the Cooks Distances.

plot(model)

Penggunaan AIC dan BIC

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] 512.498
BIC(model)
## [1] 516.8

Nilai Predicted dan Visualnya

head(predict(model), n = 11)
##        1        2        3        4        5        6        7        8 
## 328537.1 329939.5 331341.9 332744.4 334146.8 335549.2 336951.6 338354.0 
##        9       10       11 
## 339756.4 341158.9 342561.3
plot(head(predict(model), n = 10))

Nilai Residuals

head(resid(model), n = 11)
##           1           2           3           4           5           6 
## -2028.09073 -1413.51048  -325.93024    13.65000   -46.76976    67.81048 
##           7           8           9          10          11 
##   474.39073   576.97097   830.55121   239.13145  -255.28831
coef(model)
##    (Intercept) Sembuh$Tanggal 
##      327134.67        1402.42

Tabel Data Residuals dan Data Predictied

Sembuh$residuals <- model$residuals
Sembuh$predicted <- model$fitted.values
Sembuh
## # A tibble: 31 x 10
##    Tanggal SEMBUH retail_and_recreation_perce~ grocery_and_pha~ parks_percent_c~
##      <dbl>  <dbl>                        <dbl>            <dbl>            <dbl>
##  1       1 326509                          -29               -5              -51
##  2       2 328526                          -29               -5              -52
##  3       3 331016                          -27               -5              -49
##  4       4 332758                          -29              -10              -50
##  5       5 334100                          -26               -6              -45
##  6       6 335617                          -26               -2              -45
##  7       7 337426                          -31               -9              -52
##  8       8 338931                          -28               -7              -48
##  9       9 340587                          -29               -9              -49
## 10      10 341398                          -25               -5              -46
## # ... with 21 more rows, and 5 more variables:
## #   transit_stations_percent_change_from_baseline <dbl>,
## #   workplaces_percent_change_from_baseline <dbl>,
## #   residential_percent_change_from_baseline <dbl>, residuals <dbl>,
## #   predicted <dbl>

Visualisasi Data Menggunakan Scatter.Smooth, Boxplot dan Plot

scatter.smooth(x=Sembuh$Tanggal, y=Sembuh$SEMBUH, main="Tanggal ~ Sembuh")

boxplot(Sembuh$SEMBUH, main="Sembuh", boxplot.stats(Sembuh$SEMBUH)$out)

plot(density(Sembuh$SEMBUH), main="Google Mobility Index : Sembuh", ylab="Frequency")

coefs <- coef(model)
plot(SEMBUH ~ Tanggal, data = Sembuh)
abline(coefs)
text(x = 12, y = 10, paste('expression = ', round(coefs[1], 2),  '+', round(coefs[2], 2), '*Sembuh'))

Uji Korelasi AntarVariable

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.

a.) Uji Korelasi Variable Y dengan X1

cor.test(Sembuh$retail_and_recreation_percent_change_from_baseline, Sembuh$SEMBUH)
## 
##  Pearson's product-moment correlation
## 
## data:  Sembuh$retail_and_recreation_percent_change_from_baseline and Sembuh$SEMBUH
## t = 2.4971, df = 29, p-value = 0.01845
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.07794629 0.67447153
## sample estimates:
##       cor 
## 0.4206678

b.) Uji Korelasi Variable Y dengan X2

cor.test(Sembuh$grocery_and_pharmacy_percent_change_from_baseline, Sembuh$SEMBUH)
## 
##  Pearson's product-moment correlation
## 
## data:  Sembuh$grocery_and_pharmacy_percent_change_from_baseline and Sembuh$SEMBUH
## t = 1.5455, df = 29, p-value = 0.1331
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.08698778  0.57408081
## sample estimates:
##       cor 
## 0.2758551

c.) Uji Korelasi Variable Y dengan X3

cor.test(Sembuh$parks_percent_change_from_baseline, Sembuh$SEMBUH)
## 
##  Pearson's product-moment correlation
## 
## data:  Sembuh$parks_percent_change_from_baseline and Sembuh$SEMBUH
## t = 3.9692, df = 29, p-value = 0.0004349
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.3025907 0.7830341
## sample estimates:
##       cor 
## 0.5933155

d.) Uji Korelasi Variable Y dengan X4

cor.test(Sembuh$transit_stations_percent_change_from_baseline, Sembuh$SEMBUH)
## 
##  Pearson's product-moment correlation
## 
## data:  Sembuh$transit_stations_percent_change_from_baseline and Sembuh$SEMBUH
## t = 1.9882, df = 29, p-value = 0.05631
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.009117322  0.624091580
## sample estimates:
##       cor 
## 0.3463417

e.) Uji Korelasi Variable Y dengan X5

cor.test(Sembuh$workplaces_percent_change_from_baseline, Sembuh$SEMBUH)
## 
##  Pearson's product-moment correlation
## 
## data:  Sembuh$workplaces_percent_change_from_baseline and Sembuh$SEMBUH
## t = 1.1943, df = 29, p-value = 0.242
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.1492698  0.5301856
## sample estimates:
##       cor 
## 0.2165227

f.) Uji Korelasi Variable Y dengan X6

cor.test(Sembuh$residential_percent_change_from_baseline, Sembuh$SEMBUH)
## 
##  Pearson's product-moment correlation
## 
## data:  Sembuh$residential_percent_change_from_baseline and Sembuh$SEMBUH
## t = -1.0031, df = 29, p-value = 0.3241
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.5047134  0.1830979
## sample estimates:
##       cor 
## -0.183123

Referensi

https://rpubs.com/suhartono-uinmaliki/877449

https://accounting.binus.ac.id/2021/08/12/memahami-analisis-regresi-linear-berganda/

PENDITEKSIAN PENCILAN (OUTLIER) DAN RESIDUAL …https://www.litbang.pertanian.go.id

https://www.kompas.com/skola/read/2021/08/05/163826269/uji-anova-pengertian-syarat-fungsi-tujuan-dan-langkahnya?page=all