问题描述
由于有了上一个答案.现在,我对每个州都有回归拟合,我想使用晶格为每个州绘制QQ图.我还想以晶格格式绘制每个状态的误差图.如何使用lme4回归的结果制作格子图?
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)
推荐答案
我建议使用更通用的plyr软件包,而不是使用lmList
.
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
是常规R数据帧,因此易于绘制.
predictions
is a regular R data frame and so is easy to plot.
这篇关于使用莱迪思(或其他东西)在R中的lme4绘制回归结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!