Hi R-Helpers: I?m trying to fit the same linear model to a bunch of variables in a data frame, so I was trying to adapt the codes John Fox, Spencer Graves and Peter Dalgaard proposed and discused yesterday on this e-mail list: for (y in df[, 3:5]) { mod = lm(y ~ Trt*Dose, data = x, contrasts = list(Trt contr.sum, Dose = contr.sum)) Anova(mod, type = "III") } ## by John Fox or 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? Thanks, Gabriela.
"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) -- 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
How about the following: AList <- vector(mode="list", length=3) for(i in 1:3){ y <- df[,i]) mod <- lm(y~Tr*Dose, data=x, contrasts=list(Tr=contr.sum, Dose=contr.sum)) AList[[i]] <- Anova(mod, type="III") } hope this helps. spencer graves CENDOYA, Gabriela wrote:>Hi R-Helpers: > >I?m trying to fit the same linear model to a bunch of variables in a data >frame, so I was trying to adapt the codes John Fox, Spencer Graves and Peter >Dalgaard proposed and discused yesterday on this e-mail list: > >for (y in df[, 3:5]) { >mod = lm(y ~ Trt*Dose, data = x, contrasts = list(Trt >contr.sum, Dose = contr.sum)) >Anova(mod, type = "III") >} ## by John Fox > >or >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? > >Thanks, Gabriela. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >