为树模型确定R中特定结果的概率

时间:2018-07-27 20:57:58

标签: r

对于使用方法='泊松'创建的树模型,给定new.data(X1 = 4,X2 = 4,X3 = 4),我如何找到获得Y = 0的结果的概率?确切的代码如下:

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

我将以下代码用于同一件事,但使用的是负二项式模型:

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

(为此问题大声疾呼,以帮助我:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_pets.md#starting-training-and-evaluation-jobs-on-google-cloud-ml-engine

我尝试对树模型tree.pois.cp使用相同的代码:

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

但是我得到这个错误:

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

请阅读rpart文档。没有type = "response"个预测rpart对象。您可以尝试以下代码:

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