我想在R的portfolioanalytics包中使用chart.EfficientFrontier函數來繪製我創建的有效邊界對象,但它保持失敗。基本上我試圖找到一個邊界,將最大限度地減少標準偏差。最終一旦我得到這個工作,我也想最大化年回報。R投資組合分析圖表。有效的前沿函數
> prt
**************************************************
PortfolioAnalytics Portfolio Specification
**************************************************
Call:
portfolio.spec(assets = colnames(returns))
Number of assets: 3
Asset Names
[1] "Global REITs" "Au REITs" "Au Util and Infra"
Constraints
Enabled constraint types
- leverage
- long_only
Objectives:
Enabled objective names
- mean
- pasd
現在我成功地創建了:
首先,我使用此代碼
pasd <- function(R, weights){
as.numeric(StdDev(R=R, weights=weights)*sqrt(12)) # hardcoded for monthly data
# as.numeric(StdDev(R=R, weights=weights)*sqrt(4)) # hardcoded for quarterly data
}
我進口與月度收益csv文件和我的投資組合對象看起來像這樣創建的年度標準偏差功能使用此線的高效邊界對象:
prt.ef <- create.EfficientFrontier(R = returns, portfolio = prt, type = "DEoptim", match.col = "pasd")
但是當我嘗試繪製它我收到以下錯誤消息。
> chart.EfficientFrontier(prt.ef, match.col="pasd")
Error in StdDev(R = R, weights = weights) :
argument "weights" is missing, with no default
In addition: There were 26 warnings (use warnings() to see them)
Error in StdDev(R = R, weights = weights) :
argument "weights" is missing, with no default
Error in StdDev(R = R, weights = weights) :
argument "weights" is missing, with no default
Error in xlim[2] * 1.15 : non-numeric argument to binary operator
任何人都知道爲什麼會出現這種情況?當我使用摘要(prt.ef)時,我可以看到權重,但爲什麼chart.EfficientFrontier函數失敗?
'chart.EfficientFrontier'希望能夠調用沒有權重的'pasd'來計算每個資產的sigma,並用權重來計算投資組合sigma。爲了消除錯誤信息,使用'pasd < - function(R,weights = NULL)as.numeric(StdDev(R = R,權重=權重)* sqrt(12))'使'pasd'像'StdDev'一樣工作。 。此外,爲了做一個一致的分析,你應該每年回報,但然後優化將計算每月和年度組合的相同權重。 – WaltS
我改變了'pasd'函數,就像你的建議一樣,但chart.EfficientFrontier仍然給我錯誤。 (R = R,權重=權重):找不到對象'NuLL' StdDev中的錯誤(R = R,權重=權重) :找不到對象'NuLL' StdDev中的錯誤(R = R,權重=權重):找不到對象'NuLL' xlim [2] * 1.15錯誤:二進制運算符'非數字參數' – user3381431