library(moments)
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.4.3
## corrplot 0.95 loaded
library(GGally)
## Warning: package 'GGally' was built under R version 4.4.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.4.2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(ggplot2)
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.4.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(car)
## Warning: package 'car' was built under R version 4.4.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.4.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
library(readxl)
## Warning: package 'readxl' was built under R version 4.4.2
data <- read_excel("C:/Users/Raiqa/Downloads/Data Miskin Kak Bintang.xlsx")
# Lihat Data
head(data)
## # A tibble: 6 × 11
##   `Nama Wilayah`      P0 RLS     PPK   IPM   UHH  SNTS   AIR   TPT  TPAK    PDRB
##   <chr>            <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>
## 1 DKI JAKARTA      NA    <NA>     NA  NA    NA    NA    NA   NA     NA   NA     
## 2 Kepulauan Seribu 15.1  8.81  12587  72.1  69.0  83.8  94.2  8.58  65.5  3.65e6
## 3 Kota Jakarta Se…  3.56 11.64 23888  84.9  74.2  96.5 100.   7.33  61.4  4.29e8
## 4 Kota Jakarta Ti…  4.28 11.67 17733  83.0  74.5  96.7  99.6  8.23  60.9  3.14e8
## 5 Kota Jakarta Pu…  4.94 11.39 17365  81.6  74.2  89.6 100    7.75  63.2  4.60e8
## 6 Kota Jakarta Ba…  4.31 10.78 20801  81.8  73.7  97.7 100    9.06  63.2  3.28e8
str(data)
## tibble [17 × 11] (S3: tbl_df/tbl/data.frame)
##  $ Nama Wilayah: chr [1:17] "DKI JAKARTA" "Kepulauan Seribu" "Kota Jakarta Selatan" "Kota Jakarta Timur" ...
##  $ P0          : num [1:17] NA 15.06 3.56 4.28 4.94 ...
##  $ RLS         : chr [1:17] NA "8.81" "11.64" "11.67" ...
##  $ PPK         : num [1:17] NA 12587 23888 17733 17365 ...
##  $ IPM         : num [1:17] NA 72.1 84.9 83 81.6 ...
##  $ UHH         : num [1:17] NA 69 74.2 74.5 74.2 ...
##  $ SNTS        : num [1:17] NA 83.8 96.5 96.7 89.6 ...
##  $ AIR         : num [1:17] NA 94.2 100 99.6 100 ...
##  $ TPT         : num [1:17] NA 8.58 7.33 8.23 7.75 ...
##  $ TPAK        : num [1:17] NA 65.5 61.4 60.9 63.2 ...
##  $ PDRB        : num [1:17] NA 3.65e+06 4.29e+08 3.14e+08 4.60e+08 ...
summary(data)
##  Nama Wilayah             P0             RLS                 PPK       
##  Length:17          Min.   : 2.570   Length:17          Min.   :10410  
##  Class :character   1st Qu.: 4.287   Class :character   1st Qu.:12352  
##  Mode  :character   Median : 5.075   Mode  :character   Median :15586  
##                     Mean   : 5.922                      Mean   :15609  
##                     3rd Qu.: 7.210                      3rd Qu.:17641  
##                     Max.   :15.060                      Max.   :23888  
##                     NA's   :3                           NA's   :3      
##       IPM             UHH             SNTS            AIR        
##  Min.   :70.60   Min.   :68.99   Min.   :63.91   Min.   : 91.83  
##  1st Qu.:74.98   1st Qu.:71.82   1st Qu.:84.29   1st Qu.: 97.98  
##  Median :80.94   Median :73.75   Median :92.78   Median : 99.37  
##  Mean   :78.65   Mean   :72.98   Mean   :89.25   Mean   : 98.10  
##  3rd Qu.:81.72   3rd Qu.:74.20   3rd Qu.:96.98   3rd Qu.: 99.99  
##  Max.   :84.90   Max.   :75.19   Max.   :98.84   Max.   :100.00  
##  NA's   :3       NA's   :3       NA's   :3       NA's   :3       
##       TPT              TPAK            PDRB          
##  Min.   : 7.332   Min.   :60.85   Min.   :  3649179  
##  1st Qu.: 8.587   1st Qu.:62.56   1st Qu.: 64222352  
##  Median : 9.065   Median :63.19   Median :133143936  
##  Mean   : 9.447   Mean   :63.46   Mean   :192635338  
##  3rd Qu.:10.029   3rd Qu.:64.70   3rd Qu.:324177110  
##  Max.   :12.224   Max.   :65.87   Max.   :460081046  
##  NA's   :3        NA's   :3       NA's   :3
data_sederhana <- data[-c(1, 2, 8, 14), -c(1, 3, 4, 5, 6, 7, 8, 10, 11)]
colnames(data_sederhana) <- c("y", "x")

