rrandom-forestr-ranger

Predicted probabilities in R ranger package


I am trying to build a model in R with random forest classification. (By editing the code by Ned Horning) I first used randomForest package but then found ranger, which promises faster calculations.

At first, I used the code below to get predicted probabilities for each class after fitting the model with randomForest as:

predProbs <- as.data.frame(predict(randfor, imageBlock, type='prob'))

The type of probability here is as follows:

We have 500 trees in the model and 250 of them says the observation is class 1, hence the probability is 250/500 = 50%

In ranger, I realized that there is no type = 'prob' option.

I searched and tried some adjustments but couldn't get any progress. I need an object or so containing probabilities as mentioned above with ranger package.

Could anyone give some advice about the issue?


Solution

  • You need to train a "probabilistic classifier"-type ranger object:

    library("ranger")
    iris.ranger = ranger(Species ~ ., data = iris, probability = TRUE)
    

    This object computes a matrix (n_samples, n_classes) when used in the predict.ranger function:

    probabilities = predict(iris.ranger, data = iris)$predictions