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

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
dirawat <- Datagab$Dirawat
station2 <- Datagab$Station

Positif <- covidjkt$Positif
Dirawat <- covidjkt$Dirawat
Sembuh <- covidjkt$Sembuh
Meninggal <- covidjkt$Meninggal
Isolasi_Mandiri <- covidjkt$SelfIsolation

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


#korelasi kasus dirawat vs retail
cor(Dirawat,retail)
## [1] -0.3700471
#korelasi kasus dirawat vs grocery
cor(Dirawat,grocery)
## [1] 0.1195116
#korelasi kasus dirawat vs park
cor(Dirawat,park)
## [1] -0.2227841
#korelasi kasus dirawat vs station
cor(Dirawat,station)
## [1] -0.5003673
#korelasi kasus dirawat vs workplace
cor(Dirawat,workplace)
## [1] -0.07595505
#korelasi kasus dirawat vs residental
cor(Dirawat,residental)
## [1] 0.1272799
summary(covidjkt)
##     Tanggal                       Positif          Dirawat      
##  Min.   :2021-09-01 00:00:00   Min.   :851256   Min.   : 510.0  
##  1st Qu.:2021-09-08 06:00:00   1st Qu.:853676   1st Qu.: 708.8  
##  Median :2021-09-15 12:00:00   Median :855512   Median :1044.0  
##  Mean   :2021-09-15 12:00:00   Mean   :855112   Mean   :1155.8  
##  3rd Qu.:2021-09-22 18:00:00   3rd Qu.:856709   3rd Qu.:1521.5  
##  Max.   :2021-09-30 00:00:00   Max.   :857765   Max.   :2172.0  
##      Sembuh         Meninggal     SelfIsolation 
##  Min.   :831293   Min.   :13302   Min.   :1195  
##  1st Qu.:835822   1st Qu.:13389   1st Qu.:1506  
##  Median :838851   Median :13460   Median :2158  
##  Mean   :838165   Mean   :13438   Mean   :2353  
##  3rd Qu.:841002   3rd Qu.:13492   3rd Qu.:2944  
##  Max.   :842541   Max.   :13519   Max.   :4489
#dataframe KASUS DIRAWAT
dataku1 <- data.frame(Dirawat, retail, grocery, park, station, workplace, residental)
dataku3 <- 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
dataku4 <- melt(data = dataku3, id.vars = "date")
ggplot(data = dataku4, 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 SEPT 2021", 
       y = "Index Moility", x = "Tanggal") +
  theme(axis.text.x = element_text(angle = -90))

head(Datagab)
## # A tibble: 6 x 3
##   Tanggal             Dirawat Station
##   <dttm>                <dbl>   <dbl>
## 1 2021-09-01 00:00:00    2172     -41
## 2 2021-09-02 00:00:00    2043     -42
## 3 2021-09-03 00:00:00    1914     -41
## 4 2021-09-04 00:00:00    1813     -35
## 5 2021-09-05 00:00:00    1765     -38
## 6 2021-09-06 00:00:00    1644     -41
p <- ggplot(Datagab, aes(x = tanggal))
     p <- p + geom_line(aes(y = dirawat/30, colour = "Dirawat" ))
     p <- p + geom_line(aes(y = station2, colour = "Station"))
     p <- p + scale_y_continuous(sec.axis = sec_axis(~.*1, name = "Station"))
p

model <- lm(Dirawat ~ station)
summary(model)
## 
## Call:
## lm(formula = Dirawat ~ station)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -574.56 -390.81  -58.22  249.36  853.11 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) -1746.79     952.57  -1.834  0.07734 . 
## station       -77.33      25.29  -3.058  0.00486 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 440.5 on 28 degrees of freedom
## Multiple R-squared:  0.2504, Adjusted R-squared:  0.2236 
## F-statistic: 9.352 on 1 and 28 DF,  p-value: 0.004864
anova(model)
## Analysis of Variance Table
## 
## Response: Dirawat
##           Df  Sum Sq Mean Sq F value   Pr(>F)   
## station    1 1814883 1814883  9.3516 0.004864 **
## Residuals 28 5433994  194071                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(covidjkt$Dirawat ~ covidjkt$Tanggal, data = covidjkt, col = "dodgerblue", pch = 20, cex = 1.5, main = "Kasus Dirawat Covid 19") 
abline(model)

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

plot(model)

AIC(model)
## [1] 454.3459
BIC(model)
## [1] 458.5495
head(predict(model), n = 30)
##         1         2         3         4         5         6         7         8 
## 1423.8902 1501.2239 1423.8902  959.8880 1191.8891 1423.8902 1346.5565 1346.5565 
##         9        10        11        12        13        14        15        16 
## 1269.2228 1269.2228  805.2206 1037.2217 1269.2228 1655.8913 1191.8891 1269.2228 
##        17        18        19        20        21        22        23        24 
## 1191.8891  727.8869 1037.2217 1191.8891 1191.8891 1269.2228 1191.8891 1114.5554 
##        25        26        27        28        29        30 
##  495.8858  727.8869 1114.5554 1114.5554 1037.2217  882.5543
plot(head(predict(model), n = 29))

head(resid(model), n = 30)
##          1          2          3          4          5          6          7 
##  748.10984  541.77614  490.10984  853.11204  573.11094  220.10984  250.44354 
##          8          9         10         11         12         13         14 
##  189.44354  208.77724  184.77724  612.77944  364.77834  -42.22276 -555.89126 
##         15         16         17         18         19         20         21 
## -125.88906 -247.22276 -191.88906  246.11314  -74.22166 -327.88906 -396.88906 
##         22         23         24         25         26         27         28 
## -495.22276 -504.88906 -482.55536   95.11424 -120.88686 -562.55536 -574.55536 
##         29         30 
## -503.22166 -372.55426
coef(model)
## (Intercept)     station 
##  -1746.7915    -77.3337
Datagab$residuals <- model$residuals

Datagab$predicted <- model$fitted.values

Datagab
## # A tibble: 30 x 5
##    Tanggal             Dirawat Station residuals predicted
##    <dttm>                <dbl>   <dbl>     <dbl>     <dbl>
##  1 2021-09-01 00:00:00    2172     -41      748.     1424.
##  2 2021-09-02 00:00:00    2043     -42      542.     1501.
##  3 2021-09-03 00:00:00    1914     -41      490.     1424.
##  4 2021-09-04 00:00:00    1813     -35      853.      960.
##  5 2021-09-05 00:00:00    1765     -38      573.     1192.
##  6 2021-09-06 00:00:00    1644     -41      220.     1424.
##  7 2021-09-07 00:00:00    1597     -40      250.     1347.
##  8 2021-09-08 00:00:00    1536     -40      189.     1347.
##  9 2021-09-09 00:00:00    1478     -39      209.     1269.
## 10 2021-09-10 00:00:00    1454     -39      185.     1269.
## # ... with 20 more rows
scatter.smooth(x=Datagab$Tanggal, y=Datagab$Dirawat, main="Tanggal ~ Dirawat")

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

require(fuzzyreg)
## Loading required package: fuzzyreg
## Warning: package 'fuzzyreg' was built under R version 4.1.3
## Loading required package: fuzzyreg
## Warning: package 'fuzzyreg' was built under R version 4.1.3
data(fuzzydat)
f = fuzzylm(Dirawat ~ station, data = Datagab)
plot(f, res = 20, col = "#4bdb57", main = "Estimasi Kasus Dirawat")