chjackson / flexsurv

The flexsurv R package for flexible parametric survival and multi-state modelling
http://chjackson.github.io/flexsurv/
55 stars 27 forks source link

plotting confidence interval of hazard ratio over time using flexsurvspline #65

Closed IlYongChung closed 1 year ago

IlYongChung commented 5 years ago

Thank you for making such a incredible package. There is a problem which I don't have any clue to solve. Using flexsurvspline, I've tried to plot hazard ratio and confidence interval over time. But I cannot plot the confidence interval. I get an error message. Could you give me an advice to solve this problem? I cannot thank you enough.

P.S Here are my codes and error message. ################################### sp3 <- flexsurvspline(Surv(recyrs, censrec) ~ group + gamma1(group) , data = bc, k=1, scale='hazard') B <- 5000 t <- seq(0.1, 8, by=0.1) hrAFT.est <- summary(sp3, t=t, type="hazard", newdata=data.frame(group="Medium"), ci=FALSE)[[1]][,"est"]/ summary(sp3, t=t, type='hazard', newdata=data.frame(group="Good"), ci=FALSE)[[1]][,"est"] pars <- normboot.flexsurvreg(sp3, B=B, newdata=data.frame(group=c("Good","Medium"))) hrAFT <- matrix(nrow=B, ncol=length(t)) hfn <- unroll.function(hsurvspline, gamma=0:2) for(i in seq_along(t)){ haz.medium.rep <- do.call(hfn, c(list(t[i]), as.data.frame(pars[[2]]))) haz.good.rep <- do.call(hfn, c(list(t[i]), as.data.frame(pars[[1]]))) hrAFT[,i] <- haz.medium.rep / haz.good.rep } ################################### Error in dbase.survspline(q = x, gamma = gamma, knots = knots, scale = scale) : length of gamma should equal number of knots Called from: dbase.survspline(q = x, gamma = gamma, knots = knots, scale = scale) ###################################

chjackson commented 5 years ago

There's no need to use unroll.function or do.call here because the parameters to hsurvspline are collected together in a single argument gamma, that can be supplied as a matrix. Also the knots from the fitted model should be supplied. So this should work:

for(i in seq_along(t)){
    haz.medium.rep <- hsurvspline(t[i], gamma=pars[[2]], knots=sp3$knots)
    haz.good.rep <- hsurvspline(t[i], gamma=pars[[1]], knots=sp3$knots)
    hrAFT[,i] <- haz.medium.rep / haz.good.rep
}
IlYongChung commented 5 years ago

It works! I cannot thank you enough!!!