绘制具有不同DV但比例相同的回归线

时间:2018-11-14 21:45:19

标签: plot regression

我有一些带有几个结果变量的数据,这些变量的等级均在0-1范围内。我想要一种比较同一图中相同IV和不同DV之间的回归线的方法。

df <- data.frame(IV = c(2,2,1,4,5,5), DV1 = c(0,0,.25,.25,1,.75), DV2 = c(1,.5,.5,1,.5,.75))

mod1 <- lm(DV1 ~ IV, data = df) mod2 <- lm(DV2 ~ IV, data = df)

1 个答案:

答案 0 :(得分:0)

如果因变量在比例上可比,则将模型放在列表中,并以因变量命名,然后遍历,应用点和斜线:

library(ghibli)
models = list(DV1=mod1,DV2=mod2)
pal = rev(as.character(ghibli_palettes$MarnieMedium1))[1:length(models)]
names(pal) = names(models)

plot(NULL,xlim=range(df$IV),ylim=range(c(df$DV1,df$DV2)),ylab="DV",xlab="IV")

for(i in names(models)){

f = as.formula(paste(i,"~ IV"))
points(f,data=df,col=pal[i],ylim = range(c(DV1,DV2)),pch=20)
abline(models[[i]],col=pal[i],lty=4)
}

legend("topleft",fill=pal,c("DV1","DV2"))

enter image description here