具有多元回归和面板数据的 Rollregres [英] Rollregres with multiple regression and panel data

查看:100
本文介绍了具有多元回归和面板数据的 Rollregres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的面板数据集:

I have a panel data set like the following:

          id  date1    Returns Mkt.RF SMB.y HML.y RMW.y CMA.y RF.y
1 LP60068503 200002  3.9487727   5.95  0.47 -9.00  3.25 -7.18 0.43
2 LP60068503 200003  4.6201232   0.66 -5.04  3.10 -2.74  4.82 0.47
3 LP60068503 200004 -1.2757605  -5.58 -4.37  5.67 -1.08  1.22 0.46
4 LP60068503 200005 -1.3916501  -0.08  0.18  6.05 -3.30  4.84 0.50
5 LP60068503 200006 -2.4193548   0.67  0.50  2.94 -3.15  1.10 0.40
6 LP60068503 200007  0.8264463  -1.58 -0.71  3.25 -0.19 -0.10 0.48

               id  date1    Returns Mkt.RF SMB.y HML.y RMW.y CMA.y RF.y
340373 LP65117791 201207  3.4376360   0.56 -1.38 -2.57  2.29 -2.04 0.00
340374 LP65117791 201208  0.7893412   4.51  0.06  3.38 -1.68  1.45 0.01
340375 LP65117791 201209  0.2556494   3.49  1.65  2.33 -1.52  0.69 0.01
340376 LP65117791 201210 -1.0310320   1.67 -0.61  2.06 -0.93 -0.15 0.01
340377 LP65117791 201211  0.3411351   2.28 -2.40 -0.55  0.62 -0.80 0.01
340378 LP65117791 201212  0.7903986   3.38  2.48  3.09 -0.53  0.89 0.01

我想用三个独立变量计算以下滚动回归,并在新列中获得 alpha (a) 截距.

I would like to calculate the following rolling regression with three independet variables and get the alpha (a) intercept in a new column.

返回 ~ a + ß1*Mkt.RF + ß3*SMB.y + ß3*HML.y + e

Returns ~ a + ß1*Mkt.RF + ß3*SMB.y + ß3*HML.y + e

宽度窗口应为 36 个月.

The width window should be 36 months.

使用 rollRegres 函数可以处理一个自变量,但我想知道如何将此包调整为三个自变量.在具有一个因变量的新列中获取 alpha 截距的代码如下所示:

Using the rollRegres function works with one independent variable but I would like to know how to adjust this package to three independent variables. The code for getting the alpha intercept in a new column with one dependent variable would look like this:

返回 ~ a + ß1*Mkt.RF + e

Returns ~ a + ß1*Mkt.RF + e

dt[, alpha:=
   roll_regres.fit(x = cbind(1, .SD[["Mkt.RF"]]), y = .SD[["return"]],
                   width = 36L)$coefs[, 1],
   by = id]

所以我想知道如何将其调整为三个或更多自变量.

So I'd like to know how to adjust this to three or more independent variables.

推荐答案

看来您正在使用 data.table 所以我也将使用.您可以按以下方式实现您想要的

It seems like you are using data.table so I will too. You can achive what you want as follows

#####
# simulate data 
n_gr   <- 100 
n_date <- 50
n <- n_gr * n_date
id <- gl(n_gr, n_date)
date. <- rep(1:n_date, n_gr)

set.seed(39820955)
X <- matrix(rnorm(n * 3), n, 
            dimnames = list(NULL, c("Mkt.RF", "SMB.y", "HML.y")))
library(data.table)  
dt <- data.table(id = id, date1 = date., Returns = rowSums(X) + rnorm(n), X)

#####
# estimate coefficients
setkey(dt, id, date1) # sort data

library(rollRegres)
func <- function(SD){
  x <- roll_regres.fit(
    x = cbind(1, SD$Mkt.RF, SD$SMB.y, SD$HML.y), y = SD$Returns, 
    width = 36L)$coefs
  split(x, rep(1:ncol(x), each = nrow(x))) # turn matrix into list of column vectors
}
dt[, c("alpha", "b.Mkt", "b.SMB", "b.HML") := func(.SD), by = id]

tail(dt)
#R     id date1  Returns  Mkt.RF  SMB.y  HML.y  alpha b.Mkt b.SMB b.HML
#R 1: 100    45  1.08926  1.0470  0.277 -0.179 -0.355 0.854  1.25  1.09
#R 2: 100    46 -0.09738 -0.0718 -0.190  0.860 -0.318 0.813  1.26  1.06
#R 3: 100    47  0.00525  1.3981  1.618 -1.335 -0.349 0.742  1.18  1.09
#R 4: 100    48  0.65891 -0.3901 -0.239  1.558 -0.266 0.732  1.02  1.11
#R 5: 100    49 -0.18841 -0.4336  0.266  0.657 -0.265 0.761  1.06  1.14
#R 6: 100    50 -1.55515 -0.6723 -1.567  1.014 -0.275 0.769  1.08  1.14

alphab.Mktb.SMBb.HML 列是斜率并拦截.

The columns alpha, b.Mkt, b.SMB, and b.HML are the slopes and intercepts.

使用 rollRegres 函数可以处理一个自变量,但我想知道如何将此包调整为三个自变量.

Using the rollRegres function works with one independent variable but I would like to know how to adjust this package to three independent variables.

这不是真的.这些函数适用于多元回归.在撰写本文时,小插图和手册页中唯一的示例是多元回归......参见例如,help("roll_regres")vignette("Comparisons", package= "rollRegres").

This is not true. The functions work for multiple regression. As of this writing, the only examples in both the vignette and the manual pages are with multiple regression... See e.g., help("roll_regres") or vignette("Comparisons", package = "rollRegres").

这篇关于具有多元回归和面板数据的 Rollregres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