[英]plotting mixed effect model interaction in ggplot with three levels of IV
我已經運行了以下混合效果模型,現在想將其可視化:
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)的三個回歸線,並使用predict() 。 但是,對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
出於道德原因,我擔心無法提供完整的數據集。
感謝您提供數據。 我認為該網站將為您提供很大的幫助[將lmer的預測值繪制為一個圖
這顯示了如何使用effect包和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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.