2

rather than 0 or 1 ,how to calculate exact probabilities like 96% or 43% in rpart decision tree model. I have tried predict(model,data,type="prob") but it is predicting either 0 or 1

Nazia Afreen
  • 21
  • 1
  • 3
  • 2
    a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) might help. The default rpart prediction is probabilities for classification. – phiver Nov 09 '15 at 08:34

2 Answers2

6

During the creation of rpart object you have to specify method = "class" in parameters to ensure classification. Once you do that your predict method give probabilities with type="prob".

Gaurav
  • 1,597
  • 2
  • 14
  • 31
  • I already tried that it is giving me probability as either 0 or 1.what I am trying to do is to find actual probabilities like .97 or .80 – Nazia Afreen Nov 10 '15 at 07:04
  • 1
    @NaziaAfreen perhaps your data is actually yielding the result as 0 or 1 as probabilities, i.e. you have a perfect fit and there is no chance of false positives. If you do the above steps correctly you should get probabilities as they are. – Gaurav Nov 10 '15 at 07:14
2

@Nazia Afreen - below is the R scriptlet, hope this might help.

library(rpart)
model <- rpart(dependent_class_variable ~ independent var1 + var 2 + .., data = "your train data", method = "class")

## to get the probabilities of each record
probilities_ <- predict(model, "your test data without quotes", type = "prob")

## it will yield two probabilities, probability of getting class 1, and 
## probability of getting class 2, if you have two class. Sum of both = 1##
user1829708
  • 73
  • 10