[R] Noobie question, regression across levels

Ben Bolker bolker at ufl.edu
Mon Dec 15 21:09:52 CET 2008

RichardLang wrote:
> I've just started using R last week and am still scratching my head.
> I have a data set and want to run a separate regression across each level
> of a factor (treating each one separately). The data right now is arranged
> such that the value of the factor along which I want to "split" my data is
> one column among many.
> Best way to do this?
> Thanks!

You can check out lmList function in the nlme package, or more crudely:

lmfun <- function(d) { lm(y~x,data=d) }
myLmList <- lapply(split(mydata,splitfactor),lmfun)

even more compactly/confusingly:

myLmList <- lapply(split(mydata,splitfactor),lm,formula=y~x)

  good luck
   Ben Bolker

View this message in context: http://www.nabble.com/Noobie-question%2C-regression-across-levels-tp21020222p21021025.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list