2

I am new to R and am trying to produce a vast number of diagnostic plots for linear models for a huge data set. I discovered the lmList function from the nlme package. This works a treat but what I now need is a means of passing in a fraction of this data into the plot function so that the resulting plots are not minute and unreadable. In the example below 27 plots are nicely displayed. I want to produce diagnostics for much more data.
Is it necessary to subset the data first? (presumably with loops) or is it possible to subset within the plotting function (presumably with some kind of loop) rather than create 270 data frames and pass them all in separately? I'm sorry to say that my R is so basic that I do not even know how to pass variables into names and values together in for loops (I tried using the paste function but it failed). The data and function for the example are below – I would be picking values of Subject by their row numbers within the data frame. I grant that the 27 plots here show nicely but for sake of example it would be nice to split them into say into 3 sets of 9.

fm1 <- lmList(distance ~ age | Subject, Orthodont)
# observed versus fitted values by Subject
plot(fm1, distance ~ fitted(.) | Subject, abline = c(0,1)) 
Examples from:
https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/plot.lmList.html

I would be most grateful for help and hope that my question isn't insulting to anyone's intelligence or otherwise annoying.

1 Answers1

1

I can't see how to pass a subset to the plot.lmList function. But, here is a way to do it using standard split-apply-combine strategy. Here, the Subjects are just split into three arbitrary groups of 9, and lmList is applied to each group.

## Make 3 lmLists
fits <- lapply(split(unique(Orthodont$Subject), rep(1:3, each=3)), function(x) {
    eval(substitute(
        lmList(distance ~ age | Subject,                    # fit the data to subset
               data=Orthodont[Orthodont$Subject %in% x,]),  # use the subset
        list(x=x)))                                         # substitue the actual x-values so the proper call gets stored
})

## Make plots
for (i in seq_along(fits)) {
    dev.new()
    print(plot(fits[[i]], distance ~ fitted(.) | Subject, abline = c(0,1)))
}
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • That's great, thanks very much for your kindness. There's a lot that's new to me here, I shall read through the help files I can find and learn more about the functions you use. Cheers. – Andrew Francis Aug 03 '15 at 07:37