简体   繁体   English

计算数据帧内的置信区间(二项式)

[英]Calculate confidence intervals (binomial) within data frame

I want to get the confidence intervals for proportions within my tibble. 我想获得小插曲中各个部分的置信区间。 Is there a way of doing this? 有办法吗?

library(tidyverse)
library(Hmisc)
library(broom)


df <- tibble(id = c(1, 2, 3, 4, 5, 6),
  count = c(4, 1, 22, 4545, 33, 23),
           n = c(22, 65, 34, 6323, 35, 45))

Which looks like this: 看起来像这样:

# A tibble: 6 x 3
     id count     n
  <dbl> <dbl> <dbl>
1     1     4    22
2     2     1    65
3     3    22    34
4     4  4545  6323
5     5    33    35
6     6    23    45

Using binconf from Hmisc and tidy from broom the solution could be from any package: 使用Hmisc binconfbroomtidy ,解决方案可以来自任何软件包:

The intervals for the first row: 第一行的时间间隔:

tidy(binconf(4, 22))

# A tibble: 1 x 4
  .rownames PointEst  Lower Upper
  <chr>        <dbl>  <dbl> <dbl>
1 ""           0.182 0.0731 0.385

I have tried using map in purrr but get errors: 我曾尝试在purrr使用map ,但出现错误:

map(df, tidy(binconf(count, n)))

Error in x[i] : object of type 'closure' is not subsettable x [i]中的错误:“关闭”类型的对象不可子集化

I could just calculate them using dplyr but I get values below zero (eg row 2) or above one (eg row 5), which I don't want. 我可以使用dplyr来计算它们,但是我得到的值小于零(例如第2行)或大于1(例如第5行),我不希望这样。 eg 例如

df %>% 
  mutate(prop = count / n) %>%
  mutate(se = (sqrt(prop * (1-prop)/n))) %>% 
  mutate(lower = prop - (se*1.96)) %>% 
  mutate(upper = prop + (se*1.96))

# A tibble: 6 x 7
     id count     n   prop      se   lower  upper
  <dbl> <dbl> <dbl>  <dbl>   <dbl>   <dbl>  <dbl>
1     1     4    22 0.182  0.0822   0.0206 0.343 
2     2     1    65 0.0154 0.0153  -0.0145 0.0453
3     3    22    34 0.647  0.0820   0.486  0.808 
4     4  4545  6323 0.719  0.00565  0.708  0.730 
5     5    33    35 0.943  0.0392   0.866  1.02  
6     6    23    45 0.511  0.0745   0.365  0.657 

Is there a good way of doing this? 有这样做的好方法吗? I did have a look at the confint_tidy() function, but could not get that to work. 我确实confint_tidy()函数,但无法正常工作。 Any ideas? 有任何想法吗?

It may not be tidy but 它可能不整洁,但

> as.tibble(cbind(df, binconf(df$count, df$n)))
# A tibble: 6 x 6
     id count     n PointEst    Lower  Upper
  <dbl> <dbl> <dbl>    <dbl>    <dbl>  <dbl>
1     1     4    22   0.182  0.0731   0.385 
2     2     1    65   0.0154 0.000789 0.0821
3     3    22    34   0.647  0.479    0.785 
4     4  4545  6323   0.719  0.708    0.730 
5     5    33    35   0.943  0.814    0.984 
6     6    23    45   0.511  0.370    0.650 

seems to work 似乎有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM