[R] Adjusted survival curves

peter dalgaard pdalgd at gmail.com
Sat Oct 7 12:01:11 CEST 2017


> On 7 Oct 2017, at 04:58 , 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)
> 
> 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]]

None of your variables are factors (and age likely cannot be), so levels returns() NULL and you get the effect of:

> data.frame(a=NULL, b=NULL, c=NULL)
data frame with 0 columns and 0 rows

I think this is due to insufficient understanding of the example you are trying to copy. Perhaps try studying it in more detail, view intermediate results, etc. (It's unclear where the "colon" data in the rpubs example come from, though. The data set in survival differs. Ask RStudio, or the author...)

-pd


> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list