Lembaga : Universitas Islam Negeri Maulana Malik Ibrahim Malang

Fakultas : Sains dan Teknologi

Program Studi : Teknik Informatika

Mata Kuliah : Linear Algebra (C)

NIM : 210605110045

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
Data_positif_covid <- read_excel("Data positif covid.xlsx")
Data_positif_covid
## # A tibble: 30 x 8
##    Tanggal Positif `retail and recreationper~` grocery_and_pha~ parks_percent_c~
##      <dbl>   <dbl>                       <dbl>            <dbl>            <dbl>
##  1       1  430578                         -15                7              -30
##  2       2  431179                         -17                8              -40
##  3       3  431893                         -15               10              -38
##  4       4  432799                         -13               10              -34
##  5       5  434116                         -15               13              -36
##  6       6  435135                         -19                5              -45
##  7       7  436332                         -15               10              -38
##  8       8  437087                         -18                6              -41
##  9       9  438458                         -16                6              -39
## 10      10  440554                         -16                7              -37
## # ... with 20 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(Data_positif_covid)
##     Tanggal         Positif      
##  Min.   : 1.00   Min.   :430578  
##  1st Qu.: 8.25   1st Qu.:437430  
##  Median :15.50   Median :456743  
##  Mean   :15.50   Mean   :469336  
##  3rd Qu.:22.75   3rd Qu.:492586  
##  Max.   :30.00   Max.   :543468  
##  retail and recreationpercent_change_from_baseline
##  Min.   :-38.0                                    
##  1st Qu.:-27.0                                    
##  Median :-20.5                                    
##  Mean   :-22.0                                    
##  3rd Qu.:-16.0                                    
##  Max.   :-13.0                                    
##  grocery_and_pharmacy_percent_change_from_baseline
##  Min.   :-7.000                                   
##  1st Qu.: 2.000                                   
##  Median : 5.000                                   
##  Mean   : 4.633                                   
##  3rd Qu.: 7.000                                   
##  Max.   :13.000                                   
##  parks_percent_change_from_baseline
##  Min.   :-64.00                    
##  1st Qu.:-47.75                    
##  Median :-42.00                    
##  Mean   :-43.50                    
##  3rd Qu.:-38.00                    
##  Max.   :-30.00                    
##  transit_stations_percent_change_from_baseline
##  Min.   :-48.00                               
##  1st Qu.:-40.75                               
##  Median :-32.00                               
##  Mean   :-34.23                               
##  3rd Qu.:-30.00                               
##  Max.   :-21.00                               
##  workplaces_percent_change_from_baseline
##  Min.   :-67.00                         
##  1st Qu.:-35.50                         
##  Median :-29.00                         
##  Mean   :-29.23                         
##  3rd Qu.:-23.75                         
##  Max.   :-11.00                         
##  residential_percent_change_from_baseline
##  Min.   : 5.00                           
##  1st Qu.: 9.25                           
##  Median :11.00                           
##  Mean   :11.07                           
##  3rd Qu.:13.00                           
##  Max.   :17.00

Membuat Matriks dengan Fungsi Pairs()

pairs(Data_positif_covid)

pairs(Data_positif_covid, lower.panel=NULL)

Visualisasi Data Menggunakan Fungsi Plot()

plot(Data_positif_covid$Positif ~ Data_positif_covid$Tanggal, data = Data_positif_covid)

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

plot(Data_positif_covid$`retail and recreationpercent_change_from_baseline`+Data_positif_covid$grocery_and_pharmacy_percent_change_from_baseline+Data_positif_covid$parks_percent_change_from_baseline+Data_positif_covid$transit_stations_percent_change_from_baseline+Data_positif_covid$workplaces_percent_change_from_baseline+Data_positif_covid$residential_percent_change_from_baseline, data = Data_positif_covid)
## 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(Data_positif_covid$Positif,Data_positif_covid$`retail and recreationpercent_change_from_baseline`)
## [1] -0.82655

b.) Korelasi Variable Y dengan X2

cor(Data_positif_covid$Positif,Data_positif_covid$
      grocery_and_pharmacy_percent_change_from_baseline)
## [1] -0.5018102

c.) Korelasi Variable Y dengan X3

cor(Data_positif_covid$Positif,Data_positif_covid$
      parks_percent_change_from_baseline)
## [1] -0.7576415

d.) Korelasi Variable Y dengan X4

cor(Data_positif_covid$Positif,Data_positif_covid$
      transit_stations_percent_change_from_baseline)
## [1] -0.7494059

e.) Korelasi Variable Y dengan X5

cor(Data_positif_covid$Positif,Data_positif_covid$
      workplaces_percent_change_from_baseline)
## [1] -0.2578519

f.) Korelasi Variable Y dengan X6

cor(Data_positif_covid$Positif,Data_positif_covid$
      residential_percent_change_from_baseline)
## [1] 0.594001

Permodelan Regresi Linier Berganda

model <- lm(Data_positif_covid$Positif ~ Data_positif_covid$Tanggal, data = Data_positif_covid)
summary(model)
## 
## Call:
## lm(formula = Data_positif_covid$Positif ~ Data_positif_covid$Tanggal, 
##     data = Data_positif_covid)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -16509 -11104  -5406   7980  74804 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                412149.0     6779.8  60.791  < 2e-16 ***
## Data_positif_covid$Tanggal   3689.5      381.9   9.661 2.05e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 18100 on 28 degrees of freedom
## Multiple R-squared:  0.7692, Adjusted R-squared:  0.761 
## F-statistic: 93.33 on 1 and 28 DF,  p-value: 2.05e-10

Rincian Model dengan Fungsi Summary()

