微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无效的图形状态 model4you

如何解决无效的图形状态 model4you

遵循 https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6015941/ 中基于模型的递归分区,我想复制以下代码

sim_data <- function(n=2000){
  x1 <- rnorm(n)
x2 <- rbinom(n,1,0.3)
x3 <- runif(n)
x4 <- rnorm(n)
t <- rbinom(n,0.5)
z <- 1-x2+x1+2*(x1>=0)*x2*t-2*(x1<0)*x2*t
pr <- 1/(1+exp(-z))
y <- as.factor(rbinom(n,pr))
data.frame(x1,x3,x2=as.factor(x2),x4,t=factor(t,labels=c("C","A")),y,z)
}
dt <- sim_data()

dt.num = as.data.frame(sapply(dt,as.numeric))
dt.num$y <- dt.num$y-1    #only to convert outcome 1,2 into 0,1
mbase <- glm(y~t,data=dt.num,family = binomial())
round(summary(mbase)$coefficients,3)
library("model4you")
pmtr <- pmtree(mbase,zformula = ~.,data = dt.num,control = ctree_control(minbucket = 250))

plot(pmtr,terminal_panel = node_pmterminal(pmtr,plotfun = binomial_glm_plot,confint = TRUE))

但是,出现了以下莫名其妙的错误

Error in .Call.graphics(C_palette2,.Call(C_palette2,NULL)) : 
  invalid graphics state

我在帖子 Persistent invalid graphics state error when using ggplot2 中寻找解决此问题的方法。但问题依然存在。

有什么线索吗?

提前致谢

解决方法

当我试图复制这个时,我得到了一个不同的错误:

plot(pmtr,terminal_panel = node_pmterminal(pmtr,plotfun = binomial_glm_plot,confint = TRUE))
## Waiting for profiling to be done...
## Error in plotfun(mod = list(coefficients = c(`(Intercept)` = -0.16839363929017,: 
##   Plotting currently only works for models with a single factor covariate. 
##         We recommend using partykit or ggparty plotting functionalities!

这样做的原因是面板函数期望响应和处理都是二元因子(如dt)。当您改用二进制数值变量时(如 dt.num 中),glm() 中的模型估计会导致等效输出,但 plot() 功能会混淆。

当我用 glm() 而不是 pmtree() 改装 dtdt.num 时,一切都按我的预期工作,产生下图:

pmtree plot

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。