【问题标题】:Is there a way to join tables in gtsummary to obtain CIs?有没有办法在 gtsummary 中加入表格以获得 CI?
【发布时间】:2021-09-17 05:33:36
【问题描述】:

我已成功计算出一个表中的置信区间,但无法将这两个表合并为一个。它似乎不起作用。期望的输出是有 有趣的是没有错误。

library(gtsummary)
library(tidyverse)

tbl2 <-
  trial %>%
  mutate(trt = as.factor(trt)) %>%
  select(grade, response, trt) %>%
  tbl_summary(missing = "no",  type = everything() ~ "categorical")

myci2 <- tbl2$meta_data %>%
  filter(summary_type %in% c("categorical", "dichotomous")) %>%
  select(summary_type, var_label, df_stats) %>%
  unnest(df_stats) %>%
  mutate(
    conf.low = (p - qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
      style_percent(symbol = TRUE),
    conf.high =( p + qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
      style_percent(symbol = TRUE),
    ci = str_glue("{conf.low}, {conf.high}"),
    label = coalesce(variable_levels, var_label),
    row_type = ifelse(summary_type == "dichotomous", "label", "level")
  ) %>%
  select(variable, row_type, label, ci)


finaltbl <- tbl2 %>%
  modify_table_body(
    left_join,
    myci2,
    by = c("variable", "row_type", "label")
  )

【问题讨论】:

  • 请提供您的数据(样本)。
  • 试用数据在gtsummary包中
  • 加载包时,数据集将可用。谢谢

标签: r gtsummary


【解决方案1】:

问题在于,默认情况下,新列在输出中是隐藏的。您可以通过使用modify_header()modify_column_unhide() 分配标题来取消隐藏列。

library(gtsummary)
#> #BlackLivesMatter
library(tidyverse)
#> Warning: package 'readr' was built under R version 4.1.1
packageVersion("gtsummary")
#> [1] '1.4.2'

tbl2 <-
  trial %>%
  mutate(trt = as.factor(trt)) %>%
  select(grade, response, trt) %>%
  tbl_summary(missing = "no",  type = everything() ~ "categorical")

myci2 <- 
  tbl2$meta_data %>%
  filter(summary_type %in% c("categorical", "dichotomous")) %>%
  select(summary_type, var_label, df_stats) %>%
  unnest(df_stats) %>%
  mutate(
    conf.low = (p - qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
      style_percent(symbol = TRUE),
    conf.high =( p + qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
      style_percent(symbol = TRUE),
    ci = str_glue("{conf.low}, {conf.high}"),
    label = coalesce(variable_levels, var_label),
    row_type = ifelse(summary_type == "dichotomous", "label", "level")
  ) %>%
  select(variable, row_type, label, ci)


finaltbl <- 
  tbl2 %>%
  modify_table_body(
    left_join,
    myci2,
    by = c("variable", "row_type", "label")
  ) %>% 
  # adding a column header UNHIDES the column (you could also use `modify_column_unhide()`)
  modify_header(ci = "**95% CI**")

reprex package (v2.0.1) 于 2021-09-17 创建

仅供参考,在 gtsummary 的下一个版本中,有一个函数可以添加 CI,您无需自己计算:add_ci()

【讨论】:

  • 这是个好消息!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多