有点复杂!我希望使用Fama-French获得事件日的异常回报。时间窗口为:活动日前250天。
首先,我的data.frames:
require(quantmod)
require(zoo)
# STOCK DATA.FRAME
BRCM <- as.data.frame(getSymbols.yahoo("BRCM", from="2000-01-01", verbose=F, auto.assign=F))
AAPL <- as.data.frame(getSymbols.yahoo("AAPL", from="2000-01-01", verbose=F, auto.assign=F))
MSFT <- as.data.frame(getSymbols.yahoo("MSFT", from="2000-01-01", verbose=F, auto.assign=F))
BRCM$Company <- c("BRCM")
AAPL$Company <- c("AAPL")
MSFT$Company <- c("MSFT")
BRCM$Return <- Delt(BRCM$BRCM.Adjusted)
AAPL$Return <- Delt(AAPL$AAPL.Adjusted)
MSFT$Return <- Delt(MSFT$MSFT.Adjusted)
colnames(BRCM) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted", "Company", "Return")
colnames(AAPL) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted", "Company", "Return")
colnames(MSFT) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted", "Company", "Return")
data <- rbind(BRCM, AAPL, MSFT)
data$Open <- NULL
data$Close <- NULL
data$High <- NULL
data$Low <- NULL
data$Volume <- NULL
data$Adjusted <- NULL
data$DATE <- row.names(data)
# EVENT DATA.FRAME
COMP <- c("BRCM", "AAPL", "AAPL", "MSFT", "BRCM", "BRCM", "MSFT")
DATE <- c("2003-03-04", "2004-12-01", "2002-12-03", "2008-02-08", "2003-10-10", "2005-12-12", "2003-11-14")
events <- data.frame(COMP, DATE)
events$AR <- paste("")
# FAMA FRENCH DATA.FRAME
date <- data$DATE
Mkt.RF <- sample(c(-2, -1.3, -0.9, -0.5, 0.15, 0.45, 0.95, 1.4, 1.8), size = nrow(data), replace=T)
SMB <- sample(c(-0.54, -0.41, -0.3, -0.21, -0.1, 0.12, 0.23, 0.34, 0.42, 0.6), size = nrow(data), replace= T)
HML <- sample(c(-0.54, -0.41, -0.3, -0.21, -0.1, 0.12, 0.23, 0.34, 0.42, 0.6), size = nrow(data), replace= T)
ff <- data.frame(date, Mkt.RF, SMB, HML)
抱歉,我很难做出这些例子。
到目前为止唯一重要的是,我们有3个data.frames
现在我想使用for()循环来计算异常收益(AR)。 这可能是一个更简单的方法,但我想出了这个和 我希望那里有一个天才,他明白我想做什么!
# create R Objects for loop
companies <- as.vector(unique(events$COMP))
days <- as.vector(unique(data$DATE))
W <- lag(zoo(days), -c(0, 250:1))
ES_list <- vector("list", length = length(companies))
for(i in 1:length(companies)) {
data_k <- data[which(data$Company==companies[i]),] # all trading days for each firm
events_k <- events[which(events$COMP==companies[i]),] # all event days for each firm
for(j in 1:nrow(events_k)) {
d = which(days==events_k[j,"DATE"])
Z = W[d,] # time window assigned to each event-day (250 days)
Y = data_k[which(is.na(match(data_k$DATE, Z) == F)), "RET"] # all Returns of time window (250days)
X = cbind(rep(1, ncol(W)), ff[which(is.na(match(ff$DATE, Z) == F), c("Mkt.RF", "SMB", "HML"))]) # explaining variables
b = (t(X) %x% X)^(-1) %x% t(X) %x% Y # my model to get coefficients to calculate abnormal return (AR)
events_k[j, AR] = data_k[d, "Return"] -b[1] -b[2:4] %x% ff[d,2:4]
ES_list[i] = events_k
}
}
ES = do.call(rbind, ES_list)
我认为这个循环应该有效,但它让我得到错误:
Error in arr.ind && !is.null(d <- dim(x)) : invalid 'x' type in 'x && y'
有没有人知道这意味着什么以及如何解决它?
输出应该是事件(data.frame),其中包含事件日所有异常返回的列(事件$ AR)。 谢谢
答案 0 :(得分:0)
我不明白你的代码失败的原因,但我想我会使用apply
函数系列而不是for
循环。此外,我会尝试时间序列对象(我猜quantmod
返回xts
个对象),这会使窗口选择更容易。
请注意,我使用的数据框架包含Fama和法国日常因素。 Ian Gow提供了代码here。
以下内容返回xts
个对象,每个事件一列。从那里你应该能够获得你想要的任何摘要统计数据。如果我离得太远(让我不知道更大的目标或代码),请告诉我。
require(quantmod)
require(zoo)
# get data and find returns
tickers <- c("BRCM", "AAPL", "MSFT")
prices <- lapply(tickers, getSymbols, auto.assign=FALSE, from="2000-01-01")
my.ret <- function(x) {
y <- dailyReturn(x, type="log")
names(y) <- strsplit(names(x)[1], "\\.")[[1]][1]
y
}
returns <- lapply(prices, my.ret)
returns <- merge(Reduce(merge, returns), read.zoo(ff_daily_factors), all=FALSE)
# EVENT DATA.FRAME
COMP <- c("BRCM", "AAPL", "AAPL", "MSFT", "BRCM", "BRCM", "MSFT")
DATE <- c("2003-03-04", "2004-12-01", "2002-12-03", "2008-02-08", "2003-10-10", "2005-12-12", "2003-11-14")
# my function to calculate predicted abnormal returns
my.AR <- function(x, tic, day) {
x$exret <- x[, tic] - x[, "rf"]
est.beg <- as.Date(day) - 280
est.end <- as.Date(day) - 30
model <- lm(exret ~ mktrf + smb + hml +umd, data=window(x, start=est.beg, end=est.end))
pred.beg <- as.Date(day) - 29
pred.end <- as.Date(day) + 29
exret.hat <- predict(model, window(x, start=pred.beg, end=pred.end))
exret.hat <- zoo(exret.hat, order.by=as.Date(names(exret.hat)))
x <- merge(x, exret.hat)
x$exret - x$exret.hat
}
ARs <- Reduce(merge, mapply(my.AR, tic=COMP, day=DATE, MoreArgs=list(x=returns), SIMPLIFY=FALSE))
names(ARs) <- paste0(COMP, DATE)
returns <- merge(returns, ARs)
这有帮助吗?