I am using Boostrappping to fit a cox regression model in R. I am using boot function from R. I have divided my data to test and train. I generate bootstraps of train data and compute concordance index(statistics) on those bootstraps.My question is how I can apply those 100 bootstrapped models to test data and get Confidence intervals for test data as well. Following is my example
library(splitTools)
library(survival)
library(dplyr)
library(intsurv)
library(boot)
bootstrap_train <- function(data, indices)
{
set.seed(3451)
train<- data[indices,]
res.cox<- coxph(Surv(time,status)~., data=train) # fitting cox model
train_predict <- predict(res.cox,newdata = train,type = 'risk') #getting train prediction
cindex_train<- cIndex(train$time, event = train$status, train_predict, weight = NULL) #getting concordance index
return(cindex_train[1])}
cox_train<- X_train #train data frame with variables and endpoint
cox_test<- X_test # test data frame with variables and endpoint
train_boots <- boot(data=cox_train, statistic=bootstrap_train, R=100)
This returns me computed statistics and I can get confidence intervals with boot.ci function. However I am not sure how to fit all those 100 train models to test data and get 100 prediction for test data as well.I also tried passing my test data to boot function assuming that each time fitted model in statistic function will be applied to test data and I can get predictions on new data and store those predictions (in my case concordance index) in a list and then I use that list to calculate 95% confidence interval manually using the normal distribution. However, this gives me an error. Here is what I tried
bootstrap_train <- function(data, indices, test)
{
set.seed(3451)
list_test_cIndex<- NULL # list to store test results
train<- data[indices,]
res.cox<- coxph(Surv(time,status)~., data=train) # fitting model on train data
train_predict <- predict(res.cox,newdata = train,type = 'risk')
cindex_train<- cIndex(train$time, event = train$status, train_predict, weight = NULL)
test_predict <- predict(res.cox,newdata = test,type = 'risk') # fitting trained model on test
cindex_test <- cIndex(test$time, event = test$status, test_predict, weight = NULL)
list_test_cIndex<- cbind(list_test_CI, cindex_test[1]) # storing test cIndex in list
return(list(cindex_train[1]),list_test_cIndex )}
cox_train<- X_train #train data frame with variables and endpoint
cox_test<- X_test # test data frame with variables and endpoint
c(train_boots, cIndex_test) %<-% boot(data=cox_train, statistic=bootstrap_train, R=100, test=
cox_test)
#calculating Confidence intervals of concordance index for test data
error_test <- qnorm(0.975)*sd(cIndex_test)
c(CI_low_test, CI_hi_test) %<-% list(median(cIndex_test)-error_test,median(cIndex_test)+error_test)
Error in t.star[r, ] <- res[[r]] :
incorrect number of subscripts on matrix
Note that I do not want to bootstrap my test data.