Di atas merupakan rincian dari model yang telah dibuat.

Di posisi paling atas terdapat lm formula adalah Data_positif_covid\(POSITIF ~ Data_positif_covid\)Tanggal, data = Positif

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: Data_positif_covid$Positif
##                            Df     Sum Sq    Mean Sq F value   Pr(>F)    
## Data_positif_covid$Tanggal  1 3.0593e+10 3.0593e+10  93.332 2.05e-10 ***
## Residuals                  28 9.1781e+09 3.2779e+08                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Plot() Model dari Data Real dengan Data Prediksi

plot(Data_positif_covid$Positif ~ Data_positif_covid$Tanggal, data = Data_positif_covid, col = "pink", pch = 20, cex = 1.5, main = "Data Inflow Covid DKI Jakarta Juni 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 = "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] 677.3028
BIC(model)
## [1] 681.5064

Nilai Predicted dan Visualnya

head(predict(model), n = 11)
##        1        2        3        4        5        6        7        8 
## 415838.4 419527.9 423217.3 426906.8 430596.3 434285.7 437975.2 441664.6 
##        9       10       11 
## 445354.1 449043.6 452733.0
plot(head(predict(model), n = 10))

Nilai Residuals

head(resid(model), n = 11)
##         1         2         3         4         5         6         7         8 
## 14739.581 11651.120  8675.659  5892.198  3519.738   849.277 -1643.184 -4577.644 
##         9        10        11 
## -6896.105 -8489.566 -9886.027
coef(model)
##                (Intercept) Data_positif_covid$Tanggal 
##                 412148.959                   3689.461

Tabel Data Residuals dan Data Predictied

Data_positif_covid$residuals <- model$residuals
Data_positif_covid$predicted <- model$fitted.values
Data_positif_covid
## # A tibble: 30 x 10
##    Tanggal Positif `retail and recreationper~` grocery_and_pha~ parks_percent_c~
##      <dbl>   <dbl>                       <dbl>            <dbl>            <dbl>
##  1       1  430578                         -15                7              -30
##  2       2  431179                         -17                8              -40
##  3       3  431893                         -15               10              -38
##  4       4  432799                         -13               10              -34
##  5       5  434116                         -15               13              -36
##  6       6  435135                         -19                5              -45
##  7       7  436332                         -15               10              -38
##  8       8  437087                         -18                6              -41
##  9       9  438458                         -16                6              -39
## 10      10  440554                         -16                7              -37
## # ... with 20 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=Data_positif_covid$Tanggal, y=Data_positif_covid$Positif, main="Tanggal ~ Data_positif_covid")

boxplot(Data_positif_covid$Positif, main="Data_positif_covid", boxplot.stats(Data_positif_covid$Positif)$out)

plot(density(Data_positif_covid$Positif), main="Google Mobility Index : Data_positif_covid", ylab="Frequency")

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

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(Data_positif_covid$`retail and recreationpercent_change_from_baseline`, Data_positif_covid$Positif)
## 
##  Pearson's product-moment correlation
## 
## data:  Data_positif_covid$`retail and recreationpercent_change_from_baseline` and Data_positif_covid$Positif
## t = -7.7704, df = 28, p-value = 1.828e-08
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9144992 -0.6640101
## sample estimates:
##      cor 
## -0.82655

b.) Uji Korelasi Variable Y dengan X2

cor.test(Data_positif_covid$grocery_and_pharmacy_percent_change_from_baseline, Data_positif_covid$Positif)
## 
##  Pearson's product-moment correlation
## 
## data:  Data_positif_covid$grocery_and_pharmacy_percent_change_from_baseline and Data_positif_covid$Positif
## t = -3.0698, df = 28, p-value = 0.004723
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.7300890 -0.1727767
## sample estimates:
##        cor 
## -0.5018102

c.) Uji Korelasi Variable Y dengan X3

cor.test(Data_positif_covid$parks_percent_change_from_baseline, Data_positif_covid$Positif)
## 
##  Pearson's product-moment correlation
## 
## data:  Data_positif_covid$parks_percent_change_from_baseline and Data_positif_covid$Positif
## t = -6.1425, df = 28, p-value = 1.248e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.8782014 -0.5465580
## sample estimates:
##        cor 
## -0.7576415

d.) Uji Korelasi Variable Y dengan X4

cor.test(Data_positif_covid$transit_stations_percent_change_from_baseline, Data_positif_covid$Positif)
## 
##  Pearson's product-moment correlation
## 
## data:  Data_positif_covid$transit_stations_percent_change_from_baseline and Data_positif_covid$Positif
## t = -5.9892, df = 28, p-value = 1.884e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.8737683 -0.5330551
## sample estimates:
##        cor 
## -0.7494059

e.) Uji Korelasi Variable Y dengan X5

cor.test(Data_positif_covid$workplaces_percent_change_from_baseline, Data_positif_covid$Positif)
## 
##  Pearson's product-moment correlation
## 
## data:  Data_positif_covid$workplaces_percent_change_from_baseline and Data_positif_covid$Positif
## t = -1.4122, df = 28, p-value = 0.1689
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.5655808  0.1129059
## sample estimates:
##        cor 
## -0.2578519

f.) Uji Korelasi Variable Y dengan X6

cor.test(Data_positif_covid$residential_percent_change_from_baseline, Data_positif_covid$Positif)
## 
##  Pearson's product-moment correlation
## 
## data:  Data_positif_covid$residential_percent_change_from_baseline and Data_positif_covid$Positif
## t = 3.9071, df = 28, p-value = 0.0005387
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.2973688 0.7860544
## sample estimates:
##      cor 
## 0.594001

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