将混合模型的预测绘制为R中的一条线 [英] Plotting the predictions of a mixed model as a line in R

查看:60
本文介绍了将混合模型的预测绘制为R中的一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在下面绘制混合模型的预测( predict()),以便可以在下面以一条线的形式获得概念上所需的图.

I'm trying to plot the predictions (predict()) of my mixed model below such that I can obtain my conceptually desired plot as a line below.

我尝试绘制模型的预测,但未达到所需的绘制.有没有更好的方法来定义 predict()以便我可以实现所需的绘图?

I have tried to plot my model's predictions, but I don't achieve my desired plot. Is there a better way to define predict() so I can achieve my desired plot?

library(lme4)

dat3 <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/dat3.csv')

m4 <- lmer(math~pc1+pc2+discon+(pc1+pc2+discon|id), data=dat3)

newdata <- with(dat3, expand.grid(pc1=unique(pc1), pc2=unique(pc2), discon=unique(discon)))

y <- predict(m4, newdata=newdata, re.form=NA)

plot(newdata$pc1+newdata$pc2, y)

推荐答案

更多 sjPlot .参见参数 grid 将多个预测变量包装在一个图中.

More sjPlot. See the parameter grid to wrap several predictors in one plot.

library(lme4)
library(sjPlot)
library(patchwork)

dat3 <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/dat3.csv')

m4 <- lmer(math~pc1+pc2+discon+(pc1+pc2+discon|id), data=dat3) # Does not converge

m4 <- lmer(math~pc1+pc2+discon+(1|id), data=dat3) # Converges

# To remove discon
a <- plot_model(m4,type = 'pred')[[1]]
b <- plot_model(m4,type = 'pred',title = '')[[2]]

a + b 

我在删除 sjPlot 框架内的 dropcon 术语时遇到了一些麻烦.我放弃了,回到了 Patchwork .我确定丹尼尔会知道正确的方法.

Edit 1: I had some trouble removing the dropcon term within the sjPlot framework. I gave up and fell back on patchwork. I'm sure Daniel could knows the correct way.

这篇关于将混合模型的预测绘制为R中的一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