Dosen Pengampu : Prof. Dr. Suhartono, Mkom

UIN Maulana Malik Ibrahim Malang

library(readxl)
#baca data excel
mobindexjkt <- read_excel("mobindex_jkt.xlsx")
## Warning in strptime(x, format, tz = tz): unable to identify current timezone 'C':
## please set environment variable 'TZ'
covidjkt <- read_excel("c19data_jkt.xlsx")
Datagab <- read_excel("Datagab.xlsx")
datagab2 <- read_excel("datagab2.xlsx")
retail <- mobindexjkt$retail_and_recreation_percent_change_from_baseline
grocery <- mobindexjkt$grocery_and_pharmacy_percent_change_from_baseline
park <- mobindexjkt$parks_percent_change_from_baseline
station <- mobindexjkt$transit_stations_percent_change_from_baseline
workplace <- mobindexjkt$workplaces_percent_change_from_baseline
residental <- mobindexjkt$residential_percent_change_from_baseline
date <- mobindexjkt$date
tanggal <- Datagab$Tanggal
positif <- Datagab$Positif
park <- Datagab$Park
Positif <- covidjkt$Positif
Dirawat <- covidjkt$Dirawat
Sembuh <- covidjkt$Sembuh
Meninggal <- covidjkt$Meninggal
Isolasi_Mandiri <- covidjkt$SelfIsolation
Positif <- datagab2$positif
Retail <- datagab2$retail_and_recreation_percent_change_from_baseline
Grocery <- datagab2$grocery_and_pharmacy_percent_change_from_baseline
Park <- datagab2$parks_percent_change_from_baseline
Station <- datagab2$transit_stations_percent_change_from_baseline
Workplace <- datagab2$workplaces_percent_change_from_baseline
Residental <- datagab2$residential_percent_change_from_baseline

#Visualisasi Data Menggunakan Fungsi plot(). #Fungsi plot() merupakan fungsi umum yang digunakan untuk membuat pola pada R.

#korelasi kasus Positif vs retail
cor(Positif,retail)
## [1] 0.25192
#korelasi kasus Positif vs grocery
cor(Positif,grocery)
## [1] 0.02819681
#korelasi kasus Positif vs park
cor(Positif,park)
## [1] 0.3573231
#korelasi kasus Positif vs station
cor(Positif,station)
## [1] 0.3369683
#korelasi kasus Positif vs workplace
cor(Positif,workplace)
## [1] 0.2926955
#korelasi kasus Positif vs residental
cor(Positif,residental)
## [1] -0.1997964
summary(covidjkt)
##     Tanggal                       Positif          Dirawat     
##  Min.   :2021-11-01 00:00:00   Min.   :861623   Min.   :168.0  
##  1st Qu.:2021-11-08 06:00:00   1st Qu.:862300   1st Qu.:182.2  
##  Median :2021-11-15 12:00:00   Median :862865   Median :198.0  
##  Mean   :2021-11-15 12:00:00   Mean   :862897   Mean   :204.5  
##  3rd Qu.:2021-11-22 18:00:00   3rd Qu.:863534   3rd Qu.:211.2  
##  Max.   :2021-11-30 00:00:00   Max.   :863947   Max.   :273.0  
##      Sembuh         Meninggal     SelfIsolation  
##  Min.   :847066   Min.   :13562   Min.   :252.0  
##  1st Qu.:847922   1st Qu.:13566   1st Qu.:294.5  
##  Median :848657   Median :13569   Median :462.0  
##  Mean   :848654   Mean   :13570   Mean   :468.7  
##  3rd Qu.:849480   3rd Qu.:13574   3rd Qu.:642.8  
##  Max.   :849929   Max.   :13576   Max.   :802.0
#dataframe KASUS POSITIF
dataku <- data.frame(date, retail, grocery, park, station, workplace, residental)
library(ggplot2)
library(reshape2)

dataku1 <- melt(data = dataku, id.vars = "date")
ggplot(data = dataku1, aes(x = date, y = value, colour = variable))+
  geom_point() +
  geom_line() + 
  theme(legend.justification = "top") +
  labs(title = "Grafik Mobility Index", 
       subtitle = "Propinsi DKI Jakarta Indonesia November 2021", 
       y = "Index Mobility", x = "Tanggal") +
  theme(axis.text.x = element_text(angle = -90))

head(datagab2)
## # A tibble: 6 x 8
##   date                positif retail_and_recr~ grocery_and_pha~ parks_percent_c~
##   <dttm>                <dbl>            <dbl>            <dbl>            <dbl>
## 1 2021-11-01 00:00:00  861623              -16                5              -33
## 2 2021-11-02 00:00:00  861700              -12               12              -29
## 3 2021-11-03 00:00:00  861832              -12                9              -26
## 4 2021-11-04 00:00:00  861943              -12                8              -25
## 5 2021-11-05 00:00:00  862061               -6               12              -15
## 6 2021-11-06 00:00:00  862130               -7               14              -16
## # ... with 3 more variables:
## #   transit_stations_percent_change_from_baseline <dbl>,
## #   workplaces_percent_change_from_baseline <dbl>,
## #   residential_percent_change_from_baseline <dbl>
p <- ggplot(datagab2, aes(x = tanggal))
     p <- p + geom_line(aes(y = Positif/10000, colour = "Positif"))
     p <- p + geom_line(aes(y = Park, colour = "park"))
     p <- p + geom_line(aes(y = Retail, colour = "retail"))
     p <- p + geom_line(aes(y = Grocery, colour = "grocery"))
     p <- p + geom_line(aes(y = Station, colour = "station"))
     p <- p + geom_line(aes(y = Workplace, colour = "workplace"))
     p <- p + geom_line(aes(y = Residental, colour = "residental"))
     p <- p + scale_y_continuous(sec.axis = sec_axis(~.*0.1, name = "mobility"))
