Sepehr Doroudian
Sepehr Doroudian

Reputation: 1

gtsummary Wrong covariate level name

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.

Upvotes: 0

Views: 89

Answers (1)

TarJae
TarJae

Reputation: 78927

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

Upvotes: 1

Related Questions