ggadjustedcurves.conditional <-
function(data, fit, variable, reference) {
time <- surv <- NULL
lev <- sort(unique(data[,variable]))
reference[,variable] = "_reference_"
df0 <- reference
form <- paste(variable, "~", gsub(as.character(formula(fit))[3], pattern="\\+ *strata.*[^\\)].", replacement=""))
allRes <- list()
rwt <- numeric(nrow(data))
for (level in lev) {
indexes <- which(data[,variable] == level)
if (length(indexes) > 0) {
df1 <- data[indexes, ]
ndf <- rbind(df0, df1)
ndf[,variable] <- factor(ndf[,variable])
model <- glm(as.formula(form), ndf, family="binomial")
allRes[[level]] <- predict(model, newdata = data, type = "response")
rwt[indexes] <- 1/allRes[[level]][indexes]
}
}
nform <- paste(as.character(formula(fit))[2], "~", variable)
nfit <- coxph(as.formula(nform), data = data, weights = rwt)
pred <- survexp(as.formula(paste("~", variable)), data = data, ratetable = nfit)
# remove leading zeros
# while survexp returns non monotonic results
if (length(dim(pred$surv))==2) {
for (i in 1:ncol(pred$surv))
for (j in nrow(pred$surv):2)
if (pred$surv[j,i] > pred$surv[j - 1,i])
pred$surv[j - 1,i] <- 1
}
curve <- data.frame(time = rep(c(0,pred$time), length(lev)),
variable = factor(rep(lev, each=1+length(pred$time))),
surv = c(rbind(1, pred$surv)))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.