R:财务 - 通过 CAPM 为面板数据计算 Beta

R: Finance - Compute Beta via CAPM for Panel Data

我有以下数据,其中包含三个基金(A、B 和 C)及其各自的数据(Return 减去无风险利率)和(市场 Return 减去无风险利率) :

structure(list(`Fund Name` = c("A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C"), Date = c("2018-08-01", 
"2018-08-02", "2018-08-03", "2018-10-22", "2018-10-23", "2018-10-24", 
"2018-12-18", "2019-01-08", "2019-01-09", "2019-01-10", "2019-01-11", 
"2019-01-14", "2019-01-15", "2019-01-16", "2019-02-07", "2019-02-08", 
"2019-02-11", "2019-02-12", "2019-02-13", "2019-02-14", "2019-02-15", 
"2019-02-18", "2019-02-19", "2019-02-20", "2019-03-15", "2019-03-18", 
"2019-03-19", "2019-04-01", "2019-04-02", "2019-04-03", "2019-04-04", 
"2019-04-10", "2019-04-11", "2019-04-12", "2019-04-15", "2018-08-01", 
"2018-08-02", "2018-08-10", "2018-08-13", "2018-08-14", "2018-08-16", 
"2018-08-17", "2018-10-23", "2018-10-24", "2018-10-25", "2018-10-26", 
"2018-10-29", "2018-10-30", "2018-10-31", "2018-11-13", "2018-11-14", 
"2018-11-22", "2018-11-23", "2018-12-06", "2018-12-07", "2018-12-10", 
"2018-12-11", "2018-12-12", "2018-12-13", "2018-12-14", "2018-12-17", 
"2018-12-18", "2019-02-06", "2019-02-07", "2019-02-08", "2019-02-11", 
"2019-02-12", "2019-02-13", "2019-02-14", "2019-02-15", "2019-03-04", 
"2019-03-05", "2019-03-06", "2019-03-07", "2019-03-08", "2019-03-11", 
"2019-03-26", "2019-03-27", "2019-04-05", "2019-04-08", "2019-04-12", 
"2019-04-15", "2018-08-01", "2018-08-02", "2018-08-03", "2018-08-06", 
"2018-08-07", "2018-08-08", "2018-08-09", "2018-08-10", "2018-08-13", 
"2018-08-14", "2018-08-23", "2019-01-29", "2019-03-01", "2019-03-04", 
"2019-03-05", "2019-03-06", "2019-03-07", "2019-03-27", "2019-03-28", 
"2019-03-29", "2019-04-01", "2019-04-02", "2019-04-03", "2019-04-04", 
"2019-04-12", "2019-04-15"), `Return-RF` = c(NA, -0.031053409, 
-0.004149784, -0.019431914, -0.025985785, -0.022325086, -0.013000177, 
-0.005969802, 0.003743827, -0.005973689, -0.012279585, -0.012621233, 
-0.014248868, -0.000850313, -0.038296552, -0.020249538, -0.002319941, 
-0.003117846, -0.006643616, -0.012684205, 0.00480718, -0.000708029, 
-0.007510481, -0.001464912, -0.008793153, -0.003356718, -0.005595538, 
0.00592619, -0.006444843, 0.007778815, -0.01019018, -0.008793842, 
-0.003549589, 0.000596707, -0.005270976, NA, -0.024337163, -0.030609843, 
-0.012780354, -0.011857873, NA, -0.00906015, -0.035681946, -0.007920997, 
-0.020963305, -0.013154577, 0.002038879, -0.019934722, 0.007708796, 
-0.019404458, 0.000443959, -0.008925886, -0.017543139, -0.033810649, 
-0.002362211, -0.02975915, -0.002819632, -0.000687416, -0.006733802, 
-0.02423122, -0.017747687, -0.009444599, -0.006353213, -0.020454878, 
-0.028563249, -0.005726489, -0.003094262, -0.001040783, -0.012626742, 
-0.001097087, -0.009497361, -0.015542972, 5.53889e-05, -0.020560822, 
-0.023744172, -0.00744049, -0.00193544, -0.013016594, -0.008529772, 
-0.005602241, -0.004651093, -0.005644803, NA, -0.02207606, -0.006369491, 
-0.012551725, -0.003201358, -0.01153393, -0.010203346, -0.033352688, 
-0.01224557, -0.011346633, -0.012929118, -0.006728953, -0.004243723, 
-0.012659234, -0.009103863, -0.011760838, -0.023812576, -0.013908016, 
-0.013459074, -0.004005417, 0.004751808, -0.007972052, 0.006040872, 
-0.011324789, -0.000427748, -0.007779257), `Mkt-RF` = c(-0.64, 
-1.36, 0.36, -0.85, -1.53, -1.26, -0.41, 0.61, 1.51, -0.13, -0.21, 
-0.6, -0.01, 0.19, -1.63, -0.75, 0.33, 0.94, 0.07, 0.01, 1.22, 
0.46, 0.12, 0.55, 0.93, 0.39, 0.62, 1.09, 0.45, 1.01, -0.28, 
0.25, 0.11, 0.63, 0.3, -0.64, -1.36, -2.01, -0.28, -0.54, 0.71, 
0.41, -1.53, -1.26, 0.5, -0.61, 0.65, -0.07, 1.37, 1.01, -0.28, 
-0.44, -0.29, -2.49, 0.45, -1.98, 0.8, 1.98, -0.13, -1.23, -0.93, 
-0.41, -0.28, -1.63, -0.75, 0.33, 0.94, 0.07, 0.01, 1.22, 0.03, 
-0.03, -0.19, -1.44, -0.47, 0.85, 0.31, -0.14, 0.15, 0.24, 0.63, 
0.3, -0.64, -1.36, 0.36, -0.18, 0.73, -0.08, -0.42, -2.01, -0.28, 
-0.54, -0.54, 0.43, 0.52, 0.03, -0.03, -0.19, -1.44, -0.14, -0.34, 
0.67, 1.09, 0.45, 1.01, -0.28, 0.63, 0.3)), class = "data.frame", row.names = c(NA, 
-108L))

现在我想通过 CAPM 为三个不同的基金计算 beta。 我尝试使用 lm 函数,但它只给出了所有三个基金的一个测试版。

我尝试使用以下代码:

Panel <- Panel %>%
  group_by(`Fund Name`)

Regression <- lm(Panel$`Return-RF`~ Panel$`Mkt-RF`)

有人可以帮我处理代码吗?

您是否正在尝试计算方差和协方差来计算 beta?

我会把你的数据变成小标题,然后删除 NA 值,

(data %>% as_tibble() %>% drop_na())

然后您可以轻松提取每个公司的方差,

fundA <- data %>% filter(`Fund Name` == A)

然后求方差,

var(fundA$`Return-RF`)

您可以 split() 按基金分类的数据框,然后 运行 使用 lapply() 对每个子集进行回归:

Panel_Funds <- split(Panel, Panel$`Fund Name`)

Regressions <- lapply(
  Panel_Funds, 
  \(x) lm(`Return-RF` ~ `Mkt-RF`, data = x)
)

Regressions

输出:

$A

Call:
lm(formula = `Return-RF` ~ `Mkt-RF`, data = x)

Coefficients:
(Intercept)     `Mkt-RF`  
   -0.00964      0.01205  


$B

Call:
lm(formula = `Return-RF` ~ `Mkt-RF`, data = x)

Coefficients:
(Intercept)     `Mkt-RF`  
  -0.010538     0.008266  


$C

Call:
lm(formula = `Return-RF` ~ `Mkt-RF`, data = x)

Coefficients:
(Intercept)     `Mkt-RF`  
  -0.009401     0.010676  

如果要将系数保存到table,可以使用broom::tidy();例如,请参阅我的回答 here