r 中的 PortfolioAnalytics 包在 gmv_opt 中出错

Error in gmv_opt with PortfolioAnalytics package in r

我有一个 returns xts 对象

retornos_categorias <- an xts object with 20 columns where each column is a return vector

assets <- colnames(retornos_categorias)
portfolio.init <- portfolio.spec(assets)
portfolio.init <- add.constraint(portfolio.init, type = "full_investment")
portfolio.minSD <- add.objective(portfolio = portfolio.init, type="risk", name="StdDev")

portfolio.minSD.opt <- optimize.portfolio(retornos_categorias, portfolio = portfolio.minSD, optimize_method = "ROI_old", trace = TRUE)

当我使用 PortfolioAnalytics 包中的 optimize.portfolio 时,出现此错误:

Error in gmv_opt(R = R, constraints = constraints, moments = moments,  : 
  paste0("package:", plugin) %in% search() || requireNamespace(plugin,  .... is not TRUE

其他人遇到此错误?有人知道我为什么会这样以及如何解决它吗?

谢谢!

您很可能会错过(即需要安装)某个软件包,例如 ROI 或其插件之一。但是没有更完整的例子,就不好说了。


对更新的回应:它仍然不是一个可重现的例子。您没有显示数据,也没有 load/attach 所需的包。这是一个可重现的例子,它适用于我的系统。这里的“It works”是指运行没有错误;我没有检查结果。
作为数据,我使用了来自 Kenneth French 网站的数据集。尝试调试您的代码并找出 plugin 的值;它应该是丢失包的名称。

library("NMOF")
library("PortfolioAnalytics")
library("xts")

R <- French(tempdir(), "17_Industry_Portfolios_CSV.zip")
R <- as.xts(R, as.Date(row.names(R)))
R <- window(R, start = as.Date("2000-01-01"))

retornos_categorias <- R
assets <- colnames(retornos_categorias)
portfolio.init <- portfolio.spec(assets)
portfolio.init <- add.constraint(portfolio.init,
                                 type = "full_investment")
portfolio.minSD <- add.objective(portfolio = portfolio.init,
                                 type = "risk",
                                 name = "StdDev")

portfolio.minSD.opt <- optimize.portfolio(retornos_categorias,
                                          portfolio = portfolio.minSD,
                                          optimize_method = "ROI_old",
                                          trace = TRUE)

函数 sessionInfo 告诉您使用了哪些 R 和包版本。

> sessionInfo()
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.1 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-openmp/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-openmp/liblapack.so.3
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] PortfolioAnalytics_1.1.0   PerformanceAnalytics_2.0.4
## [3] foreach_1.5.0              xts_0.12-0                
## [5] zoo_1.8-8                  NMOF_2.2-0                
## 
## loaded via a namespace (and not attached):
## [1] datetimeutils_0.4-1 compiler_4.0.2      tools_4.0.2        
## [4] parallel_4.0.2      codetools_0.2-16    grid_4.0.2         
## [7] iterators_1.0.12    lattice_0.20-41     quadprog_1.5-8