[R] List of lm objects

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Oct 23 23:17:33 CEST 2003


Peter Dalgaard <p.dalgaard at biostat.ku.dk> writes:

> "CENDOYA, Gabriela" <gcendoya at balcarce.inta.gov.ar> writes:
> 
> > for (myname in names(myframe)){
> >   mycall <- substitute(lm(myvar~etc.etc.....),list(myvar=as.name(myname)))
> >   myfit <- eval(mycall)
> >   print(summary(myfit))
> > } ## by Peter Dalgaard
> > 
> > But instead of printing summary or Anova results, I need to generate a list
> > containing all the lm() objects.
> > Is that possible? How?
> 
> Very easily:
> 
> f <- function(myname){
>   mycall <- substitute(lm(myvar~etc.etc.....),list(myvar=as.name(myname)))
>   myfit <- eval(mycall)
>   print(summary(myfit))
> }
> 
> lapply(names(myframe),f)

Whoops. A bit too quick there: You want the lm objects, not summaries
and no printing, so make that

f <- function(myname){
  mycall <- substitute(lm(myvar~etc.etc.....),list(myvar=as.name(myname)))
  eval(mycall)
}
lapply(names(myframe),f)

Or, if you dont care about having the names substituted in the lm
calls (the list elements will get named appropriately)

lapply(myframe, function(y) lm(y~etc.etc.))

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list