print(data_sederhana)
## # A tibble: 13 × 2
##        y     x
##    <dbl> <dbl>
##  1  3.56  7.33
##  2  4.28  8.23
##  3  4.94  7.75
##  4  4.31  9.06
##  5  7.24  9.84
##  6  8.13 12.2 
##  7  5.21 10.1 
##  8  7.24 11.8 
##  9  4.74 10.9 
## 10  2.58  9.76
## 11  7.12  9.06
## 12  5.93  9.07
## 13  2.57  8.60
head(data_sederhana)
## # A tibble: 6 × 2
##       y     x
##   <dbl> <dbl>
## 1  3.56  7.33
## 2  4.28  8.23
## 3  4.94  7.75
## 4  4.31  9.06
## 5  7.24  9.84
## 6  8.13 12.2
str(data_sederhana)
## tibble [13 × 2] (S3: tbl_df/tbl/data.frame)
##  $ y: num [1:13] 3.56 4.28 4.94 4.31 7.24 8.13 5.21 7.24 4.74 2.58 ...
##  $ x: num [1:13] 7.33 8.23 7.75 9.06 9.84 ...
summary(data_sederhana)
##        y               x         
##  Min.   :2.570   Min.   : 7.332  
##  1st Qu.:4.280   1st Qu.: 8.601  
##  Median :4.940   Median : 9.069  
##  Mean   :5.219   Mean   : 9.514  
##  3rd Qu.:7.120   3rd Qu.:10.092  
##  Max.   :8.130   Max.   :12.224

PEMODELAN

data_sederhana$y <- as.numeric(as.character(data_sederhana$y))
data_sederhana$x <- as.numeric(as.character(data_sederhana$x))
# Definisikan x dan y secara eksplisit
x <- data_sederhana$x
y <- data_sederhana$y
n <- length(y)  # atau length(x), sama aja

MANUAL

Koefisien Regresi

b1 <- (sum(x*y)-sum(x)*sum(y)/n)/ (sum(x^2)-sum(x^2/n))
b0 <- mean(y)-b1*mean(x)

cat("Koefisien B1:", b1, "\n")
## Koefisien B1: 0.01692941
cat("Koefisien B0:", b0, "\n")
## Koefisien B0: 5.058171

Standar Error Parameter Regresi

galat <- y-(b0+b1*x)
ragam_galat <- sum(galat^2)/(n-2)

se_b1 <- sqrt(ragam_galat/sum((x-mean(x))^2))
se_b0 <- sqrt(ragam_galat*(1/n+mean(x)^2/sum((x-mean(x))^2)))

cat("Standar Error B1:", se_b1, "\n")
## Standar Error B1: 0.3699017
cat("Standar Error B0:", se_b0, "\n")
## Standar Error B0: 3.557518

Signifikansi Parameter (nilai-t)

t_b1 <- b1/se_b1
t_b0 <- b0/se_b0

p_b1 <- 2*pt(-abs(t_b1 ),df<-n-2)
p_b0 <- 2*pt(-abs(t_b0 ),df<-n-2)

cat("Nilai t pada b0 adalah", t_b0, "dengan nilai p sebesar", p_b0, "\n")  
## Nilai t pada b0 adalah 1.421826 dengan nilai p sebesar 0.1828053
cat("Nilai t pada b1 adalah", t_b1, "dengan nilai p sebesar", p_b1, "\n")
## Nilai t pada b1 adalah 0.04576731 dengan nilai p sebesar 0.964316

Koefisien Determinasi dan Penyesuaiannya

r <- (sum(x*y)-sum(x)*sum(y)/n)/
sqrt((sum(x^2)-(sum(x)^2/n))*(sum(y^2)-(sum(y)^2/n)))
koef_det <- r^2
koef_det
## [1] 0.3459232
adj_r2 <- 1-((1-koef_det))*(n-1)/(n-1-1)
adj_r2
## [1] 0.2864617

Signifikansi F

JKG <- sum((y - (b0+b1*x))^2)
JKReg <- sum(((b0+b1*x)- mean(y))^2)
JKT <- sum((y - mean(y))^2)
JKT <- JKReg+JKG

dbReg <- 1
dbg <- n-2
dbt <- n-1

Fhit <- (JKReg/dbReg)/(JKG/dbg)
p_Fhit <- 1-pf(Fhit, dbReg, dbg, lower.tail <- F)

