如何绘制两个固定类别值的比较以对另一个连续变量进行线性回归 [英] How to plot a comparisson of two fixed categorical values for linear regression of another continuous variable
问题描述
所以我想画这个图:
lmfit = lm(y〜a + b)
lmfit = lm (y ~ a + b)
但是,"b"只有零和一的值.因此,我想绘制两条单独的回归线,它们彼此平行,以显示b对y截距的影响.因此,在绘制此图形之后:
but, "b" only has the values of zero and one. So, I want to plot two separate regression lines, that are paralel to one one another to show the difference that b makes to the y-intercept. So after plotting this:
图(b,y)
plot(b,y)
然后我想使用abline(lmfit,col="red",lwd=2)
两次,一次将b的x值设置为零,一次将其设置为1.因此,一次不包含该术语,一次b仅为1b.
I want to then use abline(lmfit,col="red",lwd=2)
twice, once with the x value of b set to zero, and once with it set to one. So once without the term included, and once where b is just 1b.
要重述:b是绝对值,0或1.a是连续的,具有轻微的线性趋势.
To restate: b is categorical, 0 or 1. a is continuous with a slight linear trend.
谢谢.
示例:
推荐答案
您可能要考虑将predict(...)
与b=0
和b=1
一起使用,如下所示.由于您未提供任何数据,因此我正在使用内置的mtcars
数据集.
You might want to consider using predict(...)
with b=0
and b=1
, as follows. Since you didn't provide any data, I'm using the built-in mtcars
dataset.
lmfit <- lm(mpg~wt+cyl,mtcars)
plot(mpg~wt,mtcars,col=mtcars$cyl,pch=20)
curve(predict(lmfit,newdata=data.frame(wt=x,cyl=4)),col=4,add=T)
curve(predict(lmfit,newdata=data.frame(wt=x,cyl=6)),col=6,add=T)
curve(predict(lmfit,newdata=data.frame(wt=x,cyl=8)),col=8,add=T)
这篇关于如何绘制两个固定类别值的比较以对另一个连续变量进行线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!