gtsummary regression table 底部的观测数怎么放?

How to put the number of observations at the bottom of gtsummary regression table?

我想将回归模型集中包含的观测值数量放在 gtsummary table 的底部,与系数估计值在同一列中。将观察数放在列中很简单:

library(dplyr)
library(gtsummary)

df <- mtcars %>% 
  mutate(cyl_miss = if_else(
    cyl == 6,
    NA_real_,
    cyl
  ))
  
model_1 <- lm(
  data = df,
  formula = mpg ~ cyl + disp
)

model_2 <- lm(
  data = df,
  formula = mpg ~ cyl_miss + disp
)

table_1 <- tbl_regression(model_1) %>% 
  add_significance_stars(
    pattern = "{estimate}{stars}",
    thresholds = c(0.001, 0.01, 0.05),
    hide_ci = TRUE,
    hide_p = TRUE,
    hide_se = FALSE
  ) %>% 
  add_n()


table_2 <- tbl_regression(model_2) %>% 
  add_significance_stars(
    pattern = "{estimate}{stars}",
    thresholds = c(0.001, 0.01, 0.05),
    hide_ci = TRUE,
    hide_p = TRUE,
    hide_se = FALSE
  ) %>% 
  add_n()

tbl_merge(
  list(table_1, table_2)
)  

如何将数字(此处为 32 和 25)放入 Beta 列中标记为“N”的行中?

要将 N 添加到 table 的新行,您需要使用 add_glance_table() 函数。示例如下!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.2'
df <- 
  mtcars %>% 
  dplyr::mutate(
    cyl_miss = ifelse(cyl == 6, NA_real_, cyl)
  )

model_1 <- lm(data = df, formula = mpg ~ cyl + disp)
model_2 <- lm(data = df, formula = mpg ~ cyl_miss + disp)

table_1 <- 
  tbl_regression(model_1) %>% 
  add_significance_stars() %>%
  add_glance_table(include = nobs)

table_2 <- 
  tbl_regression(model_2) %>% 
  add_significance_stars() %>%
  add_glance_table(include = nobs)

table_final <- 
  tbl_merge(list(table_1, table_2)) %>%
  # ensure the glance statistics are at the bottom of table
  modify_table_body(~.x %>% dplyr::arrange(row_type == "glance_statistic"))

reprex package (v2.0.1)

于 2021-09-14 创建