首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >visreg:在一幅图中覆盖两个模型

visreg:在一幅图中覆盖两个模型
EN

Stack Overflow用户
提问于 2018-11-11 15:44:39
回答 1查看 938关注 0票数 1

我试图使用库visreg绘制一个简单的、经过调整的GAM模型

代码语言:javascript
复制
# Create DF
set.seed(123)
x1 = rnorm(2000)           
z = 1 + 3*x1 + 3*exp(x1)         
pr = 1/(1+exp(-z))         
y = rbinom(2000,1,pr)
df = data.frame(y=y,x1=x1, x2=exp(x1)*z)

# Fitting GAMs
library(mgcv)
crude <- gam(y ~ s(x1), family=binomial(link=logit), data=df)
adj   <- gam(y ~ s(x1) + s(x2), family=binomial(link=logit), data=df)

# Plot results using 'visreg'
library(visreg)
p.crude <- visreg(crude,  scale='response', "x1", line.par = list(col = 'red'), gg=TRUE) + theme_bw()
p.adj <- visreg(adj,  scale='response', "x1", gg=TRUE) + theme_bw()

使用gridExtra,我可以生成一个两列图,但是我将有一个单独的图覆盖这两个模型图。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-11 16:02:49

您可以使用plot=FALSE参数来获取数据,而不需要图:

代码语言:javascript
复制
p.crude <- visreg(crude,  scale='response', "x1", line.par = list(col = 'red'), plot=FALSE)
p.adj <- visreg(adj,  scale='response', "x1", plot = FALSE)

然后,用手重新创造它:

代码语言:javascript
复制
dplyr::bind_rows(
  dplyt::mutate(p.crude$fit, plt = "crude"),
  dplyr::mutate(p.adj$fit, plt = "adj")
) -> fits

ggplot() +
  geom_ribbon(
    data = fits, 
    aes(x1, ymin=visregLwr, ymax=visregUpr, group=plt), fill="gray90"
  ) +
  geom_line(data = fits, aes(x1, visregFit, group=plt, color=plt)) +
  theme_bw()

https://github.com/pbreheny/visreg/blob/master/R/ggFactorPlot.R提供了您可以在娱乐中使用的所有其他计算和几何/美学。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53250378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档