Below is the calculation using direct formula.
obs = rbind(c(762,327,468), c(484, 239, 477)) # observed counts
n = sum(obs) # total sample size
row = rowSums(obs) # row total
col = colSums(obs) # col total
ept = outer(row, col, "*")/n # expected counts
Pearson = (obs-ept)/sqrt(ept) # Pearson residual
Pearson
## [,1] [,2] [,3]
## [1,] 2.198856 0.4113702 -2.843240
## [2,] -2.504669 -0.4685829 3.238673
Pearson/sqrt(outer(1-row/n, 1-col/n, "*")) # Standardized residual
## [,1] [,2] [,3]
## [1,] 4.502054 0.6994517 -5.315946
## [2,] -4.502054 -0.6994517 5.315946
In R, you can use as.table()
function to tell R that the matrix represents a contingency table of counts. Then the functions developed for the contingency tables can be applied.
dimnames(obs) <- list(Gender=c("Female","Male"),
Party=c("Democrat","Independent","Republican"))
obs.tab = as.table(obs)
obs.tab
## Party
## Gender Democrat Independent Republican
## Female 762 327 468
## Male 484 239 477
margin.table(obs.tab,1)
## Gender
## Female Male
## 1557 1200
margin.table(obs.tab,2)
## Party
## Democrat Independent Republican
## 1246 566 945
addmargins(obs.tab)
## Party
## Gender Democrat Independent Republican Sum
## Female 762 327 468 1557
## Male 484 239 477 1200
## Sum 1246 566 945 2757
prop.table(obs.tab,1)
## Party
## Gender Democrat Independent Republican
## Female 0.4894027 0.2100193 0.3005780
## Male 0.4033333 0.1991667 0.3975000
prop.table(obs.tab,2)
## Party
## Gender Democrat Independent Republican
## Female 0.6115570 0.5777385 0.4952381
## Male 0.3884430 0.4222615 0.5047619
prop.table(obs.tab)
## Party
## Gender Democrat Independent Republican
## Female 0.27638738 0.11860718 0.16974973
## Male 0.17555314 0.08668843 0.17301415
obs.chisq = chisq.test(obs.tab) # Chi-squared Test for Independence
obs.chisq
##
## Pearson's Chi-squared test
##
## data: obs.tab
## X-squared = 30.07, df = 2, p-value = 2.954e-07
names(obs.chisq)
## [1] "statistic" "parameter" "p.value" "method" "data.name" "observed"
## [7] "expected" "residuals" "stdres"
obs.chisq$statistic
## X-squared
## 30.07015
obs.chisq$observed
## Party
## Gender Democrat Independent Republican
## Female 762 327 468
## Male 484 239 477
obs.chisq$expected
## Party
## Gender Democrat Independent Republican
## Female 703.6714 319.6453 533.6834
## Male 542.3286 246.3547 411.3166
with(obs.chisq, sum((observed - expected)^2/expected))
## [1] 30.07015
# The output residuals from chisq.test are Pearson residuals
obs.chisq$residuals
## Party
## Gender Democrat Independent Republican
## Female 2.1988558 0.4113702 -2.8432397
## Male -2.5046695 -0.4685829 3.2386734
# You can verify the output residuals by
with(obs.chisq, (observed - expected)/sqrt(expected))
## Party
## Gender Democrat Independent Republican
## Female 2.1988558 0.4113702 -2.8432397
## Male -2.5046695 -0.4685829 3.2386734
# To To get the standardized residuals, we also need marginal proportions.
n <- sum(obs.tab)
n.gender <- margin.table(obs.tab, 1)
n.party <- margin.table(obs.tab, 2)
stand.res = obs.chisq$residual/sqrt((1-n.gender/n) %o% (1-n.party/n))
stand.res
## Party
## Gender Democrat Independent Republican
## Female 4.5020535 0.6994517 -5.3159455
## Male -4.5020535 -0.6994517 5.3159455
For the trend test,
Total = c(17114, 14502, 793, 127, 38)
present = c(48, 38, 5, 1, 1)
prop.trend.test(present, Total, c(0, 0.5, 1.5, 4.0, 7))
##
## Chi-squared Test for Trend in Proportions
##
## data: present out of Total ,
## using scores: 0 0.5 1.5 4 7
## X-squared = 6.5701, df = 1, p-value = 0.01037