Simulasi Pengujian Hipotesis

library(ggplot2)
n_sims <- 1000
sample_sizes <- seq(3, 100, by = 1)
rejection_prop_normal <- numeric(length(sample_sizes))
rejection_prop_ekspo <- numeric(length(sample_sizes))

Membuat fungsi untuk menghitung proporsi penolakan H0

# Fungsi untuk menghitung proporsi penolakan H0
calculate_rejection_prop <- function(sample_size, normal_dist = TRUE) {
  rejection_count <- 0
  for (i in 1:n_sims) {
    # Menghasilkan sampel
    if (normal_dist) {
      population <- rnorm(1000, mean = 0, sd = 1)
    } else {
      population <- rexp(1000, rate = 2)
    }
    sample <- sample(population, size = sample_size)
    
    # Melakukan uji hipotesis
    z_stat <- (mean(sample) - 0) / (sd(sample) / sqrt(sample_size))
    p_value <- 2 * (1 - pnorm(abs(z_stat)))
    
    # Menentukan penolakan atau tidak penolakan H0 (alpha = 0.05)
    if (p_value < 0.05) {
      rejection_count <- rejection_count + 1
    }
  }
  
  rejection_prop <- rejection_count / n_sims
  return(rejection_prop)
}

Melakukan simulasi untuk berbagai ukuran sampel

# Melakukan simulasi untuk berbagai ukuran sampel
for (i in 1:length(sample_sizes)) {
  sample_size <- sample_sizes[i]
  
  # Sebaran populasi normal
  rejection_prop_normal[i] <- calculate_rejection_prop(sample_size, normal_dist = TRUE)
  
  # Sebaran populasi tidak normal
  rejection_prop_ekspo[i] <- calculate_rejection_prop(sample_size, normal_dist = FALSE)
}
# Membuat dataframe untuk plot
data_normal <- data.frame(sample_sizes, rejection_prop= rejection_prop_normal, Sebaran = "Normal")
data_ekspo <- data.frame(sample_sizes, rejection_prop=rejection_prop_ekspo, Sebaran = "Eksponensial")
data <- rbind(data_normal, data_ekspo)
# Membuat plot proporsi penolakan H0 terhadap ukuran sampel
ggplot(data, aes(x = sample_sizes, y = rejection_prop, color = Sebaran)) +
  geom_point() +
  geom_hline(yintercept = 0.05, linetype = "dashed", color = "red") +
  labs(x = "Ukuran Sampel", y = "Proporsi Tolak H0") +  theme_minimal()

Proporsi tolak H0 pada sebaran normal berada di sekitar 0.05 ketika ukuran sampel semakin besar.Sedangkan proporsi tolak H0 pada sebaran eksponensial semakin besar seiring meningkatnya ukuran sampel dan menuju 1.