Lembaga : UIN Maulana Malik Ibrahim, Teknik Informatika

Regresi Linier Berganda

Regresi linier berganda merupakan model persamaan yang menjelaskan hubungan satu variabel tak bebas/ response (Y) dengan dua atau lebih variabel bebas/ predictor (X1, X2,…Xn).

Mengimport Data Dari Excel

Pengambilan data Covid 19 Pasien Sembuh secara online pada web site Riwayat File Covid-19 DKI Jakarta di alamat URL https://riwayat-file-covid-19-dki-jakarta-jakartagis.hub.arcgis.com/, dan menggunakan data Google Mobility Index yang diambil secara online pada web site Laporan Mobilitas Masyarakat Selama Pandemi Covid-19 di alamat URL https://www.google.com/covid19/mobility/?hl=id.

Jenis file yang digunakan adalah file Excel. Sehingga untuk menampilkan data dalam Rstudio dengan menggunakan package {readxl} dengan fungsi read_excel untuk import data dari file Excel. Argumen path = adalah lokasi dan nama file Excel yang akan digunakan.

library(readxl)
## Warning: package 'readxl' was built under R version 4.1.2
dataselfIsolation<- read_excel(path = "selfIsolation.xlsx")
dataselfIsolation
## # A tibble: 30 x 8
##    Tanggal             `Self Isolation` retail_and_recreation_~ grocery_and_pha~
##    <dttm>                         <dbl>                   <dbl>            <dbl>
##  1 2020-01-11 00:00:00             6935                     -38              -15
##  2 2020-01-12 00:00:00             7312                     -30               -7
##  3 2020-01-13 00:00:00             6844                     -31               -9
##  4 2020-01-14 00:00:00             6665                     -31              -12
##  5 2020-01-15 00:00:00             6260                     -33              -14
##  6 2020-01-16 00:00:00             6050                     -28               -9
##  7 2020-01-17 00:00:00             6159                     -29               -8
##  8 2020-01-18 00:00:00             6338                     -33              -13
##  9 2020-01-19 00:00:00             6050                     -30              -11
## 10 2020-01-20 00:00:00             5724                     -32              -12
## # ... 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>

Mensummary Data

Summary data merupakan cara untuk melihat nilai minimum, nilai kuartil 1, nilai kuartil 2 atau median, nilai kuartil 3, dan nilai maksimum. dari setiap kolomnya.

summary(dataselfIsolation)
##     Tanggal                    Self Isolation
##  Min.   :2020-01-11 00:00:00   Min.   :4355  
##  1st Qu.:2020-01-18 06:00:00   1st Qu.:4971  
##  Median :2020-01-25 12:00:00   Median :5800  
##  Mean   :2020-01-25 12:00:00   Mean   :5818  
##  3rd Qu.:2020-02-01 18:00:00   3rd Qu.:6574  
##  Max.   :2020-02-09 00:00:00   Max.   :7823  
##  retail_and_recreation_percent_change_from_baseline
##  Min.   :-38.0                                     
##  1st Qu.:-32.0                                     
##  Median :-30.0                                     
##  Mean   :-29.8                                     
##  3rd Qu.:-28.0                                     
##  Max.   :-23.0                                     
##  grocery_and_pharmacy_percent_change_from_baseline
##  Min.   :-18.00                                   
##  1st Qu.:-12.75                                   
##  Median :-11.00                                   
##  Mean   :-10.27                                   
##  3rd Qu.: -9.00                                   
##  Max.   :  1.00                                   
##  parks_percent_change_from_baseline
##  Min.   :-57.00                    
##  1st Qu.:-54.00                    
##  Median :-51.50                    
##  Mean   :-50.53                    
##  3rd Qu.:-47.25                    
##  Max.   :-41.00                    
##  transit_stations_percent_change_from_baseline
##  Min.   :-42.00                               
##  1st Qu.:-40.00                               
##  Median :-39.00                               
##  Mean   :-36.83                               
##  3rd Qu.:-32.00                               
##  Max.   :-27.00                               
##  workplaces_percent_change_from_baseline
##  Min.   :-33.00                         
##  1st Qu.:-31.00                         
##  Median :-31.00                         
##  Mean   :-26.50                         
##  3rd Qu.:-17.25                         
##  Max.   :-13.00                         
##  residential_percent_change_from_baseline
##  Min.   : 6.00                           
##  1st Qu.: 8.00                           
##  Median :13.00                           
##  Mean   :11.13                           
##  3rd Qu.:13.00                           
##  Max.   :14.00

Membuat Data Dengan Fungsi Pairs

