简体   繁体   English

如何在滚动回归中解决此错误?

[英]How do I solve this error in a rolling regression?

I am trying to compute a beta for this financial Data.我正在尝试计算此财务数据的 beta。 I want to extract the first coefficient of the regression and put that values into a new column.我想提取回归的第一个系数并将该值放入一个新列中。 This has to be done for each stock ID individually.这必须为每个股票 ID 单独完成。 The regression is supposed to use the data of the last 30 month as indicated by the code.回归应该使用代码所示的最近 30 个月的数据。 I use a rolling regression with the following idea:我使用具有以下想法的滚动回归:

check[, b := as.data.table(roll_regres(lm(check$RET.USD ~ check$RMRF), width = 30, min_obs = 12))[,2], by = Id]

The roll_regres function works fine for itself when i don't account for changing IDs with by = Id.当我不考虑使用 by = Id 更改 ID 时,roll_regres function 可以正常工作。 It seems logical to me that this doesn't work, but i can't figure out a different way.在我看来,这不起作用是合乎逻辑的,但我想不出不同的方法。 I use this data with two Ids over 3 years:我在 3 年内将这些数据与两个 ID 一起使用:

> head(check)
       Id RET.USD month year  RMRF       ym
1: 258580   -8.06     4 2001  7.94 Apr 2001
2: 258580  -11.57     5 2001  0.72 May 2001
3: 258580  -16.94     6 2001 -1.94 Jun 2001
4: 258580  -17.30     7 2001 -2.13 Jul 2001
5: 258580  -13.97     8 2001 -6.46 Aug 2001
6: 258580  -25.06     9 2001 -9.25 Sep 2001

Maybe some of you have better ideas:) Thank you!也许你们中的一些人有更好的想法:)谢谢!

We use a width of 5 and a minimum of 4 so that we can apply it to the 6 row data table in the question.我们使用宽度为 5 且最小为 4,以便我们可以将其应用于问题中的 6 行数据表。 Use either of the equivalent roll functions defined.使用定义的任一等效roll函数。 The one actually used avoids lm giving the same result.实际使用的那个避免 lm 给出相同的结果。

library(data.table)
library(zoo)

# roll <- function(x) coef(lm(x[, 1] ~ x[, 2]))[[2]]
roll <- function(x) cov(x[, 1], x[, 2]) / var(x[, 2])
DT[, beta := rollapplyr(cbind(RET.USD, RMRF), 5, roll, fill = NA, 
  partial = 4, by.column = FALSE), by = Id]

giving:给予:

> DT
       Id RET.USD month year  RMRF       ym      beta
1: 258580   -8.06     4 2001  7.94 Apr 2001        NA
2: 258580  -11.57     5 2001  0.72 May 2001        NA
3: 258580  -16.94     6 2001 -1.94 Jun 2001        NA
4: 258580  -17.30     7 2001 -2.13 Jul 2001 0.8889737
5: 258580  -13.97     8 2001 -6.46 Aug 2001 0.5514858
6: 258580  -25.06     9 2001 -9.25 Sep 2001 0.9459004

As a check we see that using lm we get the same value for beta in the 5th row:作为检查,我们看到使用 lm 我们在第 5 行得到相同的 beta 值:

coef(lm(RET.USD ~ RMRF, DT, subset = 1:5))[[2]]
## [1] 0.5514858

Note笔记

Lines <- '
     Id RET.USD month year  RMRF       ym
 258580   -8.06     4 2001  7.94 "Apr 2001"
 258580  -11.57     5 2001  0.72 "May 2001"
 258580  -16.94     6 2001 -1.94 "Jun 2001"
 258580  -17.30     7 2001 -2.13 "Jul 2001"
 258580  -13.97     8 2001 -6.46 "Aug 2001"
 258580  -25.06     9 2001 -9.25 "Sep 2001"'
DT <- fread(Lines)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM