[英]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.