Categorical and Simphson’s paradox

#########################################
######################################### Chi-square test and Fisher's exact test
#########################################   by Caesarean section and maternal shoe size
#install.packages("ISwR")
library(ISwR)
caesar.shoe
##     <4  4 4.5  5 5.5  6+
## Yes  5  7   6  7   8  10
## No  17 28  36 41  46 140
caesar.shoe.yes <- caesar.shoe["Yes",]
caesar.shoe.total <- margin.table(caesar.shoe,2)
caesar.shoe.yes
##  <4   4 4.5   5 5.5  6+ 
##   5   7   6   7   8  10
caesar.shoe.total
##  <4   4 4.5   5 5.5  6+ 
##  22  35  42  48  54 150
# Chi-square test
chisq.test(caesar.shoe, correct=F)
## Warning in chisq.test(caesar.shoe, correct = F): Chi-squared approximation
## may be incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  caesar.shoe
## X-squared = 9.2874, df = 5, p-value = 0.09814
# Fisher's exact test 
fisher.test(caesar.shoe.yes,caesar.shoe.total)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  caesar.shoe.yes and caesar.shoe.total
## p-value = 1
## alternative hypothesis: two.sided
#Note: 자유도가 작은 경우엔 Fisher's exact test를 한다.

#########################################
######################################### Simpson's paradox 
#########################################   by Student Admissions at UC Berkeley
#install.packages("graphics")
library(graphics)
## 자료를 학과에 상관없이 분석한 결과
apply(UCBAdmissions, c(1, 2), sum)
##           Gender
## Admit      Male Female
##   Admitted 1198    557
##   Rejected 1493   1278
mosaicplot(apply(UCBAdmissions, c(1, 2), sum), main = "Student admissions at UC Berkeley")

## 자료를 학과를 기준으로 분석한 결과
opar <- par(mfrow = c(2, 3), oma = c(0, 0, 2, 0))
for(i in 1:6){
  mosaicplot(UCBAdmissions[,,i],
    xlab = "Admit", ylab = "Sex",
    main = paste("Department", LETTERS[i]))
}
mtext(expression(bold("Student admissions at UC Berkeley")),outer = TRUE, cex = 1.5)