[R] Adjusted survival curves

David Winsemius dwinsemius at comcast.net
Sat Oct 7 18:19:00 CEST 2017


> On Oct 7, 2017, at 9:01 AM, Ted Beginner (RStudio) via R-help <r-help at r-project.org> wrote:
> 
> 
> For adjusted survival curves I took the sample code from here: 
> https://rpubs.com/daspringate/survival
> and adapted for my date, but ... have a QUESTION.
> 
> library(survival)
> library(survminer)
> df<-read.csv("base.csv", header = TRUE, sep = ";")
> head(df)
> ID start stop censor sex age stage treatment
> 1 1 0 66 0 2 1 3 1
> 2 2 0 18 0 1 2 4 2
> 3 3 0 43 1 2 3 3 1
> 4 4 0 47 1 2 3 NA 2
> 5 5 0 26 0 1 4 3 NA
> 
> # THANKS, DAVID WINSEMIUS for remark!!! ("R regression functions allow you to use column names.")
> model <- coxph( Surv(time = start,
> time2 = stop,
> event = censor) ~ treatment + age + sex + stage, data = df)
> 
> plot(survfit(model), 
> las=1,
> xscale = 1.00,
> xlab = "Months after diagnosis",
> ylab = "Proportion survived",
> main = "Baseline Hazard Curve")
> 
> #HOW TO EXPLAIN TO R, THAT treatment, age, sex and stage ARE FACTORS, AND NOT CONTINUOUS VAR?
> treat <- with (df,
> data.frame (
> treatment = levels(treatment),
> age = rep(levels(age)[1], 2),
> sex = rep(levels(sex)[1], 2),
> stage = rep(levels(stage)[1], 2)))

Perhaps:

treat2 <- treat
treat2[,c('treatment', 'age', 'sex', 'stage')] <- lapply( treat2[,c('treatment', 'age', 'sex', 'stage')], factor) 


> treat2
  ID start stop censor sex age stage treatment
1  1     0   66      0   2   1     3         1
2  2     0   18      0   1   2     4         2
3  3     0   43      1   2   3     3         1
4  4     0   47      1   2   3  <NA>         2
5  5     0   26      0   1   4     3      <NA>
> str(treat2)
'data.frame':	5 obs. of  8 variables:
 $ ID       : int  1 2 3 4 5
 $ start    : int  0 0 0 0 0
 $ stop     : int  66 18 43 47 26
 $ censor   : int  0 0 1 1 0
 $ sex      : Factor w/ 2 levels "1","2": 2 1 2 2 1
 $ age      : Factor w/ 4 levels "1","2","3","4": 1 2 3 3 4
 $ stage    : Factor w/ 2 levels "3","4": 1 2 1 NA 1

I saw Peter Dalgaard's reply and I'm guessing that your 'age' was grouped into some categorical  variable, perhaps decades?

> 
> str(treat)
> 	[[alternative HTML version deleted]]

You should learn to post in plain text and use dput to present your data structures. At your console do this

dput(treat)

# and this will appear. Copy it to your plain-text message:

structure(list(ID = 1:5, start = c(0L, 0L, 0L, 0L, 0L), stop = c(66L, 
18L, 43L, 47L, 26L), censor = c(0L, 0L, 1L, 1L, 0L), sex = c(2L, 
1L, 2L, 2L, 1L), age = c(1L, 2L, 3L, 3L, 4L), stage = c(3L, 4L, 
3L, NA, 3L), treatment = c(1L, 2L, 1L, 2L, NA)), .Names = c("ID", 
"start", "stop", "censor", "sex", "age", "stage", "treatment"
), class = "data.frame", row.names = c("1", "2", "3", "4", "5"
)):



Then we can just do this:

 treat <- structure(list(ID = 1:5, start = c(0L, 0L, 0L, 0L, 0L), stop = c(66L, 
18L, 43L, 47L, 26L), censor = c(0L, 0L, 1L, 1L, 0L), sex = c(2L, 
1L, 2L, 2L, 1L), age = c(1L, 2L, 3L, 3L, 4L), stage = c(3L, 4L, 
3L, NA, 3L), treatment = c(1L, 2L, 1L, 2L, NA)), .Names = c("ID", 
"start", "stop", "censor", "sex", "age", "stage", "treatment"
), class = "data.frame", row.names = c("1", "2", "3", "4", "5"
))

-- 
David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law



More information about the R-help mailing list