I am trying to create a time series graph of the rolling average of the OLS Beta parameter estimates from a single data set. I need to regress rows 1-36 of the data with OLS, find the Beta parameter estimate, and then do so again with rows 2-37, and so on. Once that is done, I need to plot these points in a graph. I can't find a way to automate this process of finding the OLS Beta estimates for each subsection of data. Does anyone have any ideas?
Thanks!
Here is a quick example, but I could perhaps clarify if I saw an example of your data?
Let's say you have a 1000 row frame df
, with outcome y
and predictor x
, and you want the intercept and slope of a regression done in every 36-row sliding window (rows 1-36, 2-37, 3-38, etc). First, run the 964 regression models and save the coefficients.
coefs = do.call(rbind, lapply(36:nrow(df),\(i) lm(y~x,data=df[(i-36):i])$coef))
Add them to the data.table
df[36:nrow(df),c("intercept","beta"):=list(coefs[,1], coefs[,2])]
Output:
x y intercept beta
1: -0.56047565 -0.99579872 NA NA
2: -0.23017749 -1.03995504 NA NA
3: 1.55870831 -0.01798024 NA NA
4: 0.07050839 -0.13217513 NA NA
5: 0.12928774 -2.54934277 NA NA
---
996: -0.08997520 0.07664366 0.06368493 0.16388413
997: 1.07051604 0.25516476 0.11283476 0.20039460
998: -1.35110039 0.27744682 0.06146300 0.08411488
999: -0.52261670 0.53685602 0.08775808 0.08424470
1000: -0.24919068 -0.46048557 0.03574618 0.07458988
Input:
library(data.table)
set.seed(123)
df = data.table(x=rnorm(1000), y=rnorm(1000))