[R] Adjusted survival curves

David Winsemius dwinsemius at comcast.net
Sat Oct 7 05:39:22 CEST 2017


On Oct 6, 2017, at 7:58 PM, Ted (Beginner) 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 got error.
> I would like to understand what is my mistake. Thanks!
> 
> #ADAPTATION FOR MY DATA
> library(survival)
> library(survminer)
> df<-read.csv("F:/R/data/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
> 
> S <- Surv(
> time = df$start, 
> time2 = df$stop, 
> event = df$censor)
> head(S)
> [1] (0,66+] (0,18+] (0,43] (0,47] (0,26+] (0,29+]
> 
> model <- coxph(S ~ df$treatment + df$age + df$sex + df$stage, data = df)


---------
Se if this works:

model <- coxph( Surv(time = start, 
                     time2 = stop, 
                     event = censor)~ treatment + age + sex + stage, data = df)


R regression functions allow you to use column names. The tokens in the formula get handled with evaluation inside the `data` argument's environment.
------------

> plot(survfit(model), 
> las=1,
> xscale = 1.00,
> xlab = "Months after diagnosis",
> ylab = "Proportion survived",
> main = "Baseline Hazard Curve")
> 
> # BEFORE NOW everything works, but then ERROR
> treat <- with(colon,
> data.frame(
> treatment = levels(df$treatment),
> age = rep(levels(df$age)[1], 2),
> sex = rep(levels(df$sex)[1], 2),
> stage = rep(levels(df$stage)[1], 2)))
> 
> str(treat)
> 'data.frame': 0 obs. of 0 variables
> 	[[alternative HTML version deleted]]

In this instance you got away with posting in html, but it might not always be effective.
> 


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