[R] Rolling regressions with sample extended one period at a time

Peter Ehlers ehlers at ucalgary.ca
Sat Mar 17 00:35:39 CET 2012


If the OP just wants the last fitted value, use fitted():

dats <- data.frame(x = 1:100, y = rnorm(100))

num.runs <- 21
fitvec <- numeric(num.runs)

for(i in seq_len(num.runs)){
   fits <- fitted(lm(y ~ x, data = dats[1:(80+i-1), ]))
   fitvec[i] <- tail(fits, 1)
}

Peter Ehlers

On 2012-03-16 14:05, R. Michael Weylandt wrote:
> You can use predict() just as you would anywhere else:
>
> for(i in 80:100){
>      predict(lm(y ~ x, data = dats[1:i, ]), newdata = ???)
> }
>
> Michael
>
> On Fri, Mar 16, 2012 at 8:12 AM, pie'<tcpietro at yahoo.it>  wrote:
>> hey,
>>
>>
>> thanks for the hint. I too figured I'd have to write a for-loop. I have the
>> problem now of how to extract the single element of the fitted values
>> vector. For example, run 1 of the regression generates 80 fitted values, run
>> 2 generates 81 fitted values, run 3 produces 82 fitted values and so on. I
>> need to extract the 80th, 81st, 82nd (all the last values generated at every
>> run) value into a separate vector. I know that lm (and glm) provides a
>> specific function for extracting  the predicted values but I don't know how
>> to use it inside a loop. Can you suggest anything?
>>
>>
>> P.
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Rolling-regressions-with-sample-extended-one-period-at-a-time-tp4470316p4478016.html
>> Sent from the R help mailing list archive at Nabble.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.
>
> ______________________________________________
> 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