1. chisq.test
卡方检验是一种用途很广的计数资料的假设检验方法,由卡尔·皮尔逊提出。它属于非参数检验的范畴,主要是比较两个及两个以上样本率( 构成比)以及两个分类变量的关联性分析。其根本思想就是在于比较理论频数和实际频数的吻合程度或拟合优度问题。
Usage
chisq.test(x, y = NULL, correct = TRUE, p = rep(1/length(x), length(x)), rescale.p = FALSE, simulate.p.value = FALSE, B = 2000)
# p<0.05 或0.01,两个分类变量不独立!
chisq.test(c(25,59))
x <- c(A = 20, B = 15,c=30)
table(x)
chisq.test(x)
chisq.test(c(Female=59,Male=25))
table(c(Female=59,Male=25))
chisq.test(matrix(c(25,1,59,1),ncol=2))
x <- matrix(c(12, 5, 7, 7), ncol = 2)
table(x)
chisq.test(x)
chisq.test(Arthritis$Improved,Arthritis$Treatment)
mytable <- xtabs(~ Improved + Treatment, data= Arthritis)
chisq.test(mytable)
2. fisher.test
当样本比较小时,用行Fisher精确检验。
Usage
fisher.test(x, y = NULL, workspace = 200000, hybrid = FALSE, hybridPars = c(expect = 5, percent = 80, Emin = 1), control = list(), or = 1, alternative = "two.sided", conf.int = TRUE, conf.level = 0.95, simulate.p.value = FALSE, B = 2000)
# 出现 Warning message:
chisq.test(matrix(c(25,6,59,6),ncol=2))
fisher.test(matrix(c(25,6,59,6),ncol=2))
fisher.test(Arthritis$Sex,Arthritis$Improved)
fisher.test(Arthritis$Improved,Arthritis$Treatment)
3. mantelhaen.test
假设不存在三向相互作用,则对两个名义变量在每个层中条件独立的空值进行Cochran-Mantel-Haenszel卡方检验。
Usage
mantelhaen.test(x, y = NULL, z = NULL, alternative = c("two.sided", "less", "greater"), correct = TRUE, exact = FALSE, conf.level = 0.95)
#者接受的治疗与得到的改善在性别的每一水平下进行独立性检测
mytable<-table(Arthritis$Improved,Arthritis$Treatment,Arthritis$Sex)
mantelhaen.test(mytable) # p-value = 0.0006647,拒绝原假设。不独立!