# NOT RUN {
library(ggparty)
airq <- subset(airquality, !is.na(Ozone))
airct <- ctree(Ozone ~ ., data = airq)
ggparty(airct, horizontal = TRUE, terminal_space = 0.6) +
geom_edge() +
geom_edge_label() +
geom_node_splitvar() +
geom_node_plot(gglist = list(
geom_density(aes(x = Ozone))),
shared_axis_labels = TRUE)
#############################################################
## Plot with ggparty
## Demand for economics journals data
data("Journals", package = "AER")
Journals <- transform(Journals,
age = 2000 - foundingyear,
chars = charpp * pages)
## linear regression tree (OLS)
j_tree <- lmtree(log(subs) ~ log(price/citations) | price + citations +
age + chars + society, data = Journals, minsize = 10, verbose = TRUE)
pred_df <- get_predictions(j_tree, ids = "terminal", newdata = function(x) {
data.frame(
citations = 1,
price = exp(seq(from = min(x$`log(price/citations)`),
to = max(x$`log(price/citations)`),
length.out = 100)))
})
ggparty(j_tree, terminal_space = 0.8) +
geom_edge() +
geom_edge_label() +
geom_node_splitvar() +
geom_node_plot(gglist =
list(aes(x = `log(price/citations)`, y = `log(subs)`),
geom_point(),
geom_line(data = pred_df,
aes(x = log(price/citations),
y = prediction),
col = "red")))
# }
Run the code above in your browser using DataLab