Home > OS >  Create column with rolling linear model coefficients
Create column with rolling linear model coefficients

Time:07-19

I need to perform a rolling linear regression of 24 months between a variable and time and save the B1 coefficient on a column. I can do the rolling regression but I don't know how to save the B1 results in my original dataframe. Here is a reproducible example:

date <- seq(as.Date("2011-01-01"), as.Date("2012-01-01"), by="days")

set.seed(8)
df <- as.data.frame(date) %>%
  mutate(value = runif(366, min = 0, max = 100))

#Estimate rolling linear regression coef when t = 24
nr <- nrow(df)
reg <- function(ix) coef(lm(value ~ date, df, subset = ix)); rollapplyr(1:nr, 24, reg, fill = NA)

#How to save B1 results in a column of the data frame?

Does anybody knows how to do this? Thanks in advance!

CodePudding user response:

It's perhaps better practice to lm(.) on values instead of reaching out to df for it. Also, rollapplyr can work on a frame, row-wise, using by.column=FALSE, though one catch is that it converts everything to the highest class; to work with that, we need to preemptively convert date to numeric.

reg2 <- function(X) coef(lm(value ~ date, data = as.data.frame(X)))

reg2 <- function(X) setNames(coef(lm(value ~ date, data = data.frame(X))), c("intercept", "coef_date"))
df %>%
  bind_cols(rollapplyr(transmute(., value, date=as.numeric(date)),
                       24, FUN = reg2, by.column = FALSE, fill = NA)) %>%
  tail()
#           date    value intercept  coef_date
# 361 2011-12-27 10.48895 -7016.084  0.4613276
# 362 2011-12-28 45.51299 -1602.472  0.1080812
# 363 2011-12-29 35.17561 -2635.868  0.1753899
# 364 2011-12-30 44.89097 -8457.421  0.5550818
# 365 2011-12-31 58.28106 -3354.462  0.2222435
# 366 2012-01-01 22.91829  6228.027 -0.4029083

CodePudding user response:

Assuming that B1 means the slope of the regression line:

df %>% 
  mutate(slope = rollapplyr(1:nr, 24, function(x) reg(ix)[[2]], fill = NA))
  • Related