我刚开始用R编写代码,我有一个问题,那就是一次用2列对数据集应用卡方测试。
我想做一个配对分析(肿瘤和正常样本来自同一患者,因此原发性肿瘤1和正常组织1来自同一患者)。我希望看到肿瘤和正常样本在同一患者之间的分布差异,并适用于所有50名患者。
我尝试了卡方拟合优度以前,与预期的概率,我计算从所有正常样本的平均值。
我使用的代码是:
apply(mydata, 2, chisq.test, p=myprobability)这一次,我想对肿瘤及其匹配的正常组织进行皮尔逊的卡方检验(不适合)。
所以,我想用两列进行卡方检验:原发肿瘤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函数,就像我为合适的地方所做的那样,但我无法让它发挥作用。
谢谢
发布于 2020-03-10 23:16:00
你可以考虑做一个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 testhttps://stackoverflow.com/questions/60626462
复制相似问题