Home > database >  Multiple linear regression by group in a rolling window in R
Multiple linear regression by group in a rolling window in R

Time:04-26

My dataframe looks like this:

Date = c(rep(as.Date(seq(15000,15012)),2))
Group = c(rep("a",13),rep("b",13))
y = c(seq(1,26,1))
x1 = c(seq(0.01,0.26,0.01))
x2 = c(seq(0.02,0.26*2,0.02))
df = data.frame(Group,Date,y,x1,x2)

head(df,3)
Group Date y x1 x2
a 2011-01-26 1 0.01 0.02
a 2011-01-27 2 0.02 0.04
a 2011-01-28 3 0.03 0.06

And I would like to do multiple regression by group (y as the dependent variable and x1, x2 as the independent variables) in a rolling window i.e. 3.

I have tried to achieve this using packages tidyverse and zoo with following codes but failed.

  ## define multi-var-linear regression function and get the residual
  rsd <- function(df){
    lm(formula = y~x1 x2, data = as.data.frame(df), na.action = na.omit) %>%
      resid() %>%
      return()
  }
  ## apply it by group with rolling window
  x <- df %>% group_by(Group) %>%
    rollapplyr(. , width = 3, FUN = rsd)

The output of this code is not what I acutually want.

Does anyone know how to do multiple regression by group in a rolling window? Thanks in advance, Giselle

CodePudding user response:

A good old-fashioned for-loop here could be:

for (i in unique(df$Group)){
  for (j in (seq(15000,15012, 3))){
      lm_ <- lm(formula = df[df$Group== i & df$Date %in% c(j, j 1, j 2), 3] ~ df[df$Group== i & df$Date %in% c(j, j 1, j 2), 4]   df[df$Group== i & df$Date %in% c(j, j 1, j 2), 5], na.action = na.omit)
      print(paste('Group', i, 'Dates from', j, 'to', j 3, residuals(lm_)))
  }
}

CodePudding user response:

Use group_modify and use rollapplyr with the by.column = FALSE argument so that rsd is applied to all columns at once rather than one at a time.

Note that if you use width 3 with two predictors and an intercept the residuals will necessarily be all zero so we changed the width to 5.

library(dplyr, exclude = c("lag", "filter"))
library(zoo)

width <- 5

df %>% 
  group_by(Group) %>%
  group_modify(~ {
      cbind(., rollapplyr(.[c("y", "x1", "x2")], width, rsd, fill = NA,
          by.column = FALSE))
  }) %>%
  ungroup
  • Related