cat("JKG:", JKG, "\n")
## JKG: 38.88246
cat("JKReg:", JKReg, "\n")
## JKReg: 0.007404094
cat("JKT:", JKT, "\n")
## JKT: 38.88987
cat("F Hitung:", Fhit, "\n")
## F Hitung: 0.002094647
cat("P Hitung:", p_Fhit, "\n")
## P Hitung: 0.964316

FUNGSI LM

head(data_sederhana)
## # A tibble: 6 × 2
##       y     x
##   <dbl> <dbl>
## 1  3.56  7.33
## 2  4.28  8.23
## 3  4.94  7.75
## 4  4.31  9.06
## 5  7.24  9.84
## 6  8.13 12.2
str(data_sederhana)
## tibble [13 × 2] (S3: tbl_df/tbl/data.frame)
##  $ y: num [1:13] 3.56 4.28 4.94 4.31 7.24 8.13 5.21 7.24 4.74 2.58 ...
##  $ x: num [1:13] 7.33 8.23 7.75 9.06 9.84 ...
summary(data_sederhana)
##        y               x         
##  Min.   :2.570   Min.   : 7.332  
##  1st Qu.:4.280   1st Qu.: 8.601  
##  Median :4.940   Median : 9.069  
##  Mean   :5.219   Mean   : 9.514  
##  3rd Qu.:7.120   3rd Qu.:10.092  
##  Max.   :8.130   Max.   :12.224
modelRLS <- lm(y~x, data = data_sederhana)
summary(modelRLS)
## 
## Call:
## lm(formula = y ~ x, data = data_sederhana)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.81858 -0.57991 -0.00445  1.00171  2.23418 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  -1.7007     2.9003  -0.586   0.5695  
## x             0.7274     0.3016   2.412   0.0345 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.533 on 11 degrees of freedom
## Multiple R-squared:  0.3459, Adjusted R-squared:  0.2865 
## F-statistic: 5.818 on 1 and 11 DF,  p-value: 0.03449
anova(modelRLS)
## Analysis of Variance Table
## 
## Response: y
##           Df Sum Sq Mean Sq F value  Pr(>F)  
## x          1 13.668 13.6679  5.8176 0.03449 *
## Residuals 11 25.843  2.3494                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

PENGURAIAN KERAGAMAN

y.bar <- mean(y)
plot(x,y)
abline(modelRLS, col="firebrick3")
text(30, 220, "Y_duga", adj = c(-0.1, 1.5), col = "firebrick3", cex = 0.8)
abline(h=y.bar, col="steelblue")
text(31, 185, "Y_bar", adj = c(-0.1, 1.5), col = "steelblue", cex = 0.8)

anova.model <- anova(modelRLS)

print(anova.model)
## Analysis of Variance Table
## 
## Response: y
##           Df Sum Sq Mean Sq F value  Pr(>F)  
## x          1 13.668 13.6679  5.8176 0.03449 *
## Residuals 11 25.843  2.3494                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(KTG <- anova.model$`Mean Sq`[2])
## [1] 2.349402
(galat.baku <- sqrt(KTG))
## [1] 1.532776

KERAGAMAN DUGA PARAMETER

Dugaan Parameter B0

(b0 <- modelRLS$coefficients[[1]])
## [1] -1.700687
(b1 <- modelRLS$coefficients[[2]])
## [1] 0.7273714
(se_b0 <- sqrt(KTG*(1/n+mean(x)^2/sum((x-mean(x))^2))))
## [1] 2.900316
(t_b0 <- b0/se_b0)
## [1] -0.58638

Dugaan Parameter B1

(se_b1 <- sqrt(KTG/sum((x-mean(x))^2)))
## [1] 0.3015674
(t_b1 <- b1/se_b1)
## [1] 2.411969

Selang Kepercayaan Parameter

#Batas Bawah beta0
(bb.b0 <- b0 - abs(qt(0.025, df=n-2))*se_b0)
## [1] -8.084239
#Batas Bawah beta1
(bb.b1 <- b1 - abs(qt(0.025, df=n-2))*se_b1)
## [1] 0.06362598
#Batas Atas beta_0
(ba.b0 <- b0 + abs(qt(0.025, df=n-2))*se_b0)
## [1] 4.682865
#Batas Atas beta_1
(ba.b1 <- b1 + abs(qt(0.025, df=n-2))*se_b1)
## [1] 1.391117

Selang Kepercayaan

Bagi Nilai Rataan / Nilai Harapan Amatan

amatan.diduga <- data.frame(x=17)
predict(modelRLS, amatan.diduga, interval = "confidence")
##        fit      lwr      upr
## 1 10.66463 5.608234 15.72102

Bagi Individu Amatan

predict(modelRLS, amatan.diduga, interval = "prediction")
##        fit      lwr      upr
## 1 10.66463 4.586108 16.74315