我已经运行了以下混合效果模型,现在想要将其可视化:
mod_father_son <- lmer(AIP_s_child.z ~ AIP_s_parent.z*Q_mean.z +
(1 + AIP_s_parent.z:Q_mean.z || Family_number),
data = data_father_son)
AIP_s_parentz和Q_means.z之间存在显着的相互作用,在与主管交谈后,他建议我绘制交互图,显示3个IV级别(AIP_s_parent.z或Q_mean.z)的三条回归线,并使用预测()。但是,在ggplot2上非常糟糕,我不知道从哪里开始。我试图用其他问题中的一些代码来解决这个问题,但没有运气。两个IV都是连续的并且转换成相应的z分数。
数据集如下所示:
Id_parent Family_number AIP_s_parent.z Q_mean.z Child_id AIP_s_child.z
A1 1 -.008 -0.5 B1 .005
A1 1 -.008 -0.5 B2 .04
C1 2 .06 -.006 D1 -.007
E1 3 -.1 0.02 F1 -.06
我担心出于道德原因,我无法提供完整的数据集。
答案 0 :(得分:0)
感谢您提供数据。我认为这个网站将为您提供很多帮助 [Plotting predicted values from lmer as a single plot
这显示了如何使用效果包和ggplot2从lmer对象进行绘图。
这是我放在一起的一些示例代码 - 我必须添加额外的行(由我自己制作),这样我就可以让你的模型收敛。希望这可以帮助。
library(ggplot2)
library(lme4)
library(effects)
#fake data
data_father_son <- data.frame(Id_parent = c("A1","A1","C1", "E1", "H1", "H2", "A","L","K", "Z"),
Family_number = c(1,1,2,3,1:6),
AIP_s_parent.z = c(-.008,-.008,.06,-.008,-.008,.06,-.008,-.008,.06,.06),
Q_mean.z = c(-.5,-.5,-.006, .02, 4:9),
Child_id = c("B1","B2","D1", "F1", "A1", "A3","E4","P1","L9","I0"),
AIP_s_child.z = c(.005,.04,-.007,-.06, .1,.2,.3,.4,.5,.6),
stringsAsFactors = FALSE)
#model
mod_father_son <- lmer(AIP_s_child.z ~ AIP_s_parent.z*Q_mean.z +
(1 + AIP_s_parent.z:Q_mean.z || Family_number),
data = data_father_son)
#getting effects for the two variables and creating a df
ee <- as.data.frame(Effect(c("AIP_s_parent.z","Q_mean.z"),mod = mod_father_son))
#plot
ggplot(ee, aes(AIP_s_parent.z,fit, group=Q_mean.z, color = Q_mean.z))+
geom_line()