3

I have a dependent variable with four outcomes. I have used the mlogit package to conduct a multinomial logistic regression.

When I try to present the results using gtsummary package, my multinomial logistic regression results are stacked on top of each other (see code and table below).

Is there anyway of having the outcomes side by side in one row using only one set of labels for the levels, instead of stacked on top of each other like the table below?

# load packages
library(gtsummary)
library(nnet)

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

# multinom model tabulated with gtsummary  
multinom(city ~ year, data = crime) %>%
  tbl_regression(exponentiate = T)

enter image description here

Mohamed Yusuf
  • 390
  • 1
  • 11

1 Answers1

9

By default, multinomial models will be printed in long format.

I've written a small function to convert the results to wide and saved it as a GitHub Gist. https://gist.github.com/ddsjoberg/a55afa74ac58e1f895862fcabab62406


set.seed(20210511)
library(gtsummary)
library(magrittr)

multinom_pivot_wider <- function(x) {
  # check inputs match expectatations
  if (!inherits(x, "tbl_regression") || !inherits(x$model_obj, "multinom")) {
    stop("`x=` must be class 'tbl_regression' summary of a `nnet::multinom()` model.")
  }
  
  # create tibble of results
  df <- tibble::tibble(outcome_level = unique(x$table_body$groupname_col))
  df$tbl <- 
    purrr::map(
      df$outcome_level,
      function(lvl) {
        gtsummary::modify_table_body(
          x, 
          ~dplyr::filter(.x, .data$groupname_col %in% lvl) %>%
            dplyr::ungroup() %>%
            dplyr::select(-.data$groupname_col)
        )
      }
    )
  
  tbl_merge(df$tbl, tab_spanner = paste0("**", df$outcome_level, "**"))
}

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

# multinom model tabulated with gtsummary
tbl <-
  nnet::multinom(city ~ year, data = crime) %>%
  tbl_regression(exponentiate = TRUE) %>%
  multinom_pivot_wider()

enter image description here

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • Hi Daniel, Thank you for taking the time out to respond to my question. When i try to run the code for tbl_MN I get the following response: Error: Can't subset columns that don't exist. x Column `groupname_col` doesn't exist. – Mohamed Yusuf Oct 21 '20 at 17:45
  • @MohamedYusuf are you familiar with a reprex? (https://www.tidyverse.org/help/) Use both of the code chunks above to create a reprex, and show the results. After installing the correct versions of the packages, the column `groupname_col` should exist. – Daniel D. Sjoberg Oct 21 '20 at 19:13
  • 1
    Hi Daniel, so after setting up the reprex the code seems to work. I ran it again and now its fully working. Not sure what I did to get it going. Thanks for the Answer!! – Mohamed Yusuf Oct 21 '20 at 19:34
  • Hi Daniel, I think the following repo is down remotes::install_github("ddsjoberg/gtsummary@mice_nnet"). Unable to run the above code as, I get the following response : Error in modify_table_body(., dplyr::select, -groupname_col) : could not find function "modify_table_body" – Mohamed Yusuf Dec 18 '20 at 09:17
  • Run remotes::install_github("ddsjoberg/gtsummary"). The branch has since been merged – Daniel D. Sjoberg Dec 18 '20 at 09:44
  • I just managed to instal gtsummary using remotes::install_github("ddsjoberg/gtsummary"). I am still getting the same error for some reason Error in modify_table_body(., dplyr::select, -groupname_col) : could not find function "modify_table_body" – Mohamed Yusuf Dec 18 '20 at 09:59
  • 2
    When you install close all R sessions except one, start in a fresh R session, run the installation code. Read the log carefully for notes about errors. – Daniel D. Sjoberg Dec 18 '20 at 10:05
  • Great! It worked. I had to restart and follow the above! Thank you – Mohamed Yusuf Dec 18 '20 at 10:17