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 : 210605110035

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.2
isolation <- read_excel(("Book1.xlsx"))
isolation
## # A tibble: 31 x 8
##    Tanggal Self_isolation retail_and_recreati~ grocery_and_pha~ parks_percent_c~
##      <dbl>          <dbl>                <dbl>            <dbl>            <dbl>
##  1       1          49836                  -42               -5              -67
##  2       2          53014                  -32                2              -51
##  3       3          54941                  -34               -1              -53
##  4       4          59307                  -34               -2              -52
##  5       5          62873                  -37               -6              -55
##  6       6          65448                  -31                2              -45
##  7       7          69644                  -36                2              -56
##  8       8          72361                  -39               -5              -63
##  9       9          73239                  -32               -2              -50
## 10      10          71812                  -35               -6              -55
## # ... 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(isolation)
##     Tanggal     Self_isolation 
##  Min.   : 1.0   Min.   :10134  
##  1st Qu.: 8.5   1st Qu.:51425  
##  Median :16.0   Median :66581  
##  Mean   :16.0   Mean   :59973  
##  3rd Qu.:23.5   3rd Qu.:72800  
##  Max.   :31.0   Max.   :88295  
##  retail_and_recreation_percent_change_from_baseline
##  Min.   :-42.00                                    
##  1st Qu.:-34.00                                    
##  Median :-31.00                                    
##  Mean   :-31.16                                    
##  3rd Qu.:-28.00                                    
##  Max.   :-22.00                                    
##  grocery_and_pharmacy_percent_change_from_baseline
##  Min.   :-13.000                                  
##  1st Qu.: -5.500                                  
##  Median : -2.000                                  
##  Mean   : -2.161                                  
##  3rd Qu.:  0.000                                  
##  Max.   :  6.000                                  
##  parks_percent_change_from_baseline
##  Min.   :-67.0                     
##  1st Qu.:-54.0                     
##  Median :-50.0                     
##  Mean   :-50.9                     
##  3rd Qu.:-47.0                     
##  Max.   :-41.0                     
##  transit_stations_percent_change_from_baseline
##  Min.   :-61.00                               
##  1st Qu.:-51.00                               
##  Median :-47.00                               
##  Mean   :-47.84                               
##  3rd Qu.:-45.00                               
##  Max.   :-39.00                               
##  workplaces_percent_change_from_baseline
##  Min.   :-73.00                         
##  1st Qu.:-39.50                         
##  Median :-35.00                         
##  Mean   :-34.03                         
##  3rd Qu.:-23.50                         
##  Max.   :-14.00                         
##  residential_percent_change_from_baseline
##  Min.   : 7.00                           
##  1st Qu.:11.50                           
##  Median :14.00                           
##  Mean   :13.61                           
##  3rd Qu.:16.00                           
##  Max.   :22.00

Membuat Matriks dengan Fungsi Pairs()

pairs(isolation)

pairs(isolation, lower.panel=NULL)

Visualisasi Data Menggunakan Fungsi Plot()

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

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

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

b.) Korelasi Variable Y dengan X2

cor(isolation$Self_isolation,isolation$
      grocery_and_pharmacy_percent_change_from_baseline)
## [1] -0.6104201

c.) Korelasi Variable Y dengan X3

cor(isolation$Self_isolation,isolation$
      parks_percent_change_from_baseline)
## [1] -0.2592016

d.) Korelasi Variable Y dengan X4

cor(isolation$Self_isolation,isolation$
      transit_stations_percent_change_from_baseline)
## [1] -0.4785212

e.) Korelasi Variable Y dengan X5

cor(isolation$Self_isolation,isolation$
      workplaces_percent_change_from_baseline)
## [1] -0.2171917

f.) Korelasi Variable Y dengan X6

cor(isolation$Self_isolation,isolation$
      residential_percent_change_from_baseline)
## [1] 0.349588

Permodelan Regresi Linier Berganda

model <- lm(isolation$Self_isolation ~ isolation$Tanggal, data = isolation)
summary(model)
## 
## Call:
## lm(formula = isolation$Self_isolation ~ isolation$Tanggal, data = isolation)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -31287 -17461   2056  16289  29201 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        79882.8     7264.2   11.00 7.32e-12 ***
## isolation$Tanggal  -1244.4      396.3   -3.14  0.00387 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 19740 on 29 degrees of freedom
## Multiple R-squared:  0.2537, Adjusted R-squared:  0.228 
## F-statistic:  9.86 on 1 and 29 DF,  p-value: 0.003866

Rincian Model dengan Fungsi Summary()

Di atas merupakan rincian dari model yang telah dibuat.

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

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

Nilai maximum = 29201

