PortfolioAnalytics
PortfolioAnalytics copied to clipboard
itermax does not accept a class numeric from variable
can't seem to pass a value into the itermax argument.
This does not work
itermax <- 50
prtf <- optimize.portfolio(asset_returns, portfolio = base_pf, optimize_method = c("DEoptim"), search_size = search_size, itermax = itermax, traceDE=20, trace=T)
and this works just fine
prtf <- optimize.portfolio(asset_returns, portfolio = base_pf, optimize_method = c("DEoptim"), search_size = search_size, itermax = 50, traceDE=20, trace=T)
I will build an API using this, so no, I cannot just put the number in there, it comes from outside the script.
I've tried to put it into a numeric vector and extract the number from it, tried to itermax = as.numeric(itermax)
but absolutely no success.
I get a non-numeric argument to binary operator
error.
Error in search_size/itermax: non-numeric argument to binary operator
Traceback:
1. c(df_all_weights, max_sr_weights, ef_df, risk_return_plot, max_sr_rets,
. all_assets_plot, non_zero_assets_plot) %<-% portfolio_build(returns,
. group_max_alloc, group_indexes, search_size)
2. tryCatch(multi_assign(substitute(x), value, parent.frame()),
. invalid_lhs = function(e) {
. stop("invalid `%<-%` left-hand side, ", e$message, call. = FALSE)
. }, invalid_rhs = function(e) {
. stop("invalid `%<-%` right-hand side, ", e$message, call. = FALSE)
. })
3. tryCatchList(expr, classes, parentenv, handlers)
4. tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]),
. names[nh], parentenv, handlers[[nh]])
5. doTryCatch(return(expr), name, parentenv, handler)
6. tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
7. tryCatchOne(expr, names, parentenv, handlers[[1L]])
8. doTryCatch(return(expr), name, parentenv, handler)
9. multi_assign(substitute(x), value, parent.frame())
10. portfolio_build(returns, group_max_alloc, group_indexes, search_size)
11. optimize.portfolio(asset_returns, portfolio = base_pf, optimize_method = c("DEoptim"),
. search_size = search_size, itermax = itermax, traceDE = 20,
. trace = T)
What could possibly be the cause of this misinterpretation of the optimize function? Thanks!
this isn't a completely reproducible example, so I can't be sure. I can guess though.
Try setting a different variable for itermax.
n_itermax <- 50
optimize.portfolio(asset_returns, portfolio = base_pf, optimize_method = c("DEoptim"), search_size = search_size, itermax = n_itermax, traceDE=20, trace=TRUE)
my guess is that you are experiencing a scoping and lazy evaluation problem, and by the time itermax
is evaluated, it has been reset somewhere to a default, but if you use a different variable name, when it is evaluated it will always be your value.
if this doesn't work, please provide a self contained reproducible example, and we will try to debug.