Dalam membuat grafik pairs yaitu dengan memasukkan datasembuh ke dalam sintaks fungsi pairs().

pairs(dataselfIsolation)

Memvisualisasikan Data Dengan Fungsi Plot

Memasukkan data ke dalam fungsi plot(), untuk menampilkan 6 data dengan data X yang berbeda beda(X1, X2, X3, X4, X5, X6). Dan akan menghasilkan 6 grafik dari fungsi plot().

plot(dataselfIsolation$`Self Isolation` ~ dataselfIsolation$retail_and_recreation_percent_change_from_baseline, data = dataselfIsolation)

plot(dataselfIsolation$`Self Isolation` ~ dataselfIsolation$grocery_and_pharmacy_percent_change_from_baseline, data = dataselfIsolation)

plot(dataselfIsolation$`Self Isolation` ~ dataselfIsolation$parks_percent_change_from_baseline, data = dataselfIsolation)

plot(dataselfIsolation$`Self Isolation` ~ dataselfIsolation$transit_stations_percent_change_from_baseline, data = dataselfIsolation)

plot(dataselfIsolation$`Self Isolation` ~ dataselfIsolation$workplaces_percent_change_from_baseline, data = dataselfIsolation)

plot(dataselfIsolation$`Self Isolation` ~ dataselfIsolation$residential_percent_change_from_baseline, data = dataselfIsolation)

Menghitung Korelasi Antara Variabel Y dan X

Korelasi merupakan ukuran dari seberapa dekat dua variabel berubah dalam hubungan satu sama lain atau digunakan untuk mengetahui ada atau tidaknya hubungan antar linier atau variabelnya. Atau Untuk menghitung nilai koefisien Korelasi antara dua variabel kontinyu pada r dapat menggunakan fungsi cor( ).

Kolerasi Y dan X1

cor(dataselfIsolation$`Self Isolation`, dataselfIsolation$retail_and_recreation_percent_change_from_baseline)
## [1] 0.1497092

Berdasarkan hasil perhitungan diperoleh nilai Korelasi sebesar 0.49

Korelasi Y dan X2

cor(dataselfIsolation$`Self Isolation`, dataselfIsolation$grocery_and_pharmacy_percent_change_from_baseline)
## [1] 0.4373616

Berdasarkan hasil perhitungan diperoleh nilai Korelasi sebesar 0.33

Korelasi Y dan X3

cor(dataselfIsolation$`Self Isolation`, dataselfIsolation$parks_percent_change_from_baseline)
## [1] 0.0006402947

Berdasarkan hasil perhitungan diperoleh nilai Korelasi sebesar 0.44

Korelasi Y dan X4

cor(dataselfIsolation$`Self Isolation`, dataselfIsolation$transit_stations_percent_change_from_baseline)
## [1] 0.3064867

Berdasarkan hasil perhitungan diperoleh nilai Korelasi sebesar 0.13

Korelasi Y dan X5

cor(dataselfIsolation$`Self Isolation`, dataselfIsolation$workplaces_percent_change_from_baseline)
## [1] 0.1679622

Berdasarkan hasil perhitungan diperoleh nilai Korelasi sebesar 0.10

Korelasi Y dan X6

cor(dataselfIsolation$`Self Isolation`, dataselfIsolation$residential_percent_change_from_baseline)
## [1] -0.2904707

Berdasarkan hasil perhitungan diperoleh nilai Korelasi sebesar -0.12

Membuat Model Regresi Linier Berganda

model <- lm(dataselfIsolation$`Self Isolation` ~ dataselfIsolation$Tanggal, data = dataselfIsolation)

Lalu membuat summary() untuk meringkas data tersebut yang sudah diberi nama model.

summary (model)
## 
## Call:
## lm(formula = dataselfIsolation$`Self Isolation` ~ dataselfIsolation$Tanggal, 
##     data = dataselfIsolation)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1457.68  -851.22    -8.86   724.78  2059.94 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)
## (Intercept)                7.560e+04  3.847e+05   0.197    0.846
## dataselfIsolation$Tanggal -4.417e-05  2.435e-04  -0.181    0.857
## 
## Residual standard error: 997.5 on 28 degrees of freedom
## Multiple R-squared:  0.001174,   Adjusted R-squared:  -0.0345 
## F-statistic: 0.0329 on 1 and 28 DF,  p-value: 0.8574

Dari summary(model) tersebut terdapat lm formula untuk memanggil data tersebut. Lalu pada residuals di jabarkan nilai minimal, kuartil 1, median, kuartil 3, dan maksimal. Terdapat nilai Multiple R-squared sebesar 0.9997 dan nilai Adjusted R-squared sebesar 0.9997 .

