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

Lembaga : Universitas Islam Negeri Maulana Malik Ibrahim Malang

Fakultas : Sains dan Teknologi

Jurusan : Teknik Informatika

Kelas : (A) Linear Algebra

NIM : 210605110042

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)
## Warning: package 'readxl' was built under R version 4.1.3
DataSembuh <- read_excel("~/COLLEGE/DataSembuh.xlsx")
DataSembuh
## # A tibble: 31 x 7
##    Tanggal Sembuh grocery_and_pharmacy_percen~ parks_percent_c~ transit_station~
##      <dbl>  <dbl>                        <dbl>            <dbl>            <dbl>
##  1       1    427                          -18              -90              -69
##  2       2    562                          -14              -91              -58
##  3       3    622                          -16              -91              -59
##  4       4    650                          -14              -90              -59
##  5       5    711                          -13              -88              -58
##  6       6    713                          -13              -90              -55
##  7       7    718                          -18              -91              -56
##  8       8    763                          -14              -88              -53
##  9       9    767                          -16              -89              -54
## 10      10    803                          -15              -89              -53
## # ... with 21 more rows, and 2 more variables:
## #   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(DataSembuh)
##     Tanggal         Sembuh    
##  Min.   : 1.0   Min.   : 427  
##  1st Qu.: 8.5   1st Qu.: 765  
##  Median :16.0   Median :1292  
##  Mean   :16.0   Mean   :1236  
##  3rd Qu.:23.5   3rd Qu.:1590  
##  Max.   :31.0   Max.   :2102  
##                               
##  grocery_and_pharmacy_percent_change_from_baseline
##  Min.   :-18.00                                   
##  1st Qu.:-15.00                                   
##  Median :-13.00                                   
##  Mean   :-12.97                                   
##  3rd Qu.:-12.25                                   
##  Max.   : -3.00                                   
##  NA's   :1                                        
##  parks_percent_change_from_baseline
##  Min.   :-91.00                    
##  1st Qu.:-88.75                    
##  Median :-85.50                    
##  Mean   :-86.03                    
##  3rd Qu.:-84.00                    
##  Max.   :-77.00                    
##  NA's   :1                         
##  transit_stations_percent_change_from_baseline
##  Min.   :-69.00                               
##  1st Qu.:-54.00                               
##  Median :-49.50                               
##  Mean   :-50.97                               
##  3rd Qu.:-47.00                               
##  Max.   :-40.00                               
##  NA's   :1                                    
##  workplaces_percent_change_from_baseline
##  Min.   :-68.00                         
##  1st Qu.:-35.75                         
##  Median :-34.00                         
##  Mean   :-32.47                         
##  3rd Qu.:-28.25                         
##  Max.   :-15.00                         
##  NA's   :1                              
##  residential_percent_change_from_baseline
##  Min.   :10.00                           
##  1st Qu.:14.00                           
##  Median :16.00                           
##  Mean   :16.17                           
##  3rd Qu.:18.00                           
##  Max.   :27.00                           
##  NA's   :1

Membuat Matriks dengan Fungsi Pairs()

pairs(DataSembuh)

pairs(DataSembuh, lower.panel=NULL)

Visualisasi Data Menggunakan Fungsi Plot()

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

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

plot(DataSembuh$Sembuh,DataSembuh$grocery_and_pharmacy_percent_change_from_baseline+DataSembuh$parks_percent_change_from_baseline+DataSembuh$transit_stations_percent_change_from_baseline+DataSembuh$workplaces_percent_change_from_baseline+DataSembuh$residential_percent_change_from_baseline, data = DataSembuh)
## 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(DataSembuh$Sembuh,DataSembuh$
      grocery_and_pharmacy_percent_change_from_baseline)
## [1] NA

b.) Korelasi Variable Y dengan X2

cor(DataSembuh$Sembuh,DataSembuh$
      parks_percent_change_from_baseline)
## [1] NA

c.) Korelasi Variable Y dengan X3

cor(DataSembuh$Sembuh,DataSembuh$
      transit_stations_percent_change_from_baseline)
## [1] NA

d.) Korelasi Variable Y dengan X4

cor(DataSembuh$Sembuh,DataSembuh$
      workplaces_percent_change_from_baseline)
## [1] NA

f.) Korelasi Variable Y dengan X6

cor(DataSembuh$Sembuh,DataSembuh$
      residential_percent_change_from_baseline)
## [1] NA

Permodelan Regresi Linier Berganda

model <- lm(DataSembuh$Sembuh ~ DataSembuh$Tanggal, data = DataSembuh)
summary(model)
## 
## Call:
## lm(formula = DataSembuh$Sembuh ~ DataSembuh$Tanggal, data = DataSembuh)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -182.345  -62.604    1.089   43.608  226.608 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         434.826     34.905   12.46 3.63e-13 ***
## DataSembuh$Tanggal   50.047      1.904   26.28  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 94.83 on 29 degrees of freedom
## Multiple R-squared:  0.9597, Adjusted R-squared:  0.9583 
## F-statistic: 690.7 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 DataSembuh\(DataSembuh ~ DataSembuh\)Tanggal, data = DataSembuh

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 = -1573.0

