【问题标题】:How to calculate SMD between 3 groups or more?如何计算 3 组或更多组之间的 SMD?
【发布时间】:2021-12-13 05:26:15
【问题描述】:

我有兴趣通过一个分层变量计算成对标准化平均差 (SMD)。通常这是在两组之间计算,但我们可以在 3 组或更多组之间进行计算吗?

附:我是 gtsummary 包的忠实粉丝,所以我尝试使用example 2 from this amazing package 进行分析,如下所示:

library(tidyverse)
library(gtsummary)
#> #BlackLivesMatter
add_difference_ex2 <-
  trial %>%
  mutate(trt=ifelse(age<40,"Drug C", trt)) %>% 
  select(trt, age, marker, grade, stage) %>%
  tbl_summary(
    by = trt,
    statistic = list(all_continuous() ~ "{mean} ({sd})"),
    missing = "no",
    include = c(age, marker, trt)
  ) %>%
  add_n() %>%
  add_difference(adj.vars = c(grade, stage))
#> 11 observations missing `trt` have been removed. To include these observations, use `forcats::fct_explicit_na()` on `trt` column before passing to `tbl_summary()`.
#> Error: 'tbl_summary'/'tbl_svysummary' object must have a `by=` value with exactly two levels

reprex package (v2.0.1) 于 2021 年 10 月 27 日创建

【问题讨论】:

  • 您是否尝试过使用cobalt 包?有一个help page 和一个section 专门用于它的小插图。

标签: gtsummary balance propensity-score-matching smd


【解决方案1】:

要添加成对标准化均值差 (SMD),您首先需要定义一个函数来计算并返回成对 SMD 估计值。完成后,您可以使用通用函数 add_stat() 将其添加到 gtsummary 表中。下面的例子!

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)

reprex package (v2.0.1) 于 2021 年 10 月 27 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多