使用 mutate 函数如何最好地提取 PropCIs 包中的置信区间?

How best will one extract confidence intervals in PropCIs package using mutate function?

我试图提取使用 PropCIs 包生成的置信区间,但我似乎没有做对。有什么帮助吗?我收到此警告:

警告信息:

1:mutate() 输入问题 conf.low
i 条件的长度 > 1 且仅使用第一个元素 i 输入 conf.lowPropCIs::exactci(n, N, 0.95)[["conf.int"]][[1]].
i 错误发生在第 1 组:trt = "Drug A".

我也尝试过使用 map2,但无法理解它。

# Purpose:  Calculate exact (Clopper-Pearson) confidence intervals

# Libraries ----

library(gtsummary)
library(tidyverse)
library(PropCIs)

# Problem: I would like to get the exact confidence intervals based on the
# proportions


# Code ----

trial %>%
  filter(!is.na(response)) %>%
  mutate(trt = as.factor(trt),
         response  = as.factor(response)) %>%
  group_by(trt) %>%
  count(response) %>%
  mutate( N = sum(n),
          conf.low = exactci(n, N, 0.95) [["conf.int"]][[1]],
          conf.high = exactci(n, N, 0.95) [["conf.int"]][[2]])

exactci(67, 95, 0.95) [["conf.int"]][[1]] # 0.60
exactci(67, 95, 0.95) [["conf.int"]][[2]] # 0.79

# Any way o doing in map2?
map2(c(67, 28), c(95, 95), ~exactci(.x, .y, 0.95))

通过一位同事的深刻见解,我意识到使用 DescTools 包可以实现同样的目的。下面是解决方法。

# Use DescTools
library(DescTools)

trial %>%
  filter(!is.na(response)) %>%
  mutate(trt = as.factor(trt),
         response  = as.factor(response)) %>%
  group_by(trt) %>%
  count(response) %>%
  mutate( N = sum(n),
          ci = Format(BinomCI(n, N, 0.95, method = "clopper-pearson"))) %>%
  dtplyr::lazy_dt() %>% as_tibble() %>%
  mutate(ci.lwr.ci = round(as.numeric(ci.lwr.ci) * 100,1),
         ci.upr.ci = round(as.numeric(ci.upr.ci) * 100,1),
         ci.est = as.numeric(ci.est) *100) %>%
  mutate(ci = str_glue("{ci.lwr.ci}",  "{ci.upr.ci}", .sep = ","))

exactci(67, 95, 0.95) [["conf.int"]][[1]] # 0.6029022
exactci(33, 98, 0.95) [["conf.int"]][[1]] # 0.2443701

只需在 dplyr 中添加 rowwise() 和另一个变异,它应该可以正常工作,警告

the condition has length > 1 and only the first element will be used

很常见,不应忽略,这意味着该函数不支持矢量化操作(在本例中为行操作),但用户希望它执行此操作。因此,您提供了许多输入并期望有许多输出,或者换句话说,您希望执行一项操作,一次接受一个输入,一次获得一个输出。 可以试试这个解决方法:

library(gtsummary)
library(tidyverse)
library(PropCIs)
trial %>%
  filter(!is.na(response)) %>%
  mutate(trt = as.factor(trt),
         response  = as.factor(response)) %>%
  group_by(trt) %>%
  count(response)  %>% 
  mutate( N = sum(n)) %>% 
  rowwise() %>% 
  mutate(conf.low = exactci(n, N, 0.95) [["conf.int"]][[1]],
         conf.high = exactci(n, N, 0.95) [["conf.int"]][[2]])

我希望下面是您的预期输出:

# A tibble: 4 x 6
# Rowwise:  trt
#  trt    response     n     N conf.low conf.high
#  <fct>  <fct>    <int> <int>    <dbl>     <dbl>
#1 Drug A 0           67    95    0.603     0.794
#2 Drug A 1           28    95    0.206     0.397
#3 Drug B 0           65    98    0.561     0.756
#4 Drug B 1           33    98    0.244     0.439