tbl_regression() 函数使用 broom 包中的 tidy 函数来清理模型结果,包括标准误差和 CI 估计。但是,我们可以在计算稳健估计的 tbl_regression(tidy_fun=) 参数中传递一个自定义的整洁器。在下面的示例中,我编写了一个更简洁的包装 jtools::summ() 函数。
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.2'
# build logistic regression model
mod <- glm(response ~ age + trt, data = trial, family = binomial)
# write a tidy function wrapping jtools::summ
tidy_robust <- function(x,
exponentiate = FALSE,
conf.level = 0.95,
robust = c("HC0", "HC1", "HC2", "HC3", "HC4", "HC4m", "HC5"),
...) {
robust <- match.arg(robust)
jtools::summ(x, confint = TRUE, ci.width = conf.level,
exp = exponentiate, robust = robust) %>%
purrr::pluck("coeftable") %>%
as.data.frame() %>%
tibble::rownames_to_column() %>%
setNames(c("term", "estimate", "conf.low", "conf.high",
"statistic", "p.value"))
}
tidy_robust(mod)
#> term estimate conf.low conf.high statistic p.value
#> 1 (Intercept) -1.7424131 -2.918252621 -0.5665735 -2.9043646 0.003679993
#> 2 age 0.0189970 -0.003406793 0.0414008 1.6619255 0.096527704
#> 3 trtDrug B 0.1254909 -0.504805356 0.7557872 0.3902255 0.696369799
# use default robust SE, HC0
tbl1 <-
tbl_regression(
mod,
exponentiate = TRUE,
tidy_fun = tidy_robust
)
# use robust SE, HC1
tbl2 <-
tbl_regression(
mod,
exponentiate = TRUE,
tidy_fun = purrr::partial(tidy_robust, robust = "HC1")
)
由reprex package (v2.0.1) 于 2021-09-18 创建
强大的 SE 报告功能将在 gtsummary 的未来版本中变得更容易。