[R] Glmnet survival cox predict

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Fri Nov 15 23:00:17 CET 2019

On 11/15/19 10:49 AM, Amir Hadanny wrote:
> Hi all,
> i'm trying to get the prediction probabilities for a survival elastic net.
> When i use try to predict using the train model on the test set, it creates
> an object with the number rows of the train data (6400 rows) instead of the
> test data (2400 rows). I really don't understand why, and that doesn't let
> me check for performance c-index.

If you call most `predict` functions with a second argument that fails 
to contain the predictors in the model, it returns the predictions on 
the original data. The only place where the `test` object appears prior 
to the predict operation is in your call to `predict.coxph`, so my guess 
is that it fails to meet the requirements of the function for a valid 
newdata argument. (Another thought was that maybe `test` didn't exist, 
but that should have thrown an error with the predict call and the nrow 

But since you don't provide code that creates `test` or even an 
unambiguous way of examining its structure, that is entirely a guess.

And finally ... Rhelp is a plain text mailing list, so please to read 
the message at the bottom of every transmission from the mailserver ... 
i.e.  read the Posting Guide. (It is not at all difficult to get 
gmail.com to send plain text.)



> the code:
> data<-read.csv("old4.csv", header=TRUE)
> library(imputeMissings)
> data<-impute(data,object = NULL ,method = "median/mode")
> trainstatus<-train$DIED1095
> trainTime<-train$TIME
> y<-Surv(trainTime,trainstatus)
> trainX<-train[-c(12,63,64,65,66,67,68,69,70,71)]
> x<-data.matrix(trainX)
> library(glmnet)
> fit <- glmnet(x,Surv(trainTime,trainstatus),family="cox",alpha=0.1,
> ,maxit=10000)
> max.dev.index     <- which.max(fit$dev.ratio)
> optimal.lambda <- fit$lambda[max.dev.index]
> optimal.beta  <- fit$beta[,max.dev.index]
> nonzero.coef <- abs(optimal.beta)>0
> selectedBeta <- optimal.beta[nonzero.coef]
> selectedTrainX   <- x[,nonzero.coef]
> coxph.model<- coxph(Surv(train$TIME,train$DIED365) ~x,data=train,
> init=selectedBeta,iter=0)
> coxph.predict<-predict(coxph.model,test)
> nrow(test)
> 2872
> nrow(train
> 6701
> length(coxph.predict)
> 6701
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list