0

I created a tree in R.

rp <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)

## coerce to `constparty'
rp.party <- as.party(rp)

Now, I want to find the deviance of the rp.party object. Is there a way to do so without going to the rp object?

Thanks!

  • Probably not... it shouldn't be a problem to find this by pulling the relevant pieces from the object. Use `str(rp.party)` to look at the structure of the object. – User7598 Apr 08 '15 at 20:47

1 Answers1

0

The constparty objects in partykit currently do not have a deviance() method because they don't assume a formal model. The method is completely nonparametric. (The model-based glmtree() objects do provide a deviance() method.) However, it might be useful in some cases...I'll discuss with Torsten whether we should add it for constparty as well.

In any case, it is not very difficult to set up the binomial deviance for your example "by hand". The fitted() method provides the predicted node ID and the observed response. The predict(..., type = "prob") method yields the predicted probabilities. With these two pieces of information you can directly compute the deviance as:

obs <- fitted(rp.party)[, "(response)"]
pred <- predict(rp.party, type = "prob")
-2 * sum(log(pred[cbind(1:nrow(pred), as.numeric(obs))]))
## [1] 46.90789

Or you can re-fit a constant in every terminal node using glm() and extract the deviance() from that:

deviance(glm(`(response)` ~ factor(`(fitted)`),
  data = fitted(rp.party), family = binomial))
## [1] 46.90789
Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49