计算R中4个组的单个比例

时间:2019-05-08 12:53:56

标签: r data.table

这是我的数据

  tub=structure(list(Тub = c(1L, 2L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 
1L, 1L, 2L, 1L, 0L, 0L, 1L, 0L, 2L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 
2L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 3L, 0L, 1L, 1L, 1L, 1L, 
0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 3L, 
2L, 0L, 1L, 0L, 3L, 2L, 2L, 0L, 0L, 0L, 1L, 0L, 0L, 3L, 1L, 1L, 
3L, 1L)), .Names = "tub", class = "data.frame", row.names = c(NA, 
-75L))

我只能使用一根色谱柱。它具有四个类别0、1、2、3。所以n = 75 我如何使用prop.test计算类别百分比和它们之间差异的统计显着性水平(p-value) 现在我得到了错误

Error in prop.test(tub$Тюбинген) : 
  argument "n" is missing, with no default

2 个答案:

答案 0 :(得分:0)

prop.test适用于2 x 2矩阵。也许您正在寻找卡方检验?

chisq.test(table(tub$tub))

    Chi-squared test for given probabilities

data:  table(tub$tub)
X-squared = 33.96, df = 3, p-value = 2.02e-07

或者,您可以分别检查两列的每种组合,例如0和1的值。

prop.test(table(tub$tub)[1:2])

    1-sample proportions test with continuity correction

data:  table(tub$tub)[1:2], null probability 0.5
X-squared = 0.79032, df = 1, p-value = 0.374
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
 0.4331124 0.6879144
sample estimates:
        p 
0.5645161 

这是一个应用循环,可为您提供所有组合:

apply(combn(1:4,2),2,function(x) prop.test(table(tub$tub)[c(x[1],x[2])]))

答案 1 :(得分:0)

我认为您可以使用chisq.testpairwise.prop.test函数:

> tub <- as.factor(c("1L", "2L", "0L", "2L", "0L", "0L", "0L", "0L", "0L", 
 "1L", "1L", "2L", "1L", "0L", "0L", "1L", "0L", "2L", "1L", "1L", "0L", "0L",
 "1L", "0L", "0L", "2L", "0L", "1L", "1L", "0L", "0L", "1L", "0L", "0L", "0L",
 "3L", "0L", "1L", "1L", "1L", "1L", "0L", "1L", "0L", "1L", "0L", "0L", "0L",
 "1L", "1L", "0L", "0L", "1L", "1L", "0L", "1L", "3L", "2L", "0L", "1L", "0L",
 "3L", "2L", "2L", "0L", "0L", "0L", "1L", "0L", "0L", "3L", "1L", "1L", 
 "3L", "1L"))

> table(tub) # group size
tub
0L 1L 2L 3L 
35 27  8  5 

> round(table(tub)/sum(table(tub)),2) # proportions
tub
  0L   1L   2L   3L 
0.47 0.36 0.11 0.07 

> chisq.test(table(tub), p=rep(1/nlevels(tub),nlevels(tub))) # join prop test

    Chi-squared test for given probabilities

data:  table(tub)
X-squared = 33.96, df = 3, p-value = 2.02e-07

> pairwise.prop.test(table(tub), c(rep(sum(table(tub)),nlevels(tub)))) # pairwise prop test

    Pairwise comparisons using Pairwise comparison of proportions 
data:  table(tub) out of c(rep(sum(table(tub)), nlevels(tub))) 

   0L      1L      2L     
1L 0.74801 -       -      
2L 0.00037 0.00704 -      
3L 2.7e-05 0.00082 0.74801

P value adjustment method: holm