true

{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)

Distribusi Poisson — Analisis Lengkap

Dokumen ini berisi analisis lengkap distribusi Poisson menggunakan R, mulai dari simulasi data, statistik deskriptif, visualisasi, hingga uji kecocokan (goodness-of-fit).


# 1. Load Library

{r} # install.packages(c(“ggplot2”, “fitdistrplus”)) library(ggplot2) library(fitdistrplus)


# 2. Simulasi Data Poisson

{r} set.seed(123) lambda_true <- 3 n <- 200 x <- rpois(n, lambda_true)

mean_x <- mean(x) var_x <- var(x) lambda_hat <- mean_x

cat(“Jumlah observasi:”, n, “”) cat(“Mean:”, mean_x, “”) cat(“Var:”, var_x, “”) cat(“Estimasi lambda (MLE):”, lambda_hat, “”)


3. Tabel Frekuensi Observasi

{r} tab_obs <- as.data.frame(table(x)) colnames(tab_obs) <- c(“k”,“freq”) tab_obs\(k <- as.integer(as.character(tab_obs\)k)) tab_obs


# 4. Probabilitas Teoretik (PMF)

{r} k_max <- max(tab_obs$k, qpois(0.999, lambda_hat)) k_vals <- 0:k_max pmf_theo <- dpois(k_vals, lambda_hat) expected_freq <- pmf_theo * n

obs_extended <- merge(data.frame(k=k_vals), tab_obs, by=“k”, all.x=TRUE) obs_extended\(freq[is.na(obs_extended\)freq)] <- 0 obs_extended$expected <- expected_freq

obs_extended


5. Grafik Observed vs Expected

{r} df_plot <- obs_extended df_plot\(label <- factor(df_plot\)k, levels=df_plot$k)

ggplot(df_plot, aes(x=label)) + geom_bar(aes(y=freq), stat=“identity”, alpha=0.6) + geom_point(aes(y=expected), color=“red”, size=2) + geom_line(aes(y=expected, group=1), color=“red”, linetype=“dashed”) + labs(title=paste(“Observed vs Expected Poisson — λ =”, round(lambda_hat,3)), x=“Jumlah kejadian (k)”, y=“Frekuensi”) + theme_minimal()


# 6. Grafik PMF

{r} pmf_df <- data.frame(k=k_vals, pmf=pmf_theo)

ggplot(pmf_df, aes(x=k, y=pmf)) + geom_col() + labs(title=paste(“PMF Distribusi Poisson (λ =”, round(lambda_hat,3),“)”), x=“k”, y=“P(X=k)”) + theme_minimal()


# 7. Contoh Perhitungan CDF

{r} cat(“P(X <= 2) =”, ppois(2, lambda_hat), “”) cat(“P(X >= 4) =”, 1 - ppois(3, lambda_hat), “”)


# 8. Uji Chi-Square Goodness-of-Fit

## 8.1 Fungsi Gabung Tail (agar expected ≥ 5)

{r} combine_tail <- function(k, obs, exp, min_expected = 5) { k2 <- k obs2 <- obs exp2 <- exp

while(any(exp2 < min_expected) && length(exp2) > 1) { i <- length(exp2) exp2[i-1] <- exp2[i-1] + exp2[i] obs2[i-1] <- obs2[i-1] + obs2[i] k2 <- k2[-i] exp2 <- exp2[-i] obs2 <- obs2[-i] } data.frame(k=k2, obs=obs2, exp=exp2) }

comb_df <- combine_tail( obs_extended\(k, obs_extended\)freq, obs_extended$expected )

comb_df


## 8.2 Chi-square Manual

{r} chisq_stat <- sum((comb_df\(obs - comb_df\)exp)^2 / comb_df$exp) df_chisq <- nrow(comb_df) - 1 - 1 p_value <- 1 - pchisq(chisq_stat, df = df_chisq)

cat(“Chi-square statistic:”, chisq_stat, “”) cat(“df:”, df_chisq, “”) cat(“p-value:”, p_value, “”)


## 8.3 Chi-square dari fungsi bawaan R

{r} chisq_builtin <- suppressWarnings( chisq.test( x = comb_df\(obs, p = comb_df\)exp / sum(comb_df$exp), rescale.p = FALSE ) ) chisq_builtin


9. Estimasi Parameter via MLE (fitdist)

{r} fit <- fitdist(x, “pois”, method = “mle”) fit


10. Probabilitas Tambahan

{r} cat(“P(X >= 6) =”, 1 - ppois(5, lambda_hat), “”)


11. Bootstrap CI untuk Lambda

{r} B <- 2000 boot_means <- replicate(B, mean(sample(x, replace = TRUE))) quantile(boot_means, c(0.025, 0.975))


Selesai

Analisis distribusi Poisson selesai dilakukan dengan simulasi, visualisasi, uji kecocokan, dan estimasi parameter.