0

我有一个返回 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

其他人得到这个错误?有人知道我为什么得到它以及如何解决它?

谢谢!

4

1 回答 1

0

您很可能会错过(即需要安装)一个包,例如ROI它的一个插件。但如果没有更完整的例子,就很难说。


对更新的回应:它仍然不是一个可重现的例子。您没有显示数据,也没有加载/附加所需的包。这是一个可重现的示例,它适用于我的系统。这里的“它可以工作”意味着它运行时没有错误;我没有检查结果。
作为数据,我使用来自 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     
于 2020-09-22T06:17:29.173 回答