首页 > 解决方案 > R:通过两列应用 Pearson 卡方检验

问题描述

我刚开始在 R 中编码,我有一个关于一次将卡方检验应用于数据集 2 列的问题。

我想做配对分析(肿瘤和正常样本来自同一患者,因此原发肿瘤 1 和正常组织 1 来自同一患者)。我想查看同一患者的肿瘤和正常样本之间分布的差异,并适用于所有 50 名患者。

我之前尝试过卡方拟合优度,我通过从所有正常样本中取平均值来计算预期概率。

我使用的代码是:

apply(mydata, 2, chisq.test, p=myprobability)

这次,我想对肿瘤及其匹配的正常组织进行 Pearson 卡方检验(不是拟合优度)。

所以,我想通过两列运行卡方检验:原发性肿瘤 1 + 正常 1 ...然后接下来,原发性肿瘤 2 + 正常 2

并获得一张卡方统计量和 p 值表。(在这种情况下,我必须使用调整后的 p 值,对吗?因为我在 50 组样本上运行它?)

我的数据如下所示: 在此处输入图像描述

作为一个可复制的例子......

mydata <-
structure(list(Tumor1 = c(17, 28, 80, 63, 20, 
10), Normal1 = c(18, 27, 89, 62, 24, 
11), Tumor2 = c(25, 40, 80, 65, 23, 
11), Normal2 = c(27, 29, 100, 72, 34, 
6)), class = "data.frame", 
row.names = c("trim3", "trim2", "trim1", "add1", "add2", 
"add3"))

head(mydata)

      Tumor1 Normal1 Tumor2 Normal2
trim3     17      18     25      27
trim2     28      27     40      29
trim1     80      89     80     100
add1      63      62     65      72
add2      20      24     23      34
add3      10      11     11       6

我尝试使用 apply 函数,就像我为拟合优度所做的那样,但我无法让它工作。

谢谢

标签: rstatisticsbioinformaticschi-squared

解决方案


您可以考虑进行 Cochran-Mantel-Haenszel 检验,这是通过重复测量来检验两个变量的独立性,在您的情况下,是不同的肿瘤/正常对。所以使用你的例子,我们首先得到一个数组:

test = array(unlist(mydata),dim=c(nrow(mydata),2,ncol(mydata)/2))
test
, , 1

     [,1] [,2]
[1,]   17   18
[2,]   28   27
[3,]   80   89
[4,]   63   62
[5,]   20   24
[6,]   10   11

, , 2

     [,1] [,2]
[1,]   25   27
[2,]   40   29
[3,]   80  100
[4,]   65   72
[5,]   23   34
[6,]   11    6

然后做:

mantelhaen.test(test)

    Cochran-Mantel-Haenszel test

data:  test
Cochran-Mantel-Haenszel M^2 = 5.0277, df = 5, p-value = 0.4125

当然,您可以单独测试每个样本对:

library(broom)
# assign groups to columns
grps = rep(1:(ncol(mydata)/2),each=2)
result = do.call(rbind,lapply(unique(grps),function(i)tidy(chisq.test(mydata[,grps==i]))))
result

# A tibble: 2 x 4
  statistic p.value parameter method                    
      <dbl>   <dbl>     <int> <chr>                     
1     0.569   0.989         5 Pearson's Chi-squared test
2     6.89    0.229         5 Pearson's Chi-squared test

推荐阅读