Search code examples
rlmrollapply

variable lengths differ error when rollapply lm


I am trying to run a rolling window regression on a number of time series but encountered this strange problem. The following codes reproduce my data. I have a data frame containing returns named "rt" and a data frame containing factors named "factors". Then I produce a function to obtain the regression constant variable.

    mat<-as.data.frame(matrix(runif(88*6), nrow = 88, ncol = 6))



    colnames(mat)<-c("MKT","SMB","HML","AA","BB","CC")
    rt<-mat[,c(4,6)]
    factors<-mat[,c(1:3)]

    coeffstat_alpha<-function(x){
      fit<-lm(x~MKT+SMB+HML,data=factors,na.action=na.omit)
      nn<-c(t(coeftest(fit)))[1]

      return(nn)

    }

When I run this function on the whole sample, it works.

    apply(rt,2,FUN=coeffstat_alpha)

but when I rollapply the function, I received the error message

    rollapply(reg[,1],width=24,FUN=coeffstat_alpha,by=1,align="left")

"Error in model.frame.default(formula = x ~ MKT + SMB + HML, data = factors, : variable lengths differ (found for 'MKT')"

I have tried to fixed the problem by search online but couldn't find a post with the similar question. Can anyone help? Thanks!


Solution

  • As the error message suggests the length of variables differ meaning you are passing x in the function which is of length 24 (width) whereas using factors matrix which has 88 rows in it. For this to run you need to have equal length of x as well as factor. You can change the function to

    library(lmtest)
    
    coeffstat_alpha<-function(x){
      fit<-lm(rt[x, 1]~MKT+SMB+HML,data=factors[x, ],na.action=na.omit)
      nn<-c(t(coeftest(fit)))[1]
      return(nn)
    }
    

    and use sapply as :

    sapply(1:(nrow(rt)-23), function(x) coeffstat_alpha(x:(x+23)))