[R] iterative regressions, adding a new line of data each time

Greg Snow Greg.Snow at imail.org
Mon Feb 1 21:17:55 CET 2010


Possibly something like:

lapply( 100:200, function(i) {
	fit <- lm( y ~ x, data=mydata, subset=seq(length.out=i) )
	... do something with fit to return the desired prediction(s) ...
} )

You can stop standing now :-)

The process might be a little more efficient (or it might not be) by using the biglm package and updating the original fit with 1 row at a time (depends also on what the original poster meant by "predicted y" and how complicated it is to compute).

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Rolf Turner
> Sent: Monday, February 01, 2010 12:44 PM
> To: Steve Lianoglou
> Cc: r-help at r-project.org
> Subject: Re: [R] iterative regressions, adding a new line of data each
> time
> 
> 
> On 2/02/2010, at 8:38 AM, Steve Lianoglou wrote:
> 
> > On Mon, Feb 1, 2010 at 2:16 PM, Rolf Turner <r.turner at auckland.ac.nz>
> wrote:
> >>
> >> On 31/01/2010, at 12:14 PM, ace834 wrote:
> >>
> >>>
> >>> Hi, I am pretty new to R. I'm trying run a regression repeatedly,
> adding a
> >>> new data point each time, and then storing the predicted Y values.
> For
> >>> example, let's say I have 500 data points and I run the regression.
> I would
> >>> then like to store the Y value, run the regression again using 501
> data
> >>> points, store the new Y, run the regression with 502 data points,
> store the
> >>> Y, and so on. What I would like is a new object that has all of the
> >>> predicted Y values.  I'm using time series data, and I'm running a
> >>> multivariable regression.  Any help would be greatly appreciated.
> Thanks.
> >>
> >>
> >> This would seem to be very easy to do using a for loop and a list.
> >
> > And what sounds like a perfect opportunity for the OP to learn how to
> > use `lapply` to make this happen ...
> 
> It's not at all clear to me that lapply() is the appropriate tool here.
> I.e. I can see a very simple syntax for a for loop construction, but
> not
> for a lapply() construction.
> 
> I stand to be corrected, but.
> 
> 	cheers,
> 
> 		Rolf Turner
> ######################################################################
> Attention:
> This e-mail message is privileged and confidential. If you are not the
> intended recipient please delete the message and notify the sender.
> Any views or opinions presented are solely those of the author.
> 
> This e-mail has been scanned and cleared by MailMarshal
> www.marshalsoftware.com
> ######################################################################
> 
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list