# Methods & CRAN packages to predict probability using neural networks or others machine learning algorithms

I have a medical database containing 7 input variables (4 are binary) and a binary outcome variable (Survival: yes/no). My objective is to train and test an algorithm that predict probability of survival (not a binary output).

I used nnet package. But it returned for me a binary output (using "raw" and "class" type). Which method can i use? Is there any R CRAN Packages to predict probability of survival using neural networks or others machine learning algorithms?

Thank you very much

• Are you perhaps interested in the pros & cons of different ML algorithms for situations like this? Note that CV is not an R Q&A site (you may want to read our FAQ). Regarding your outcome variable, do you have survival durations or just yes / no? Might there be any censoring? – gung - Reinstate Monica Nov 22 '12 at 22:41
• I have both. but my primer y outcome is 1-year-survival. – user1594303 Nov 23 '12 at 4:58

I like to use the caret package for predictive modeling, as it provided a unified interface to a variety of algorithms, including nnet. It's very easy to get predicted probabilities for any model that supports them, neural networks included:

set.seed(42)
require(caret)
model <- train(Species~., data=iris, method='nnet',
trControl=trainControl(method='cv'))
model
probs <- predict(model, iris, type='prob')
setosa versicolor    virginica
1 0.9881512 0.01143536 0.0004134043
2 0.9839565 0.01550938 0.0005341130
3 0.9866016 0.01293839 0.0004599962
4 0.9820605 0.01735453 0.0005849775
5 0.9884653 0.01113064 0.0004040207
6 0.9875201 0.01204961 0.0004302507

• Hello! Thank you for this example. I had a problem with the predict() step because it showed Error in eval(expr, envir, enclos) : object 'Id1002025' not found. I can't move on since I haven't found a solution to this and I want to simulate OP's goal for multi-time survival predictions. – Saggy Manatee And Swan Folk Jan 3 '14 at 20:30
• @llorgge Very odd. I am getting the same error when I re-run this code. One of the newer versions of caret must have introduced a bug. Oddly, if I try other methods (e.g. rpart or glm instead of nnet) I get the same error. It must be a problem with the predict function in caret. – Zach Jan 3 '14 at 20:40
• @llorgge I changed the example to use the iris dataset. It would appear there is a very specific bug in caret when using the formula interface for classification models that contain factors. – Zach Jan 3 '14 at 21:01
• @llorgge You should probably post a separate question, with a reproducible example. I'm not sure caret works with survival data. – Zach Jan 3 '14 at 21:20
• @HaskellFun It does take a test set. see ?predict.train. I updated my answer to demonstrate. – Zach Apr 28 '16 at 13:06

I would second the recommendation of an "ensemble" method such as Random Forests. There is a variant of RF, "randomSurvivalForest" that is specific to survival analysis. Here is a link to the the R manual for the package randomSurvivalForest.

you can try for example the glm function with the family=binomial and the logit link (or the probit) doing a logistic regression, which outputs are probabilitys.. Here you have a link logistic regression R

You also can try with trees or with "ensembles" of trees , boosting, bagging or random forest wuth packages like rpart ,randomForest etc If your dependent variable is 1/0 you will have outcomes with estimated proportions.

You might also want to look at package RSNNS, which provides an R interface to the package SNNS, with ample functionality for general neural networks.