要添加成对标准化均差 (SMD),您首先需要定义一个函数来计算并返回成对 SMD 估计值。完成此操作后,您可以使用通用函数将其添加到 gtsummary 表中add_stat()
。下面的例子!
library(gtsummary)
library(tidyverse)
# function to calculate pairwise smd
pairwise_smd <- function(data, variable, by, ...) {
data <-
dplyr::select(data, all_of(c(variable, by))) %>%
rlang::set_names(c("variable", "by")) %>%
dplyr::filter(complete.cases(.)) %>%
arrange(desc(.data$by))
tibble(exclude = unique(data$by)) %>%
mutate(
include = map_chr(.data$exclude, ~unique(data$by) %>% setdiff(.x) %>% paste(collapse = " vs. ")),
data_subset =
map(
.data$exclude,
~data %>%
filter(!.data$by %in% .x) %>%
mutate(by = factor(.data$by))
),
smd = map_dbl(.data$data_subset, ~smd::smd(.x$variable, .x$by)$estimate)
) %>%
select(include, smd) %>%
spread(include, smd)
}
tbl <-
trial %>%
select(age, grade, stage) %>%
tbl_summary(
by = grade,
statistic = list(all_continuous() ~ "{mean} ({sd})"),
missing = "no"
) %>%
add_stat(fns = everything() ~ pairwise_smd)
Created on 2021-10-27 by the reprex package https://reprex.tidyverse.org (v2.0.1)