## read in data (which were saved from Excel ## through "save as> Text (tab delimited)" dj <- read.table(file="DJ.txt", h=T, sep="\t", na="") ## have a look #fix(dj) ## inspect object structure (to see if everything ok) str(dj) ## dj*_open are the opening prices for the given indices, where ## * can be "c"=Composite, "i"=Industrial, "t"=Transportation, ## "u"=Utilities ## DGS10 is the return on 10y US Treasury bonds ## model: log(P_t/P_(t-1)) - Rf = beta * [log(M_t/M_(t-1)) - Rf] + u ## make model formula for Industrials ## (notice we take out the first observation from the Treasuries series ## to match its length with the log returns, where the first data point ## is lost in taking first differences) fm.i <- I(diff(log(dji_open))-DGS10[-1]/100) ~ I(diff(log(djc_open))-DGS10[-1]/100) ## estimate CAPM for Industrials mod.i <- lm(fm.i, data=dj) ## inspect it summary(mod.i) ## alternatively: all in one line ## (Notice: no model object remains in the workspace, results ## are only printed out!) summary(lm(fm.i, data=dj)) ## comments: beta is very close to one, alpha not significant at the ## 5 percent level (see Jensen!) ## test hypothesis beta = 1 beta.hat <- coef(mod.i)[2] se.beta.hat <- sqrt(diag(vcov(mod.i)))[2] # by the functional nature of R, # this is solved as h(g(f(x))) ## a look at how it worked: vcov(mod.i) diag(vcov(mod.i)) sqrt(diag(vcov(mod.i))) sqrt(diag(vcov(mod.i)))[2] ## another way to retrieve it: summary(mod.i)$coef[2, 2] ## actual test: t.stat <- (beta.hat - 1)/se.beta.hat ## get critical value t.bar.05 <- qt(0.025, df=169) # notice that we are looking at the left tail! ## compare them: t.stat < t.bar.05 ## or: calculate p.value pt(t.stat, df=169, lower.tail=TRUE)*2 ## Utilities: #fm.u <- diff(log(dju_open)) ~ diff(log(djc_open)) fm.u <- I(diff(log(dju_open))-DGS10[-1]/100) ~ I(diff(log(djc_open))-DGS10[-1]/100) ## estimate CAPM for Utilities mod.u <- lm(fm.u, data=dj) ## inspect it summary(mod.u) ## comments on the different beta? And on the significance of alpha? ## Transportation: fm.t <- I(diff(log(djt_open))-DGS10[-1]/100) ~ I(diff(log(djc_open))-DGS10[-1]/100) summary(lm(fm.t, dj))