Re: [R] Fitting models in a loop
Thanks to all for their help. I am busy today but tomorrow I will have time to digest all the feedback and follow up if necessary Cheers, Murray -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: [EMAIL PROTECTED]Fax 7 838 4155 Phone +64 7 838 4773 wkHome +64 7 825 0441Mobile 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.
Re: [R] Fitting models in a loop
Thanks to all who helped me with this problem, especially Bill Venables and Gabor Grothendieck. I hope one day to learn more about the advanced features of the language used by Bill. From a practical standpoint I think I will just avoid doing things like this in my teaching. It is hard enough just getting across the elementary ideas. Murray Jorgensen -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: [EMAIL PROTECTED]Fax 7 838 4155 Phone +64 7 838 4773 wkHome +64 7 825 0441Mobile 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.
Re: [R] Fitting models in a loop
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 01 August 2006 06:16 To: [EMAIL PROTECTED]; 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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: [EMAIL PROTECTED]Fax 7 838 4155 Phone +64 7 838 4773 wkHome +64 7 825 0441Mobile 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.
Re: [R] Fitting models in a loop
Gesmann, Markus [EMAIL PROTECTED] writes: 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) Ouch. Make that mod - vector(list,6) Otherwise you'll be extending the vector on every pass through the loop. -- O__ Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ 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.
Re: [R] Fitting models in a loop
Markus Gesmann writes: 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) I think it is important to see why this deceptively simple solution does not achieve the result that Murray wanted. Take any fitted model object, say mod[[4]]. For this object the formula component of the call will be, literally, y ~ poly(x, i), and not y ~ poly(x, 4), as would be required to use the object, e.g. for prediction. In fact all objects have the same formula. You could, of course, re-create i and some things would be OK, but getting pretty messy. You would still have a problem if you wanted to plot the fit with termplot(), for example, as it would try to do a two-dimensional plot of the component if both arguments to poly were variables. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 01 August 2006 06:16 To: [EMAIL PROTECTED]; 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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 -- __ 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.
Re: [R] Fitting models in a loop
A simple way around this is to pass it as a data frame. In the code below the only change we made was to change the formula from y ~ poly(x, i) to y ~ . and pass poly(x,i) in a data frame as argument 2 of lm: # test data set.seed(1) x - 1:10 y - x^3 + rnorm(10) # run same code except change the lm call mod - list() for (i in 1:3) { mod[[i]] - lm(y ~., data.frame(poly(x, i))) print(summary(mod[[i]])) } After running the above we can test that it works: for(i in 1:3) print(formula(mod[[i]])) y ~ X1 y ~ X1 + X2 y ~ X1 + X2 + X3 On 8/1/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Markus Gesmann writes: 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) I think it is important to see why this deceptively simple solution does not achieve the result that Murray wanted. Take any fitted model object, say mod[[4]]. For this object the formula component of the call will be, literally, y ~ poly(x, i), and not y ~ poly(x, 4), as would be required to use the object, e.g. for prediction. In fact all objects have the same formula. You could, of course, re-create i and some things would be OK, but getting pretty messy. You would still have a problem if you wanted to plot the fit with termplot(), for example, as it would try to do a two-dimensional plot of the component if both arguments to poly were variables. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 01 August 2006 06:16 To: [EMAIL PROTECTED]; 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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 -- __ 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.
Re: [R] Fitting models in a loop
Actually in thinking about this some more that still gets you into a mess if you want to do prediction at anything other than the original points. On 8/1/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: A simple way around this is to pass it as a data frame. In the code below the only change we made was to change the formula from y ~ poly(x, i) to y ~ . and pass poly(x,i) in a data frame as argument 2 of lm: # test data set.seed(1) x - 1:10 y - x^3 + rnorm(10) # run same code except change the lm call mod - list() for (i in 1:3) { mod[[i]] - lm(y ~., data.frame(poly(x, i))) print(summary(mod[[i]])) } After running the above we can test that it works: for(i in 1:3) print(formula(mod[[i]])) y ~ X1 y ~ X1 + X2 y ~ X1 + X2 + X3 On 8/1/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Markus Gesmann writes: 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) I think it is important to see why this deceptively simple solution does not achieve the result that Murray wanted. Take any fitted model object, say mod[[4]]. For this object the formula component of the call will be, literally, y ~ poly(x, i), and not y ~ poly(x, 4), as would be required to use the object, e.g. for prediction. In fact all objects have the same formula. You could, of course, re-create i and some things would be OK, but getting pretty messy. You would still have a problem if you wanted to plot the fit with termplot(), for example, as it would try to do a two-dimensional plot of the component if both arguments to poly were variables. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 01 August 2006 06:16 To: [EMAIL PROTECTED]; 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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 -- __ 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.
Re: [R] Fitting models in a loop
This (below) also runs into trouble if you try to predict with new data since you have no rule for re-constructing the formula. Also, you can't plot the term as a single contributor to the linear predictor with termplot(). I'm sure given enough ingenuity you can get round these two, but why avoid the language manipulation solution, when it does the lot? Bill. -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Wednesday, 2 August 2006 12:01 PM To: Venables, Bill (CMIS, Cleveland) Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED]; r-help@stat.math.ethz.ch Subject: Re: [R] Fitting models in a loop A simple way around this is to pass it as a data frame. In the code below the only change we made was to change the formula from y ~ poly(x, i) to y ~ . and pass poly(x,i) in a data frame as argument 2 of lm: # test data set.seed(1) x - 1:10 y - x^3 + rnorm(10) # run same code except change the lm call mod - list() for (i in 1:3) { mod[[i]] - lm(y ~., data.frame(poly(x, i))) print(summary(mod[[i]])) } After running the above we can test that it works: for(i in 1:3) print(formula(mod[[i]])) y ~ X1 y ~ X1 + X2 y ~ X1 + X2 + X3 On 8/1/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Markus Gesmann writes: 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) I think it is important to see why this deceptively simple solution does not achieve the result that Murray wanted. Take any fitted model object, say mod[[4]]. For this object the formula component of the call will be, literally, y ~ poly(x, i), and not y ~ poly(x, 4), as would be required to use the object, e.g. for prediction. In fact all objects have the same formula. You could, of course, re-create i and some things would be OK, but getting pretty messy. You would still have a problem if you wanted to plot the fit with termplot(), for example, as it would try to do a two-dimensional plot of the component if both arguments to poly were variables. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 01 August 2006 06:16 To: [EMAIL PROTECTED]; 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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 -- __ 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.
Re: [R] Fitting models in a loop
Here is another attempt. This one allows general prediction yet its actually shorter and does not use any advanced language constructs (although to understand why it works one must understand that formulas have environments and the environment of the formula corresponding to each component of mod is the environment within the anonymous function instance that created it): # test data - as before set.seed(1) x - 1:10 y - x^3 + rnorm(10) mod - lapply(1:3, function(i) lm(y ~ poly(x,i))) print(mod) # test - each component of mod remembers its 'i' # This returns 1, 2 and 3 as required. for (j in 1:3) print(environment(formula(mod[[j]]))$i) # following two lines give same answer # showing prediction works predict(mod[[2]], list(x = 1:10)) fitted(lm(y ~ poly(x,2))) On 8/1/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Actually in thinking about this some more that still gets you into a mess if you want to do prediction at anything other than the original points. On 8/1/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: A simple way around this is to pass it as a data frame. In the code below the only change we made was to change the formula from y ~ poly(x, i) to y ~ . and pass poly(x,i) in a data frame as argument 2 of lm: # test data set.seed(1) x - 1:10 y - x^3 + rnorm(10) # run same code except change the lm call mod - list() for (i in 1:3) { mod[[i]] - lm(y ~., data.frame(poly(x, i))) print(summary(mod[[i]])) } After running the above we can test that it works: for(i in 1:3) print(formula(mod[[i]])) y ~ X1 y ~ X1 + X2 y ~ X1 + X2 + X3 On 8/1/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Markus Gesmann writes: 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) I think it is important to see why this deceptively simple solution does not achieve the result that Murray wanted. Take any fitted model object, say mod[[4]]. For this object the formula component of the call will be, literally, y ~ poly(x, i), and not y ~ poly(x, 4), as would be required to use the object, e.g. for prediction. In fact all objects have the same formula. You could, of course, re-create i and some things would be OK, but getting pretty messy. You would still have a problem if you wanted to plot the fit with termplot(), for example, as it would try to do a two-dimensional plot of the component if both arguments to poly were variables. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 01 August 2006 06:16 To: [EMAIL PROTECTED]; 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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 -- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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: [EMAIL PROTECTED]Fax 7 838 4155 Phone +64 7 838 4773 wkHome +64 7 825 0441Mobile 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.