Spanning header for gtsummary regression table

2.1k views Asked by At

I am using gtsummary package to tabulate my regression results.

With difficulty, I have tried to give my table a spanning header using the following function modify_spanning_header(starts_with("stat_") ~ "**Logistic regression for years in US states**").

When I use this function along with the code below, I get the following response:

Error: Can't join on `x$column` x `y$column` because of incompatible types.
ℹ `x$column` is of type <character>>.
ℹ `y$column` is of type <integer>>.

Any idea what this could be? The full code which includes dummy data and packages is as follows:

 # load packages
library(gtsummary)


# dummy data 
crime <-data.frame(State = sample(c("SF", "AR", "NYC","MN"),13000,replace = TRUE),
                   Year = sample(as.factor(c(1990, 2000)),13000, replace = TRUE)
                   )

# logistic model with visual  
glm(Year ~ State, data = crime, family = binomial) %>%
  tbl_regression(exponentiate = TRUE)

I am trying to follow and reproduce example two in this vignette - see here.

1

There are 1 answers

2
Daniel D. Sjoberg On BEST ANSWER

This issue you're experiencing is that you're selecting all columns that begin with "stat_". But in a tbl_regression() table, there are no columns that begin with "stat_". Use the helper function show_header_names() to print the current column names along with their headers. This will help guide you to select the appropriate columns. Example below.

# load packages
library(gtsummary)


# dummy data 
crime <-data.frame(State = sample(c("SF", "AR", "NYC","MN"),13000,replace = TRUE),
                   Year = sample(as.factor(c(1990, 2000)),13000, replace = TRUE)
)

# logistic model with visual  
tbl <- 
  glm(Year ~ State, data = crime, family = binomial) %>%
  tbl_regression(exponentiate = TRUE)

show_header_names(tbl)
#> 
#> 
#> Column Name   Column Header      
#> ------------  -------------------
#> label         **Characteristic** 
#> estimate      **OR**             
#> ci            **95% CI**         
#> p.value       **p-value**
#> i As a usage guide, the code below re-creates the current column headers.
#>   modify_header(update = list(
#>     label ~ "**Characteristic**",
#>     estimate ~ "**OR**",
#>     ci ~ "**95% CI**",
#>     p.value ~ "**p-value**"
#>   ))
# adding header here
tbl %>%
  modify_spanning_header(
    c(estimate, ci, p.value) ~ 
      "**Logistic regression for years in US states**")

enter image description here

Created on 2020-10-21 by the reprex package (v0.3.0)