-1

I have a dataset and need to compare survival between Treatment A and Treatment B using survminer.

I want treatment A to be my reference, but R automatically assigns the reference as treatment B and gives me the HR the wrong way (for treatment B but I want it for treatment A)

Here is my example data:

library(survival)
library(survminer)
library(gt_summary)

  id     time event treatment
   <chr> <dbl> <dbl> <chr>    
 1 NA    59.2      0 B        
 2 NA    14.0      1 B        
 3 NA    10.6      0 B        
 4 NA     2.79     1 B        
 5 NA    21.4      0 B        
 6 NA     1.84     0 A        
 7 NA    41.9      0 A        
 8 NA     5.59     0 A        
 9 NA    26.8      1 A        
10 NA     6.81     0 A     

df$treatment <- factor(df$treatment, levels=c("A", "B"))

os_chemo_io <- survfit(formula = Surv(time = time, type = 'right', event = event)~treatment, data = df) 

coxph(Surv(time, event) ~ treatment, data=df) %>% 
  gtsummary::tbl_regression(exp=TRUE)

Mike
  • 3,797
  • 1
  • 11
  • 30
lovebead33
  • 41
  • 3
  • while you might be using gtsummary and survminer the question solely relates to data management and the survvial package. try `df$treatment <- factor(df$treatment, levels=c("B", "A"))` – Mike Feb 03 '22 at 21:52
  • No MCVE. Unable to diagnose problem. Voting to close. – IRTFM Feb 07 '22 at 04:50

1 Answers1

1

try this function: df$treatment <- relevel(treatment, ref = A)

https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/relevel

Before, make "treatment" a factor: df$treatment <- as.factor(df$treatment)

Phil.He
  • 144
  • 9
  • Thanks - did not work. Still giving me the HR with treatment B as ref – lovebead33 Feb 03 '22 at 19:42
  • hmm, worked for me. Did you make the variable a factor before calling relevel()? df$treatment <- as.factor(df$treatment) and then relevel() as written above. – Phil.He Feb 03 '22 at 20:11