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

Gabor Grothendieck ggrothendieck at gmail.com
Fri Sep 2 17:00:10 CEST 2005


First load the zoo package so that we can use some of its
date handling facilities: 

- zoo includes an extra as.Date method, not in base, that 
  will convert daily numeric series that are represented
  as the number of days since the Date class' origin
  to Date class

- zoo includes a new date class, yearmon, that represents
  monthly time series using the same internal representation
  that ts uses (for easy conversion)

First download the quotes specifying (1) monthly quotes and
that the origin is to be the Date class origin.

Now extract the data part from ibm.ts.0 using the coredata
function in zoo and omit the missing values.  To get the
start date convert the numeric ts start date to Date class
(which we can do since we specified the origin consistently)
and from that convert it to yearmon class.  Finally get the
numeric representation of the start date, which is what
ts uses.

library(tseries)
library(zoo) 
ibm.ts.0 <- get.hist.quote("cryp", compression = "m", origin = as.Date(0))
ibm.ts <- ts( na.omit(coredata(ibm.ts.0)), frequency = 12,
	start = as.numeric(as.yearmon(as.Date(start(ibm.ts.0)[1]))) )


On 9/1/05, Andrew West <jgalt70 at yahoo.com> wrote:
> 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)
> }
> 
> _______________________________________________
> R-sig-finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>



More information about the R-sig-finance mailing list