Nilai min sebesar -314.38

Nilai Q1 sebesar -93.82

Nilai median sebesar -36.65

Nilai Q3 sebesar 161.48

Nilai max sebesar 216.00

Menguji Data Dengan Fungsi Anova()

Uji Anova adalah singkatan dari Analysis of Variance, yaitu merupakan salah satu metode yang digunakan untuk mengetahui perbedaan rerata data antara dua kelompok atau lebih.

Table anova dalam hasil regresi linier yaitu dalam perhitungan anova digunakan untuk membandingkan nilai mea square dimana hasilnya ialan untuk mnenilai apakah model prediksi linier tidak berbeda nyata dengan nilai koefisien estimasi serta standar error.

anova (model)
## Analysis of Variance Table
## 
## Response: dataselfIsolation$`Self Isolation`
##                           Df   Sum Sq Mean Sq F value Pr(>F)
## dataselfIsolation$Tanggal  1    32732   32732  0.0329 0.8574
## Residuals                 28 27858043  994930

Grafik Plot dan Abline

plot (dataselfIsolation$`Self Isolation` ~ dataselfIsolation$Tanggal, data = dataselfIsolation, col = "pink", pch = 20, cex =         1.5, main = "Data Self Isolation Covid di DKI Jakarta Pada November 2020")
abline(model)

plot(cooks.distance(model), pch = 16, col = "purple")

plot (model)

AIC dan BIC

AIC dan BIC banyak digunakan dalam kriteria pemilihan model. AIC berarti kriteria data Akaike dan kriteria data BIC Bayesian. Meskipun kedua istilah ini merujuk pada pemilihan model, mereka tidak sama. Mungkin ada perbedaan antara dua pendekatan untuk pemilihan model.

AIC dapat disebut kriteria kepatuhan dengan model statistik yang diprediksi. BIC adalah jenis pemilihan model antara kelas model parametrik dengan parameter yang berbeda. Kriteria data Akaike biasanya mencari model yang tidak diketahui dengan realitas dimensional. Di sisi lain, kriteria data Bayesian hanya dapat ditemukan dalam model nyata.

AIC (model)
## [1] 503.3794
BIC (model)
## [1] 507.583

Data Dengan Predict dan Visualisasi Grafik Dengan Plot(Head(Predict))

head(predict(model), n = 16)
##        1        2        3        4        5        6        7        8 
## 5873.735 5869.919 5866.103 5862.287 5858.471 5854.654 5850.838 5847.022 
##        9       10       11       12       13       14       15       16 
## 5843.206 5839.389 5835.573 5831.757 5827.941 5824.124 5820.308 5816.492
plot (head(predict(model), n = 30))

Data Dengan Residuals

head (resid(model), n = 16)
##          1          2          3          4          5          6          7 
##  1061.2645  1442.0808   977.8970   802.7132   401.5295   195.3457   308.1620 
##          8          9         10         11         12         13         14 
##   490.9782   206.7944  -115.3893  -674.5731  -953.7568  -799.9406 -1114.1244 
##         15         16 
##  -868.3081 -1396.4919
coef(model)
##               (Intercept) dataselfIsolation$Tanggal 
##              7.560408e+04             -4.416945e-05

Menampilkan Nilai Residuals dan Predict Pada Tabel

dataselfIsolation$residuals <- model$residuals
dataselfIsolation$predict <- model$fitted.values
dataselfIsolation
## # A tibble: 30 x 10
##    Tanggal             `Self Isolation` retail_and_recreation_~ grocery_and_pha~
##    <dttm>                         <dbl>                   <dbl>            <dbl>
##  1 2020-01-11 00:00:00             6935                     -38              -15
##  2 2020-01-12 00:00:00             7312                     -30               -7
##  3 2020-01-13 00:00:00             6844                     -31               -9
##  4 2020-01-14 00:00:00             6665                     -31              -12
##  5 2020-01-15 00:00:00             6260                     -33              -14
##  6 2020-01-16 00:00:00             6050                     -28               -9
##  7 2020-01-17 00:00:00             6159                     -29               -8
##  8 2020-01-18 00:00:00             6338                     -33              -13
##  9 2020-01-19 00:00:00             6050                     -30              -11
## 10 2020-01-20 00:00:00             5724                     -32              -12
## # ... 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>,
## #   predict <dbl>

Membuat Grafik Dengan Scatter.Smooth

scatter.smooth(x=dataselfIsolation$Tanggal, y=dataselfIsolation$Sembuh, main = "Grafik Scatter.Smooth")
## Warning: Unknown or uninitialised column: `Sembuh`.

