1

I'm trying to calculate a rolling Beta regression for multiple stocks with a width of 12 past months.

I have the following dataset

Looks like:

enter image description here

I was searching a lot of posts, but somehow I didn't get it to work for my data frame.

func1 <- . %>% {
roll_regres.fit(x = cbind(1, .$MKT_ex),
              y = .$r_rf, width = 12L)$coefs }
out <- dt %>%
group_by(Stkcd) %>%
# make it explicit that data needs to be sorted
arrange(Date, .by_group = TRUE) %>%
do(cbind(reg_col = select(., MKT_ex, r_rf) %>% func1,
       date_col = select(., Date))) %>%
ungroup

I get the error message:

Error in roll_cpp(Y = y, X = x, window = width, do_compute_R_sqs = 
do_compute_R_sqs,  :     'dchdd' failed with code -1

My goal is to get an output, which contains the Date, Stkcd (Stocknumber) and the calculatet Beta (r_rf regressed on MKT_ex). What did I miss in my code?

I found also this code in the forum from

G. Grothendieck

, sadly it doesn't work for my dataset and I can't find out why.

rolli <- function(ix) {
data.frame(coef = rollapplyr(ix, width = 12, function(ix) { 
     coef(lm(y ~ x, data = dat, subset = ix))[2]
  }, by = 1), Date = dt$Date[ix][1], Stkcd = dt$Stkcd[ix][1])
}
do.call("rbind", by(1:nrow(dt), dat[c("Date", "Stkcd")], rolli)
MLavoie
  • 9,671
  • 41
  • 36
  • 56
a.student
  • 11
  • 3
  • I have similar problem when using roll_regres.fit. `Error in roll_cpp(Y = y, X = x, window = width, do_compute_R_sqs = do_compute_R_sqs, : 'dchdd' failed with code 1`. Does anybody know what it means? – Moysey Abramowitz Jun 15 '19 at 14:19
  • I know it is an old question, but I had the same problem a couple of days ago and the problem actually was a mistake in my series.I discovered a row that produced "NA" and as soon as I removed it, the rolling regression worked. So, if someone stop by, check the series to make sure you don not have NA´s, or other kind of error. – unmark1 Dec 21 '19 at 13:59

0 Answers0