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

JD *_*ong 3 r linear-regression 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)
Run Code Online (Sandbox Code Playgroud)

had*_*ley 7

而不是使用lmList,我建议更一般的plyr包.

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))
Run Code Online (Sandbox Code Playgroud)

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