有没有办法在 gtsummary 中连接表以获得 CI?
Is there a way to join tables in gtsummary to obtain CIs?
我已经成功计算了 table 中的置信区间,但无法将两个 table 合并为一个。它似乎不起作用。期望的输出是没有错误。
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")
)
问题是默认情况下,新列在输出中是隐藏的。您可以通过将 header 分配给 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()
.
我已经成功计算了 table 中的置信区间,但无法将两个 table 合并为一个。它似乎不起作用。期望的输出是没有错误。
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")
)
问题是默认情况下,新列在输出中是隐藏的。您可以通过将 header 分配给 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**")
仅供参考,在下一版本的 gtsummary 中,有一个函数会添加 CI,您无需自己计算:add_ci()
.