从线性模型绘制交互效应的最佳方法
为了帮助填充此处的 R 标签,我发布了一些经常从学生那里收到的问题。多年来我已经对这些问题提出了自己的答案,但也许还有我不知道的更好的方法。
问题:我刚刚使用连续的 y
和 x
进行回归,但因子 f
(其中 levels(f)
生成 c("level1","level2")
)
thelm <- lm(y~x*f,data=thedata)
现在我想绘制按组细分的 y
和 x
的预测值由f
定义。我得到的所有情节都很丑陋并且显示了太多线条。
我的答案:尝试使用 predict()
函数。
##restrict prediction to the valid data
##from the model by using thelm$model rather than thedata
thedata$yhat <- predict(thelm,
newdata=expand.grid(x=range(thelm$model$x),
f=levels(thelm$model$f)))
plot(yhat~x,data=thethedata,subset=f=="level1")
lines(yhat~x,data=thedata,subset=f=="level2")
是否还有其他想法(1)对于新手来说更容易理解和/或(2)从其他角度来看更好?
In an effort to help populate the R tag here, I am posting a few questions I have often received from students. I have developed my own answers to these over the years, but perhaps there are better ways floating around that I don't know about.
The question: I just ran a regression with continuous y
and x
but factor f
(where levels(f)
produces c("level1","level2")
)
thelm <- lm(y~x*f,data=thedata)
Now I would like to plot the predicted values of y
by x
broken down by groups defined by f
. All of the plots I get are ugly and show too many lines.
My answer: Try the predict()
function.
##restrict prediction to the valid data
##from the model by using thelm$model rather than thedata
thedata$yhat <- predict(thelm,
newdata=expand.grid(x=range(thelm$model$x),
f=levels(thelm$model$f)))
plot(yhat~x,data=thethedata,subset=f=="level1")
lines(yhat~x,data=thedata,subset=f=="level2")
Are there other ideas out there that are (1) easier to understand for a newcomer and/or (2) better from some other perspective?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
效果包具有良好的绘图方法来可视化回归的预测值。
The effects package has good ploting methods for visualizing the predicted values of regressions.
哈 - 仍然试图让我的大脑围绕
expand.grid()
。只是为了进行比较,这就是我的做法(使用 ggplot2):我认为 ggplot() 逻辑非常直观 - 按 f 对线条进行分组和着色。随着组数量的增加,不必为每个组指定一个层会变得越来越有帮助。
Huh - still trying to wrap my brain around
expand.grid()
. Just for comparison's sake, this is how I'd do it (using ggplot2):The ggplot() logic is pretty intuitive, I think - group and color the lines by f. With increasing numbers of groups, not having to specify a layer for each is increasingly helpful.
我不是 R 专家。但我使用:
这也是一个选项:
I am no expert in R. But I use:
This is also an option:
这是对 Matt 的出色建议的一个小改动,以及一个类似于 Helgi 但使用 ggplot 的解决方案。与上面的唯一区别是我使用了 geom_smooth(method='lm) 直接绘制回归线。
Here is a small change to the excellent suggestion by Matt and a solution similar to Helgi but with ggplot. Only difference from above is that I have used the geom_smooth(method='lm) which plots regression lines directly.