Mata Kuliah : Linear Algebra (C)

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

Lembaga : Universitas Islam Negeri Maulana Malik Ibrahim Malang

Fakultas : Sains dan Teknologi

Jurusan : Teknik Informatika

NIM : 210605110050

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)
SelfIsolation <- read_excel((path = "DataSelfIsolationJanuari2021.xlsx"))
SelfIsolation
## # A tibble: 30 x 8
##    Tanggal             Self_isolation retail_and_recreation_pe~ grocery_and_pha~
##    <dttm>                       <dbl>                     <dbl>            <dbl>
##  1 2021-01-01 00:00:00          10082                       -25               -8
##  2 2021-01-02 00:00:00          10872                       -20                3
##  3 2021-01-03 00:00:00          10978                       -21               -2
##  4 2021-01-04 00:00:00          10371                       -15                5
##  5 2021-01-05 00:00:00          10897                       -18                3
##  6 2021-01-06 00:00:00          12196                       -15                4
##  7 2021-01-07 00:00:00          12781                       -16                4
##  8 2021-01-08 00:00:00          13397                       -18                0
##  9 2021-01-09 00:00:00          13076                       -22               -4
## 10 2021-01-10 00:00:00          13166                       -20               -1
## # ... 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() dari Data

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

summary(SelfIsolation)
##     Tanggal                    Self_isolation 
##  Min.   :2021-01-01 00:00:00   Min.   :10082  
##  1st Qu.:2021-01-08 06:00:00   1st Qu.:13098  
##  Median :2021-01-15 12:00:00   Median :15702  
##  Mean   :2021-01-15 12:00:00   Mean   :15440  
##  3rd Qu.:2021-01-22 18:00:00   3rd Qu.:18293  
##  Max.   :2021-01-30 00:00:00   Max.   :20539  
##  retail_and_recreation_percent_change_from_baseline
##  Min.   :-32.00                                    
##  1st Qu.:-27.75                                    
##  Median :-25.00                                    
##  Mean   :-24.33                                    
##  3rd Qu.:-20.25                                    
##  Max.   :-15.00                                    
##  grocery_and_pharmacy_percent_change_from_baseline
##  Min.   :-20.000                                  
##  1st Qu.:-15.000                                  
##  Median :-10.500                                  
##  Mean   : -8.733                                  
##  3rd Qu.: -2.000                                  
##  Max.   :  5.000                                  
##  parks_percent_change_from_baseline
##  Min.   :-36.00                    
##  1st Qu.:-30.00                    
##  Median :-24.50                    
##  Mean   :-20.37                    
##  3rd Qu.:-14.50                    
##  Max.   : 18.00                    
##  transit_stations_percent_change_from_baseline
##  Min.   :-46.00                               
##  1st Qu.:-43.00                               
##  Median :-40.00                               
##  Mean   :-37.43                               
##  3rd Qu.:-33.00                               
##  Max.   :-22.00                               
##  workplaces_percent_change_from_baseline
##  Min.   :-66.00                         
##  1st Qu.:-32.00                         
##  Median :-28.50                         
##  Mean   :-27.80                         
##  3rd Qu.:-23.25                         
##  Max.   : -8.00                         
##  residential_percent_change_from_baseline
##  Min.   : 7.00                           
##  1st Qu.:10.00                           
##  Median :11.00                           
##  Mean   :10.97                           
##  3rd Qu.:12.00                           
##  Max.   :19.00

Membuat Matriks dengan Fungsi Pairs()

pairs(SelfIsolation)

pairs(SelfIsolation, lower.panel=NULL)

Visualisasi Data Menggunakan Fungsi Plot()

plot(SelfIsolation$Self_isolation ~ SelfIsolation$Tanggal, data = SelfIsolation)

Visualisasikan Data dengan Data Self-Isolation sebagai Variable Y dan Google Mobility Index sebagai Variable X

plot(SelfIsolation$Self_isolation, SelfIsolation$retail_and_recreation_percent_change_from_baseline+SelfIsolation$grocery_and_pharmacy_percent_change_from_baseline+SelfIsolation$parks_percent_change_from_baseline+SelfIsolation$transit_stations_percent_change_from_baseline+SelfIsolation$workplaces_percent_change_from_baseline+SelfIsolation$residential_percent_change_from_baseline, data = SelfIsolation)
## 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(SelfIsolation$Self_isolation,SelfIsolation$
      retail_and_recreation_percent_change_from_baseline)
## [1] -0.7015973

b.) Korelasi Variable Y dengan X2

cor(SelfIsolation$Self_isolation,SelfIsolation$
      grocery_and_pharmacy_percent_change_from_baseline)
## [1] -0.7301716

c.) Korelasi Variable Y dengan X3

cor(SelfIsolation$Self_isolation,SelfIsolation$
      parks_percent_change_from_baseline)
## [1] -0.8460493

d.) Korelasi Variable Y dengan X4

cor(SelfIsolation$Self_isolation,SelfIsolation$
      transit_stations_percent_change_from_baseline)
## [1] -0.7076793

e.) Korelasi Variable Y dengan X5

cor(SelfIsolation$Self_isolation,SelfIsolation$
      workplaces_percent_change_from_baseline)
## [1] 0.01549127

f.) Korelasi Variable Y dengan X6

cor(SelfIsolation$Self_isolation,SelfIsolation$
      residential_percent_change_from_baseline)
## [1] -0.3639995

Permodelan Regresi Linier Berganda

