1

我要再次感谢 Daniel Sjoberg 和其他合作者在 gtsummary 包中不断实现功能。对我来说,R 中最有效的套件之一,用于处理和报告表格/在线结果。

不久前,我寻求帮助,将效应大小 [90%CI] 包含在 gtsummary 包生成的分析表中。然而,在这篇新文章中,我打算更改 ES 计算包,以便为我提供大量索引并获得它们的定性大小。我试图在新代码中实现这个其他包。但是,返回此消息:

变量“年龄”出现错误:.deal_with_cohens_d_arguments 中的错误(x,y,数据):请提供数据参数。

我相信我无法配置该功能(CohenD 对象)。有人可以帮我写代码吗?

我在下面复制了它:

CohenD <- function(data, variable, by, ...) {
  # Cohen's d, Hedges's g (correction=TRUE or FALSE) and Glass’s delta
  ES <- effectsize::cohens_d(data[[variable]] ~ as.factor(data[[by]]),
                             ci=.90,
                             pooled_sd=TRUE,
                             paired=FALSE,
                             correction=TRUE)
 
  # Formatting statistic with CI
  est <- style_sigfig(abs(ES$Cohens_d))
  ci_lower <- style_sigfig(ES$CI_low)
  ci_upper <- style_sigfig(ES$CI_high)
 
  # Returning estimate with CI together
  str_glue("{est} ({ci_lower, ci_upper})")
}

Table <-
  trial %>%
  select(trt, age) %>%
  tbl_summary(by = trt, missing = "no", label = list (age ~ "Age (yrs)"),
              statistic = list(all_continuous() ~ "{mean} ± {sd}"),
              digits = list(all_continuous() ~ c(1,1))) %>%
  bold_labels() %>%
  italicize_levels() %>%
  add_p(test = everything() ~ t.test, pvalue_fun = partial(style_pvalue, digits = 2)) %>%
  add_stat(
    fns = everything() ~ CohenD,
    fmt_fun = NULL,
    header = "**ES (90% CI)**"
  ) %>%
  modify_footnote(add_stat_1 ~ "Hedges's g (90% CI)") %>%
  modify_header(label = "**Variables**", stat_by = "**{level}** (N= {n})")
Table

是否可以在 Table 中包含一个新列或加入该函数已经提供的 ES +/- CI,观察到的 ES 的定性大小(根据一组规则解释一个值)?建议针对此功能:

effectsize::interpret_d(ES$Cohens_d, rules = "cohen1988")

干杯,克里斯蒂亚诺

4

1 回答 1

0

您遇到的问题在于您的用户定义函数CohenD():它不喜欢您传递公式的方式。在下面的示例中,我更正了语法。我还包括对效应大小的解释。

library(gtsummary)
library(tidyverse)

# function that returns either Cohen's D or the 1988 interpretation of its size
CohenD <- function(data, variable, by, ...) {
  # Cohen's d, Hedges's g (correction=TRUE or FALSE) and Glass’s delta
  ES <- effectsize::cohens_d(data[[variable]], factor(data[[by]]),
                             ci=.90,
                             pooled_sd=TRUE,
                             paired=FALSE,
                             correction=TRUE)
  
  # Formatting statistic with CI
  est <- style_sigfig(ES$Cohens_d)
  ci_lower <- style_sigfig(ES$CI_low)
  ci_upper <- style_sigfig(ES$CI_high)
  
  # Returning estimate with CI together
  tibble(
    cohen_d = stringr::str_glue("{est} ({ci_lower}, {ci_upper})"),
    interpret_d = stringr::str_glue("{effectsize::interpret_d(ES$Cohens_d, rules = 'cohen1988')}")
  )
  
}

tbl <-
  trial %>%
  select(trt, age, marker) %>%
  tbl_summary(by = trt, missing = "no", statistic = all_continuous() ~ "{mean} ± {sd}") %>%
  add_p(test = everything() ~ t.test) %>%
  add_stat(fns = everything() ~ CohenD) %>%
  modify_header(cohen_d = "**ES (90% CI)**", interpret_d = "**Interpretation**")

在此处输入图像描述 reprex 包于 2021-04-15 创建 (v2.0.0 )

于 2020-11-29T14:13:22.847 回答