Set specified factor level as reference in GT regression?

Viewed 66

I am using gtsummary package to generate tables from logistic regressions.

I would like to, for example, use the stage level "T3" in the trial data as the reference level, instead of the default "T1". How can I do that within this example code?

I aim to do this for both univariate and multiple variable logistic regression, so I am presuming the answer shall work on both scenarios.

library(gtsummary)
library(dplyr)

trial %>%
  dplyr::select(age, trt, marker, stage, response, death, ttdeath) %>%
  tbl_uvregression(
    method = glm,
    y = death,
    method.args = list(family = binomial),
    exponentiate = TRUE,
    pvalue_fun = function(x) style_pvalue(x, digits = 2)) %>%
  # overrides the default that shows p-values for each level
  add_global_p() %>%
  # adjusts global p-values for multiple testing (default method: FDR)
  add_q() %>%
  # bold p-values under a given threshold (default 0.05)
  bold_p() %>%
  # now bold q-values under the threshold of 0.10
  bold_p(t = 0.10, q = TRUE) %>%
  bold_labels() %>% as_gt()

Sincerely, nelly

1 Answers

I managed to solve my own problem by using the forcats function "fct_relevel" to set the desired level for a categorical variable as the reference.

trial$stage <- forcats::fct_relevel(trial$stage, "T3")
Related