## binary outcome
library(rms)
set.seed(7)
d <- sampleData(80,outcome="binary")
nd <- sampleData(80,outcome="binary")
fit <- lrm(Y~X1+X8,data=d)
predictRisk(fit,newdata=nd)
## survival outcome
## simulate learning and validation data
set.seed(10)
learndat <- sampleData(80,outcome="survival")
valdat <- sampleData(10,outcome="survival")
## use the learning data to fit a Cox model
library(survival)
fitCox <- coxph(Surv(time,event)~X8+X9,data=learndat,x=TRUE,y=TRUE)
## suppose we want to predict the survival probabilities for all subjects
## in the validation data at the time points 0,5,10:
psurv <- predictRisk(fitCox,newdata=valdat,times=c(0,5,10))
## This is a matrix with event probabilities,
## one column for each of the 5 time points
## one row for each validation set individual
## where event probability = 1 - survival probability
if (FALSE) {
if (require("randomForestSRC",quietly=TRUE)){
# Do the same with a randomSurvivalForest model
library(randomForestSRC)
rsfmodel <- rfsrc(Surv(time,event)~X1+X2,data=learndat,ntree=5)
prsfsurv=predictRisk(rsfmodel,newdata=valdat,times=seq(0,5,10))
}
}
## competing risks
library(survival)
library(riskRegression)
library(prodlim)
train <- sampleData(50)
test <- sampleData(10)
cox.fit <- CSC(Hist(time,event)~X7+X8,data=train)
predictRisk(cox.fit,newdata=test,times=c(3,8),cause=1)
## with strata and cause 2
cox.fit2 <- CSC(list(Hist(time,event)~strata(X1)+X2,Hist(time,cause)~X1+X2),data=train)
predictRisk(cox.fit2,newdata=test,times=c(3,5),cause=2)
Run the code above in your browser using DataLab