Nilai maximum = 980.8

Nilai median = 116.0

Nilai quartil 1 = -195.8

Nilai quartil 3 = 250.9

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: DataSembuh$Sembuh
##                    Df  Sum Sq Mean Sq F value    Pr(>F)    
## DataSembuh$Tanggal  1 6211706 6211706  690.75 < 2.2e-16 ***
## Residuals          29  260790    8993                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Plot() Model dari Data Real dengan Data Prediksi

plot(DataSembuh$Sembuh ~ DataSembuh$Tanggal, data = DataSembuh, col = "pink", pch = 20, cex = 1.5, main = "Data Inflow Covid DKI Jakarta Mei 2020 dan Google Mobility Index")
abline(model)

Titik-titik merah muda yang ada pada grafik tersebut adalah data real dan garis hitam di dalam kotak adalah data prediksi.

plot(cooks.distance(model), pch = 16, col = "pink") #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] 374.1362
BIC(model)
## [1] 378.4381

Nilai Predicted dan Visualnya

head(predict(model), n = 11)
##        1        2        3        4        5        6        7        8 
## 484.8730 534.9202 584.9673 635.0145 685.0617 735.1089 785.1560 835.2032 
##        9       10       11 
## 885.2504 935.2976 985.3448
plot(head(predict(model), n = 10))

Nilai Residuals

head(resid(model), n = 11)
##          1          2          3          4          5          6          7 
##  -57.87298   27.07984   37.03266   14.98548   25.93831  -22.10887  -67.15605 
##          8          9         10         11 
##  -72.20323 -118.25040 -132.29758 -182.34476
coef(model)
##        (Intercept) DataSembuh$Tanggal 
##          434.82581           50.04718

Tabel Data Residuals dan Data Predictied

DataSembuh$residuals <- model$residuals
DataSembuh$predicted <- model$fitted.values
DataSembuh
## # A tibble: 31 x 9
##    Tanggal Sembuh grocery_and_pharmacy_percen~ parks_percent_c~ transit_station~
##      <dbl>  <dbl>                        <dbl>            <dbl>            <dbl>
##  1       1    427                          -18              -90              -69
##  2       2    562                          -14              -91              -58
##  3       3    622                          -16              -91              -59
##  4       4    650                          -14              -90              -59
##  5       5    711                          -13              -88              -58
##  6       6    713                          -13              -90              -55
##  7       7    718                          -18              -91              -56
##  8       8    763                          -14              -88              -53
##  9       9    767                          -16              -89              -54
## 10      10    803                          -15              -89              -53
## # ... with 21 more rows, and 4 more variables:
## #   workplaces_percent_change_from_baseline <dbl>,
## #   residential_percent_change_from_baseline <dbl>, residuals <dbl>,
## #   predicted <dbl>

Visualisasi Data Menggunakan Scatter.Smooth, Boxplot dan Plot

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

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

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(DataSembuh$grocery_and_pharmacy_percent_change_from_baseline, DataSembuh$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSembuh$grocery_and_pharmacy_percent_change_from_baseline and DataSembuh$Sembuh
## t = 4.9531, df = 28, p-value = 3.153e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.4286331 0.8374611
## sample estimates:
##       cor 
## 0.6833735

b.) Uji Korelasi Variable Y dengan X2

cor.test(DataSembuh$parks_percent_change_from_baseline, DataSembuh$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSembuh$parks_percent_change_from_baseline and DataSembuh$Sembuh
## t = 12.999, df = 28, p-value = 2.207e-13
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.8493440 0.9646012
## sample estimates:
##       cor 
## 0.9262023

c.) Uji Korelasi Variable Y dengan X3

cor.test(DataSembuh$transit_stations_percent_change_from_baseline, DataSembuh$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSembuh$transit_stations_percent_change_from_baseline and DataSembuh$Sembuh
## t = 11.072, df = 28, p-value = 9.707e-12
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.8030037 0.9528083
## sample estimates:
##       cor 
## 0.9022533

d.) Uji Korelasi Variable Y dengan X4

cor.test(DataSembuh$workplaces_percent_change_from_baseline, DataSembuh$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSembuh$workplaces_percent_change_from_baseline and DataSembuh$Sembuh
## t = 3.0192, df = 28, p-value = 0.005356
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.1647268 0.7261962
## sample estimates:
##      cor 
## 0.495585

e.) Uji Korelasi Variable Y dengan X5

cor.test(DataSembuh$residential_percent_change_from_baseline, DataSembuh$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSembuh$residential_percent_change_from_baseline and DataSembuh$Sembuh
## t = -5.109, df = 28, p-value = 2.058e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.8437286 -0.4459113
## sample estimates:
##        cor 
## -0.6945951

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