Let’s estimate a stock’s beta and alpha using the CAPM. First prepare the data by calculating stock returns and market returns. Subtract the risk-free rate from both and form a data.frame called “urdata” where ExRet is the excess return on the stock and MktRP is the risk premium on the market. Next, specify the formula for the estimated model and call it “m2”. Use the lm() function to estimate the model and assign the results to a variable called “ols”. Finally, use summary() to see the regression results.
m2=ExRet~MktRP ols=lm(m2,data=urdata) summary(ols)
If you are using a lot of fixed effects, try using coef(ols)[1:5,] to get the first 5 rows of coefficients.