使用 Lattice (或其他东西)在 R 中绘制 lme4 的回归结果
感谢 之前的答案,我已经使用 lme4 进行了回归。现在我已经对每个州进行了回归拟合,我想使用点阵来绘制每个州的 QQ 图。我还想以网格格式绘制每个状态的误差图。如何使用 lme4 回归的结果绘制格子图?
下面是一个使用两种状态的简单示例(是的,我喜欢好的头韵)。我想制作一个由物体配合制成的两面板格子。
library(lme4)
d <- data.frame(state=rep(c('NY', 'CA'), c(10, 10)), year=rep(1:10, 2), response=c(rnorm(10), rnorm(10)))
fits <- lmList(response ~ year | state, data=d)
I have fit a regression using lme4 thanks to a previous answer. Now that I have a regression fit for each state I'd like to use lattice to plot QQ plots for each state. I would also like to plot error plots for each state in a lattice format. How do I make a lattice plot using the results of a lme4 regression?
Below is a simple sample (yeah, I like a good alliteration) using two states. I would like to make a two panel lattice made from the object fits.
library(lme4)
d <- data.frame(state=rep(c('NY', 'CA'), c(10, 10)), year=rep(1:10, 2), response=c(rnorm(10), rnorm(10)))
fits <- lmList(response ~ year | state, data=d)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用更通用的 plyr 包,而不是使用 lmList 。
predictions
是一个常规的 R 数据框,因此很容易绘制。Instead of using
lmList
, I'd recommend the more general plyr package.predictions
is a regular R data frame and so is easy to plot.我不确定你是否可以轻松地将其放入网格中。
fits
中的内容是一个 S4 对象,其中包含一个 .Data 槽,其中包含标准lm
对象列表:最后一个图只是简单地绘制了您
lm
对象的标准 2x2 图两次,针对拟合对象列表的每个元素一次。使用plot
的which
参数来选择这些子集或进行其他回归诊断。如果您确实想要预测与实际的
点阵
图,您可能必须对此进行编程。I am not sure you can get this into lattice easily. What you have in
fits
is a an S4 object containing a .Data slot with a list of standardlm
objects:This last plot simply plots you the standard 2x2 plot for
lm
objects twice, once for each element of the list of fitted objects. Use thewhich
argument toplot
to select subsets of these or for other regression diagnostics.If you actually want
lattice
plots of predicted vs actual, you may have to program this.我在使用 lme4::lmList 时遇到了一些麻烦。例如,摘要似乎不起作用。因此,您可能会遇到一些问题。
因此,即使我使用 lmer,而不是 lme,我仍然显式调用 nlme::lmList 。然后总结等就可以了。
I've had some trouble with lme4::lmList. For example, summary doesn't seem to work. So you might run into some problems because of that.
So even though I use lmer, instead of lme, I've been explicitly calling nlme::lmList instead. Then summary etc. will work.