简体   繁体   中英

How do I solve this error in a rolling regression?

I am trying to compute a beta for this financial Data. 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. The regression is supposed to use the data of the last 30 month as indicated by the code. 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. 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:

> 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. Use either of the equivalent roll functions defined. The one actually used avoids lm giving the same result.

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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