table <- matrix(c(189,104,10845,10933),nrow=2)
dimnames(table) = list(Group=c("Placebo", "Aspirin"), MI=c("Yes","No"))
table <- as.table(table)
table.df = as.data.frame(table)
table.df
## Group MI Freq
## 1 Placebo Yes 189
## 2 Aspirin Yes 104
## 3 Placebo No 10845
## 4 Aspirin No 10933
#n=2450, person/gender/party
Political <- read.table("https://users.stat.ufl.edu/~aa/cat/data/Political.dat")
names(Political) <- c("person", "gender", "party")
Political <- Political[-1,]
GenderGap<- xtabs(~gender + party, data=Political)
GenderGap
## party
## gender Dem Ind Rep
## female 495 590 272
## male 330 498 265
out <- chisq.test(GenderGap)
out
##
## Pearson's Chi-squared test
##
## data: GenderGap
## X-squared = 12.569, df = 2, p-value = 0.001865
names(out)
## [1] "statistic" "parameter" "p.value" "method" "data.name" "observed"
## [7] "expected" "residuals" "stdres"
out$stdres
## party
## gender Dem Ind Rep
## female 3.272365 -1.032199 -2.498557
## male -3.272365 1.032199 2.498557
out$observed
## party
## gender Dem Ind Rep
## female 495 590 272
## male 330 498 265
out$expected
## party
## gender Dem Ind Rep
## female 456.949 602.6188 297.4322
## male 368.051 485.3812 239.5678
#G^2 (cRT Score)
with(out, 2*sum(observed*log(observed/expected)))
## [1] 12.6009
library(epitools)
depr <- matrix(c(495,272,330,265), ncol=2, byrow=T)
dimnames(depr) <- list(Gender=c("female", "male"), Party=c("Dem", "Rep"))
depr <- as.table(depr)
oddsratio(depr, method="wald", conf=0.95, correct=F)
## $data
## Party
## Gender Dem Rep Total
## female 495 272 767
## male 330 265 595
## Total 825 537 1362
##
## $measure
## odds ratio with 95% C.I.
## Gender estimate lower upper
## female 1.000000 NA NA
## male 1.461397 1.173813 1.819439
##
## $p.value
## two-sided
## Gender midp.exact fisher.exact chi.square
## female NA NA NA
## male 0.0006953425 0.0007909605 0.0006758479
##
## $correction
## [1] FALSE
##
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"
[1] Alan Agresti, 『범주형 자료분석 개론』, 박태성, 이승연 옮긴이,
자유아카데미, 2020
[2] Alan Agresti, 『Categorical Data Analysis
(3rd Edition)』, Wiley, 2018