我正在尝试为gtsummary包含回归模型的回归表生成多个级别的标题,这些模型应在表中按年份分组。这是一个玩具示例:
library(dplyr)
library(gtsummary)
library(purrr)
set.seed(92922)
df <- tibble(y_1980 = rbinom(n = 10, size = 1, prob = .4),
y_1990 = rbinom(n = 10, size = 1, prob = .7),
x1 = rnorm(10, sd = 1),
x2 = rnorm(10, sd = 2))
tbls <- c("y_1980 ~ x1", "y_1980 ~ x1 + x2", "y_1990 ~ x1", "y_1990 ~ x1 + x2") %>%
map(as.formula) %>%
map(glm,
data = df,
family = binomial(link = "logit")) %>%
map(tbl_regression, exponentiate = TRUE) %>%
map(add_significance_stars, hide_ci = TRUE, hide_p = TRUE, hide_se = FALSE) %>%
map(add_glance_table, include = nobs)
Run Code Online (Sandbox Code Playgroud)
我可以得到这个:
tbls %>%
tbl_merge(tab_spanner = c("1980 (1)", "1980 (2)", "1990 (1)", "1990 (2)")) %>%
modify_table_body(~.x %>% dplyr::arrange(row_type == "glance_statistic"))
Run Code Online (Sandbox Code Playgroud)
但我想要两个级别——年份在上,型号在下:
1980 1990
_______ ______
(1) (2) (1) (2)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
您需要将 gtsummary 表转换为 gt 表。然后您可以用来gt::tab_spanner()放置更高级别的跨越标头。
library(dplyr)
library(gtsummary)
library(purrr)
set.seed(92922)
df <- tibble(y_1980 = rbinom(n = 10, size = 1, prob = .4),
y_1990 = rbinom(n = 10, size = 1, prob = .7),
x1 = rnorm(10, sd = 1),
x2 = rnorm(10, sd = 2))
tbls <- c("y_1980 ~ x1", "y_1980 ~ x1 + x2", "y_1990 ~ x1", "y_1990 ~ x1 + x2") %>%
map(as.formula) %>%
map(glm,
data = df,
family = binomial(link = "logit")) %>%
map(tbl_regression, exponentiate = TRUE) %>%
map(add_significance_stars, hide_ci = TRUE, hide_p = TRUE, hide_se = FALSE) %>%
map(add_glance_table, include = nobs)
tbl <-
tbls %>%
tbl_merge(tab_spanner = FALSE) %>%
modify_table_body(~.x %>% dplyr::arrange(row_type == "glance_statistic"))
show_header_names(tbl)
gt_tbl <-
as_gt(tbl) %>%
gt::tab_spanner(
columns = c(estimate_1, std.error_1, estimate_3, std.error_3),
label = "(1)",
gather = FALSE
) %>%
gt::tab_spanner(
columns = c(estimate_2, std.error_2, estimate_4, std.error_4),
label = "(2)",
gather = FALSE
) %>%
gt::tab_spanner(
columns = c(estimate_1, std.error_1, estimate_2, std.error_2),
label = "1980",
level = 2,
gather = FALSE
) %>%
gt::tab_spanner(
columns = c(estimate_3, std.error_3, estimate_4, std.error_4),
label = "1990",
level = 2,
gather = FALSE
)
Run Code Online (Sandbox Code Playgroud)