Laporan ini disusun untuk memenuhi Tugas 6 mata kuliah Analisis Data Kategorik dengan topik Inferensi Tabel Kontingensi Dua Arah. Terdapat dua kasus yang dianalisis:
Pada kedua kasus dilakukan estimasi titik, estimasi interval, serta beberapa metode uji hipotesis independensi dan asosiasi, disertai interpretasi substantif dan kesimpulan akhir.
# Penyusunan data sesuai soal
tabel1 <- matrix(c(688, 650,
21, 59),
nrow = 2, byrow = TRUE,
dimnames = list(Status_Merokok = c("Smoker", "Non-Smoker"),
Kanker_Paru = c("Cancer (+)", "Control (-)")))
kable(addmargins(tabel1), caption = "Tabel Kontingensi 2x2: Status Merokok vs Kanker Paru") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE)| Cancer (+) | Control (-) | Sum | |
|---|---|---|---|
| Smoker | 688 | 650 | 1338 |
| Non-Smoker | 21 | 59 | 80 |
| Sum | 709 | 709 | 1418 |
Pada Kasus 1 digunakan beberapa metode inferensi:
n_smoker <- 1338; x_smoker <- 688
n_nonsmoker <- 80; x_nonsmoker <- 21
p_smoker <- x_smoker / n_smoker
p_nonsmoker <- x_nonsmoker / n_nonsmoker
data.frame(
Kelompok = c("Smoker", "Non-Smoker"),
n = c(n_smoker, n_nonsmoker),
x_kanker = c(x_smoker, x_nonsmoker),
Proporsi = round(c(p_smoker, p_nonsmoker), 4)
) %>% kable(caption = "Estimasi Titik Proporsi Kejadian Kanker Paru") %>%
kable_styling(full_width = FALSE)| Kelompok | n | x_kanker | Proporsi |
|---|---|---|---|
| Smoker | 1338 | 688 | 0.5142 |
| Non-Smoker | 80 | 21 | 0.2625 |
Interpretasi: proporsi kejadian kanker paru pada kelompok Smoker diestimasi sebesar 0.5142, jauh lebih tinggi dibandingkan kelompok Non-Smoker yang sebesar 0.2625.
ci_smoker <- prop.test(x_smoker, n_smoker, correct = FALSE)$conf.int
ci_nonsmoker <- prop.test(x_nonsmoker, n_nonsmoker, correct = FALSE)$conf.int
data.frame(
Kelompok = c("Smoker", "Non-Smoker"),
Proporsi = round(c(p_smoker, p_nonsmoker), 4),
CI_Lower = round(c(ci_smoker[1], ci_nonsmoker[1]), 4),
CI_Upper = round(c(ci_smoker[2], ci_nonsmoker[2]), 4)
) %>% kable(caption = "CI 95% Proporsi Kejadian Kanker Paru per Kelompok") %>%
kable_styling(full_width = FALSE)| Kelompok | Proporsi | CI_Lower | CI_Upper |
|---|---|---|---|
| Smoker | 0.5142 | 0.4874 | 0.5409 |
| Non-Smoker | 0.2625 | 0.1786 | 0.3682 |
RD <- p_smoker - p_nonsmoker
SE_RD <- sqrt(p_smoker*(1-p_smoker)/n_smoker + p_nonsmoker*(1-p_nonsmoker)/n_nonsmoker)
z <- qnorm(0.975)
RD_lower <- RD - z*SE_RD
RD_upper <- RD + z*SE_RD
data.frame(RD = round(RD,4), SE = round(SE_RD,4),
CI_Lower = round(RD_lower,4), CI_Upper = round(RD_upper,4)) %>%
kable(caption = "Estimasi RD dan CI 95%") %>% kable_styling(full_width = FALSE)| RD | SE | CI_Lower | CI_Upper |
|---|---|---|---|
| 0.2517 | 0.0511 | 0.1516 | 0.3518 |
RR <- p_smoker / p_nonsmoker
SE_lnRR <- sqrt((1-p_smoker)/(x_smoker) + (1-p_nonsmoker)/(x_nonsmoker))
lnRR <- log(RR)
RR_lower <- exp(lnRR - z*SE_lnRR)
RR_upper <- exp(lnRR + z*SE_lnRR)
data.frame(RR = round(RR,4),
CI_Lower = round(RR_lower,4), CI_Upper = round(RR_upper,4)) %>%
kable(caption = "Estimasi RR dan CI 95%") %>% kable_styling(full_width = FALSE)| RR | CI_Lower | CI_Upper |
|---|---|---|
| 1.9589 | 1.3517 | 2.8387 |
a <- 688; b <- 650; c <- 21; d <- 59
OR <- (a*d)/(b*c)
SE_lnOR <- sqrt(1/a + 1/b + 1/c + 1/d)
lnOR <- log(OR)
OR_lower <- exp(lnOR - z*SE_lnOR)
OR_upper <- exp(lnOR + z*SE_lnOR)
data.frame(OR = round(OR,4),
CI_Lower = round(OR_lower,4), CI_Upper = round(OR_upper,4)) %>%
kable(caption = "Estimasi OR dan CI 95%") %>% kable_styling(full_width = FALSE)| OR | CI_Lower | CI_Upper |
|---|---|---|
| 2.9738 | 1.7867 | 4.9494 |
Interpretasi ukuran asosiasi:
\[H_0: p_1 = p_2 \quad \text{vs} \quad H_1: p_1 \neq p_2\]
uji_2prop <- prop.test(c(x_smoker, x_nonsmoker), c(n_smoker, n_nonsmoker), correct = FALSE)
uji_2prop##
## 2-sample test for equality of proportions without continuity correction
##
## data: c(x_smoker, x_nonsmoker) out of c(n_smoker, n_nonsmoker)
## X-squared = 19.129, df = 1, p-value = 1.222e-05
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## 0.1516343 0.3517663
## sample estimates:
## prop 1 prop 2
## 0.5142003 0.2625000
Interpretasi: statistik uji chi-square = 19.1292 dengan p-value = 1.222e-05. Karena p-value < 0.05, \(H_0\) ditolak, artinya terdapat perbedaan proporsi kejadian kanker paru yang signifikan antara kelompok smoker dan non-smoker.
\[H_0: \text{status merokok dan kejadian kanker paru saling bebas}\]
##
## Pearson's Chi-squared test
##
## data: tabel1
## X-squared = 19.129, df = 1, p-value = 1.222e-05
Interpretasi: statistik \(\chi^2\) = 19.1292, df = 1, p-value = 1.222e-05. Karena p-value < 0.05, \(H_0\) ditolak sehingga status merokok dan kejadian kanker paru tidak saling bebas (terdapat asosiasi).
##
## Log likelihood ratio (G-test) test of independence without correction
##
## data: tabel1
## G = 19.878, X-squared df = 1, p-value = 8.254e-06
Interpretasi: statistik \(G^2\) = 19.878 dengan df = 1 dan p-value = 8.254e-06. Kesimpulan sejalan dengan uji chi-square Pearson: \(H_0\) ditolak, mendukung adanya asosiasi antara merokok dan kanker paru.
##
## Fisher's Exact Test for Count Data
##
## data: tabel1
## p-value = 1.476e-05
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 1.755611 5.210711
## sample estimates:
## odds ratio
## 2.971634
Interpretasi: p-value Fisher exact test = 1.476e-05, dengan estimasi odds ratio = 2.9716 dan CI 95% = [1.7556; 5.2107]. Hasil ini konsisten dengan uji-uji sebelumnya, yaitu menolak \(H_0\) independensi.
perbandingan <- data.frame(
Uji = c("Dua Proporsi (z/chi-sq)", "Chi-Square Pearson",
"Likelihood Ratio (G^2)", "Fisher Exact"),
Hipotesis = c("p1 = p2", "Independensi", "Independensi", "Independensi"),
Statistik_Uji = c(round(uji_2prop$statistic,4), round(uji_chisq1$statistic,4),
round(G2_test$statistic,4), NA),
P_value = c(format.pval(uji_2prop$p.value,4), format.pval(uji_chisq1$p.value,4),
format.pval(G2_test$p.value,4), format.pval(uji_fisher$p.value,4)),
Keputusan = c("Tolak H0", "Tolak H0", "Tolak H0", "Tolak H0")
)
kable(perbandingan, caption = "Perbandingan Uji Dua Proporsi, Chi-Square, LRT, dan Fisher") %>%
kable_styling(bootstrap_options = c("striped","hover"))| Uji | Hipotesis | Statistik_Uji | P_value | Keputusan |
|---|---|---|---|---|
| Dua Proporsi (z/chi-sq) | p1 = p2 | 19.1292 | 1.222e-05 | Tolak H0 |
| Chi-Square Pearson | Independensi | 19.1292 | 1.222e-05 | Tolak H0 |
| Likelihood Ratio (G^2) | Independensi | 19.8780 | 8.254e-06 | Tolak H0 |
| Fisher Exact | Independensi | NA | 1.476e-05 | Tolak H0 |
Interpretasi substantif: keempat metode uji (dua proporsi, chi-square Pearson, likelihood ratio, dan Fisher exact) menghasilkan kesimpulan yang konsisten, yaitu menolak \(H_0\). Perbedaan utama antar metode terletak pada asumsi dan pendekatan: uji chi-square dan \(G^2\) merupakan uji asimtotik yang mengandalkan aproksimasi distribusi chi-square (cocok untuk ukuran sampel besar seperti pada kasus ini), sedangkan Fisher exact test menghitung p-value secara eksak berdasarkan distribusi hipergeometrik sehingga valid meskipun frekuensi sel kecil. Karena ukuran sampel pada kasus ini besar, keempat metode memberi hasil yang sangat sejalan.
df_prop <- data.frame(
Kelompok = c("Smoker","Non-Smoker"),
Proporsi = c(p_smoker, p_nonsmoker),
Lower = c(ci_smoker[1], ci_nonsmoker[1]),
Upper = c(ci_smoker[2], ci_nonsmoker[2])
)
ggplot(df_prop, aes(x = Kelompok, y = Proporsi, fill = Kelompok)) +
geom_col(width = 0.5) +
geom_errorbar(aes(ymin = Lower, ymax = Upper), width = 0.15) +
labs(title = "Proporsi Kejadian Kanker Paru per Kelompok Merokok",
subtitle = "Error bar menunjukkan CI 95%",
y = "Proporsi Kanker Paru", x = NULL) +
theme_minimal() +
theme(legend.position = "none")Berdasarkan seluruh rangkaian analisis, terdapat bukti statistik yang kuat dan konsisten (dari uji dua proporsi, chi-square, likelihood ratio, dan Fisher exact) bahwa kebiasaan merokok berasosiasi dengan kejadian kanker paru. Kelompok smoker memiliki proporsi, risiko relatif (RR \(\approx\) 1.96), dan odds (OR \(\approx\) 2.97) kanker paru yang jauh lebih tinggi dibandingkan non-smoker, dan seluruh interval kepercayaan mendukung kesimpulan tersebut (tidak mencakup nilai netral RD=0, RR=1, atau OR=1).
tabel2 <- matrix(c(495, 272, 590,
330, 265, 498),
nrow = 2, byrow = TRUE,
dimnames = list(Gender = c("Female","Male"),
Partai = c("Democrat","Republican","Independent")))
kable(addmargins(tabel2), caption = "Tabel Kontingensi 2x3: Gender vs Identifikasi Partai Politik") %>%
kable_styling(bootstrap_options = c("striped","hover"), full_width = FALSE)| Democrat | Republican | Independent | Sum | |
|---|---|---|---|---|
| Female | 495 | 272 | 590 | 1357 |
| Male | 330 | 265 | 498 | 1093 |
| Sum | 825 | 537 | 1088 | 2450 |
Pada Kasus 2 dilakukan:
uji_chisq2 <- chisq.test(tabel2, correct = FALSE)
expected2 <- uji_chisq2$expected
kable(round(expected2,2), caption = "Frekuensi Harapan (Expected Count) per Sel") %>%
kable_styling(bootstrap_options = c("striped","hover"), full_width = FALSE)| Democrat | Republican | Independent | |
|---|---|---|---|
| Female | 456.95 | 297.43 | 602.62 |
| Male | 368.05 | 239.57 | 485.38 |
\[H_0: \text{gender dan identifikasi partai politik saling bebas}\]
##
## Pearson's Chi-squared test
##
## data: tabel2
## X-squared = 12.569, df = 2, p-value = 0.001865
Interpretasi: statistik \(\chi^2\) = 12.5693, df = 2, p-value = 0.001865. Karena p-value < 0.05, \(H_0\) ditolak, sehingga terdapat asosiasi antara gender dan identifikasi partai politik.
std_resid <- uji_chisq2$stdres
kable(round(std_resid,3), caption = "Standardized Residual per Sel") %>%
kable_styling(bootstrap_options = c("striped","hover"), full_width = FALSE)| Democrat | Republican | Independent | |
|---|---|---|---|
| Female | 3.272 | -2.499 | -1.032 |
| Male | -3.272 | 2.499 | 1.032 |
Interpretasi: sel dengan \(|standardized\ residual| > 2\) menunjukkan kontribusi signifikan terhadap penolakan independensi. Berdasarkan tabel residual di atas, sel Female-Independent dan Male-Independent (beserta pasangan Democrat/Republican-nya) merupakan kandidat kontributor utama; tanda positif menunjukkan frekuensi observasi lebih tinggi dari harapan, sedangkan tanda negatif menunjukkan sebaliknya.
Partisi dilakukan dengan memecah tabel \(2\times3\) (df = 2) menjadi dua tabel \(2\times2\) independen (masing-masing df = 1) sedemikian sehingga jumlah kedua statistik chi-square partisi sama dengan statistik chi-square tabel keseluruhan.
tabel_part1 <- tabel2[, c("Democrat","Republican")]
uji_part1 <- chisq.test(tabel_part1, correct = FALSE)
kable(addmargins(tabel_part1), caption = "Sub-tabel: Democrat vs Republican") %>%
kable_styling(full_width = FALSE)| Democrat | Republican | Sum | |
|---|---|---|---|
| Female | 495 | 272 | 767 |
| Male | 330 | 265 | 595 |
| Sum | 825 | 537 | 1362 |
##
## Pearson's Chi-squared test
##
## data: tabel_part1
## X-squared = 11.555, df = 1, p-value = 0.0006758
DR <- tabel2[,"Democrat"] + tabel2[,"Republican"]
Ind <- tabel2[,"Independent"]
tabel_part2 <- cbind(DR_Republican = DR, Independent = Ind)
uji_part2 <- chisq.test(tabel_part2, correct = FALSE)
kable(addmargins(tabel_part2), caption = "Sub-tabel: (Democrat+Republican) vs Independent") %>%
kable_styling(full_width = FALSE)| DR_Republican | Independent | Sum | |
|---|---|---|---|
| Female | 767 | 590 | 1357 |
| Male | 595 | 498 | 1093 |
| Sum | 1362 | 1088 | 2450 |
##
## Pearson's Chi-squared test
##
## data: tabel_part2
## X-squared = 1.0654, df = 1, p-value = 0.302
rekap_partisi <- data.frame(
Komponen = c("Democrat vs Republican", "(Democrat+Republican) vs Independent", "Jumlah Partisi", "Chi-Square Keseluruhan"),
Chi_Square = c(round(uji_part1$statistic,4), round(uji_part2$statistic,4),
round(uji_part1$statistic + uji_part2$statistic,4),
round(uji_chisq2$statistic,4)),
df = c(uji_part1$parameter, uji_part2$parameter,
uji_part1$parameter + uji_part2$parameter, uji_chisq2$parameter)
)
kable(rekap_partisi, caption = "Rekapitulasi Partisi Chi-Square") %>%
kable_styling(bootstrap_options = c("striped","hover"), full_width = FALSE)| Komponen | Chi_Square | df |
|---|---|---|
| Democrat vs Republican | 11.5545 | 1 |
| (Democrat+Republican) vs Independent | 1.0654 | 1 |
| Jumlah Partisi | 12.6200 | 2 |
| Chi-Square Keseluruhan | 12.5693 | 2 |
Interpretasi: jumlah statistik chi-square dari kedua partisi ( 12.62) sama (atau sangat mendekati) dengan statistik chi-square tabel keseluruhan (12.5693), sesuai sifat aditif partisi chi-square dengan total df = 2. Dengan membandingkan p-value masing-masing partisi, dapat dilihat komponen mana yang memberikan kontribusi lebih besar terhadap signifikansi hubungan gender dan partai politik.
kontribusi_sel <- round(std_resid^2, 3)
kable(kontribusi_sel, caption = "Kontribusi Chi-Square per Sel (Standardized Residual^2)") %>%
kable_styling(bootstrap_options = c("striped","hover"), full_width = FALSE)| Democrat | Republican | Independent | |
|---|---|---|---|
| Female | 10.708 | 6.243 | 1.065 |
| Male | 10.708 | 6.243 | 1.065 |
Interpretasi: berdasarkan besarnya statistik chi-square pada masing-masing partisi dan nilai kontribusi per sel di atas, kategori Independent teridentifikasi sebagai kontributor utama terhadap hubungan antara gender dan identifikasi partai politik, mengindikasikan bahwa perbedaan proporsi gender paling menonjol pada kelompok yang mengidentifikasi diri sebagai Independent dibandingkan pada perbandingan antara Democrat dan Republican.
mosaic(tabel2, shade = TRUE, legend = TRUE,
main = "Mosaic Plot: Gender vs Identifikasi Partai Politik")df_long <- as.data.frame(tabel2) %>%
tibble::rownames_to_column("Gender") %>%
pivot_longer(-Gender, names_to = "Partai", values_to = "Frekuensi") %>%
group_by(Gender) %>%
mutate(Proporsi = Frekuensi/sum(Frekuensi))
ggplot(df_long, aes(x = Partai, y = Proporsi, fill = Gender)) +
geom_col(position = "dodge") +
labs(title = "Proporsi Identifikasi Partai Politik per Gender",
y = "Proporsi", x = "Partai Politik") +
theme_minimal()Uji chi-square independensi pada tabel \(2\times3\) menunjukkan bahwa gender dan identifikasi partai politik tidak saling bebas (p-value < 0.05). Hasil partisi chi-square mengonfirmasi bahwa komponen (Democrat+Republican) vs Independent memberikan kontribusi yang relevan terhadap total statistik chi-square, dan kategori Independent merupakan kategori dengan kontribusi terbesar terhadap hubungan tersebut, sebagaimana juga terlihat pada pola standardized residual dan mosaic plot.
Melalui kedua kasus di atas, mahasiswa telah menerapkan berbagai metode inferensi pada tabel kontingensi dua arah, meliputi estimasi titik dan interval untuk proporsi serta ukuran asosiasi (RD, RR, OR), uji dua proporsi, uji chi-square Pearson, uji likelihood ratio (\(G^2\)), Fisher exact test, serta partisi chi-square untuk tabel yang lebih besar dari \(2\times2\). Seluruh metode pada Kasus 1 memberikan kesimpulan yang konsisten mengenai adanya asosiasi kuat antara merokok dan kanker paru, sedangkan pada Kasus 2 teridentifikasi adanya asosiasi antara gender dan identifikasi partai politik, dengan kategori Independent sebagai kontributor utama.