[R-sig-finance] function to calc Fama-French 3 factor model

Andrew West jgalt70 at yahoo.com
Thu Sep 1 20:24:31 CEST 2005


Hello,
I have written a function that downloads data from
Yahoo and Ken French's website to automatically
calculate a regular beta, a robust beta, and the betas
for the Fama-French 3-factor model, then using these
factor estimates to estimate various costs of equity
for the company. After pasting in the function, one
calls it by typing:
getffBeta("CSX") -for example to look at CSX

The function doesn't work however for companies whose
stock history doesn't go back to Jan 1991. I haven't
been able to figure out how to automatically set the
starts of the time series to adjust to short histories
in a get.hist.quote data download, mostly because I
cannot figure out how to connect dates from the
get.hist.quote data to the dating of ts objects. The
code also assumes a C: drive exists.

I am quite a novice programmer, and know that the code
is pretty poor, but may be still be worthwhile to
someone. Any improvements offered for the function's
code would be appreciated. It follows here:

getffBeta=function(tool) {
require(MASS);
require(tseries);
require(wle)
stock= (get.hist.quote(instrument = tool, quote =
c("Ad"),compression=”m”));
spx=(get.hist.quote(instrument = "^gspc", quote =
c("Ad"),compression=”m”));
today=Sys.Date()
seqmo=length(seq(as.Date("1926-07-01"),today,by="month"))-2
url=
”http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors.zip”
destfile="C:\\F-F_Research_Data_Factors.zip"
dataff=download.file(url, destfile, mode=’wb’)
unzip=unz(destfile,"F-F_Research_Data_Factors.txt")
ff4 <- read.table(unzip, header=FALSE, sep="",
na.strings="NA", dec=".", strip.white=TRUE, skip=4,
nrows=seqmo)
ffdata <- ff4
attach(ffdata);
ffdata=ffdata/100
#find out starting year month for ffdata;
ffdatats=ts(ffdata, start=c(1926,7), frequency=12);
stock=ts(na.remove(stock), start=c(1991, 1),
frequency=12);
spx=ts(na.remove(spx), start=c(1991, 1),
frequency=12);
combined= na.remove(ts.union(stock,spx));
combreturn= na.remove(diff(log(combined)));
combreturn=ts(combreturn,
start=c(1991,2),frequency=12);
combined=na.remove(ts.intersect(combreturn,ffdatats),
names=c(“stockret”, “spxret”, “dates”,”ffmktret”,
“smb”,”hml”,”rf”));
combined[,1]=combined[,1]-combined[,7]
combined[,2]=combined[,2]-combined[,7]

simplereg=lm(combined[,1]~combined[,2]);
stockbeta=simplereg$coef[2];
textout=paste(”stock beta=”, stockbeta);
robustreg=wle.lm(combined[,1]~combined[,2]);
robstockbeta=robustreg$coef[2];
textout2=paste(”robust stock beta=”, robstockbeta);

plot(as.numeric(combined[,1])~as.numeric(combined[,2]),xlab=”S&P”,
ylab=tool)
title(main=textout, sub=list(textout2, col=”red”))
abline(coef(simplereg));
abline(coef(robustreg),col=”red”)
print(textout);
print(textout2);
ffreg=wle.lm(combined[,1]~combined[,4]+combined[,5]+combined[,6]);
ffbeta=ffreg$coef[2];
ffsmb=ffreg$coef[3];
ffhml=ffreg$coef[4] ;
textout3=paste(‘robust ff beta=’,ffbeta) ;
textout4=paste(‘robust ff smb=’,ffsmb) ;
textout5=paste(‘robust ff hml=’,ffhml) ;
print(textout3) ;
print(textout4) ;
print(textout5) ;
rf=5
smb=1
hml=2
rm=5
ffcoe=rf+ffbeta*rm+ffsmb*smb+ffhml*hml
robcoe=rf+robstockbeta*rm
regcoe=rf+stockbeta*rm
textout6=paste(’regular coe=’,regcoe,’ robustcoe=’,
robcoe)
textout7=paste(‘Fama-French coe=’,ffcoe)
print(textout6)
print(textout7)
}



More information about the R-sig-finance mailing list