Marco B
Marco B

Reputation: 121

Maintaining glance footnotes after merge

I’d like to get the same results as those from gtsummary::add_glance_source_note() when creating a gtsummary::tbl_merge().

The function itself takes a tbl_regression for an argument, so there’s no using it in the merge pipeline, and if I add the notes to individual tables, they are lost when tables are merged.

library(tidyverse)
library(gtsummary)
library(nycflights13)

lm_1 <- lm(arr_delay ~ air_time, flights)

tbl_1 <- tbl_regression(lm_1, exponentiate = F) %>% 
    add_glance_source_note(include = c('r.squared'))

lm_2 <- lm(distance ~ air_time, flights)

tbl_2 <- tbl_regression(lm_2, exponentiate = F) %>% 
    add_glance_source_note(include = c('r.squared'))

tbl_1
tbl_2

Both tables have footnote indicating their model’s R squared. However, when I merge the tables, the fit information in footnotes is lost:

table.pub <- tbl_merge(
    list(tbl_1, tbl_2),
        tab_spanner = c("Delay", "Distance")
    )

Is there any way to keep the “glance” information, or re-attach it in the final merged table?

Thanks!

Upvotes: 2

Views: 388

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11699

UPDATE: As of gtsummary v.1.4.0 this is more easily accomplished using add_glance_table().

library(gtsummary)
library(nycflights13)
packageVersion("gtsummary")
#> [1] '1.4.0'

tbl_1 <- 
  lm(arr_delay ~ air_time, flights) %>%
  tbl_regression(exponentiate = F) %>% 
  add_glance_table(include = c('r.squared'))

tbl_2 <- 
  lm(distance ~ air_time, flights) %>%
  tbl_regression(exponentiate = F) %>% 
  add_glance_table(include = c('r.squared'))

tbl <-
  tbl_merge(
    list(tbl_1, tbl_2),
    tab_spanner = c("**Delay**", "**Distance**")
  )

enter image description here Created on 2021-04-15 by the reprex package (v2.0.0)

PREVIOUS RESPONSE:

The glance statistics are added as a source note. The tricky thing is that source notes apply to the entire table. It's entirely clear what the statistics refer to when you have a single tbl_regression() table. But once one or more are merged, it's not clear how the source notes should be presented. For that reason they are not presented after the merge.

But, the note is saved within the gtsummary table, and you can print them. In the example below, I label each of the R2 values by the outcome of the model and add them to the merged table.

Happy Programming!

library(tidyverse)
library(gtsummary)
library(nycflights13)

lm_1 <- lm(arr_delay ~ air_time, flights)

tbl_1 <- tbl_regression(lm_1, exponentiate = F) %>% 
  add_glance_source_note(include = c('r.squared'))

lm_2 <- lm(distance ~ air_time, flights)

tbl_2 <- tbl_regression(lm_2, exponentiate = F) %>% 
  add_glance_source_note(include = c('r.squared'))

tbl_1
tbl_2

tbl_merge(
  list(tbl_1, tbl_2),
  tab_spanner = c("**Delay**", "**Distance**")
) %>%
  as_gt() %>%
  gt::tab_source_note(
    str_glue("Delay {tbl_1$list_output$source_note}; ",
             "Distance {tbl_1$list_output$source_note}")
  )

enter image description here

Upvotes: 1

Related Questions