I fitted a glmtertree
model and like to apply a modified node_barplot
function my_node_barplot
for plotting. However, it seems that function my_node_barpolot
is not passed to the plot.constparty
function, resulting in using the node_barplot
function shipped with package partykit
.
Reproducible example from package glmertree
vignette:
library("glmertree")
data("MHserviceDemo", package = "glmertree")
MHserviceDemo$outcome_bin <- factor(MHserviceDemo$outcome > 0)
MH_gtree <- glmertree(outcome_bin ~ 1 | cluster_id | age + gender +
emotional + autism + impact + conduct,
data = MHserviceDemo, family = "binomial")
plot(MH_gtree, which = "tree")
plot(MH_gtree, which = "tree", terminal_panel = my_node_barplot)
shows the same plot. my_panel_barplot
is a slightly modified version of panel_barplot
from GitHub, I changed the lines building the mainlab
variable to print more information::
## ... skipped
function(id, nobs) sprintf("Node %s (n = %s)", id, nobs)
## rest skipped ...
to
## ... skipped
function(id, nobs, resp) {
frq <- table(resp)
perc <- round(frq[1]/sum(frq)*100, 1)
sprintf("Node %s (n = %s)\n: %s%% (%s/%s)",
id, nobs, perc, frq[1], sum(frq))
} else {
function(id, nobs, resp) sprintf("n = %s", nobs)
}
}
if (is.function(mainlab)) {
mainlab <- mainlab(names(obj)[nid], nobs[nid], y)
}
## skipped ...
The function has many lines, but when helpful I can post or share it.