0

I have a regression model created with by. I know I can use sapply to extract specific parts of the model for each factor, but what if I wanted something like the whole summary, anova, etc.?

 model <- with(data, by(data, factor, function(data) lm(y ~ x, data=data)))
Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Welcome to SO! Next time, please post a [**minimal, reproducible example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). It makes it much easier to test code when trying to help you. Cheers. – Henrik Feb 25 '14 at 23:06

1 Answers1

1

sapply will coerce the results of summary.lm and anova.lm to a matrix. I think you may want to use lapply, which applies a function (here summary) on each element in the list produced by by, and returns a list.

models <- by(warpbreaks, warpbreaks$tension, function(x){
  lm(breaks ~ wool, data = x)
  })

lapply(models, summary)
Henrik
  • 65,555
  • 14
  • 143
  • 159