Dosen Pengampu : Prof. Dr. Suhartono, Mkom

UIN Maulana Malik Ibrahim Malang

library(readxl)
## Warning: package 'readxl' was built under R version 4.1.3
#baca data excel
mobindexjkt <- read_excel("mobindex_jkt.xlsx")
covidjkt <- read_excel("c19data_jkt.xlsx")
Datagab <- read_excel("Datagab.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
isoman <- Datagab$SelfIsolation
park2 <- Datagab$Park
Positif <- covidjkt$Positif
Dirawat <- covidjkt$Dirawat
Sembuh <- covidjkt$Sembuh
Meninggal <- covidjkt$Meninggal
Isoman <- covidjkt$SelfIsolation

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

#korelasi kasus isoman vs retail
cor(Isoman,retail)
## [1] 0.05594085
#korelasi kasus isoman vs grocery
cor(Isoman,grocery)
## [1] 0.1833959
#korelasi kasus isoman vs park
cor(Isoman,park)
## [1] -0.4566216
#korelasi kasus isoman vs station
cor(Isoman,station)
## [1] -0.1480901
#korelasi kasus isoman vs workplace
cor(Isoman,workplace)
## [1] -0.03295439
#korelasi kasus isoman vs residental
cor(Isoman,residental)
## [1] -0.01767973
summary(covidjkt)
##     Tanggal                       Positif          Dirawat     
##  Min.   :2021-10-01 00:00:00   Min.   :857916   Min.   :256.0  
##  1st Qu.:2021-10-08 12:00:00   1st Qu.:858971   1st Qu.:289.5  
##  Median :2021-10-16 00:00:00   Median :860014   Median :348.0  
##  Mean   :2021-10-16 00:00:00   Mean   :859866   Mean   :357.8  
##  3rd Qu.:2021-10-23 12:00:00   3rd Qu.:860815   3rd Qu.:437.0  
##  Max.   :2021-10-31 00:00:00   Max.   :861540   Max.   :504.0  
##      Sembuh         Meninggal     SelfIsolation   
##  Min.   :842715   Min.   :13524   Min.   : 618.0  
##  1st Qu.:843780   1st Qu.:13542   1st Qu.: 849.0  
##  Median :845121   Median :13551   Median : 978.0  
##  Mean   :844977   Mean   :13549   Mean   : 983.1  
##  3rd Qu.:846121   3rd Qu.:13556   3rd Qu.:1202.0  
##  Max.   :847021   Max.   :13562   Max.   :1285.0
#dataframe KASUS ISOMAN
dataku <- data.frame(date, retail, grocery, park, station, workplace, residental)
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
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 Google Mobility Index", 
       subtitle = "Propinsi DKI Jakarta Indonesia Okt 2021", 
       y = "Index Moility", x = "Tanggal") +
  theme(axis.text.x = element_text(angle = -90))

head(Datagab)
## # A tibble: 6 x 3
##   Tanggal             SelfIsolation  Park
##   <dttm>                      <dbl> <dbl>
## 1 2021-10-01 00:00:00          1173   -29
## 2 2021-10-02 00:00:00          1217   -32
## 3 2021-10-03 00:00:00          1235   -39
## 4 2021-10-04 00:00:00          1261   -39
## 5 2021-10-05 00:00:00          1198   -39
## 6 2021-10-06 00:00:00          1217   -35
p <- ggplot(Datagab, aes(x = tanggal))
     p <- p + geom_line(aes(y = isoman/30, colour = "Isoman"))
     p <- p + geom_line(aes(y = park2, colour = "Park"))
     p <- p + scale_y_continuous(sec.axis = sec_axis(~.*1, name = "Park"))
p

model <- lm(Isoman ~ park)
summary(model)
## 
## Call:
## lm(formula = Isoman ~ park)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -397.02  -92.77  -22.66  146.84  305.83 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  541.247    163.424   3.312  0.00249 **
## park         -14.357      5.194  -2.764  0.00982 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 189.2 on 29 degrees of freedom
## Multiple R-squared:  0.2085, Adjusted R-squared:  0.1812 
## F-statistic: 7.639 on 1 and 29 DF,  p-value: 0.009819
anova(model)
## Analysis of Variance Table
## 
## Response: Isoman
##           Df  Sum Sq Mean Sq F value   Pr(>F)   
## park       1  273603  273603  7.6394 0.009819 **
## Residuals 29 1038622   35815                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(covidjkt$SelfIsolation ~ covidjkt$Tanggal, data = covidjkt, col = "dodgerblue", pch = 20, cex = 1.5, main = "Kasus Isoman Covid 19") 
abline(model)

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

plot(model)

AIC(model)
## [1] 416.9762
BIC(model)
## [1] 421.2781
head(predict(model), n = 31)
##         1         2         3         4         5         6         7         8 
##  957.5928 1000.6631 1101.1605 1101.1605 1101.1605 1043.7334 1000.6631  900.1658 
##         9        10        11        12        13        14        15        16 
##  971.9496 1043.7334 1043.7334 1043.7334 1015.0199 1000.6631  900.1658 1000.6631 
##        17        18        19        20        21        22        23        24 
## 1086.8037 1201.6579 1043.7334  814.0252  986.3064  885.8090  885.8090  928.8793 
##        25        26        27        28        29        30        31 
## 1000.6631 1000.6631 1015.0199  928.8793  756.5981  857.0955  857.0955
plot(head(predict(model), n = 31))

head(resid(model), n = 31)
##           1           2           3           4           5           6 
##  215.407169  216.336865  133.839490  159.839490   96.839490  173.266561 
##           7           8           9          10          11          12 
##  252.336865  305.834241  257.050401  241.266561   34.266561  -96.733439 
##          13          14          15          16          17          18 
##  -43.019903  -22.663135  129.834241   -8.663135  -88.803742 -217.657886 
##          19          20          21          22          23          24 
##  -76.733439  132.974848  -52.306367    9.191009  -39.808991  -76.879295 
##          25          26          27          28          29          30 
## -226.663135 -340.663135 -397.019903 -258.879295  -43.598080 -197.095456 
##          31 
## -171.095456
coef(model)
## (Intercept)        park 
##   541.24656   -14.35677
Datagab$residuals <- model$residuals

Datagab$predicted <- model$fitted.values

Datagab
## # A tibble: 31 x 5
##    Tanggal             SelfIsolation  Park residuals predicted
##    <dttm>                      <dbl> <dbl>     <dbl>     <dbl>
##  1 2021-10-01 00:00:00          1173   -29     215.       958.
##  2 2021-10-02 00:00:00          1217   -32     216.      1001.
##  3 2021-10-03 00:00:00          1235   -39     134.      1101.
##  4 2021-10-04 00:00:00          1261   -39     160.      1101.
##  5 2021-10-05 00:00:00          1198   -39      96.8     1101.
##  6 2021-10-06 00:00:00          1217   -35     173.      1044.
##  7 2021-10-07 00:00:00          1253   -32     252.      1001.
##  8 2021-10-08 00:00:00          1206   -25     306.       900.
##  9 2021-10-09 00:00:00          1229   -30     257.       972.
## 10 2021-10-10 00:00:00          1285   -35     241.      1044.
## # ... with 21 more rows
scatter.smooth(x=Datagab$Tanggal, y=Datagab$SelfIsolation, main="Tanggal ~ Isoman")

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

require(fuzzyreg)
## Loading required package: fuzzyreg
## Warning: package 'fuzzyreg' was built under R version 4.1.3
data(fuzzydat)
f = fuzzylm(Isoman ~ park, data = Datagab)
plot(f, res = 20, col = "lightblue", main = "PLRLS")