#We need to quickly produce a model again - a conditional inference tree. Documentation here (we will switch back to rpart shortly after this)
install.packages("ggparty")
install.packages("partykit")
library(ggparty)
library(partykit)

Brexit_model_ggparty <- partykit::ctree(Percent.Leave ~ ., data = Brexit_train, control = ctree_control(testtype = "Teststatistic"))
ggparty(Brexit_model_ggparty) +
 geom_edge() +
 geom_edge_label() +
 geom_node_label(
 line_list = list(
 aes(label = splitvar),
 aes(label = paste("N =", nodesize))),
 line_gpar = list(
 list(size = 13),
 list(size = 10)), ids = "inner") +
 geom_node_label(aes(label = paste0("Node ", id, ", N = ", nodesize)),
 ids = "terminal", nudge_y = -0.3, nudge_x = 0.01) +
 geom_node_plot(gglist = list(geom_bar(aes(x = "", fill = Percent.Leave),
 position = position_fill(), color = "black"
 ),theme_minimal(),
 scale_fill_manual(values = c("white", "red"), guide = FALSE),
 scale_y_continuous(breaks = c(0, 1)),
 xlab(""), ylab("proportion declined"),
 geom_text(aes(x = "", group = Percent.Leave,
 label = stat(count)),
 stat = "count", position = position_fill(), vjust = 1.7)),
 shared_axis_labels = TRUE)