Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot predictions over time for survival model #199

Closed
cperk opened this issue Mar 22, 2021 · 2 comments
Closed

Plot predictions over time for survival model #199

cperk opened this issue Mar 22, 2021 · 2 comments

Comments

@cperk
Copy link

cperk commented Mar 22, 2021

Hello @strengejacke ,

I am excited to use ggeffects for generating predictions from survival models; a few days ago I posted this question because I couldn't figure out how to plot survival probabilities over time. Since then I've figured out how to do it using type="survival" (see below). However, I have run into another issue: I cannot figure out how to tell the ggeffects plot function that I want the x axis to start at day 1.

#read in dataset; relevant columns include Treatment, Source Salinity, Days_to_death and censored
df1 <- data.frame(production=c(15,12,10,9,6,8,9,5,3,3,2,1,0,0,0,0), Treatment_Num=c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4), Genotype=c(1,1,2,2,1,1,2,2,1,1,2,2,1,1,2,2), Source_Salinity=c("Fresh","Fresh","Brackish","Brackish","Fresh","Fresh","Brackish","Brackish","Fresh","Fresh","Brackish","Brackish","Fresh","Fresh","Brackish","Brackish"), Days_to_death <- c(500, 500, 500, 500, 400, 350, 300, 500, 200, 202, 260, 280, 150,150,160,140), censored <- c(1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0))

#create surv object
surv_object_toy <- Surv(time = df1$Days_to_death, event = df1$censored)

#run cox survival analysis
Coxfit1_toy <- coxph(surv_object_toy ~ Treatment_Num + Source_Salinity, data = df1)

#run predict with type="survival"
gg_cox_toy <- ggpredict(Coxfit1_toy, c("Treatment_Num [all]", "Source_Salinity [all]"),type = "survival")


#try plotting, but find that X axis doesn't start at day 1

plot(gg_cox_toy)

Screen Shot 2021-03-24 at 3 58 52 PM

#trying to specify scale_x_continuous limits throws error
plot(gg_cox_toy, scale_x_continuous(limits = c(1,500)))

Screen Shot 2021-03-24 at 3 58 25 PM

#by comparison, ggadjustedcurves can plot it starting with day 1, so we can track individuals all the way from day 1 when all individuals are alive, till day 500 which is the end of the experiment period.

survminer::ggadjustedcurves(Coxfit1_toy,data = df1, variable="Treatment_Num", xlab="Days", ylab="Probability of Maintaining >50% Green Leaf Length", legend.title="")

Screen Shot 2021-03-24 at 3 59 21 PM

Could you please help me out?

Thank you!

-Carrie

@strengejacke
Copy link
Owner

Hi Carrie, the issue here is that the first event occurs at time = 140. That's why the x-axis isn't starting from 1. I'll see how I can fix that.

strengejacke added a commit that referenced this issue Mar 26, 2021
@strengejacke
Copy link
Owner

Should work now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants