Search for corresponding node in a regression tree using rpart

后端 未结 3 618
再見小時候
再見小時候 2020-12-17 00:04

I\'m pretty new to R and I\'m stuck with a pretty dumb problem.

I\'m calibrating a regression tree using the rpart package in order to do some class

3条回答
  •  执念已碎
    2020-12-17 00:59

    Benjamin's answer unfortunately doesn't work: type="vector" still returns the predicted values.

    My solution is pretty klugy, but I don't think there's a better way. The trick is to replace the predicted y values in the model frame with the corresponding node numbers.

    tree2 = tree
    tree2$frame$yval = as.numeric(rownames(tree2$frame))
    predict = predict(tree2, newdata=validationData)
    

    Now the output of predict will be node numbers as opposed to predicted y values.

    (One note: the above worked in my case where tree was a regression tree, not a classification tree. In the case of a classification tree, you probably need to omit as.numeric or replace it with as.factor.)

提交回复
热议问题