![](/img/trans.png)
[英]R chart.RollingPerformance stock price unable to output correct y axis
[英]How to overlay multiple timeseries in chart.RollingPerformance of package PerformanceAnalytics in R
亲爱的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'命令:
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帕特里克
好吧,这是我的数据:
> 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帕特里克
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.