问题描述
我安装了一个 glmtertree
模型,并且喜欢应用修改后的 node_barplot
函数 my_node_barplot
进行绘图。但是,函数 my_node_barpolot
似乎没有传递给 plot.constparty
函数,导致使用 node_barplot
包附带的 partykit
函数。
来自包 glmertree
小插图的可重现示例:
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)
显示相同的图。 my_panel_barplot
是来自 GitHub 的 panel_barplot
的略微修改版本,我更改了构建 mainlab
变量的行以打印更多信息::
## ... skipped
function(id,nobs) sprintf("Node %s (n = %s)",id,nobs)
## rest skipped ...
到
## ... 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)",perc,frq[1],sum(frq))
} else {
function(id,resp) sprintf("n = %s",nobs)
}
}
if (is.function(mainlab)) {
mainlab <- mainlab(names(obj)[nid],nobs[nid],y)
}
## skipped ...
解决方法
您是对的,在此特定设置中忽略了 terminal_panel
参数。原因是 plot()
对象的 glmertree
方法主要侧重于显示具有真实回归量(例如,处理变量)的树作为 GLMM 规范的一部分。相比之下,仅拦截回归器的情况(如您的示例中所示)被传递给 glmtree
,它使用强制转换 constparty
并为此应用默认值。如果您接受默认值,但要传递参数,我们必须复制 partykit
中 glmertree
中的某些代码,此解决方案非常方便。我将与主要作者 Marjolein 讨论这是否值得付出努力。
针对您的具体情况的解决方法是对自己进行强制 constparty
。随后,您可以使用所有标准策略来扩展该图,例如,
MH_ctree <- as.constparty(MH_gtree)
plot(MH_ctree,terminal_panel = my_node_barplot)
请注意,我删除了 mainlab
中的换行符,它仍然适用于这个特定的情节。对于具有更多终端节点的图,您可能需要换行。然后您还需要增加 mainlab
的行数。
最后,您还可以在树的外部组合 node_barplot
,然后通过 mainlab
参数插入它,而不是通过 mainlab
的内部结构。有关有效示例,请参阅此帖子:
partykit: Displaying terminal node percentile values above terminal node boxplots