rquantitative-financeportfolior-portfolioanalytics

Error in gmv_opt with PortfolioAnalytics package in r


I have a returns xts object like

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)

When I use optimize.portfolio from PortfolioAnalytics package I get this error:

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

Someone else get this error? someone knows why I get that and how to fix it?

Thanks!


Solution

  • You very likely miss (i.e. need to install) a package, such as ROI or one of its plugins. But without a more complete example, it is hard to tell.


    Response to the update: It's still not a reproducible example. You don't show the data, and you don't load/attach the required packages. Here is a reproducible example, and it works on my system. "It works" here means that it runs without an error; I did not inspect the results.
    As data, I use a dataset from Kenneth French's website. Try to debug your code and find out what value plugin has; it should be the name of the missing package.

    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)
    

    The function sessionInfo tells you what R and package versions have been used.

    > 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