Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 计算数据帧内的置信区间(二项式)_R_Dplyr_Purrr_Confidence Interval_Broom - Fatal编程技术网

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

R 计算数据帧内的置信区间(二项式),r,dplyr,purrr,confidence-interval,broom,R,Dplyr,Purrr,Confidence Interval,Broom,我想得到tibble中比例的置信区间。有没有办法做到这一点 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)) 我已尝试在purrr中使用map,但出现错误: map(df, tidy(binconf(

我想得到tibble中比例的置信区间。有没有办法做到这一点

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))
我已尝试在purrr中使用map,但出现错误:

map(df, tidy(binconf(count, n)))
x[i]中出错:“closure”类型的对象不可子集

我可以使用dplyr计算它们,但我得到的值低于零,例如第2行,或者高于1,例如第5行,这是我不想要的。e、 g

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 

有什么好办法吗?我确实查看了confint_tidy函数,但无法使其正常工作。有什么想法吗?

它可能不整洁,但

> 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 

好像有用

谢谢。这不是我想要的,虽然它确实提供了我想要的结果,但它给了我一些想法。我想我已经解决了。df%>%group_byid%>%mutateci=LISTTIDYBINCOFCOUNT,n%>%unnest%>%clean_names%>%select rownames与来自看门人的clean_names一起工作。
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 
> 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