Saturday, October 27, 2007

Categorical Data Analysis

### Binomial Test

## Approximate
prop.test(x, n, p=NULL, alternative=c("two.sided", "less", "greater"), conf.level=0.95, corrrect=TRUE)

# chi-squared with continuity correction 1/(2n)
((0.7-0.5-1/20)/(sqrt(0.5*0.5/10)))^2
> prop.test(7, 10)

# without continuity correction
> prop.test(7, 10, correct=F)

## Exact
binom.test(x, n, p = 0.5, alternative = c("two.sided", "less", "greater"), conf.level = 0.95)

### Comparing proportions in 2*2 Tables
prop.test(x) # x is a 2*2 table

### Two-Way Contingency Tables
respire <- matrix(c(16,48,40,20), byrow=T, nrow=2)
dimnames(respire) <- list(treat=c("placebo","test"), outcome=c("f","u"))

# chi-squred test
chisq.test(respire)
chisq.test(respire, correct=F)

# Monte Carlo method
chisq.test(respire, simulate.p.value=T, B=10000)

# Fisher's exact method
fisher.test(respire)

# For non-aggregate data
chisq.test(table(cat1, cat2))

# Mantel-Haenszel Chi-square Test (z=stratum)
mantelhaen.test(x, y = NULL, z = NULL, alternative = c("two.sided", "less", "greater"), correct = TRUE, exact = FALSE, conf.level = 0.95)

# McNemar's Chi-square test
mcnemar.test(x, y=NULL, correct=TRUE)

No comments: