0

How would I find the the probability of getting an outcome of Y=0 given new.data (X1=4,X2=4,X3=4) for a tree model I created with method = 'poisson'? Exact code below:

tree.pois.cp<-rpart(Y ~ X1 + X2 + X3, data = data, method = 'poisson', control = rpart.control(cp = 1.1034e-02))

I used the below code for the same thing but using my negative binomial model:

pred.y.nb<-predict(nb, newdata = new.data, type = "response")
prob0.nb<-dnbinom(0, mu=pred.y.nb, size=nb$theta)
prob0.nb
#this is my answer for probability of Y=0 given my negative binom model

(Shout out to this question for helping me out: How to calculate the predicted probability of negative binomial regression model?)

I tried using the same code for my tree model tree.pois.cp:

pred.y.pois.cp<-predict(tree.pois.cp, newdata = new.data, type = "response")

But I get this error:

Error in match.arg(type) : 'arg' should be one of “vector”, “prob”, “class”, “matrix”

Thanks for your help!

PalDel
  • 1
  • 2

1 Answers1

1

Please read rpart documentation. There is no type = "response" for predict rpart object. You can try below code:

data<-data.frame(Y=as.character(rpois(n = 20000,.2)),X1=sample(1:4,20000,replace = T),X2=sample(1:4,20000,replace = T),X3=sample(1:4,20000,replace = T),X4=sample(1:4,20000,replace = T))

tree.pois.cp<-rpart(Y ~ X1 + X2 + X3, data = data, method = 'class')
new.data<- data.frame(Y="0",X1=4,X2=4,X3=4,X4=4)
pred.y.pois<-predict(tree.pois.cp, newdata = new.data, type = "prob")
pred.y.pois
Iman
  • 2,224
  • 15
  • 35