多个比例的二项精确置信区间

时间:2017-04-24 05:38:09

标签: r confidence-interval

我正在尝试计算比例的一长串二项式置信区间。我有一个样本列表,其中包含一个用于村庄编号(24个村庄)的列和一个带有+或 - 的列用于测试结果(PCR)。我需要知道每个村庄的+测试结果的比例以及相应的置信区间。下面的代码给出了整组比例的置信区间,但我需要按村庄分解。

a = sum(dat$PCR=='+')
p = length(dat$PCR)
binom.test(a, p, p = 0.5,
       alternative = c("two.sided", "less", "greater"),
       conf.level = 0.95)

1 个答案:

答案 0 :(得分:1)

这是一种tidyverse方法,用假数据说明:

library(tidyverse)
library(broom)

# Fake data
set.seed(2)
dat = data.frame(village=rep(LETTERS[1:24], each=100), 
                 PCR=sample(c("+","-"), 100*24, replace=TRUE))

# Set "+" to be the reference level
dat$PCR = factor(dat$PCR, levels=c("+","-"))

现在,下面的代码首先将dat拆分为village,然后将结果反馈给map_dfmap_dfbinom.test的每个级别的数据运行villagebinom.test返回一个列表,但将其包含在tidy中会将binom.test的输出转换为“整洁”的数据框。因此,我们最终得到一个数据框,其中每一行都是一个村庄binom.test的输出。

split(dat, dat$village) %>% 
        map_df(~ tidy(binom.test(table(.x$PCR), p=0.5, 
                      alternative = c("two.sided", "less", "greater"),
                      conf.level = 0.95)), .id="Village")

binom.test.results
   Village estimate statistic    p.value parameter  conf.low conf.high              method alternative
1        A     0.55        55 0.36820162       100 0.4472802 0.6496798 Exact binomial test   two.sided
2        B     0.54        54 0.48411841       100 0.4374116 0.6401566 Exact binomial test   two.sided
3        C     0.48        48 0.76435343       100 0.3790055 0.5822102 Exact binomial test   two.sided
4        D     0.55        55 0.36820162       100 0.4472802 0.6496798 Exact binomial test   two.sided
5        E     0.43        43 0.19334790       100 0.3313910 0.5328663 Exact binomial test   two.sided
6        F     0.47        47 0.61729941       100 0.3694052 0.5724185 Exact binomial test   two.sided
7        G     0.49        49 0.92041076       100 0.3886442 0.5919637 Exact binomial test   two.sided
8        H     0.49        49 0.92041076       100 0.3886442 0.5919637 Exact binomial test   two.sided
9        I     0.53        53 0.61729941       100 0.4275815 0.6305948 Exact binomial test   two.sided
10       J     0.50        50 1.00000000       100 0.3983211 0.6016789 Exact binomial test   two.sided
11       K     0.49        49 0.92041076       100 0.3886442 0.5919637 Exact binomial test   two.sided
12       L     0.50        50 1.00000000       100 0.3983211 0.6016789 Exact binomial test   two.sided
13       M     0.45        45 0.36820162       100 0.3503202 0.5527198 Exact binomial test   two.sided
14       N     0.59        59 0.08862608       100 0.4871442 0.6873800 Exact binomial test   two.sided
15       O     0.41        41 0.08862608       100 0.3126200 0.5128558 Exact binomial test   two.sided
16       P     0.50        50 1.00000000       100 0.3983211 0.6016789 Exact binomial test   two.sided
17       Q     0.54        54 0.48411841       100 0.4374116 0.6401566 Exact binomial test   two.sided
18       R     0.52        52 0.76435343       100 0.4177898 0.6209945 Exact binomial test   two.sided
19       S     0.44        44 0.27125302       100 0.3408360 0.5428125 Exact binomial test   two.sided
20       T     0.55        55 0.36820162       100 0.4472802 0.6496798 Exact binomial test   two.sided
21       U     0.47        47 0.61729941       100 0.3694052 0.5724185 Exact binomial test   two.sided
22       V     0.52        52 0.76435343       100 0.4177898 0.6209945 Exact binomial test   two.sided
23       W     0.54        54 0.48411841       100 0.4374116 0.6401566 Exact binomial test   two.sided
24       X     0.51        51 0.92041076       100 0.4080363 0.6113558 Exact binomial test   two.sided