Forum: help


RE: plot tree using data different from training [ Reply ] By: Achim Zeileis on 2019-03-16 20:47 | [forum:46619] |
Great! :-) Glad if it's useful... |
RE: plot tree using data different from training [ Reply ] By: Markus Loecher on 2019-03-16 16:44 | [forum:46618] |
Thanks a lot, that is exactly what I was looking for ! And it even works for rpart models: t1 <- as.party(rpart(log(dist) ~ speed, data = cars)) with the same following code, wonderful Thx Markus |
RE: plot tree using data different from training [ Reply ] By: Achim Zeileis on 2019-03-15 23:20 | [forum:46616] |
Yes, you can do the following: - take the $node structure from the current tree - set up the model.frame() based on the terms() from the current tree and the new data - collect everything in a party() and coerce it as.constparty() As an example I'm learning a tree for the cars data with log-transformed response so that original data and model frame differ. As the "new" data I'm simply using the first 20 observations from the cars data again. ## original tree library("partykit") t1 <- ctree(log(dist) ~ speed, data = cars) plot(t1) ## model frame for new data nd <- model.frame(terms(t1), data = cars[1:20, ]) ## collect in party() and coerce as.constparty() t2 <- party(t1$node, data = nd, fitted = data.frame( "(fitted)" = fitted_node(t1$node, data = nd), "(response)" = model.response(nd), check.names = FALSE), terms = terms(t1) ) t2 <- as.constparty(t2) ## this should be the plot you want plot(t2) Putting the tree together like this follows Section 3.4 from vignette("partykit", package = "partykit"). |
plot tree using data different from training [ Reply ] By: Markus Loecher on 2019-03-15 18:21 | [forum:46614] |
I love the standard plot functions that visualize both the structure of the tree as well as the distribution of the (training) data in the leafs. I would very much like to create analogous plots for the tree applied to new data ("test set"), i.e. the structure of the tree would remain unchanged but the distribution of the (test) data in the leafs would be different. Is that possible? Thanks! Markus |