(source: https://www.r-bloggers.com/chi-squared-test/) Let’s consider a hypothetical case where we test the effectiveness of a drug for a certain medical condition. Suppose we have 105 patients under study and 50 of them were treated with the drug. The remaining 55 patients were kept as control samples. The health condition of all patients was checked after a week. 35 patients in the treatment group and 26 the in the control group showed improvement.
H0: H1:
Input frequencies as an R table() object
freqs = matrix(c(35, 15, 26, 29), ncol = 2 , byrow = TRUE)
rownames(freqs) = c("Treated", "Not treated")
colnames(freqs) = c("Responded", "Not responded")
drugs <- as.table(freqs)
What does the table look like?
print(drugs)
## Responded Not responded
## Treated 35 15
## Not treated 26 29
Calculate the chi squared test
chisq.test(drugs, correct = F)
##
## Pearson's Chi-squared test
##
## data: drugs
## X-squared = 5.5569, df = 1, p-value = 0.01841
Example 2 (source: https://www.r-bloggers.com/the-chi-squared-test-of-independence-an-example-in-both-r-and-sas/) Let’s consider a hypothetical case where we want to determine whether or nor there is an association between gender and preference for ice cream flavour. Out of 920 respondents, 280 were male and 640 female. Among men, 100 preferred chocolate, 120 vanilla and 60 strawberry. Among women, 350 preferred chocolate, 200 vanilla and 90 strawberry. H0: H1:
Input frequencies as an R table() object
freqs = matrix(c(100, 120, 60, 350, 200, 90), ncol = 3 , byrow = TRUE)
rownames(freqs) = c("men", "women")
colnames(freqs) = c("chocolate", "vanilla", "strawberry")
icecream <- as.table(freqs)
What does the table look like?
print(icecream)
## chocolate vanilla strawberry
## men 100 120 60
## women 350 200 90
Calculate the chi squared test
chisq.test(icecream, correct = F)
##
## Pearson's Chi-squared test
##
## data: icecream
## X-squared = 28.362, df = 2, p-value = 6.938e-07
Post-hoc test
# install.packages('fifer')
library('fifer')
## Warning: package 'fifer' was built under R version 3.3.3
## Loading required package: MASS
chisq.post.hoc(icecream, control = 'bonferroni', popsInRows = F)
## Adjusted p-values used the bonferroni method.
## comparison raw.p adj.p
## 1 chocolate vs. vanilla 0.0000 0e+00
## 2 chocolate vs. strawberry 0.0000 1e-04
## 3 vanilla vs. strawberry 0.6121 1e+00
(source: https://www.r-bloggers.com/contingency-tables-%E2%80%93-fisher%E2%80%99s-exact-test/) Consider a trial comparing the performance of two challengers. Each of the challengers undertook the trial eight times and the number of successful trials was recorded. The hypothesis under investigation in this experiment is that the performance of the two challengers is similar. The first challenger was only successful on one trial and the second challenger was successful on four of the eight trials.
H0: H1:
Input frequencies as an R table() object
freqs = matrix(c(1, 7, 4, 4), ncol = 2, byrow = TRUE)
rownames(freqs) = c("Challenger 1", "Challenger 2")
colnames(freqs) = c("Succesful", "Not succesful")
challenge <- as.table(freqs)
What does the table look like?
print(challenge)
## Succesful Not succesful
## Challenger 1 1 7
## Challenger 2 4 4
Calculate the chi squared test
fisher.test(challenge)
##
## Fisher's Exact Test for Count Data
##
## data: challenge
## p-value = 0.2821
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.002553456 2.416009239
## sample estimates:
## odds ratio
## 0.1624254