Membuat Diagram Dengan Boxplot

boxplot(dataselfIsolation$`Self Isolation`, main = "Grafik Self Isolation" , boxplot.stats(dataselfIsolation$POSITIF)$out)
## Warning: Unknown or uninitialised column: `POSITIF`.

Membuat Grafik Dengan Plot

plot(density(dataselfIsolation$`Self Isolation`), main = "Grafik Mobility Index Self Isolation", ylab = "frequency")

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

Menghitung Korelasi Antara Variabel X dan Y

Untuk menghitung nilai koefisien Korelasi antara dua variabel kontinyu pada r dapat menggunakan fungsi cor( ), sedangkan untuk menguji apakah nilai tersebut signifikan dapat menggunakan fungsi cor.test( ).

Korelasi Y dan X1

cor.test(dataselfIsolation$retail_and_recreation_percent_change_from_baseline, dataselfIsolation$`Self Isolation`)
## 
##  Pearson's product-moment correlation
## 
## data:  dataselfIsolation$retail_and_recreation_percent_change_from_baseline and dataselfIsolation$`Self Isolation`
## t = 0.80122, df = 28, p-value = 0.4298
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.2225642  0.4838800
## sample estimates:
##       cor 
## 0.1497092

Berdasarkan hasil uji diperoleh nilai p-value sebesar 0.005459 atau > 5% menunjukkan nilai korelasi antara variabel data X1 dan Y.

Korelasi Y dan X2

cor.test(dataselfIsolation$grocery_and_pharmacy_percent_change_from_baseline, dataselfIsolation$`Self Isolation`)
## 
##  Pearson's product-moment correlation
## 
## data:  dataselfIsolation$grocery_and_pharmacy_percent_change_from_baseline and dataselfIsolation$`Self Isolation`
## t = 2.5735, df = 28, p-value = 0.01565
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.09151166 0.68905746
## sample estimates:
##       cor 
## 0.4373616

Berdasarkan hasil uji diperoleh nilai p-value sebesar 0.07211 atau > 5% menunjukkan nilai korelasi antara variabel data X2 dan Y.

Korelasi Y dan X3

cor.test(dataselfIsolation$parks_percent_change_from_baseline, dataselfIsolation$`Self Isolation`)
## 
##  Pearson's product-moment correlation
## 
## data:  dataselfIsolation$parks_percent_change_from_baseline and dataselfIsolation$`Self Isolation`
## t = 0.0033881, df = 28, p-value = 0.9973
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.3597119  0.3608263
## sample estimates:
##          cor 
## 0.0006402947

Berdasarkan hasil uji diperoleh nilai p-value sebesar 0.01486 atau > 5% menunjukkan nilai korelasi antara variabel data X3 dan Y.

Korelasi Y dan X4

cor.test(dataselfIsolation$transit_stations_percent_change_from_baseline, dataselfIsolation$`Self Isolation`)
## 
##  Pearson's product-moment correlation
## 
## data:  dataselfIsolation$transit_stations_percent_change_from_baseline and dataselfIsolation$`Self Isolation`
## t = 1.7038, df = 28, p-value = 0.0995
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.06045816  0.60045506
## sample estimates:
##       cor 
## 0.3064867

Berdasarkan hasil uji diperoleh nilai p-value sebesar 0.4694 atau > 5% menunjukkan nilai korelasi antara variabel data X4 dan Y.

Korelasi Y dan X5

cor.test(dataselfIsolation$workplaces_percent_change_from_baseline, dataselfIsolation$`Self Isolation`)
## 
##  Pearson's product-moment correlation
## 
## data:  dataselfIsolation$workplaces_percent_change_from_baseline and dataselfIsolation$`Self Isolation`
## t = 0.90158, df = 28, p-value = 0.375
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.2046934  0.4980911
## sample estimates:
##       cor 
## 0.1679622

Berdasarkan hasil uji diperoleh nilai p-value sebesar 0.5878 atau > 5% menunjukkan nilai korelasi antara variabel data X5 dan Y.

Korelasi Y dan X6

cor.test(dataselfIsolation$residential_percent_change_from_baseline, dataselfIsolation$`Self Isolation`)
## 
##  Pearson's product-moment correlation
## 
## data:  dataselfIsolation$residential_percent_change_from_baseline and dataselfIsolation$`Self Isolation`
## t = -1.6063, df = 28, p-value = 0.1194
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.58909272  0.07795653
## sample estimates:
##        cor 
## -0.2904707

Berdasarkan hasil uji diperoleh nilai p-value sebesar 0.4944 atau > 5% menunjukkan nilai korelasi antara variabel data X6 dan Y.