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)
DataSelfIsolation_2_ <- read_excel("DataSelfIsolation (2).xlsx")
DataSelfIsolation_2_
## # A tibble: 30 x 8
##    Tanggal SelfIsolation retail_and_recreatio~ grocery_and_pha~ parks_percent_c~
##      <dbl>         <dbl>                 <dbl>            <dbl>            <dbl>
##  1       1          5124                   -15                7              -30
##  2       2          5178                   -17                8              -40
##  3       3          5402                   -15               10              -38
##  4       4          5414                   -13               10              -34
##  5       5          5756                   -15               13              -36
##  6       6          5966                   -19                5              -45
##  7       7          6261                   -15               10              -38
##  8       8          6057                   -18                6              -41
##  9       9          6120                   -16                6              -39
## 10      10          6991                   -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(DataSelfIsolation_2_)
##     Tanggal      SelfIsolation  
##  Min.   : 1.00   Min.   : 5124  
##  1st Qu.: 8.25   1st Qu.: 6155  
##  Median :15.50   Median :11398  
##  Mean   :15.50   Mean   :17035  
##  3rd Qu.:22.75   3rd Qu.:22443  
##  Max.   :30.00   Max.   :47829  
##  retail_and_recreation_percent_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(DataSelfIsolation_2_)

pairs(DataSelfIsolation_2_, lower.panel=NULL)

Visualisasi Data Menggunakan Fungsi Plot()

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

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

plot(DataSelfIsolation_2_$retail_and_recreation_percent_change_from_baseline+DataSelfIsolation_2_$grocery_and_pharmacy_percent_change_from_baseline+DataSelfIsolation_2_$parks_percent_change_from_baseline+DataSelfIsolation_2_$transit_stations_percent_change_from_baseline+DataSelfIsolation_2_$workplaces_percent_change_from_baseline+DataSelfIsolation_2_$residential_percent_change_from_baseline)

Korelasi AntarVariable

a.) Korelasi Variable Y dengan X1

cor(DataSelfIsolation_2_$SelfIsolation,DataSelfIsolation_2_$retail_and_recreation_percent_change_from_baseline)
## [1] -0.869818

b.) Korelasi Variable Y dengan X2

cor(DataSelfIsolation_2_$SelfIsolation,DataSelfIsolation_2_$
      grocery_and_pharmacy_percent_change_from_baseline)
## [1] -0.4253658

c.) Korelasi Variable Y dengan X3

cor(DataSelfIsolation_2_$SelfIsolation,DataSelfIsolation_2_$
      parks_percent_change_from_baseline)
## [1] -0.8095529

d.) Korelasi Variable Y dengan X4

cor(DataSelfIsolation_2_$SelfIsolation,DataSelfIsolation_2_$
      transit_stations_percent_change_from_baseline)
## [1] -0.824553

e.) Korelasi Variable Y dengan X5

cor(DataSelfIsolation_2_$SelfIsolation,DataSelfIsolation_2_$
      workplaces_percent_change_from_baseline)
## [1] -0.2837592

f.) Korelasi Variable Y dengan X6

cor(DataSelfIsolation_2_$SelfIsolation,DataSelfIsolation_2_$
      residential_percent_change_from_baseline)
## [1] 0.6341725

Permodelan Regresi Linier Berganda

model <- lm(DataSelfIsolation_2_$SelfIsolation ~ DataSelfIsolation_2_$Tanggal, data = DataSelfIsolation_2_)
summary(model)
## 
## Call:
## lm(formula = DataSelfIsolation_2_$SelfIsolation ~ DataSelfIsolation_2_$Tanggal, 
##     data = DataSelfIsolation_2_)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##  -6167  -4049  -2147   3910  10886 
## 
## Coefficients:
##                              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                   -4245.3     1986.0  -2.138   0.0414 *  
## DataSelfIsolation_2_$Tanggal   1372.9      111.9  12.273 8.75e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5303 on 28 degrees of freedom
## Multiple R-squared:  0.8432, Adjusted R-squared:  0.8376 
## F-statistic: 150.6 on 1 and 28 DF,  p-value: 8.751e-13

Rincian Model dengan Fungsi Summary()

Di atas merupakan rincian dari model yang telah dibuat.

Di posisi paling atas terdapat lm formula adalah DataSelfIsolation\(SelfIsolation ~ DataSelfIsolation\)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 = -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: DataSelfIsolation_2_$SelfIsolation
##                              Df     Sum Sq    Mean Sq F value    Pr(>F)    
## DataSelfIsolation_2_$Tanggal  1 4236471828 4236471828  150.62 8.751e-13 ***
## Residuals                    28  787554477   28126946                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Plot() Model dari Data Real dengan Data Prediksi

plot(DataSelfIsolation_2_$SelfIsolation ~ DataSelfIsolation_2_$Tanggal, data = DataSelfIsolation_2_, 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] 603.6337
BIC(model)
## [1] 607.8373

Nilai Predicted dan Visualnya

head(predict(model), n = 11)
##          1          2          3          4          5          6          7 
## -2872.3312 -1499.3888  -126.4464  1246.4960  2619.4383  3992.3807  5365.3231 
##          8          9         10         11 
##  6738.2655  8111.2079  9484.1502 10857.0926
plot(head(predict(model), n = 10))

Nilai Residuals

head(resid(model), n = 11)
##          1          2          3          4          5          6          7 
##  7996.3312  6677.3888  5528.4464  4167.5040  3136.5617  1973.6193   895.6769 
##          8          9         10         11 
##  -681.2655 -1991.2079 -2493.1502 -3107.0926
coef(model)
##                  (Intercept) DataSelfIsolation_2_$Tanggal 
##                    -4245.274                     1372.942

Tabel Data Residuals dan Data Predictied

DataSelfIsolation_2_$residuals <- model$residuals
DataSelfIsolation_2_$predicted <- model$fitted.values
DataSelfIsolation_2_
## # A tibble: 30 x 10
##    Tanggal SelfIsolation retail_and_recreatio~ grocery_and_pha~ parks_percent_c~
##      <dbl>         <dbl>                 <dbl>            <dbl>            <dbl>
##  1       1          5124                   -15                7              -30
##  2       2          5178                   -17                8              -40
##  3       3          5402                   -15               10              -38
##  4       4          5414                   -13               10              -34
##  5       5          5756                   -15               13              -36
##  6       6          5966                   -19                5              -45
##  7       7          6261                   -15               10              -38
##  8       8          6057                   -18                6              -41
##  9       9          6120                   -16                6              -39
## 10      10          6991                   -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=DataSelfIsolation_2_$Tanggal, y=DataSelfIsolation_2_$SelfIsolation, main="Tanggal ~ DataSelfIsolation")

boxplot(DataSelfIsolation_2_$SelfIsolation, main="DataSelfIsolation", boxplot.stats(DataSelfIsolation_2_$SelfIsolation)$out)

plot(density(DataSelfIsolation_2_$SelfIsolation), main="Google Mobility Index : DataSelfIsolation", ylab="Frequency")

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

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(DataSelfIsolation_2_$retail_and_recreation_percent_change_from_baseline,DataSelfIsolation_2_$SelfIsolation)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSelfIsolation_2_$retail_and_recreation_percent_change_from_baseline and DataSelfIsolation_2_$SelfIsolation
## t = -9.3289, df = 28, p-value = 4.359e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9365894 -0.7420995
## sample estimates:
##       cor 
## -0.869818

b.) Uji Korelasi Variable Y dengan X2

cor.test(DataSelfIsolation_2_$grocery_and_pharmacy_percent_change_from_baseline, DataSelfIsolation_2_$SelfIsolation)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSelfIsolation_2_$grocery_and_pharmacy_percent_change_from_baseline and DataSelfIsolation_2_$SelfIsolation
## t = -2.487, df = 28, p-value = 0.01911
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.68123788 -0.07687777
## sample estimates:
##        cor 
## -0.4253658

c.) Uji Korelasi Variable Y dengan X3

cor.test(DataSelfIsolation_2_$parks_percent_change_from_baseline, DataSelfIsolation_2_$SelfIsolation)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSelfIsolation_2_$parks_percent_change_from_baseline and DataSelfIsolation_2_$SelfIsolation
## t = -7.2971, df = 28, p-value = 6.05e-08
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9056755 -0.6342742
## sample estimates:
##        cor 
## -0.8095529

d.) Uji Korelasi Variable Y dengan X4

cor.test(DataSelfIsolation_2_$transit_stations_percent_change_from_baseline, DataSelfIsolation_2_$SelfIsolation)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSelfIsolation_2_$transit_stations_percent_change_from_baseline and DataSelfIsolation_2_$SelfIsolation
## t = -7.7116, df = 28, p-value = 2.118e-08
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9134668 -0.6604895
## sample estimates:
##       cor 
## -0.824553

e.) Uji Korelasi Variable Y dengan X5

cor.test(DataSelfIsolation_2_$workplaces_percent_change_from_baseline, DataSelfIsolation_2_$SelfIsolation)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSelfIsolation_2_$workplaces_percent_change_from_baseline and DataSelfIsolation_2_$SelfIsolation
## t = -1.5659, df = 28, p-value = 0.1286
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.58429601  0.08522228
## sample estimates:
##        cor 
## -0.2837592

f.) Uji Korelasi Variable Y dengan X6

cor.test(DataSelfIsolation_2_$residential_percent_change_from_baseline, DataSelfIsolation_2_$SelfIsolation)
## 
##  Pearson's product-moment correlation
## 
## data:  DataSelfIsolation_2_$residential_percent_change_from_baseline and DataSelfIsolation_2_$SelfIsolation
## t = 4.3401, df = 28, p-value = 0.0001678
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.3550145 0.8094943
## sample estimates:
##       cor 
## 0.6341725

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