Hi: rollapply is a fine solution but if you frame your problem as a state
space
model where the observation equation is the linear regression you're
interested in,
the state space framework provided by the DLM package will give you the
output
you want at each step t. the gory details are explained in the paper below
but
I only have a hard copy ( somewhere ? ) and it's 14.00 from JSTOR.

http://www.jstor.org/discover/10.2307/2284643?uid=3739832&uid=2&uid=4&uid=3739256&sid=21101972960881




On Tue, Apr 2, 2013 at 8:58 PM, David Chertudi <david_txertudi@yahoo.fr>wrote:

> Thank you, Michael--it looks like a fantastic package.  I'm fairly new to
> R, and not at all familiar with the zoo class, but it seems to have great
> potential.  I'll need to spend some time sorting out how to use regressions
> in it, because the syntax used in the examples is alien to me.
>
> Mendi
>
>
> ________________________________
> De : Michael Weylandt <michael.weylandt@gmail.com>
> À : Rui Barradas <ruipbarradas@sapo.pt>
>
> @r-project.org" <r-help@r-project.org>
> Envoyé le : Mardi 2 avril 2013 11h58
> Objet : Re: [R] Iterative regression through a series
>
> Also look at zoo's rollapply.
>
> MW
>
> On Apr 2, 2013, at 13:51, Rui Barradas <ruipbarradas@sapo.pt> wrote:
>
> > Hello,
> >
> > The error comes from NAs where you would expect coefficients. Try the
> following.
> >
> >
> >
> > set.seed(7511)  # Make the example reproducible
> >
> > N <- 100
> > Time <-1:N
> > Price <- rnorm(N, 8, 2)
> >
> > estim <- numeric(N)  # It's better to allocate the results
> > error <- numeric(N)  # vectors in advance
> > for (i in Time) {
> >    regr <- lm(Price[1:i] ~ Time[1:i])
> >    estim[i] <- coef(regr)[2]
> >    if(is.na(coef(regr)[2]))
> >        error[i] <- NA
> >    else
> >        error[i] <- summary(regr)$coefficients[2,2]
> >
> > }
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
>
> >> Hello,
> >>
> >> Some context:  let's say I have a data series (let's call it PRICE, for
> simplicity), sample size N.  I have a desire to regress that on TIME, and
> then to use the TIME and intercept coefficients to predict the price in the
> next period and to use the standard error to calculate a confidence
> interval.  This is all very easy.
> >>
> >> However, what I need help for is to calculate a confidence interval for
> each point in time:  imagining that at the end of the 10th period I have 10
> data points, and wish to regress them on the 10 periods to create a
> confidence interval for the next 'predicted' price.  And so on from
> TIME[10:100].  So the first regression would be of PRICE[1:10] on
> TIME[1:10], the second of PRICE[1:11] on TIME[1:11], the third of
> PRICE[1:11] on TIME[1:11], and so on to PRICE[1:N] and TIME[1:N].  I'd like
> to be able to vary the starting point (so it would need to be an argument
> in the function, in this case it would be 10).  The ultimate output of the
> code would be to save the TIME coefficients and standard errors it
> generates to two vectors, say TIME.coef and TIME.SE.
> >>
> >> I'm not sure if lapply() can be bent to my will, or if a for loop would
> be too inefficient, or what.  I'm not new to R, but I'm fairly new to this
> kind of programming.
> >>
> >> This is a bungled mess of a narrative, and I apologize.  Please feel
> free to use TIME=1:100 and PRICE=rnorm(100,8,2).
> >>
> >> Here's an attempt, which has failed for reasons I can only imagine.
> Any help getting this to work would be greatly appreciated.  Any help doing
> this without loops would be even better.
> >>
> >>
> >>> Time=1:100
> >>> Price=rnorm(100,8,2)
> >>>
> >>> estim=0    #I'm hoping this will be the Time coefficient
> >>> error=0      #I'm hoping this will be the standard error of the Time
> coefficient
> >>> for (i in Time) {
> >> +    regr=lm(Price[1:i]~Time[1:i])
> >> +    estim=c(estim,coef(summary(regr))[2,1])
> >> +    error=c(error,coef(summary(regr))[2,1])
> >> +    }
> >> Error: subscript out of bounds
> >>
> >>
> >> Many, many thanks in advance.
> >>
> >> Mendi
> >>    [[alternative HTML version deleted]]
> >>
> >>
> >>
> >> ______________________________________________
> >> R-help@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.
> >
> > ______________________________________________
> > R-help@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.
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@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.
>
>

	[[alternative HTML version deleted]]

