使用 Lattice (或其他东西)在 R 中绘制 lme4 的回归结果

发布于 2024-08-02 16:47:57 字数 461 浏览 4 评论 0原文

感谢 之前的答案,我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温暖的光 2024-08-09 16:47:58

我建议使用更通用的 plyr 包,而不是使用 lmList 。

library(plyr)

d <- data.frame(
 state = rep(c('NY', 'CA'), c(10, 10)), 
 year = rep(1:10, 2), 
 response = c(rnorm(10), rnorm(10))
)

# Create a list of models
# dlply = data frame -> list
models <- dlply(d, ~ state, function(df) { 
  lm(response ~ year, data = df)
})

# Extract the coefficients in a useful form
# ldply = list -> data frame
ldply(models, coef)

# We can get the predictions in a similar way, but we need
# to cast to a data frame so the numbers come out as rows,
# not columns.
predictions <- ldply(models, as.data.frame(predict))

predictions 是一个常规的 R 数据框,因此很容易绘制。

Instead of using lmList, I'd recommend the more general plyr package.

library(plyr)

d <- data.frame(
 state = rep(c('NY', 'CA'), c(10, 10)), 
 year = rep(1:10, 2), 
 response = c(rnorm(10), rnorm(10))
)

# Create a list of models
# dlply = data frame -> list
models <- dlply(d, ~ state, function(df) { 
  lm(response ~ year, data = df)
})

# Extract the coefficients in a useful form
# ldply = list -> data frame
ldply(models, coef)

# We can get the predictions in a similar way, but we need
# to cast to a data frame so the numbers come out as rows,
# not columns.
predictions <- ldply(models, as.data.frame(predict))

predictions is a regular R data frame and so is easy to plot.

佞臣 2024-08-09 16:47:58

我不确定你是否可以轻松地将其放入网格中。 fits 中的内容是一个 S4 对象,其中包含一个 .Data 槽,其中包含标准 lm 对象列表:

R> class(fits)
[1] "lmList"
attr(,"package")
[1] "lme4"
R> class([email protected])
[1] "list"
R> class([email protected][[1]])
[1] "lm"
R> op <- par(mfrow=c(2,4))
R> invisible(lapply([email protected], plot))

最后一个图只是简单地绘制了您lm 对象的标准 2x2 图两次,针对拟合对象列表的每个元素一次。使用 plotwhich 参数来选择这些子集或进行其他回归诊断。

如果您确实想要预测与实际的点阵图,您可能必须对此进行编程。

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 standard lm objects:

R> class(fits)
[1] "lmList"
attr(,"package")
[1] "lme4"
R> class([email protected])
[1] "list"
R> class([email protected][[1]])
[1] "lm"
R> op <- par(mfrow=c(2,4))
R> invisible(lapply([email protected], plot))

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 the which argument to plot 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.

月隐月明月朦胧 2024-08-09 16:47:58

我在使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文