R:按两列应用 Pearson 的 Chi-square 测试

R: applying Pearson's Chi-square test by two columns

我刚开始在 R 中编码,我有一个关于一次对数据集应用 Chi-square 测试 2 列的问题。

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

我之前尝试过 Chi-square 拟合优度,预期概率是我从所有正常样本中取平均值计算得出的。

我使用的代码是:

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

这次,我想对肿瘤及其匹配的正常组织进行皮尔逊Chi-square检验(不是拟合优度)。

所以,我想 运行 Chi-square 通过两列进行测试:原发性肿瘤 1 + 正常 1 ... 那么接下来,原发肿瘤2 + 正常2

并获得 table 的 Chi-square 统计数据和 p-values。 (在这种情况下,我必须使用调整后的 p-values 对吗?因为我 运行 它在 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

我试过像以前一样使用应用函数来获得合适的效果,但我无法让它工作。

谢谢

您可以考虑进行 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