model <- lm(SelfIsolation$Self_isolation ~ SelfIsolation$Tanggal, data = SelfIsolation)
summary(model)
## 
## Call:
## lm(formula = SelfIsolation$Self_isolation ~ SelfIsolation$Tanggal, 
##     data = SelfIsolation)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2055.83  -382.37    57.26   423.54  1910.02 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -6.484e+06  3.260e+05  -19.89   <2e-16 ***
## SelfIsolation$Tanggal  4.035e-03  2.024e-04   19.93   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 829.1 on 28 degrees of freedom
## Multiple R-squared:  0.9342, Adjusted R-squared:  0.9318 
## F-statistic: 397.3 on 1 and 28 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 SelfIsolation\(Self_isolation ~ SelfIsolation\)Tanggal, data = SelfIsolation

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

Nilai maximum = 1910.02

Nilai median = 57,26

Nilai quartil 1 = -382.37

Nilai quartil 3 = 423.54

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: SelfIsolation$Self_isolation
##                       Df    Sum Sq   Mean Sq F value    Pr(>F)    
## SelfIsolation$Tanggal  1 273150934 273150934  397.32 < 2.2e-16 ***
## Residuals             28  19249339    687476                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Plot() Model dari Data Real dengan Data Prediksi

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

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

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

Nilai Predicted dan Visualnya

head(predict(model), n = 11)
##        1        2        3        4        5        6        7        8 
## 10384.69 10733.31 11081.92 11430.54 11779.16 12127.78 12476.40 12825.02 
##        9       10       11 
## 13173.64 13522.26 13870.88
plot(head(predict(model), n = 10))

Nilai Residuals

head(resid(model), n = 11)
##           1           2           3           4           5           6 
##  -302.68602   138.69462  -103.92473 -1059.54409  -882.16344    68.21720 
##           7           8           9          10          11 
##   304.59785   571.97849   -97.64086  -356.26022  -329.87957
coef(model)
##           (Intercept) SelfIsolation$Tanggal 
##         -6.483697e+06          4.034946e-03

Tabel Data Residuals dan Data Predictied

SelfIsolation$residuals <- model$residuals
SelfIsolation$predicted <- model$fitted.values
SelfIsolation
## # A tibble: 30 x 10
##    Tanggal             Self_isolation retail_and_recreation_pe~ grocery_and_pha~
##    <dttm>                       <dbl>                     <dbl>            <dbl>
##  1 2021-01-01 00:00:00          10082                       -25               -8
##  2 2021-01-02 00:00:00          10872                       -20                3
##  3 2021-01-03 00:00:00          10978                       -21               -2
##  4 2021-01-04 00:00:00          10371                       -15                5
##  5 2021-01-05 00:00:00          10897                       -18                3
##  6 2021-01-06 00:00:00          12196                       -15                4
##  7 2021-01-07 00:00:00          12781                       -16                4
##  8 2021-01-08 00:00:00          13397                       -18                0
##  9 2021-01-09 00:00:00          13076                       -22               -4
## 10 2021-01-10 00:00:00          13166                       -20               -1
## # ... 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>

Visualisasi Data Menggunakan Scatter.Smooth, Boxplot dan Plot

scatter.smooth(x=SelfIsolation$Tanggal, y=SelfIsolation$Self_isolation, main="Tanggal ~ Self Isolation")

boxplot(SelfIsolation$Self_isolation, main="Self Isolation", boxplot.stats(SelfIsolation$Self_isolation)$out)

plot(density(SelfIsolation$Self_isolation), main="Google Mobility Index : Self Isolation", ylab="Frequency")

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

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(SelfIsolation$retail_and_recreation_percent_change_from_baseline, SelfIsolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  SelfIsolation$retail_and_recreation_percent_change_from_baseline and SelfIsolation$Self_isolation
## t = -5.21, df = 28, p-value = 1.562e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.8476190 -0.4567874
## sample estimates:
##        cor 
## -0.7015973

b.) Uji Korelasi Variable Y dengan X2

cor.test(SelfIsolation$grocery_and_pharmacy_percent_change_from_baseline, SelfIsolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  SelfIsolation$grocery_and_pharmacy_percent_change_from_baseline and SelfIsolation$Self_isolation
## t = -5.6548, df = 28, p-value = 4.652e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.8633337 -0.5019426
## sample estimates:
##        cor 
## -0.7301716

c.) Uji Korelasi Variable Y dengan X3

cor.test(SelfIsolation$parks_percent_change_from_baseline, SelfIsolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  SelfIsolation$parks_percent_change_from_baseline and SelfIsolation$Self_isolation
## t = -8.3977, df = 28, p-value = 3.911e-09
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9245198 -0.6987686
## sample estimates:
##        cor 
## -0.8460493

d.) Uji Korelasi Variable Y dengan X4

cor.test(SelfIsolation$transit_stations_percent_change_from_baseline, SelfIsolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  SelfIsolation$transit_stations_percent_change_from_baseline and SelfIsolation$Self_isolation
## t = -5.3001, df = 28, p-value = 1.222e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.8509855 -0.4662942
## sample estimates:
##        cor 
## -0.7076793

e.) Uji Korelasi Variable Y dengan X5

cor.test(SelfIsolation$workplaces_percent_change_from_baseline, SelfIsolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  SelfIsolation$workplaces_percent_change_from_baseline and SelfIsolation$Self_isolation
## t = 0.081982, df = 28, p-value = 0.9352
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.346713  0.373675
## sample estimates:
##        cor 
## 0.01549127

f.) Uji Korelasi Variable Y dengan X6

cor.test(SelfIsolation$residential_percent_change_from_baseline, SelfIsolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  SelfIsolation$residential_percent_change_from_baseline and SelfIsolation$Self_isolation
## t = -2.068, df = 28, p-value = 0.04799
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.64030106 -0.00429332
## sample estimates:
##        cor 
## -0.3639995

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