R:根据月度回报计算季度回报
我有以下形式的数据框(return.monthly),
Date Return
2001-09-1 0.0404775
2001-10-1 -0.01771575
2001-11-1 -0.03304925
etc.
即一段时间内(2年)的每月回报。我想计算季度回报,即只需进行 3 个观察并计算总和。
我试过
return.quarterly <- xts(return.monthly[, -1], return.monthly[,1])
function <- function(x) sum(x)
time <- 3
return.quarterly$return_q <- rollapply(return.quarterly$Return, FUN=function,
width=time, align="left", na.pad=T)
显然这个公式计算滚动窗口的回报,即它需要观察1-3并计算总和,然后2-4并计算总和,等等。我想要的是1-3、4-6、7- 9...
我怎么能这么做呢?
预先感谢,丹尼
I have dataframe (return.monthly) of the form
Date Return
2001-09-1 0.0404775
2001-10-1 -0.01771575
2001-11-1 -0.03304925
etc.
i.e. monthly returns over a period of time (2 years). I would like to calculate quarterly returns, i.e just take 3 observations and calculate the sum.
I tried
return.quarterly <- xts(return.monthly[, -1], return.monthly[,1])
function <- function(x) sum(x)
time <- 3
return.quarterly$return_q <- rollapply(return.quarterly$Return, FUN=function,
width=time, align="left", na.pad=T)
Obviously this formula calculates returns over a rolling window, i.e. it takes observation 1-3 and calculates the sum, then 2-4 and calculates the sum, etc. What I want is however 1-3, 4-6, 7-9...
How could I do that?
Thanks in advance, Dani
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
xts
中的apply.quarterly
来计算一个季度的平均值:顺便说一句:对于季度回报,您不应该考虑总和而不是平均值吗?
You can use
apply.quarterly
fromxts
to compute the mean over a quarter:BTW: shouldn't you consider instead of the mean the sum, for quarterly returns?