R比例的置信区间因子

5
我试图总结一份家庭调查的数据,因此我的大部分数据都是分类(因子)数据。我想用某些问题的响应频率的图表来概括它(例如,百分比条形图回答某些问题的家庭数量,并显示置信区间的误差线)。我找到了这个很棒的教程,我本以为这就是我祈求的答案(http://www.cookbook-r.com/Manipulating_data/Summarizing_data/),但事实证明这只能帮助连续数据。

我需要的是类似的东西,它将允许我计算计数的比例和这些比例的标准误差/置信区间。

基本上,我想要能够为我调查数据中提出的每个问题生成像这样的摘要表:

# X5employf X5employff  N(count) proportion SE of prop.  ci of prop
#   1          1        20    0.64516129    ?             ?       
#   1          2         1    0.03225806    ?             ?  
#   1          3         9    0.29032258    ?             ?
#   1          NA        1    0.290322581    ?            ?
#   2          4             1    0.1            ?             ?


structure(list(X5employf = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor"), X5employff = structure(c(1L, 2L, 3L, NA, 4L, 5L, 6L, 7L, 8L, 4L, 5L, 6L, 7L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8"), class = "factor"), count = c(20L, 1L, 9L, 1L, 1L, 5L, 2L, 1L, 1L, 4L, 5L, 4L, 1L)), .Names = c("X5employf", "X5employff", "count"), row.names = c(NA, -13L), class = "data.frame")

我希望能使用ggplot(或类似工具)绘制条形图,使用这些摘要数据并显示置信区间误差线。我曾尝试修改上面教程中提供的代码来计算上述列,但由于我是R的新手,所以有些困难!我一直在尝试使用ggply包,但对语法不太熟悉,因此我只能使用以下代码到达目前为止:
> X5employ_props <- ddply(X5employ_counts, .(X5employf), transform, prop=count/sum(count))

但最终我得到了这个:
   X5employf X5employff count      prop
1          1          1    20 1.0000000
2          1          2     1 1.0000000
3          1          3     9 1.0000000
4          2          4     1 0.2000000
5          3          4     4 0.8000000
6          2          5     5 0.5000000
7          3          5     5 0.5000000
8          2          6     2 0.3333333
9          3          6     4 0.6666667
10         2          7     1 0.5000000
11         3          7     1 0.5000000
12         2          8     1 1.0000000
13         1       <NA>     1 1.0000000

我的所有比例都为1,这可能是因为它们是跨计算而不是跨计算。

我想知道是否有人可以帮助我或者知道能够完成这个工作的包/代码!


