forked from ncornwell/R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFINA6202Project.R
38 lines (26 loc) · 1.3 KB
/
FINA6202Project.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#Uncomment this to install the quantmod package for the first time
#install.packages("quantmod")
#Load required libraries
require('quantmod')
periodToUse <- 'monthly'
periodStart <- '1970-02-01'
periodRange <- '1970-02::2009-12'
famaPeriodStart <- 197002
#Url for fama french monthly data and the file name in the zip
famaFrenchZip <- "http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Benchmark_Factors_Monthly.zip"
famaFrenchFile <- "F-F_Benchmark_Factors_Monthly.txt"
temp <- tempfile()
download.file(famaFrenchZip,temp)
famaFrenchData <- read.table(unz(temp, famaFrenchFile), header = TRUE)
unlink(temp)
famaSubset = famaFrenchData[famaFrenchData$Rm. >= famaPeriodStart, c("Rm.", "Rf", "SMB", "HML")]
famaSubset = famaSubset[famaSubset$Rm. <= 200912, c("Rm.", "Rf", "SMB", "HML")]
getSymbols(c("JNJ", "^IRX"), src="yahoo", from = periodStart)
#Convert data to monthly returns
equityMonthly <- periodReturn(JNJ$JNJ.Adjusted,period=periodToUse,subset=periodRange) * 100
riskFreeMonthly <- apply.monthly(IRX[periodRange], last)$IRX.Adjusted / 12
equityExcessReturn = equityMonthly - riskFreeMonthly
capmModel = lm(equityExcessReturn ~ famaSubset$Rf)
threeFactorModel = lm(equityExcessReturn ~ famaSubset$Rf + famaSubset$SMB + famaSubset$HML)
print(summary(capmModel))
print(summary(threeFactorModel))