I'm using rfsrc
to model a survival problem, like this:
library(OIsurv)
library(survival)
library(randomForestSRC)
data(burn)
attach(burn)
library(randomForestSRC)
fit <- rfsrc(Surv(T1, D1) ~ ., data=burn)
# predict on the train set
pred <- predict(fit, burn, OOB=TRUE, type=response)
pred$predicted
this gives me the overall survival probability of all patients.
How do I get the survival probability for each person for different timepoints, say 0-5 months or 0-10 months?
The documentation on this isn't immediately obvious if you aren't familiar with the package, but it is possible.
Load data
data(pbc, package = "randomForestSRC")
Create trial and test datasets
pbc.trial <- pbc %>% filter(!is.na(treatment))
pbc.test <- pbc %>% filter(is.na(treatment))
Build our model
rfsrc_pbc <- rfsrc(Surv(days, status) ~ .,
data = pbc.trial,
na.action = "na.impute")
Test out model
test.pred.rfsrc <- predict(rfsrc_pbc,
pbc.test,
na.action="na.impute")
All of the good stuff is held within our prediction object. The $survival
object is a matrix of n rows (1 per patient) and n columns (one per time.interest
- these are automatically chosen though you can constrain them using the ntime
argument. Our matrix is 106x122)
test.pred.rfsrc$survival
The $time.interest
object is a list of the different "time.interests" (122, same as the number of columns in our matrix from $surival
)
test.pred.rfsrc$time.interest
Let's say we wanted to see our predicted status at 5 years, we would
need to figure out which time interest was closest to 1825 days (since our
measurement period is days) when we look at our $time.interest
object, we see that row 83 = 1827 days or roughly 5 years. row 83 in $time.interest
corresponds to column 83 in our $survival
matrix. Thus to see the predicted probability of survival at 5 years we would just look at column 83 of our matrix.
test.pred.rfsrc$survival[,83]
You could then do this for whichever timepoints you're interested in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With