我是R新手,本学期正在做作业。
vi。使用CAPM模型根据样本数据查找股票的Beta。
这是我到目前为止的代码:
library(quantmod)
library(PerformanceAnalytics)
library(fBasics) #Load packages
###Download daily price data of S&P500 and AAPL###
getSymbols("SPY",from="1993-01-01", to="2013-12-31")
getSymbols("AAPL",from="1993-01-01", to="2013-12-31")
###Compute monthly returns for adjusted closing price for each stock###
spy_returns_adjusted <- monthlyReturn(SPY$SPY.Adjusted)
aapl_returns_adjusted <- monthlyReturn(AAPL$AAPL.Adjusted)
###Summary stats, histogram, and correlation matrix###
summary(spy_returns_adjusted)
hist(spy_returns_adjusted)
cor((spy_returns_adjusted), (aapl_returns_adjusted))
summary(monthlyReturn(aapl_returns_adjusted))
hist(monthlyReturn(aapl_returns_adjusted))
cor((aapl_returns_adjusted), (spy_returns_adjusted))
colnames(spy_returns_adjusted) <- "SPY_returns"
colnames(aapl_returns_adjusted) <- "AAPL_returns"
getSymbols('TB3MS',src='FRED')
TB3MS_1993_2013 <- TB3MS["1993/2013"] #Filter tbill data to corrrect date range
stockmatrix <- merge(spy_returns_adjusted, aapl_returns_adjusted) #combine SPY and AAPL returns into
one matrix
stockmatrix$TB3MS_returns <- coredata(TB3MS_1993_2013) #Add tbill to return matrix
rtn <- data.frame(matrixrtn) #convert object to data frame
attach(rtn)
lm.fit <- lm(rtn)
summary(lm.fit)
因此,我对Beta和CAPM模型感到困惑。我尝试过:
CAPM.beta(rtn, aapl_returns_adjusted, TB3MS_1993_2013)
但这是结果:
SPY_returns AAPL_returns TB3MS_returns
Beta: AAPL_returns NA NA NA
有人能指出我正确的方向吗?