R语言检验类别型变量独立性

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,拒绝原假设。不独立!

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值