如何获得R中C5.0算法中每个预测行的规则

时间:2017-03-22 17:28:36

标签: r

我按照以下方式使用C5.0模型

model <- c5.0(Target ~.,data=df,rules=TRUE,trials=10)

在数据上拟合此模型之后,我想为每个预测的行设置规则。

我正在使用以下功能

pathpred <- function(object, ...) {
   ## coerce to "party" object if necessary
   if(!inherits(object, "party")) object <- as.party(object)

   ## get standard predictions (response/prob) and collect in data frame
   rval <- data.frame(response = predict(object, type = "response", ...))
   rval$prob <- predict(object, type = "prob", ...)

   ## get rules for each node
   rls <- partykit:::.list.rules.party(object)

   ## get predicted node and select corresponding rule
   rval$rule <- rls[as.character(predict(object, type = "node", ...))]

   return(rval)
 }

 pred <- pathpred(model)
 pred[c(1, 51, 101), ]

但它给了我一个错误Error in if (!n.cat[i]) { : missing value where TRUE/FALSE needed

如果我使用rpart

拟合算法,则上面的代码非常有效