-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontingency_tables.R
More file actions
54 lines (33 loc) · 1.47 KB
/
contingency_tables.R
File metadata and controls
54 lines (33 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#for assignment 2, 1b
#we can create a contingency table based on the observed values
observed_table <- matrix(c(38, 12,
293, 154,
475, 431,
489, 570,
55, 129,
7, 61),
nrow = 6, byrow = TRUE)
#test using Pearson Chi-Square test
chi_test <- chisq.test(observed_table, correct= FALSE)
print(chi_test)
#test using G-Test (Likelihood Ratio Chi-square test)
#for this problem, I manually calculated it due to library errors
expected <- chisq.test(observed_table, correct=FALSE)$expected
#Compute G-statistic correctly
G_stat <- 2 * sum(observed_table * log(observed_table / expected))
#Compute degrees of freedom
df <- (nrow(observed_table) - 1) * (ncol(observed_table) - 1)
#Compute p-value
p_value <- pchisq(G_stat, df, lower.tail = FALSE)
#Print output
cat(sprintf("G-squared = %.4f, df = %d, p-value = %s\n",
G_stat, df, ifelse(p_value < 2.2e-16, "< 2.2e-16", format(p_value, digits=5))))
#use Fisher's exact test for this contingency table
new_table <- matrix(c(1295, 1167, 62, 190), nrow = 2, byrow = TRUE)
#Perform Fisher's Exact Test (one-sided test for ?? < 1)
fisher.test(new_table, alternative = "less")
#pearson's chi-square test
chisq.test(new_table, correct=FALSE)
#perform mid-p test
library(exact2x2)
exact2x2(new_table, alternative ="less", midp = TRUE)