0

I want to ask some general questions on the possibility of regression in R.

For instance, I have data between two variables for 58 regions. I want to conduct the whole regression process including assumption check, model fitting and diagnostics for each region, but get the overall result by one command, which means without a loop.

I already know that I can use the lmList function to do model fitting all in one trial. However, I do not know whether it is possible to get Q-Q normal residual plot for all the 58 regressions in one go.

Does anyone get idea whether this is feasible? If so, what kind of functions I might need?

ekad
  • 14,436
  • 26
  • 44
  • 46
Wendan Zhong
  • 119
  • 1
  • 1
  • 4
  • Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of what you're trying to do and where you're encountering problems. – Thomas Jul 20 '13 at 13:08

1 Answers1

0

Depends what you mean by "one command", and why you want to avoid loops. How about:

library(nlme)
L <- lmList(y~x|region,data=yourData)
lapply(L,plot,which=2)

should work; however, it will spit out 58 plots in sequence. If you try to capture them all on a single page you'll probably get errors about too-small margins.

You have lots of other choices based on working on the list of regressions that lmList returns. For example,

library(plyr)
qqDat <- ldply(L,function(x) as.data.frame(qqnorm(residuals(x))))

will give you a data frame containing the Q-Q plot information (expected and observed values) for each group in the data.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks, I've already used the lapply function to spit out Q-Q plot in sequence... – Wendan Zhong Jul 21 '13 at 10:08
  • 1
    if that's not what you want to do then you need to be a little more specific by what you mean by "in one go". Do you want a single plot with 58 sets of residuals on it? Or what? – Ben Bolker Jul 21 '13 at 13:27