data.table多个因子和键的残差方差

时间:2015-08-16 10:21:15

标签: r data.table

我有一个很大的'data.table',由多个股票的每日回报组成。

rm(list = ls())
library(data.table)
set.seed(1080)
Firm1 <- rbind(data.table(Month = rep(200001, 3), Firm = rep(1, 3), Rt = rnorm(3)) ,data.table(Month = rep(200002, 4), Firm = rep(1, 4), Rt = rnorm(4)),
           data.table(Month = rep(200003, 5), Firm = rep(1, 5), Rt = rnorm(5)), data.table(Month = rep(200004, 2), Firm = rep(1, 2), Rt = rnorm(2)),
           data.table(Month = rep(200005, 3), Firm = rep(1, 3), Rt = rnorm(3)))
Firm2 <- rbind(data.table(Month = rep(200001, 1), Firm = rep(2, 1), Rt = rnorm(1)) , data.table(Month = rep(200002, 3), Firm = rep(2, 3), Rt = rnorm(3)),
           data.table(Month = rep(200003, 4), Firm = rep(2, 4), Rt = rnorm(4)), data.table(Month = rep(200004, 1), Firm = rep(2, 1), Rt = rnorm(1)),
           data.table(Month = rep(200005, 3), Firm = rep(2, 3), Rt = rnorm(3)))
Firm3 <- rbind(data.table(Month = rep(200001, 3), Firm = rep(3, 3), Rt = rnorm(3)) ,data.table(Month = rep(200002, 6), Firm = rep(3, 6), Rt = rnorm(6)),
           data.table(Month = rep(200003, 5), Firm = rep(3, 5), Rt = rnorm(5)), data.table(Month = rep(200004, 5), Firm = rep(3, 5), Rt = rnorm(5)),
           data.table(Month = rep(200005, 2), Firm = rep(3, 2), Rt = rnorm(2)))

DT <- rbind(Firm1, Firm2, Firm3)

DT[, Mar := rnorm(50)]

哪个给出了

Month Firm          Rt          Mar
200001    1 -1.34767475  0.865598407
200001    1 -0.70741105 -0.782668556
200001    1  0.61342578  0.021440129
200002    1 -1.53156217  1.988291260
200002    1 -0.42512876 -0.384017585
...       

在此示例中,多个“月”因素与每日观察有关。每个公司每个月都有不同数量的每日观察结果。

我想做的是从Rt与Mar的线性回归估计残差的方差。为了每月这样做,我会使用

DT[, var(lm(Rt ~ Mar)$residuals), by = c("Firm", "Month")] 

与上述不同,我希望每个月都使用之前的n个月的观察结果。天真的尝试可能看起来像

DT[, var(lm(Rt ~ Mar)$residuals), by = c("Firm", Month[t : t-2])]

最终输出

Month Firm          Rt          Mar      resVariance
200001    1 -1.34767475  0.865598407  NA
200001    1 -0.70741105 -0.782668556  NA
200001    1  0.61342578  0.021440129  NA
200002    1 -1.53156217  1.988291260  NA
200002    1 -0.42512876 -0.384017585  NA
200002    1  1.06399050 -1.123293332  NA
200002    1 -1.59751358 -0.188190495  NA
200003    1  0.08854875  0.897471055  0.8745559
200003    1  0.37822085 -0.654418019  0.8745559
200003    1  1.07786336  1.665720591  0.8745559
200003    1  0.92820233 -1.983931767  0.8745559
200003    1 -0.43148095 -0.286302699  0.8745559
200004    1 -0.80384703 -0.927657523  0.841801
200004    1 -0.79383439  1.281582524  0.841801
...

对此的任何建议都将不胜感激。

0 个答案:

没有答案