错误无法在 R 中分配 591.3 MB:如何使用 ff 包解决?

Error can not allocate 591.3 MB in R: How to resolve using ff package?

我正在尝试生成随机投资组合,但出现以下错误。

从资产名称开始

port <- portfolio.spec(assets = c("^NSEI","ITC.NS", "SBIN.NS", "COALINDIA.NS", "ICICIBANK.NS", "TATAMOTORS.NS", "ADANIPORTS.NS", "UPL.NS", "LICHSGFIN.NS", "NTPC.NS", "ONGC.NS", "TCS.NS", "INFY.NS", "INFRATEL.NS", "DABUR.NS", "BHARTIARTL.NS", "GAIL.NS", "VEDL.NS", "MOTHERSUMI.NS", "WELSPUNIND.NS", "WIPRO.NS", "TATAPOWER.NS", "IDEA.NS", "CAIRN.NS", "UNITECH.NS"))

盒子

port <- add.constraint(port, type = "box", min = 0.05, max = 0.8)

杠杆

port <- add.constraint(portfolio = port, type = "full_investment")

目标Return

port <- add.constraint(portfolio = port, type="return", return_target=0.015)

生成随机投资组合

rportfolios <- random_portfolios(port, permutations = 3100000, rp_method = "sample")

我的投资组合中共有 24 种证券,我使用的是过去 1 年的调整后收盘价。我做了一些试错法,发现排列数存在一些问题,随着我增加数字,它增加了 Vector MB,就像我输入

Permuations = 4100000

Error: cannot allocate vector of size 782.0 Mb

同样,如果我减少

Permutations = 2100000

Error in rp_transform(w = tportfolio, min_sum = min_sum, max_sum = max_sum, :Infeasible portfolio created, perhaps increase max_permutations and/or adjust your parameters.

如果有人来这里寻找这个问题的答案。

最初主要错误

Error in rp_transform(w = tportfolio, min_sum = min_sum, max_sum = max_sum, :Infeasible portfolio created, perhaps increase max_permutations and/or adjust your parameters.

是由于生成的可行投资组合权重数量不足。

source code 中,函数 rp_tranform 试图将不可行的投资组合权重向量转换为可行的向量,失败时会抛出此错误。来自源代码

# checks for infeasible portfolio
# Stop execution and return an error if an infeasible portfolio is created
# This will be useful in fn_map so that we can catch the error and take
# action (try again with more permutations, relax constraints, different
# method to normalize, etc.)

因此,您可以将框限制放宽到

port <- add.constraint(port, type = "box", min = 0.01, max = 0.95)

或尝试更改一些其他约束。