nicholas
nicholas

Reputation: 983

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?

Upvotes: 2

Views: 3671

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11595

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

Upvotes: 3

Related Questions