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

Mata Kuliah : Linear Algebra

Prodi : Teknik Informatika

Lembaga : Universitas Islam Negeri Maulana Malik Ibrahim Malang”

0.1 __ Pengertian Regresi Linear__

Regresi Linear Berganda adalah model regresi linear dengan melibatkan lebih dari satu variable bebas atau predictor.Dengan Y adalah variabel bebas, dan X adalah variabel-variabel bebas, a adalah konstanta (intersept) dan b adalah koefisien regresi pada masing-masing variabel bebas. Berikut Regresi Linear Berganda Menggunakan Data Covid Pasien Sembuh di DKI Jakarta dan Google Mobility Index pada Bulan Agustus 2020 :

0.2 __ Data Penduduk Sembuh Covid-19 Beserta Google Mobility index DKI Jakarta pada Bulan Agustus 2020__

library(readxl)
Data<- read_excel(path = "C:/Users/DELL LATITUDE 7280/Documents/KULIAH/SEMESTER 2/LINEAR ALGEBRA/Regresi Liniear/Data Sembuh Agustus 2020.xlsx")
Data
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.1.3
x <- Data$Sembuh
retail <- Data$retail_and_recreation_percent_change_from_baseline
grocery <- Data$grocery_and_pharmacy_percent_change_from_baseline
park <- Data$parks_percent_change_from_baseline
station <- Data$transit_stations_percent_change_from_baseline
workplace <- Data$workplaces_percent_change_from_baseline
residental <- Data$residential_percent_change_from_baseline
df <- data.frame(x, retail, grocery, park, station, workplace,residental  )

# melt the data to a long format
df2 <- melt(data = df, id.vars = "x")

# plot, using the aesthetics argument 'colour'
ggplot(data = df2, aes(x = x, y = value, colour = variable))+
  geom_point() +
  geom_line() + 
  theme(legend.justification = "top") +
  labs(title = "Google Mobility Index", 
         subtitle = "Provinsi DKI Jakarta Indonesia Bulan Agustus 2020", 
         y = "Mobility", x = "Data Sembuh") +
theme(axis.text.x = element_text(angle = -90))

0.3 __ Summary() dari Data Sembuh__

summary(Data)
##     Tanggal                        Sembuh     
##  Min.   :2020-08-01 00:00:00   Min.   :13887  
##  1st Qu.:2020-08-08 12:00:00   1st Qu.:15989  
##  Median :2020-08-16 00:00:00   Median :19708  
##  Mean   :2020-08-16 00:00:00   Mean   :20556  
##  3rd Qu.:2020-08-23 12:00:00   3rd Qu.:24515  
##  Max.   :2020-08-31 00:00:00   Max.   :30538  
##  retail_and_recreation_percent_change_from_baseline
##  Min.   :-37.00                                    
##  1st Qu.:-32.00                                    
##  Median :-27.00                                    
##  Mean   :-28.32                                    
##  3rd Qu.:-26.00                                    
##  Max.   :-21.00                                    
##  grocery_and_pharmacy_percent_change_from_baseline
##  Min.   :-19.000                                  
##  1st Qu.:-14.000                                  
##  Median : -9.000                                  
##  Mean   : -9.871                                  
##  3rd Qu.: -7.000                                  
##  Max.   : -2.000                                  
##  parks_percent_change_from_baseline
##  Min.   :-71.00                    
##  1st Qu.:-65.50                    
##  Median :-62.00                    
##  Mean   :-62.06                    
##  3rd Qu.:-59.00                    
##  Max.   :-49.00                    
##  transit_stations_percent_change_from_baseline
##  Min.   :-57.00                               
##  1st Qu.:-42.00                               
##  Median :-41.00                               
##  Mean   :-40.84                               
##  3rd Qu.:-38.50                               
##  Max.   :-32.00                               
##  workplaces_percent_change_from_baseline
##  Min.   :-70.0                          
##  1st Qu.:-33.0                          
##  Median :-31.0                          
##  Mean   :-30.1                          
##  3rd Qu.:-20.0                          
##  Max.   :-12.0                          
##  residential_percent_change_from_baseline
##  Min.   : 7.00                           
##  1st Qu.: 9.00                           
##  Median :14.00                           
##  Mean   :12.58                           
##  3rd Qu.:14.00                           
##  Max.   :21.00

0.4 __ Matriks Data Sembuh__

pairs(Data)

0.5 __ Visualisasi Data Sembuh__

plot(Data$Sembuh ~ Data$Tanggal, data = Data)

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

plot(Data$Sembuh, Data$retail_and_recreation_percent_change_from_baseline+Data$grocery_and_pharmacy_percent_change_from_baseline+Data$parks_percent_change_from_baseline+Data$transit_stations_percent_change_from_baseline+Data$workplaces_percent_change_from_baseline+Data$residential_percent_change_from_baseline, data = Data)
## 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

0.6 __ Korelasi Antar Variable__

0.6.1 __ Korelasi Variable Y dengan X1__

cor(Data$Sembuh,Data$retail_and_recreation_percent_change_from_baseline)
## [1] 0.3471107

0.6.2 __ Korelasi Variable Y dengan X2__

cor(Data$Sembuh,Data$grocery_and_pharmacy_percent_change_from_baseline)
## [1] 0.2403645

0.6.3 __ Korelasi Variable Y dengan X3__

cor(Data$Sembuh,Data$parks_percent_change_from_baseline)
## [1] 0.5663378

0.6.4 __ Korelasi Variable Y dengan X4__

cor(Data$Sembuh,Data$transit_stations_percent_change_from_baseline)
## [1] 0.1327249

0.6.5 __ Korelasi Variable Y dengan X5__

cor(Data$Sembuh,Data$workplaces_percent_change_from_baseline)
## [1] 0.1139768

0.6.6 __ Korelasi Variable Y dengan X6__

cor(Data$Sembuh,Data$residential_percent_change_from_baseline)
## [1] -0.01385342

0.7 __ Permodelan Regresi Linier Berganda__

model <- lm(Data$Sembuh ~ Data$Tanggal, data = Data)
summary(model)
## 
## Call:
## lm(formula = Data$Sembuh ~ Data$Tanggal, data = Data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##  -1242   -984   -183   1003   2074 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -1.076e+07  4.091e+05  -26.29   <2e-16 ***
## Data$Tanggal  6.746e-03  2.561e-04   26.34   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1102 on 29 degrees of freedom
## Multiple R-squared:  0.9599, Adjusted R-squared:  0.9585 
## F-statistic: 693.8 on 1 and 29 DF,  p-value: < 2.2e-16

Di atas merupakan rincian dari model yang telah dibuat.

Di posisi paling atas terdapat lm formula adalah Data\(Sembuh ~ Data\)Tanggal, data = DataSembuh

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

Nilai maximum = 2074

Nilai median = -183

Nilai quartil 1 = -984

Nilai quartil 3 = 1003

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

0.8 __ 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)

0.9 __ Visualisasi Model Dari Data Real dengan Data Prediksi__

plot(Data$Sembuh ~ Data$Tanggal, data = Data, col = "skyblue", pch = 20, cex = 1.5, main = "Data Inflow Covid DKI Jakarta Agustus 2020 dan Google Mobility Index")
abline(model)

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

plot(cooks.distance(model), pch = 16, col = "gold") #Plot the Cooks Distances.

plot(model)

0.10 __ Penggunaan AIC dan BIC__

AIC(model)
## [1] 526.2057
BIC(model)
## [1] 530.5076

0.11 __ Predicted dan Residuals__

0.11.1 __ Nilai Predicted dan Residuals__

head(predict(model), n = 11)
##        1        2        3        4        5        6        7        8 
## 11813.09 12395.93 12978.78 13561.62 14144.46 14727.31 15310.15 15892.99 
##        9       10       11 
## 16475.84 17058.68 17641.52
plot(head(predict(model), n = 10))

head(resid(model), n = 11)
##         1         2         3         4         5         6         7         8 
## 2073.9113 1631.0677 1186.2242  819.3806  615.5371  278.6935 -109.1500 -182.9935 
##         9        10        11 
## -207.8371 -612.6806 -714.5242
coef(model)
##   (Intercept)  Data$Tanggal 
## -1.075622e+07  6.745874e-03

0.11.2 __ Tabel Data Predicted dan Data Residuals__

Data$residuals <- model$residuals
Data$predicted <- model$fitted.values
Data

0.12 __ Visualisasi Data Menggunakan Scatter.Smooth, Boxplot dan Plot__

scatter.smooth(x=Data$Tanggal, y=Data$Sembuh, main="Tanggal ~ Sembuh")

boxplot(Data$Sembuh, main="Data Sembuh", boxplot.stats(Data$Sembuh)$out)

plot(density(Data$Sembuh), main="Google Mobility Index : Data Sembuh", ylab="Frequency")

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

0.13 __ Uji Korelasi Antar Variable__

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. ### __ Uji Korelasi Variable Y dengan X1__

cor.test(Data$retail_and_recreation_percent_change_from_baseline, Data$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$retail_and_recreation_percent_change_from_baseline and Data$Sembuh
## t = 1.9932, df = 29, p-value = 0.05573
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.008243346  0.624624899
## sample estimates:
##       cor 
## 0.3471107

0.13.1 __ Uji Korelasi Variable Y dengan X2__

cor.test(Data$grocery_and_pharmacy_percent_change_from_baseline, Data$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$grocery_and_pharmacy_percent_change_from_baseline and Data$Sembuh
## t = 1.3335, df = 29, p-value = 0.1927
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.1245868  0.5480286
## sample estimates:
##       cor 
## 0.2403645

0.13.2 __ Uji Korelasi Variable Y dengan X3__

cor.test(Data$parks_percent_change_from_baseline, Data$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$parks_percent_change_from_baseline and Data$Sembuh
## t = 3.7005, df = 29, p-value = 0.0008964
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.2652212 0.7667995
## sample estimates:
##       cor 
## 0.5663378

0.13.3 __ Uji Korelasi Variable Y dengan X4__

cor.test(Data$transit_stations_percent_change_from_baseline, Data$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$transit_stations_percent_change_from_baseline and Data$Sembuh
## t = 0.72113, df = 29, p-value = 0.4766
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.2325520  0.4651874
## sample estimates:
##       cor 
## 0.1327249

0.13.4 __ Uji Korelasi Variable Y dengan X5__

cor.test(Data$workplaces_percent_change_from_baseline, Data$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$workplaces_percent_change_from_baseline and Data$Sembuh
## t = 0.61781, df = 29, p-value = 0.5415
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.2504793  0.4501375
## sample estimates:
##       cor 
## 0.1139768

0.13.5 __ Uji Korelasi Variable Y dengan X6__

cor.test(Data$residential_percent_change_from_baseline, Data$Sembuh)
## 
##  Pearson's product-moment correlation
## 
## data:  Data$residential_percent_change_from_baseline and Data$Sembuh
## t = -0.07461, df = 29, p-value = 0.941
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.3663950  0.3421663
## sample estimates:
##         cor 
## -0.01385342