0

I'm trying to create Cox tables using gtsummary/tbl_uvregression. One of my covariates is a factor df$Nodes12 <- cut(df$Nodes12, breaks=c(0,11,200), include.lowest= T, labels=c("<12 LN","≥12 LN"), ordered=T)

When I use this code:

df %>%
  select(time, status, Age, ASA, Nodes12) %>%
  tbl_uvregression(
   y = Surv(time = time, event = status),
   method = coxph,
   exponentiate = TRUE,
   pvalue_fun = ~ style_pvalue(.x, digits = 2),
 ) 

Unfortunately, I get "Nodes12.Q" and "Nodes12.L" as level names instead of "<12 LN" and "≥12 LN"

This is what I get

Any suggestions?

tried to add mutate(Nodes12 = factor(Nodes12, labels = c("Yes","No"))) but that didn't help.

  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community Jan 16 '23 at 20:56
  • 1
    Please provide a minimal reproducible example, including both code and data that we can run on our machines. My guess is that you have an ordered factor, and this is the default contrast in that case – Daniel D. Sjoberg Jan 16 '23 at 21:34
  • @DanielD.Sjoberg colud you please check my answer and give me feedback. Many thanks. – TarJae Jan 16 '23 at 22:05
  • @DanielD.Sjoberg Great! Thank you very much! I'll try to provide reproducible code in the future! – Sepehr Doroudian Feb 02 '23 at 20:20

1 Answers1

1

Why this output:

enter image description here

The output means that your predictor LN12 is an "ordered factor". R knows that the elements in your category are not only distinct, but also have a natural order.

The .L (=linear) and .Q (=quadratic) comes from the intention of R to fit a series of polynomial functions or contrasts of your variable LN12. From fantastic answer.

Here is an example how you can tweak and play around:

Example data from here

data:

dataSOF <-
  structure(
    list(
      ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
      age = c(62, 57, 67, 74, 71, 67, 46, 71, 53, 63),
      disease = c(0, 1, 1, 1, 1, 0, 1, 0, 0, 0), 
      death = c(0, 0, 1, 0, 1, 1, 0, 1, 0, 1), 
      censored_survival_days = c(60, 60, 60, 60, 60, 60, 60, 60, 60, 60), 
      censored_survival_status = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    ), 
    row.names = c(NA, -10L), 
    class = c("tbl_df", "tbl", "data.frame")
  )

ORDERED FACTOR

ord_dataSOF <- dataSOF %>% 
  mutate(age50 = cut(age, breaks=c(0,50,100), include.lowest=TRUE,
                     labels=c("< 50 years", ">= 50 years"), ordered = TRUE))


tbl_uvregression(
    ord_dataSOF,
    method=coxph,
    y = Surv(time = censored_survival_days, event = censored_survival_status),
    exponentiate = TRUE,
    include = -ID)
  ) 

enter image description here

FACTOR

factor_dataSOF <- dataSOF %>% 
  mutate(age50 = ifelse(age <50, "age<50", "age>=50"),
         age50 = factor(age50)) 

tbl_uvregression(
    factor_dataSOF,
    method=coxph,
    y = Surv(time = censored_survival_days, event = censored_survival_status),
    exponentiate = TRUE,
    include = -ID)
  ) 

enter image description here

@Daniel D. Sjoberg Why does this not work using modify_table_body from (here)[https://stackoverflow.com/questions/70887776/rename-levels-of-a-factor-in-tbl-regression]

library(gtsummary)
library(survival)
 
dataSOF %>% 
  mutate(age50 = cut(age, breaks=c(0,50,100), include.lowest=TRUE,
                     labels=c("< 50 years", ">= 50 years"), ordered = TRUE)) %>% 
tbl_uvregression(
    method=coxph,
    y = Surv(time = censored_survival_days, event = censored_survival_status),
    exponentiate = TRUE,
    include = -ID,
    label = list(
      age50 ~ "50 years")
  ) %>% 
  modify_table_body(
    ~.x %>% 
      mutate(age50  = ifelse(age50 == "0", "<50 year",
                             ifelse(age50 =="1", ">= 50 years",age50)))
  )

#error:
Error in `mutate()`:
! Problem while computing `age50 = ifelse(...)`.
Caused by error in `ifelse()`:
! object 'age50' not found
TarJae
  • 72,363
  • 6
  • 19
  • 66