rolling regression with dplyr

China☆狼群 提交于 2019-11-30 16:10:03
## lms is a function which calculate the linear regression coefficient
lms <- function(y, x){
s = which(is.finite(x * y))
y = y[s]
x = x[s]
return(cov(x, y)/var(x))
}

## z is a dataframe which stores our final result
z <- data.frame()

## x has to be ungrouped
x <- ungroup(x)

## subset with "filter" and roll with "rollapply"
symbols <- unique(x$company)
for(i in 1:length(symbols)){
temp <- filter(x, company == symbols[i])
z <- rbind(z, mutate(temp, beta = rollapply(temp[, c(3, 4)], 
                                          FUN = function(x) lms(x[, 1], x[, 2]),
                                          width = 20, fill = NA,
                                          by.column = FALSE, align = "right")))
}

## final result
print(z)

Here is a dplyr solution

#####
# setup data as OP (notice the fix when computing the market return)
library(dplyr)
set.seed(41797642)
n.dates <- 60
n.stocks <- 2
date <- seq(as.Date("2011-07-01"), by=1, len=n.dates)
symbol <- replicate(n.stocks, paste0(sample(LETTERS, 5), collapse = ""))
x <- expand.grid(date, symbol)
x$return <- rnorm(n.dates*n.stocks, 0, sd = 0.05)
names(x) <- c("date", "company", "return")

x <- x %>%
  group_by(date) %>%
  mutate(market.ret = mean(return))

#####
# compute coefs using rollRegres
library(rollRegres)
func <- . %>% {
    roll_regres.fit(x = cbind(1, .$market.ret),
                    y = .$return, width = 20L)$coefs }
out <- x %>%
  group_by(company) %>%
  # make it explicit that data needs to be sorted
  arrange(date, .by_group = TRUE) %>%
  do(cbind(reg_col = select(., market.ret, return) %>% func,
           date_col = select(., date))) %>%
  ungroup

head(out[!is.na(out$reg_col.1), ], 5)
#R # A tibble: 5 x 4
#R company reg_col.1 reg_col.2 date
#R   <fct>       <dbl>     <dbl> <date>
#R 1 SNXAD    -0.0104      0.746 2011-07-20
#R 2 SNXAD    -0.00953     0.755 2011-07-21
#R 3 SNXAD    -0.0124      0.784 2011-07-22
#R 4 SNXAD    -0.0167      0.709 2011-07-23
#R 5 SNXAD    -0.0148      0.691 2011-07-24
tail(out[!is.na(out$reg_col.1), ], 5)
#R # A tibble: 5 x 4
#R company  reg_col.1 reg_col.2 date
#R   <fct>        <dbl>     <dbl> <date>
#R 1 UYLTS   -0.00276       0.837 2011-08-25
#R 2 UYLTS    0.0000438     0.928 2011-08-26
#R 3 UYLTS    0.000250      0.936 2011-08-27
#R 4 UYLTS   -0.000772      0.886 2011-08-28
#R 5 UYLTS    0.00173       0.902 2011-08-29

It is very close to the answer here which is fairly close to this answer though using the rollRegres package.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!