R: Grouped rolling window linear regression with rollapply and ddply
I have a data set with several grouping variables on which I want to run a rolling window linear regression. The ultimate goals is to extract the 10 linear regressions with the lowest slopes and average them together to provide a mean minimum rate of change. I have found examples using rollapply to calculate rolling window linear regressions, but I have the added complication that I would like to apply these linear regressions to groups within the data set. Here is a sample data set and my current code which is close and isn't quite working. dat<-data.frame(w=c(rep(1,27), rep(2,27),rep(3,27)),