当前位置: 动力学知识库 > 问答 > 编程问答 >

Plot regression lines with interaction in R

问题描述:

I'm trying to get a scatterplot graph showing 2 regression lines so that interaction effects are clear.

My code is as follows:

lm <- lm(Cage_Int ~ Sex + AVP_mvBNST_ros_caud + Sex:AVP_mvBNST_ros_caud, data=data)

plot(data$Cage_Int, data$AVP_mvBNST_ros_caud, xlab="V1aR in BNSTmv",

ylab="Interaction time", col=colors, pch=20)

curve(cbind(x,1,x,1*x) %*% coef(lm), add=TRUE, col="red")

curve(cbind(1,0,x,0*x) %*% coef(lm), add=TRUE, col="blue")

this gives me the right scatterplot graph,but the regression lines are not showing, which is not what I want (I want both regression lines to show, one for sex=0 and one for sex=1)

Thanks a lot in advance!

网友答案:

Run the regression with sex=0 and sex=1 and create two separate linear models using lm(). Once you define the two models model1 = lm(<insert equation>) and model2 = lm(<insert second equaiton>), you can simply ad a line using abline() or, in this case:

abline(model1) abline(model2)

after the plot function and that should do the trick. You can add colors and different line effects using abline() as well.

分享给朋友:
您可能感兴趣的文章:
随机阅读: