Search code examples
rplotpredictionpredictsurvival-analysis

Effect plot prediction in R


So far, i have reached to fit the model in the survreg function like below:

model <- survreg(formula = Surv(TimeDeath, event) ~ age + BM1 + BM2 + 
                       mutation + sex + BM1:BM2 + BM1:mutation, 
                       data = DF, dist = "lognormal")

Now, i need to predict failure time of a male patient who is 51 years old, he did not have the gene mutation, and for BM1 he had the value 3.7 mg/dL and for BM2 the value 251 mg/dL.

I continued like below:

ND <- with(DF, data.frame(
  age = rep(seq(min(age), max(age), length.out = 20), 2),
  BM1 = rep(seq(min(BM1), max(BM1), length.out = 20), 2),
  BM2 = rep(seq(min(BM2), max(BM2), length.out = 20), 2),
  mutation = c("No", "Yes"),
  sex = c("male", "40")
))

prs <- predict(model_final, ND, se.fit = TRUE, type = "lp")
ND$pred <- prs[[1]]
ND$se <- prs[[2]]
ND$lo <- exp(ND$pred - 1.96 * ND$se)
ND$up <- exp(ND$pred + 1.96 * ND$se)
ND$pred <- exp(ND$pred)

library(lattice)
xyplot(pred + lo + up ~ age + BM1, data = ND, type = "l",
       lty = c(1,2,2), col = "black", lwd = 4, xlab = "Age",
       ylab = "Survival Time")

I know i have not defined the ND object correctly, but i don't know how to do it, and also, the plot function.

Some help please?


Solution

  • Look at ?predict.survreg. The construction of CI's does look suspicious, I would have thought you would instead have set se.fit=TRUE There is a new data argument which is where you include parameters needed for prediction as part of the newdata argument:

     all.combos < expand.grid( mutation=c("No", "Yes"), BM1= 3.7 , BM2= 251 ,
                              sex = c("male", "40"),
                               age-seq(min(age), max(age), length.out = 20)  )   )
      preds.combos <- predict(model, all.combos, se.fit=TRUE)