I am new to R and I am trying to run rolling regressions with an expanding window (that is for each date t use data up to t), with two independent variables in a data frame grouped by a categorical column.
For example, in the data frame below, I would like to extract coefficients of lm(return ~ regress1 + regress 2) grouped by category K using all rows until the row of interest. Thus for row 2, data set for regression will be rows 1:2, for row 3 will be rows 1:3, for row 4 will be just row 4 as it is the first row with categorical variable K = B.
myinput <- data.frame(K = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
date = c(1:3) , return = rnorm(9), regress1 = rnorm(9), regress2 = rnorm(9))
I found a very useful thread on this topic here: Rolling regression with expanding window in R, but I am having a difficult time applying it to my data set.
If anyone could help me understand how I need to adapt the approach they used it'd be very much appreciated. Thanks.