Nilai median = 2056

Nilai quartil 1 = -17461

Nilai quartil 3 = 16289

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: isolation$Self_isolation
##                   Df     Sum Sq    Mean Sq F value   Pr(>F)   
## isolation$Tanggal  1 3.8403e+09 3840280620  9.8599 0.003866 **
## Residuals         29 1.1295e+10  389483245                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Plot() Model dari Data Real dengan Data Prediksi

plot(isolation$Self_isolation ~ isolation$Tanggal, data = isolation, col = "pink", pch = 20, cex = 1.5, main = "Data Inflow Covid DKI Jakarta July 2021 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 = "red") #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] 705.097
BIC(model)
## [1] 709.399

Nilai Predicted dan Visualnya

head(predict(model), n = 11)
##        1        2        3        4        5        6        7        8 
## 78638.46 77394.07 76149.68 74905.30 73660.91 72416.52 71172.13 69927.75 
##        9       10       11 
## 68683.36 67438.97 66194.58
plot(head(predict(model), n = 10))

Nilai Residuals

head(resid(model), n = 11)
##          1          2          3          4          5          6          7 
## -28802.458 -24380.070 -21208.683 -15598.295 -10787.908  -6968.520  -1528.133 
##          8          9         10         11 
##   2433.255   4555.642   4373.030   2056.417
coef(model)
##       (Intercept) isolation$Tanggal 
##         79882.845         -1244.388

Tabel Data Residuals dan Data Predictied

isolation$residuals <- model$residuals
isolation$predicted <- model$fitted.values
isolation
## # A tibble: 31 x 10
##    Tanggal Self_isolation retail_and_recreati~ grocery_and_pha~ parks_percent_c~
##      <dbl>          <dbl>                <dbl>            <dbl>            <dbl>
##  1       1          49836                  -42               -5              -67
##  2       2          53014                  -32                2              -51
##  3       3          54941                  -34               -1              -53
##  4       4          59307                  -34               -2              -52
##  5       5          62873                  -37               -6              -55
##  6       6          65448                  -31                2              -45
##  7       7          69644                  -36                2              -56
##  8       8          72361                  -39               -5              -63
##  9       9          73239                  -32               -2              -50
## 10      10          71812                  -35               -6              -55
## # ... 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=isolation$Tanggal, y=isolation$Self_isolation, main="Tanggal ~ isolation")

boxplot(isolation$Self_isolation, main="isolation", boxplot.stats(isolation$Self_isolation)$out)

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

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

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(isolation$retail_and_recreation_percent_change_from_baseline, isolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  isolation$retail_and_recreation_percent_change_from_baseline and isolation$Self_isolation
## t = -3.6951, df = 29, p-value = 0.0009094
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.7664581 -0.2644513
## sample estimates:
##        cor 
## -0.5657751

b.) Uji Korelasi Variable Y dengan X2

cor.test(isolation$grocery_and_pharmacy_percent_change_from_baseline, isolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  isolation$grocery_and_pharmacy_percent_change_from_baseline and isolation$Self_isolation
## t = -4.1501, df = 29, p-value = 0.0002658
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.7931951 -0.3267562
## sample estimates:
##        cor 
## -0.6104201

c.) Uji Korelasi Variable Y dengan X3

cor.test(isolation$parks_percent_change_from_baseline, isolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  isolation$parks_percent_change_from_baseline and isolation$Self_isolation
## t = -1.4452, df = 29, p-value = 0.1591
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.5619309  0.1047603
## sample estimates:
##        cor 
## -0.2592016

d.) Uji Korelasi Variable Y dengan X4

cor.test(isolation$transit_stations_percent_change_from_baseline, isolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  isolation$transit_stations_percent_change_from_baseline and isolation$Self_isolation
## t = -2.9347, df = 29, p-value = 0.006467
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.7121155 -0.1495364
## sample estimates:
##        cor 
## -0.4785212

e.) Uji Korelasi Variable Y dengan X5

cor.test(isolation$workplaces_percent_change_from_baseline, isolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  isolation$workplaces_percent_change_from_baseline and isolation$Self_isolation
## t = -1.1982, df = 29, p-value = 0.2405
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.5306901  0.1485833
## sample estimates:
##        cor 
## -0.2171917

f.) Uji Korelasi Variable Y dengan X6

cor.test(isolation$residential_percent_change_from_baseline, isolation$Self_isolation)
## 
##  Pearson's product-moment correlation
## 
## data:  isolation$residential_percent_change_from_baseline and isolation$Self_isolation
## t = 2.0094, df = 29, p-value = 0.05389
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.005423954  0.626341342
## sample estimates:
##      cor 
## 0.349588

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