at first use setwd() function to set your directory where you saved ur file
the use read.csv(” your file name.csv”) to read it
Thanks for your comment on percentages. I am having difficulty reproducing the results because nowhere have I found whether the monthly FF data is percentages (which you confirm, thank you), but is it annualized percentages or percentages on the month, and if annualized, is it compounded or not. Very frustrating that an academic site does not define precisely what the data represents!
Thanks, in anticipation!
]]>Thanks for your helpful tutorial. We all download these factor from French’s data library. But how does he construct these 25 portfolios? Since I’m trying to construct the 3 factors using Chinese stock market’s data, I have to construct these portfolios by myself. Could you give me some help on constructing FF’s 25 portfolios? Thanks in advance.
Kind Regards,
Shuhua
# Fama-French Regression example in R
# Load CSV file into R
ff_data <- read.table("ffdata.csv",header=TRUE,sep=",")
# Extract Fama-French Factors and Fund Returns
rmrf <- ff_data[,1]
smb <- ff_data[,2]
hml <- ff_data[,3]
rf <- ff_data[,4]
fund1 <- ff_data[,5]
fund2 <- ff_data[,6]
fund3 <- ff_data[,7]
fund4 <- ff_data[,8]
fund5 <- ff_data[,9]
fund6 <- ff_data[,10]
fund7 <- ff_data[,11]
fund8 <- ff_data[,12]
fund9 <- ff_data[,13]
fund10 <- ff_data[,14]
fund11 <- ff_data[,15]
fund12 <- ff_data[,16]
fund13 <- ff_data[,17]
fund14 <- ff_data[,18]
fund15 <- ff_data[,17]
fund16 <- ff_data[,20]
fund17 <- ff_data[,21]
fund18 <- ff_data[,22]
fund19 <- ff_data[,23]
fund20 <- ff_data[,24]
fund21 <- ff_data[,25]
fund22 <- ff_data[,26]
fund23 <- ff_data[,27]
fund24 <- ff_data[,28]
fund25 <- ff_data[,29]
fund26 <- ff_data[,30]
fund27 <- ff_data[,31]
fund28 <- ff_data[,32]
fund29 <- ff_data[,33]
fund30 <- ff_data[,34]
fund31 <- ff_data[,35]
fund32 <- ff_data[,36]
fund33 <- ff_data[,37]
fund34 <- ff_data[,38]
fund35 <- ff_data[,39]
fund36 <- ff_data[,40]
fund37 <- ff_data[,41]
fund38 <- ff_data[,42]
fund39 <- ff_data[,43]
fund40 <- ff_data[,44]
fund41 <- ff_data[,45]
fund42 <- ff_data[,46]
fund43 <- ff_data[,47]
fund44 <- ff_data[,48]
fund45 <- ff_data[,49]
fund46 <- ff_data[,50]
fund47 <- ff_data[,51]
fund48 <- ff_data[,52]
fund49 <- ff_data[,53]
fund50 <- ff_data[,54]
fund51 <- ff_data[,55]
fund52 <- ff_data[,56]
fund53 <- ff_data[,57]
fund54 <- ff_data[,58]
fund55 <- ff_data[,59]
fund56 <- ff_data[,60]
fund57 <- ff_data[,61]
fund58 <- ff_data[,62]
fund59 <- ff_data[,63]
fund60 <- ff_data[,64]
fund61 <- ff_data[,65]
fund62 <- ff_data[,66]
fund63 <- ff_data[,67]
fund64 <- ff_data[,68]
fund65 <- ff_data[,69]
fund66 <- ff_data[,70]
fund67 <- ff_data[,71]
fund68 <- ff_data[,72]
fund69 <- ff_data[,73]
fund70 <- ff_data[,74]
fund71 <- ff_data[,75]
fund72 <- ff_data[,76]
fund73 <- ff_data[,77]
ffregression <- lm(fund1 ~ rmrf + smb + hml)
# Print summary of regression results
print(summary(ffregression))}
ffregression <- lm(fund2 ~ rmrf + smb + hml)
# Print summary of regression results
print(summary(ffregression))}
ffregression <- lm(fund3 ~ rmrf + smb + hml)
# Print summary of regression results
print(summary(ffregression))}
ffregression <- lm(fund4 ~ rmrf + smb + hml)
# Print summary of regression results
print(summary(ffregression))}
ffregression <- lm(fund5 ~ rmrf + smb + hml)
# Print summary of regression results
print(summary(ffregression))}
ffregression <- lm(fund6 ~ rmrf + smb + hml)
# Print summary of regression results
print(summary(ffregression))}
ffregression <- lm(fund7 ~ rmrf + smb + hml)
# Print summary of regression results
print(summary(ffregression))}
ffregression <- lm(fund8 ~ rmrf + smb + hml)
# Print summary of regression results
print(summary(ffregression))}
# Load data into R
setwd("/Users/alexbadoi/Desktop/College/Postgrad/Master/Seminar Pc/R")
ff_data = read.csv("ff5_bab_qmj.csv", header = TRUE, sep=",")
library(nlme)
# Extract Fama-French Factors + Momentum + Franziini Factors
rmrf <- ff_data[,2]/100
rf <- ff_data[,7]/100
smb <- ff_data[,3]/100
hml <- ff_data[,4]/100
rmw <- ff_data[,5]/100
cma <- ff_data[,6]/100
umd <- ff_data[,8]
bab <- ff_data[,9]/100
qmj <- ff_data[,10]/100
for(i in 11:18){ #Loop across rows of assets
fund <- ff_data[,i]/100
fund.xcess <- fund - rf
ffgls <- gls(fund.xcess ~ rmrf + smb + hml + rmw + cma,
correlation=corARMA(p=1, q=1), method='ML')
print(summary(ffgls))
}
Thanks for the updated code, really helpful! I have a further question, however: How can you export all of the regression outputs and how do you see which regression summary corresponds to which fund?
Cheers,
M
# Load CSV file into R
df <- read.table("ffdata.csv",header=TRUE,sep=",")
# Extract Fama-French Factors and Fund Returns
rmrf <- df[,2]
smb <- df[,3]
hml <- df[,4]
rf <- df[,5]
fund <- df[,6]
Via my University I get access to CRSP, CRSP has a lot of possible variables to use. I was wondering which variable to use to get the monthly return of a firm. The variable called ‘holding period return’ seems like the most compared to your data. Please could someone give their opinion.
Thanks in advance!
]]>