This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
if(!require(ggplot2)) install.packages("ggplot2")
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.4.3
library(ggplot2)
alpha <- 0.05
conf_level <- 0.95
n_levels <- c(5, 30, 100)
sd_levels <- c(10, 50, 90)
sd_known_levels <- c("Diketahui (z)", "Tidak Diketahui (t)")
df <- expand.grid(n = n_levels,
SD = sd_levels,
Status_SD = sd_known_levels)
df$Margin_Error <- mapply(function(n, sd, status) {
if (status == "Diketahui (z)") {
return(qnorm(0.975) * (sd / sqrt(n)))
} else {
return(qt(0.975, df = n - 1) * (sd / sqrt(n)))
}
}, df$n, df$SD, df$Status_SD)
df$Lebar_Interval <- 2 * df$Margin_Error
df <- df[order(df$Lebar_Interval), ]
print(df)
## n SD Status_SD Margin_Error Lebar_Interval
## 3 100 10 Diketahui (z) 1.959964 3.919928
## 12 100 10 Tidak Diketahui (t) 1.984217 3.968434
## 2 30 10 Diketahui (z) 3.578388 7.156777
## 11 30 10 Tidak Diketahui (t) 3.734061 7.468123
## 1 5 10 Diketahui (z) 8.765225 17.530451
## 6 100 50 Diketahui (z) 9.799820 19.599640
## 15 100 50 Tidak Diketahui (t) 9.921085 19.842170
## 10 5 10 Tidak Diketahui (t) 12.416640 24.833280
## 9 100 90 Diketahui (z) 17.639676 35.279352
## 18 100 90 Tidak Diketahui (t) 17.857953 35.715905
## 5 30 50 Diketahui (z) 17.891941 35.783883
## 14 30 50 Tidak Diketahui (t) 18.670307 37.340614
## 8 30 90 Diketahui (z) 32.205495 64.410989
## 17 30 90 Tidak Diketahui (t) 33.606552 67.213105
## 4 5 50 Diketahui (z) 43.826127 87.652254
## 13 5 50 Tidak Diketahui (t) 62.083200 124.166400
## 7 5 90 Diketahui (z) 78.887029 157.774057
## 16 5 90 Tidak Diketahui (t) 111.749760 223.499520
ggplot(df, aes(x = factor(n), y = Lebar_Interval, color = factor(SD), group = SD)) +
geom_line() +
geom_point() +
facet_wrap(~Status_SD) +
labs(title = "Analisis Lebar Interval Kepercayaan 95%",
x = "Ukuran Sampel (n)",
y = "Lebar Interval",
color = "Standar Deviasi (SD)") +
theme_minimal()