the rolling regression in R using roll apply

后端 未结 2 1188
北荒
北荒 2021-01-05 02:20

My imported data contains 7 variables: Y and X1, X2, X3, X4, X5, X6. I tried apply

2条回答
  •  猫巷女王i
    2021-01-05 02:25

    It is not really clear what your data actually is (use dput(example_data) to give reproducible examples).

    But the lm call in your example is simply doing the same regression over and over again (your x is not changing) and as josilber points out, it is supposed to be a function. Here is an example where all the data is in the data.frame allRegData and it has at least two columns, one named y and another named x:

    require(zoo)
    rollapply(zoo(allRegData),
              width=262,
              FUN = function(Z) 
              { 
                 t = lm(formula=y~x, data = as.data.frame(Z), na.rm=T); 
                 return(t$coef) 
              },
              by.column=FALSE, align="right") 
    

提交回复
热议问题