[R-sig-finance] Financial Beta calculation

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jul 31 23:15:48 CEST 2005


On 7/31/05, August Petrillo <august.petrillo at gmail.com> wrote:
> I was wondering if anyone can point me towards an example R script or
> towards example code to calculate beta.  I have read some of the R
> documentation but I thought that being able to look at code that does
> something I already understand would make it a lot easier to pick up
> the R language.


str(EuStockMarkets)  # describes data set included with R
summary(EuStockMarkets)  # summary statistics

logret <- diff(log(EuStockMarkets))  # log returns
logret[1:10,] # first 10 rows
euz.lm <- lm(DAX ~ FTSE, logret)  # regress logret of DAX wrt logret of FTSE
euz.lm  # beta is shown among output
summary(euz.lm) # more output
coef(euz.lm)[2]  # beta as a single number
cov(logret[,"DAX"], logret[,"FTSE"])/var(logret[,"FTSE"]) # another way

# graphics
plot(DAX ~ FTSE, logret)
abline(euz.lm, col = "red")
legend("topleft", c("data", "regression"), fill = c("black", "red"))



More information about the R-sig-finance mailing list