Hi,
I have a time-series with prices of two stocks. I would like to regress them on each other, and calculate the coefficient on every 125 day, based on the previous 250 observation. I found out, that the code below does this more or less. My only problem is that, with this solution, the windows are 1 day shorter, bacause they do not overlap. Is there a method which lets that the last day of a period be the first day of the next period as well?
coef<- function(x){
return(lm(x[,1]~0+x[,2])$coefficient[1])
}
beta<-na.locf(rollapply(merged,width = 250, FUN=coef, by.column = F, align = "right", by=125), fromLast = T)