Adjusted survival curves

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Adjusted survival curves

R help mailing list-2

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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Adjusted survival curves

David Winsemius

On Oct 6, 2017, at 7:58 PM, Ted (Beginner) via R-help <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Adjusted survival curves

Peter Dalgaard-2
In reply to this post by R help mailing list-2

> On 7 Oct 2017, at 04:58 , Ted (Beginner) via R-help <[hidden email]> 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


>
> ______________________________________________
> [hidden email] 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.