【问题标题】:dplyr::group_modify to apply functiondplyr::group_modify 应用函数
【发布时间】:2019-10-22 15:59:30
【问题描述】:

我想对数据框中的每个组应用一个函数(计算二项式置信区间)。假设我有一个包含以下内容的数据框:

 df <- data.frame(group_name = sample(letters[1:5], 20, replace = T), numbers = 1:20, other_numbers = 51:70)

我按 group_name 分组并创建一个向量以输入到 exactci 函数中:

install.packages('PropCIs');library('PropCIs') 

df2 <- df %>% 
   group_by(group_name) %>% 
   vector = c(numbers, other_numbers) %>%
   mutate(CI = exactci(vector, conf.level = 0.95))

但我的二项式变量的水平不可用。

理想情况下,除了包含 CI 的数据框中的新变量之外,我还想应用 dplyr::group_modify 创建一个表,其中包含每个组的二项式变量的置信区间。

【问题讨论】:

  • 也许 broom 包在这种情况下会有所帮助:它将模型或统计测试的输出转换为小数据框。但是我不确定它是否与 PropCI 很好地集成。见broom.tidyverse.org

标签: r dataframe dplyr


【解决方案1】:

我试图让它尽可能干净。我敢打赌,有人可以制作出看起来更干净的代码。干杯。

library(tidyverse)

df <- data.frame(group_name = sample(letters[1:5], 20, replace = T), numbers = 1:20, other_numbers = 51:70)

mean_ci <- function(x, conf = 0.95) {
  se <- sd(x) / sqrt(length(x))
  alpha <- 1 - conf
  mean(x) + se * qnorm(c(alpha / 2, 1 - alpha / 2))
}

df2 <- df %>% 
  group_by(group_name) %>%
  mutate(n_LCI = mean_ci(numbers)[1],
         n_UCI = mean_ci(numbers)[2],
         on_LCI = mean_ci(other_numbers)[1],
         on_UCI = mean_ci(other_numbers)[2])

【讨论】:

  • 但我想使用指定的函数,我不想计算两个变量的平均 CI。我想要二项式变量(数字和其他数字)的两个级别的置信区间。
【解决方案2】:

我不完全确定我是否正确使用了exactci(),但也许nest() 可以提供一些帮助。

df <- data.frame(
  group_name = sample(letters[1:5], 20, replace = T), 
  numbers = 1:20, 
  other_numbers = 51:70
)

library(PropCIs)
library(tidyverse) # tidyverse loads both dplyr and tidyr

df %>%
  nest(CI = -group_name) %>%
  mutate(CI = map(CI, ~ exactci(x = .x$numbers,
                                n = .x$other_numbers,
                                conf.level = .95) %>%
    unlist())
  ) %>%
  unnest_wider(CI)

#> # A tibble: 5 x 15
#>   group_name conf.int1 conf.int2 conf.int3 conf.int4 conf.int5 conf.int6
#>   <fct>          <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
#> 1 c           0.000496    0.115     0.155     0.184      0.104     0.327
#> 2 b           0.00469     0.0722    0.132     0.270     NA        NA    
#> 3 a           0.0118      0.0508    0.125     0.157      0.237     0.340
#> 4 e           0.0206      0.0403    0.104     0.135      0.145     0.165
#> 5 d           0.0302      0.0615    0.0829    0.0936     0.200     0.254
#> # ... with 8 more variables: conf.int7 <dbl>, conf.int8 <dbl>,
#> #   conf.int9 <dbl>, conf.int10 <dbl>, conf.int11 <dbl>, conf.int12 <dbl>,
#> #   conf.int13 <dbl>, conf.int14 <dbl>

reprex package (v0.3.0) 于 2019 年 10 月 22 日创建

nest() 的作用类似于group_by()map(),便于应用exactci() 等函数。

希望这会有所帮助!

【讨论】:

  • 这对您提供的示例数据集有用吗?我收到 CI 找不到的错误。
【解决方案3】:

我不确定您是否(或如何)为每个组计算一个单个 CI。但是您可以按行方式运行exactci,如下所示:

library('PropCIs') 
df <- data.frame(group_name = sample(letters[1:5], 20, replace = T), 
                 numbers = 1:20, 
                 other_numbers = 51:70)  

df2 <- df %>% 
    rowwise() %>% 
    mutate(myci = list(exactci(numbers, other_numbers, .95) %>% unlist )) %>% 
    unnest_wider(myci)
  
# A tibble: 20 x 5
   group_name numbers other_numbers conf.int1 conf.int2
   <chr>        <int>         <int>     <dbl>     <dbl>
 1 d                1            51  0.000496     0.104
 2 a                2            52  0.00469      0.132
 3 d                3            53  0.0118       0.157
 4 c                4            54  0.0206       0.179
 5 c                5            55  0.0302       0.200
 6 c                6            56  0.0403       0.219
 7 c                7            57  0.0508       0.237
 8 e                8            58  0.0615       0.254
 9 d                9            59  0.0722       0.270
10 c               10            60  0.0829       0.285
11 e               11            61  0.0936       0.300
12 b               12            62  0.104        0.314
13 e               13            63  0.115        0.327
14 b               14            64  0.125        0.340
15 a               15            65  0.135        0.352
16 a               16            66  0.145        0.364
17 c               17            67  0.155        0.375
18 e               18            68  0.165        0.386
19 c               19            69  0.175        0.396
20 c               20            70  0.184        0.406

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多