p

head(Datagab)
## # A tibble: 6 x 3
##   Tanggal             Positif  Park
##   <dttm>                <dbl> <dbl>
## 1 2021-11-01 00:00:00  861623   -33
## 2 2021-11-02 00:00:00  861700   -29
## 3 2021-11-03 00:00:00  861832   -26
## 4 2021-11-04 00:00:00  861943   -25
## 5 2021-11-05 00:00:00  862061   -15
## 6 2021-11-06 00:00:00  862130   -16
p <- ggplot(Datagab, aes(x = tanggal))
     p <- p + geom_line(aes(y = Positif/10000, colour = "Positif"))
     p <- p + geom_line(aes(y = Park, colour = "Park"))
     p <- p + scale_y_continuous(sec.axis = sec_axis(~.*0.1, name = "Park"))
p

model <- lm(positif ~ park)
summary(model)
## 
## Call:
## lm(formula = positif ~ park)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1238.86  -442.64   -10.47   534.06  1233.57 
## 
## Coefficients:
##              Estimate Std. Error  t value Pr(>|t|)    
## (Intercept) 864074.59     595.05 1452.093   <2e-16 ***
## park            51.65      25.51    2.024   0.0526 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 686.9 on 28 degrees of freedom
## Multiple R-squared:  0.1277, Adjusted R-squared:  0.09653 
## F-statistic: 4.098 on 1 and 28 DF,  p-value: 0.05256
anova(model)
## Analysis of Variance Table
## 
## Response: positif
##           Df   Sum Sq Mean Sq F value  Pr(>F)  
## park       1  1933470 1933470  4.0983 0.05256 .
## Residuals 28 13209646  471773                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(covidjkt$Positif ~ covidjkt$Tanggal, data = covidjkt, col = "dodgerblue", pch = 20, cex = 1.5, main = "Kasus Positif Covid 19") 
abline(model)

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

plot(model)

AIC(model)
## [1] 480.9941
BIC(model)
## [1] 485.1977
head(predict(model), n = 30)
##        1        2        3        4        5        6        7        8 
## 862370.2 862576.8 862731.7 862783.4 863299.9 863248.2 862266.9 862938.3 
##        9       10       11       12       13       14       15       16 
## 862731.7 862783.4 863041.6 862938.3 862525.1 863041.6 862835.0 862886.7 
##       17       18       19       20       21       22       23       24 
## 862990.0 863144.9 863403.2 862835.0 862990.0 862990.0 862783.4 862990.0 
##       25       26       27       28       29       30 
## 863144.9 862938.3 862886.7 862628.4 863196.6 862990.0
plot(head(predict(model), n = 30))

head(resid(model), n = 30)
##           1           2           3           4           5           6 
##  -747.18295  -876.77787  -899.72406  -840.37279 -1238.86010 -1118.21137 
##           7           8           9          10          11          12 
##   -19.88549  -662.31898  -361.72406  -318.37279  -469.61645  -295.31898 
##          13          14          15          16          17          18 
##   171.87086  -252.61645   -10.02152    17.32975    44.03228   -10.91391 
##          19          20          21          22          23          24 
##  -172.15756   509.97848   466.03228   492.03228   767.62721   643.03228 
##          25          26          27          28          29          30 
##   542.08609   818.68102   924.32975  1233.57340   706.43736   957.03228
coef(model)
##  (Intercept)         park 
## 864074.59106     51.64873
Datagab$residuals <- model$residuals

Datagab$predicted <- model$fitted.values

Datagab
## # A tibble: 30 x 5
##    Tanggal             Positif  Park residuals predicted
##    <dttm>                <dbl> <dbl>     <dbl>     <dbl>
##  1 2021-11-01 00:00:00  861623   -33    -747.    862370.
##  2 2021-11-02 00:00:00  861700   -29    -877.    862577.
##  3 2021-11-03 00:00:00  861832   -26    -900.    862732.
##  4 2021-11-04 00:00:00  861943   -25    -840.    862783.
##  5 2021-11-05 00:00:00  862061   -15   -1239.    863300.
##  6 2021-11-06 00:00:00  862130   -16   -1118.    863248.
##  7 2021-11-07 00:00:00  862247   -35     -19.9   862267.
##  8 2021-11-08 00:00:00  862276   -22    -662.    862938.
##  9 2021-11-09 00:00:00  862370   -26    -362.    862732.
## 10 2021-11-10 00:00:00  862465   -25    -318.    862783.
## # ... with 20 more rows
scatter.smooth(x=Datagab$Tanggal, y=Datagab$Positif, main="Tanggal ~ Positif")

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

require(fuzzyreg)
## Loading required package: fuzzyreg
data(fuzzydat)
f = fuzzylm(positif ~ park, data = Datagab)
plot(f, res = 20, col = "lightblue", main = "PLRLS")