Re: [R] Fitting models in a loop

From: Gesmann, Markus <Markus.Gesmann_at_lloyds.com>
Date: Tue 01 Aug 2006 - 19:58:42 EST


Murray,

How about creating an empty list and filling it during your loop:

 mod <- list()
 for (i in 1:6) {

	mod[[i]] <- lm(y ~ poly(x,i))
	print(summary(mod[[i]]))
	}

All your models are than stored in one object and you can use lapply to do something on them, like:
 lapply(mod, summary) or lapply(mod, coef)

Kind Regards

Markus Gesmann
FPMA
Lloyd's Market Analysis
Lloyd's * One Lime Street * London * EC3M 7HA Telephone +44 (0)20 7327 6472
Facsimile +44 (0)20 7327 5718
http://www.lloyds.com

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Bill.Venables@csiro.au
Sent: 01 August 2006 06:16
To: maj@waikato.ac.nz; r-help@stat.math.ethz.ch Subject: Re: [R] Fitting models in a loop

Murray,

Here is a general paradigm I tend to use for such problems. It extends to fairly general model sequences, including different responses, &c

First a couple of tiny, tricky but useful functions:

subst <- function(Command, ...) do.call("substitute", list(Command, list(...)))

abut <- function(...) ## jam things tightly together   do.call("paste", c(lapply(list(...), as.character), sep = ""))

Name <- function(...) as.name(do.call("abut", list(...)))

Now the gist.

fitCommand <- quote({

	MODELi <- lm(y ~ poly(x, degree = i), theData)
	print(summary(MODELi))

})
for(i in 1:6) {

        thisCommand <- subst(fitCommand, MODELi = Name("model_", i), i = i)

	print(thisCommand)  ## only as a check
	eval(thisCommand)

}

At this point you should have the results and

objects(pat = "^model_")

should list the fitted model objects, all of which can be updated, summarised, plotted, &c, because the information on their construction is all embedded in the call.

Bill.

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Murray Jorgensen Sent: Tuesday, 1 August 2006 2:09 PM
To: r-help@stat.math.ethz.ch
Subject: [R] Fitting models in a loop

If I want to display a few polynomial regression fits I can do something

like

for (i in 1:6) {

	mod <- lm(y ~ poly(x,i))
	print(summary(mod))
	}

Suppose that I don't want to over-write the fitted model objects, though. How do I create a list of blank fitted model objects for later use in a loop?

Murray Jorgensen

-- 
Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: maj@waikato.ac.nz                                Fax 7 838 4155
Phone  +64 7 838 4773 wk    Home +64 7 825 0441    Mobile 021 1395 862


______________________________________________
R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ********************************************************************** The information in this E-Mail and in any attachments is CON...{{dropped}}
______________________________________________
R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Received on Tue Aug 01 20:07:16 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 01 Aug 2006 - 22:17:35 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.