Home > database >  R: Finance - Compute Beta via CAPM for Panel Data
R: Finance - Compute Beta via CAPM for Panel Data

Time:03-29

I have the following data containing three Funds (A, B and C) and their the respective data for (Return minus Risk Free Rate) and (Market Return minus Risk Free Rate):

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))

Now I would like to compute the beta via the CAPM for the three different funds. I tried with the lm function but I it gives only one beta for all three funds together.

I tried with the following code:

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

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

Could someone help me here with the code?

CodePudding user response:

You can split() your dataframe by fund, then run the regression on each subset using lapply():

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

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

Regressions

Output:

$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  

If you want to save the coefficients to a table, you can use broom::tidy(); see my answer here for an example.

CodePudding user response:

Are you trying to calculate the variance and covariance to compute the beta?

I would turn your data into a tibble then drop the NA values,

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

then you can easily extract variance for each company,

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

then get variance,

var(fundA$`Return-RF`)
  • Related