1
你是否了解http://docs.ggplot2.org/current/geom_errorbar.html?你可以使用`stat = "identity"`参数绘制条形图,请参阅http://docs.ggplot2.org/current/geom_bar.html以获取更多详细信息。为了获得更好的响应,建议您提供一些可重现的数据。 - Roman Luštrik
嗨Roman,是的,我已经阅读了有关geom_errorbar的ggplot2文档,并且已经生成了我的条形图。但是,geom_errorbar需要您指定绘制误差线的限制-这就是为什么我首先尝试总结我的数据的原因。理想情况下,我正在寻找一种自动化此过程的方法,因为我有49个变量。 - marty_c
前三个向量整数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 因子1 1 3 1 1 1 3 1 1 1 3 1 1 1 2 2 3 3 3 1 2 2 2 2 2 1 1 1 3 3 3 3 3 3 2 1 1 3 1 3 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 因子2 1 4 <NA> 1 2 4 3 1 1 6 1 1 1 5 5 6 7 5 1 6 6 7 5 4 1 3 1 6 5 5 5 6 4 5 3 3 5 1 4 5 1 1 1 1 1 3 3 3 1 3 1 1 1 3 8 - marty_c
抱歉,我的错。这个怎么样:structure(list(X5employf = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor"), X5employff = structure(c(1L, 2L, 3L, NA, 4L, 5L, 6L, 7L, 8L, 4L, 5L, 6L, 7L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8"), class = "factor"), count = c(20L, 1L, 9L, 1L, 1L, 5L, 2L, 1L, 1L, 4L, 5L, 4L, 1L)), .Names = c("X5employf", "X5employff", "count"), row.names = c(NA, -13L), class = "data.frame") - marty_c
你有特定的方法想法吗?(链接为:http://www.nucmed.com/documents/Academic/Grants/GBM_SVM_BN/Articles/Two-Sided%20Confidence%20Intervals%20for%20the%20Single%20Proportion.pdf) - dardisco
显示剩余3条评论
2个回答

3

有很多方法可以计算二项式置信区间,我怀疑在哪种方法最好方面还没有达成共识。尽管如此,以下是一种使用多种不同方法计算二项式置信区间的方法。我不确定这是否有帮助。

library(binom)

x <- c(3, 4, 5, 6, 7)
n <- rep(10, length(x))

binom.confint(x, n, conf.level = 0.95, methods = "all")

          method x  n      mean      lower     upper
1  agresti-coull 3 10 0.3000000 0.10333842 0.6076747
2  agresti-coull 4 10 0.4000000 0.16711063 0.6883959
3  agresti-coull 5 10 0.5000000 0.23659309 0.7634069
4  agresti-coull 6 10 0.6000000 0.31160407 0.8328894
5  agresti-coull 7 10 0.7000000 0.39232530 0.8966616
6     asymptotic 3 10 0.3000000 0.01597423 0.5840258
7     asymptotic 4 10 0.4000000 0.09636369 0.7036363
8     asymptotic 5 10 0.5000000 0.19010248 0.8098975
9     asymptotic 6 10 0.6000000 0.29636369 0.9036363
10    asymptotic 7 10 0.7000000 0.41597423 0.9840258
11         bayes 3 10 0.3181818 0.09269460 0.6058183
12         bayes 4 10 0.4090909 0.15306710 0.6963205
13         bayes 5 10 0.5000000 0.22352867 0.7764713
14         bayes 6 10 0.5909091 0.30367949 0.8469329
15         bayes 7 10 0.6818182 0.39418168 0.9073054
16       cloglog 3 10 0.3000000 0.07113449 0.5778673
17       cloglog 4 10 0.4000000 0.12269317 0.6702046
18       cloglog 5 10 0.5000000 0.18360559 0.7531741
19       cloglog 6 10 0.6000000 0.25266890 0.8272210
20       cloglog 7 10 0.7000000 0.32871659 0.8919490
21         exact 3 10 0.3000000 0.06673951 0.6524529
22         exact 4 10 0.4000000 0.12155226 0.7376219
23         exact 5 10 0.5000000 0.18708603 0.8129140
24         exact 6 10 0.6000000 0.26237808 0.8784477
25         exact 7 10 0.7000000 0.34754715 0.9332605
26         logit 3 10 0.3000000 0.09976832 0.6236819
27         logit 4 10 0.4000000 0.15834201 0.7025951
28         logit 5 10 0.5000000 0.22450735 0.7754927
29         logit 6 10 0.6000000 0.29740491 0.8416580
30         logit 7 10 0.7000000 0.37631807 0.9002317
31        probit 3 10 0.3000000 0.08991347 0.6150429
32        probit 4 10 0.4000000 0.14933907 0.7028372
33        probit 5 10 0.5000000 0.21863901 0.7813610
34        probit 6 10 0.6000000 0.29716285 0.8506609
35        probit 7 10 0.7000000 0.38495714 0.9100865
36       profile 3 10 0.3000000 0.08470272 0.6065091
37       profile 4 10 0.4000000 0.14570633 0.6999845
38       profile 5 10 0.5000000 0.21765974 0.7823403
39       profile 6 10 0.6000000 0.30001552 0.8542937
40       profile 7 10 0.7000000 0.39349089 0.9152973
41           lrt 3 10 0.3000000 0.08458545 0.6065389
42           lrt 4 10 0.4000000 0.14564246 0.7000216
43           lrt 5 10 0.5000000 0.21762124 0.7823788
44           lrt 6 10 0.6000000 0.29997837 0.8543575
45           lrt 7 10 0.7000000 0.39346107 0.9154146
46     prop.test 3 10 0.3000000 0.08094782 0.6463293
47     prop.test 4 10 0.4000000 0.13693056 0.7263303
48     prop.test 5 10 0.5000000 0.20142297 0.7985770
49     prop.test 6 10 0.6000000 0.27366969 0.8630694
50     prop.test 7 10 0.7000000 0.35367072 0.9190522
51        wilson 3 10 0.3000000 0.10779127 0.6032219
52        wilson 4 10 0.4000000 0.16818033 0.6873262
53        wilson 5 10 0.5000000 0.23659309 0.7634069
54        wilson 6 10 0.6000000 0.31267377 0.8318197
55        wilson 7 10 0.7000000 0.39677815 0.8922087

我不确定你需要什么,但这里是创建表格的代码,我认为它包含了你所需的所有参数。我从 Package binom 中使用 Agresti-Coull 方法挖出了这段代码。

conf.level <- 0.95

x <-  c( 4, 5, 6)     # successes
n <-  c(10,10,10)     # trials

method <- 'ac'

# source code from package binom:

xn <- data.frame(x = x, n = n)
  all.methods <- any(method == "all")
  p <- x/n
  alpha <- 1 - conf.level
  alpha <- rep(alpha, length = length(p))
  alpha2 <- 0.5 * alpha
  z <- qnorm(1 - alpha2)
  z2 <- z * z
  res <- NULL
  if(any(method %in% c("agresti-coull", "ac")) || all.methods) {
    .x <- x + 0.5 * z2
    .n <- n + z2
    .p <- .x/.n
    lcl <- .p - z * sqrt(.p * (1 - .p)/.n)
    ucl <- .p + z * sqrt(.p * (1 - .p)/.n)
    res.ac <- data.frame(method = rep("agresti-coull", NROW(x)),
                         xn, mean = p, lower = lcl, upper = ucl)
    res <- res.ac    
  }

SE <- sqrt(.p * (1 - .p)/.n)
SE

以下是所有数据和参数的表格。 参见:http://www.stat.sc.edu/~hendrixl/stat205/Lecture%20Notes/Confidence%20Interval%20for%20the%20Population%20Proportion.pdf
my.table <- data.frame(res, SE)
my.table

         method x  n mean     lower     upper        SE
1 agresti-coull 4 10  0.4 0.1671106 0.6883959 0.1329834
2 agresti-coull 5 10  0.5 0.2365931 0.7634069 0.1343937
3 agresti-coull 6 10  0.6 0.3116041 0.8328894 0.1329834

我还没有检查这些估计值是否与Agresti的书中的任何示例相匹配。 然而,下面来自佛罗里达大学的第一个R函数返回与包binom相同的CI估计值。 下面来自佛罗里达大学的第二个R函数则不会。

http://www.stat.ufl.edu/~aa/cda/R/one-sample/R1/

x <- 4
n <- 10
conflev <- 0.95

addz2ci <- function(x,n,conflev){
   z = abs(qnorm((1-conflev)/2))
   tr = z^2     #the number of trials added
   suc = tr/2   #the number of successes added
   ptilde = (x+suc)/(n+tr)
   stderr = sqrt(ptilde * (1-ptilde)/(n+tr))
   ul = ptilde + z * stderr
   ll = ptilde - z * stderr
   if(ll < 0) ll = 0
   if(ul > 1) ul = 1
   c(ll,ul)
}
# Computes the Agresti-Coull CI for x successes out of n trials
# with confidence coefficient conflev. 

add4ci <- function(x,n,conflev){
   ptilde = (x+2)/(n+4)
   z = abs(qnorm((1-conflev)/2))
   stderr = sqrt(ptilde * (1-ptilde)/(n+4))
   ul = ptilde + z * stderr
   ll = ptilde - z * stderr
   if(ll < 0) ll = 0
   if(ul > 1) ul = 1
   c(ll,ul)
}
# Computes the Agresti-Coull `add 4' CI for x successes out of n trials
# with confidence coefficient conflev. Adds 2 successes and
# 4 trials.

注意,根据上面的第一个链接,当n < 40时不建议使用Agresti-Coull区间。至于您提到的其他软件包,我很少使用它们,但我非常确定您可以在调用这些软件包的R脚本中包含上述代码。

嗨Mark,谢谢,那几乎是我想要的。但是我能否将binom.confint参数的一个元素与ddply包(或其他)组合起来?我希望自动创建包含所有这些值以及计数、标准错误等的表格,然后在ggplot中绘制这些值... 我正在努力实现这样一个目标:能够快速地为数据表中任何变量/一组变量生成这些图,并显示“美观”的结果.... :) - marty_c
嗨马克,再次感谢您的迅速回复,但我不确定这是否完全符合我的需求。首先,我需要每个因素的比例,就像页面顶部的原始示例一样,并从中得出每个变量的标准误差/置信区间。例如,根据我在顶部的示例,女性服用阿司匹林占所有女性患者的比例;服用安慰剂的女性患者占所有女性患者的比例;服用阿司匹林的男性患者占所有男性患者的比例等等。您的示例提供了平均值,我无法理解如何从比例中得出平均值? - marty_c
@marty_c 我不确定如何进一步帮助你,因为我不知道表格中的比例来自哪里:0.9979145、0.5247655、1.0674848 和 0.5291503。如果您编辑帖子以显示您如何获得这四个数字,也许我可以提供更多的帮助。如果这四个比例与您的示例数据集无关,则请显示从您的示例数据集中产生这四个比例的内容,并可能展示如何手动获取这四个比例。 - Mark Miller
标记,我现在已经编辑了帖子顶部的原始表格,并手动计算了比例值。X5employff观测值按X5employf分组的比例非常重要,例如第一行是20/31(其中31是X5employf = 1组中的总观测数)。谢谢。 - marty_c

2
这里提供一种估算多项式95%置信区间的方法。
library(MultinomialCI)

x <- c(20,1,9,1)

multinomialCI(x,alpha=0.05,verbose=FALSE)

#           [,1]      [,2]
# [1,] 0.5161290 0.8322532
# [2,] 0.0000000 0.2193499
# [3,] 0.1612903 0.4774145
# [4,] 0.0000000 0.2193499

我还没有查看源代码以了解如何获取SE。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接