Search code examples
rgtsummary

How to add test for trend p-value in tbl_regression table using gtsummary package


I am currently trying to add a column that has p-values assessing linear trend linear regression models. I haven't been able to find a solution for this within the documentation. Has anyone found a way around this? If so, could you share with me?

I have included dummy data and code below:

 # install dev versions
remotes::install_github("ddsjoberg/gtsummary@mice_nnet")
remotes::install_github("larmarange/broom.helpers")

# load packages
library(gtsummary)
library(nnet)

theme_gtsummary_compact()

# dummy data 
crime <-data.frame(city = sample(as.factor(c(1, 2, 3,4)),13000,replace = TRUE),
                   sex = sample(c("Male", "Female"),13000,replace = TRUE),
                   year = sample(as.numeric(sample(10:70, 13000, replace = TRUE)))
)

# serperate data sets by sex
crime_f <- crime %>%
  filter(sex == "Female")

crime_m <- crime %>%
  filter(sex == "Male")

# build model for females
mod_f <- lm(year ~ city, data = crime_f, na.action=na.exclude) 

# build model for males
mod_m <- lm(year ~ city, data = crime_m, na.action=na.exclude)

# linear trend test between year and city
# females
mod2_f <- lm(year ~ as.numeric(city), data = crime_f, na.action=na.exclude)

# males
mod2_m <- lm(year ~ as.numeric(city), data = crime_m, na.action=na.exclude)

# make regression table from results
# femlaes
tbl_regression(mod_f,
               exponentiate = TRUE) %>%
  modify_header(estimate ~ "**OR**")

# males
tbl_regression(mod_m,
               exponentiate = TRUE) %>%
  modify_header(estimate ~ "**OR**")


# lm model tabulated with gtsummary  
tbl <- tbl_merge(
  tbls = list(mod_f, mod_m),
  tab_spanner = c("**Female**", "**Male**")
)

Solution

  • The easiest way to to this is to build both model (one regular, and one treating the variable as continuous), then merge the tables together. Example below.

    # load packages
    library(gtsummary)
    theme_gtsummary_compact()
    
    # model cyl as a categorical 
    mod1 <- lm(mpg ~ cyl, data = mtcars %>% dplyr::mutate(cyl = factor(cyl)))
    # model cyl as continuous (p-trend)
    mod2 <- lm(mpg ~ cyl, data = mtcars)
    
    # summarize primary model
    tbl1 <- tbl_regression(mod1)
    # summarize model with p-trend, and hide the estimate and CI
    tbl2 <- tbl_regression(mod2) %>%
      modify_table_header(c(estimate, ci), hide = TRUE) %>%
      modify_header(p.value ~ "**p-trend**")
    
    # merge primary model and p-trend
    tbl_merge(list(tbl1, tbl2)) %>%
      # remove spanning header
      modify_spanning_header(everything() ~ NA)
    

    enter image description here