我有300+公司,需要为他们计算月收益,然后将其用作数据集中的变量之一。我从雅虎下载价格,并使用quantmod软件包计算月收益:
require(quantmod)
stockData <- lapply(symbols,function(x) getSymbols(x,auto.assign=FALSE, src='yahoo', from = '2000-01-01'))
stockDataReturn <- lapply(stockData,function(x) monthlyReturn(Ad(x))) 我遇到的问题是,一些公司有不同的月底(由于交易停止,等等)。这反映在产出清单中: 2013-12-30年为AAA公司,2013-12-31年为BBB公司和其余样本。
当我将列表合并时
returns <- do.call(merge.xts, stockDataReturn)它为2013-12-30年创建了一个单独的行,除了AAA公司之外,所有的NAs都是这样。我怎么解决这个问题?我的理解是,我需要坚持月-年格式,这是我需要使用的索引,然后我合并。
理想情况下,我想要的是,在monthlyReturn阶段,它使用月份的开始日期,而不是月底。
发布于 2016-12-11 10:54:34
您可以使用lubridate的floor_date在当月的同一开始进行合并,而不是在月底的时间戳上进行合并。或者在合并之前,可以使用ceiling date对所有证券使用相同的月底时间戳。
library(lubridate)
stockDataReturn <- lapply(stockDataReturn,
function(x) {
index(x) <- floor_date(index(x), "month")
# Or if you want to round to end of month change to:
# index(x) <- ceiling_date(index(x), "month")
x
})
returns <- do.call(merge, stockDataReturn)
colnames(returns) <- symbolshttps://stackoverflow.com/questions/41084913
复制相似问题