Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to modify variable labels in gtsummary table

As recommended in the tutorial for gtsummary‘s tbl_regression function, I am using the labelled package to assign attribute labels to my regression variables. However, when my regression formula includes a quadratic term, the resulting table includes the same variable label twice:

library(gtsummary)
library(labelled)
library(tidyverse)

df <- as_tibble(mtcars)

var_label(df) <- list( disp = "Displacement", vs = "Engine type")

c("disp", "disp + I(disp^2)") %>% 
  map(
    ~ paste("vs", .x, sep = " ~ ") %>% 
      as.formula() %>% 
      glm(data = df,
          family = binomial(link = "logit")) %>% 
      tbl_regression(exponentiate = TRUE)) %>% 
  tbl_merge()

Example of gtsummary table

Is there a way to modify the label for the quadratic term in this case?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you assign the label inside the tbl_regression() function, you’ll see what you want to get.

library(gtsummary)

c("disp", "disp + I(disp^2)") %>% 
  purrr::map(
    ~ paste("vs", .x, sep = " ~ ") %>% 
      as.formula() %>% 
      glm(data = mtcars, family = binomial(link = "logit")) %>% 
      tbl_regression(
        exponentiate = TRUE,
        label = list(
          disp = "Displacement",
          `I(disp^2)` = "Displacement^2"
        )
      )
  ) %>%
  tbl_merge() %>%
  as_kable()
#> ✖ `I(disp^2)` terms have not been found in `x`.
Characteristic OR 95% CI p-value OR 95% CI p-value
Displacement 0.98 0.96, 0.99 0.002 0.99 0.92, 1.07 0.8
Displacement^2 1.00 1.00, 1.00 0.8

Created on 2022-09-19 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading