【问题标题】:How to overlay multiple timeseries in chart.RollingPerformance of package PerformanceAnalytics in R如何在图表中覆盖多个时间序列。R中包PerformanceAnalytics的RollingPerformance
【发布时间】:2019-01-28 14:27:33
【问题描述】:

亲爱的 R 编码社区,

我目前正在写我的硕士论文,但我很难将多个滚动年化回报结合起来。 更具体地说,我尝试使用命令“chart.RollingPerformance”覆盖 3 个不同的投资组合的回报。 1个投资组合的代码如下:

chart.RollingPerformance(R=bt_benchmarkM_returns, width=6,
                         main='Rolling 6-month annualized return',
                         FUN="Return.annualized",legend.loc="bottomleft")

我尝试了“行”命令:

lines(pf_mad_returnmonthly, col="red")

还有

lines(chart.RollingPerformance(R=pf_bl_returnmonthly, width=6, 
                                 colorset=rich8equal,                              
                                 FUN="Return.annualized", 
                                 legend.loc="bottomleft",
                                 main="BLCOP - rolling 6-month annualized 
                                 return"))

但它们都没有产生我想要的结果。

我希望有人帮助我解决这个问题,因为我对 R 还比较陌生,但由于已经实现了编码,所以仍然没有什么经验。

KR 帕特里克

【问题讨论】:

  • 如果您的目标是在同一个图表中绘制多条线,请尝试使用 ggplot。看看这个帖子:stackoverflow.com/questions/3777174/…
  • 其实这是我的目标。我还多次阅读了有关“ggplot2”包的信息,但我还没有弄清楚如何使用 ggplot 从“PerformanceAnalytics”将系列添加到我的“chart.rollingperformance”命令中以添加多行(例如,反映滚动回报分析所有投资组合都在一张图中)。这听起来可能很愚蠢,但是你能告诉我它是怎么做的吗?我仍然不知道 R 中的这些基础知识。谢谢 KR Patrick
  • 查看数据框的外观(通过使用 head(pf_bl_returnmonthly))并查看您获得的输出(或错误消息)(如果有)将很有用。

标签: r plot rstudio performanceanalytics


【解决方案1】:

好吧,这是我的数据:

    > head(bt_benchmarkM_returns)
GMT
            EUROSTOXX50
2016-07-31  0.043990038
2016-08-31  0.010823293
2016-09-30 -0.006910022
2016-10-31  0.017656820
2016-11-30 -0.001191357
2016-12-31  0.078289790
> head(pf_bl_returnmonthly)
           portfolio.returns
2016-08-31       0.009311298
2016-09-30      -0.001002361
2016-10-31       0.015371851
2016-11-30      -0.012162073
2016-12-31       0.073918208
2017-01-31      -0.010528706
> head(pf_mad_returnmonthly)
           portfolio.returns
2016-08-31       0.025284203
2016-09-30       0.011681711
2016-10-31       0.008464807
2016-11-30       0.006796675
2016-12-31       0.058598377
2017-01-31       0.007101811

它由我之前创建的投资组合优化获得的 2 个时间序列的月度回报组成。 如果我尝试以下代码:

chart.RollingPerformance(R=bt_benchmarkM_returns, width=6,
                         main='Rolling 6-month annualized return',
                         FUN="Return.annualized",legend.loc="bottomleft")
  lines(pf_mad_returnmonthly, col="red")
  lines(pf_bl_returnmonthly, col="blue")
  legend("bottomleft", legend=c("BLCOP", "MAD", "Benchmark"),
           col=c("blue", "red", "black"), lty=1, cex=0.8)

然后我将获得一个包含三条线的图表,但只有“bt_benchmarkM_returns”线是滚动年化收益率,而其他两条线不是。

此外,我尝试了以下代码:

> chart.RollingPerformance(R=bt_benchmarkM_returns, width=6,
+                          main='Rolling 6-month annualized return',
+                          FUN="Return.annualized",legend.loc="bottomleft")
>   lines(chart.RollingPerformance(R=pf_mad_returnmonthly, width=6, colorset=rich8equal,
+                                FUN="Return.annualized", legend.loc="bottomleft"))
Error in as.double(y) : 
  cannot coerce type 'environment' to vector of type 'double'
>   lines(chart.RollingPerformance(R=pf_bl_returnmonthly, width=6, colorset=rich8equal,
+                                  FUN="Return.annualized", legend.loc="bottomleft"))
Error in as.double(y) : 
  cannot coerce type 'environment' to vector of type 'double'
>   legend("bottomleft", legend=c("BLCOP", "MAD", "Benchmark"),
+            col=c("blue", "red", "black"), lty=1, cex=0.8)

图上只画了一条线。

整个代码是从控制台复制粘贴的,因此,只有在第二种情况下,才会出现错误。

希望对你有帮助。

KR 帕特里克

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2021-10-10
    • 2020-06-13
    • 2014-05-08
    • 2017-12-20
    相关资源
    最近更新 更多