[R] Iterative regression through a series

Rui Barradas ruipbarradas at sapo.pt
Tue Apr 2 20:51:26 CEST 2013


The error comes from NAs where you would expect coefficients. Try the 

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]
	     error[i] <- NA
	     error[i] <- summary(regr)$coefficients[2,2]


Hope this helps,

Rui Barradas

Em 02-04-2013 17:44, triskell4-umbrella at yahoo.fr escreveu:
> 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 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