DEoptim - Error in get(as.character(FUN)) - r

I ran into a problem with the DEoptim package. I am trying to minimize the function minF by optimizing vector optVector (see code).
##construct function
minF <- function(x, y, z){
return(mean((z-rowSums(t(x*t(y))))^2))
}
#random matrix and vector
testmrx <- matrix(rnorm(6),38,9)
vctr <- runif(38, min=0, max=50)
#Vector to be optimized and its bounds
optVector = c(20,20,50,30,30,10,3,5,5)
lowr = c(0,0,0,0,0,0,0,0,0)
uppr = c(50,50,200,100,100,50,20,20,20)
#Call of DEoptim
DEoptim(fn = minF(optVector, testmrx, vctr), lower=lowr, upper=uppr)
When I try to do this I get following error:
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'fn' of mode 'function' was not found
I found a similar error posted (link), but the proposed solution of changing variable names did not work. I have no idea what might be causing the problem here. Any help would be greatly appreciated!

Related

Warning when building a function in R

Since I use cross-validation with many different algorithms I decided to build myself the following function:
crossFun <- function(myseed, vars, labels, par, tim, algo, len) {
set.seed(myseed)
multiFolds <- createMultiFolds(labels, k = par, times = tim)
cv_ctrl <- trainControl(method ='repeatedcv', number = par, repeats = tim, index = multiFolds)
cl <- makeCluster(3, type = 'SOCK')
registerDoSNOW(cl)
result <- train(x = vars, y = labels, method = algo, tuneLength = len, trControl = cv_ctrl)
stopCluster(cl)
return(result)
}
The function does work, but with the result, I get the following warning:
Warning message: Setting row names on a tibble is deprecated.
I couldn't find any clear explanation to it. I tried writing the function in different ways but nothing seems to get rid of that message.
Any ideas what it means?
The tidyverse way encourages not using rownames.
You can always coerce back to a base data frame with as.data.frame().
The encouraged way is to use tibble::rownames_to_column() to make rownames a new variable.
See this issue.

"promise already under evaluation" error in R caret's rfe function

I have a matrix X and vector Y which I use as arguments into the rfe function from the caret package. It's as simple as:
I get a weird error which I can't decipher:
promise already under evaluation: recursive default argument reference or earlier problems?
EDIT:
Here is a reproducible example for the first 5 rows of my data:
library(caret)
X_values = c(29.04,96.57,4.57,94.23,66.81,26.71,69.01,77.06,49.52,97.59,47.57,64.07,24.25,11.27,77.30,90.99,44.05,30.96,96.32,16.04)
X = matrix(X_values, nrow = 5, ncol=4)
Y = c(5608.11,2916.61,5093.05,3949.35,2482.52)
rfe(X, Y)
My R version is 3.2.3. Caret package is 6.0-76.
Does anybody know what this is?
There are two problems with your code.
You need to specify the function/algorithm that you want to fit. (this is what causes the error message you get. I am unsure why rfe throws such a cryptic error message; it makes it difficult to debug, indeed.)
You need to name your columns in the input data.
The following works:
library(caret)
X_values = c(29.04,96.57,4.57,94.23,66.81,26.71,69.01,77.06,49.52,97.59,47.57,64.07,24.25,11.27,77.30,90.99,44.05,30.96,96.32,16.04)
X = matrix(X_values, nrow = 5, ncol=4)
Y = c(5608.11,2916.61,5093.05,3949.35,2482.52)
ctrl <- rfeControl(functions = lmFuncs)
colnames(X) <- letters[1:ncol(X)]
set.seed(123)
rfe(X, Y, rfeControl = ctrl)
I chose a linear model for the rfe.
The reason for the warning messages is the low number of observations in your data during cross validation. You probably also want to set the sizes argument to get a meaningful feature elimination.

Passing arguments to nloptr in r

I'm optimising a simple function in r using 'nloptr' and I'm having difficulty passing arguments in to the objective function. Here is the code I'm using:
require("nloptr")
Correl <- matrix(c(1,-.3,-.3,1), nrow=2, ncol=2)
Wghts <- c(.01,.99)
floor <- .035
expret <- c(.05,.02)
pf.return <- function(r, x, threshold=0){
return(r * x - threshold)
}
pf.vol <- function(x, C){
return(sqrt(x %*% C %*% x))
}
res <- nloptr(x0=Wghts,eval_f = pf.vol,eval_g_ineq=pf.return,opts=list(algorithm="NLOPT_GN_ISRES"), x=Wghts,C=Correl)
(I know I'm missing parameters here but I'm trying to highlight a behaviour I don't understand)
Running this gives the following error:
Error in .checkfunargs(eval_f, arglist, "eval_f") :
x' passed to (...) in 'nloptr' but this is not required in the eval_f function.
Just to see what happens I can try running it without the x argument:
res <- nloptr(x0=Wghts,eval_f = pf.vol,eval_g_ineq=pf.return,opts=list(algorithm="NLOPT_GN_ISRES"), C=Correl)
which gives the error:
Error in .checkfunargs(eval_g_ineq, arglist, "eval_g_ineq") :
eval_g_ineq requires argument 'x' but this has not been passed to the 'nloptr' function.
So including x throws an error that it is unnecessary and omitting it throws an (at least understandable) error that it has been omitted.
Ok for posterity:
I rewrote the functions so that they had the same set of arguments, in the same order.
I also omitted the x=Wghts bit as this is the parameter I'm trying to search over.

VSURF and randomForest

I'm trying to use VSURF and randomForest in R, but the functions in the libraries like predict.VSURF, predict.randomForest and plot.VSURF are not working and I'm getting the following error:
Error: could not find function "predict.VSURF"
Here's a reproducible example:
library(randomForest)
library(VSURF)
data(cars)
fit <- VSURF(x = cars[1:402,2:ncol(cars)], y = cars[1:402,1])
#At this step I get the error: Error: could not find function "predict.VSURF"
preds <- predict.VSURF(fit, newdata = cars[403:804,2:ncol(cars)])
R will recognize fit as a VSURF class object and call VSURF.predict for it. You just use predict() instead.
Also, in looking at your example, VSURF seems to fail for only one x variable throwing this error:
Error in matrix(NA, nrow = nfor.thres, ncol = ncol(x)) :
non-numeric matrix extent
Using mtcars and only predict(), VSURF works fine for me.
data("mtcars")
fit <- VSURF(x = mtcars[1:25,2:ncol(mtcars)], y = mtcars[1:25,1])
preds <- predict(fit, newdata = mtcars[26:32, 2:ncol(mtcars)])

PortfolioAnalytics Error with function gmv_opt

I have below script which creates an error. Does anyone know how to solve this. I'm running the latest version of R & RStudio, and all packages are up to date.
library('quantmod')
library('PortfolioAnalytics')
library('PerformanceAnalytics')
ETF_Names <- c("IVV","IJH","IWM","EZU","EEM","SCZ","ILF","EPP")
ETF_All <- lapply(ETF_Names, function(x) getSymbols(x,from="2006-01-01",auto.assign = FALSE))
names(ETF_All) <- ETF_Names
ETF_MR <- do.call(merge,lapply(ETF_All,monthlyReturn))
colnames(ETF_MR) <- ETF_Names
ETF_spec <- portfolio.spec(assets = colnames(ETF_MR))
ETF_spec <- add.constraint(portfolio=ETF_spec, type="full_investment")
ETF_spec <- add.constraint(portfolio=ETF_spec, type="box", min=0, max=1)
ETF.ef <- create.EfficientFrontier(R=ETF_MR['2015'], portfolio=ETF_spec, type="mean-StdDev")
Below the Error message:
Error in gmv_opt(R = R, constraints = constraints, moments = moments, :
No solution found: Error in UseMethod("as.constraint") :
no applicable method for 'as.constraint' applied to an object of class "c('matrix', 'list')"
There have never been issues before (I just recently updated RStudio and the relevant packages). And that is when the error popped up.
Hope someone can help
I got the same error.
Try adding:
library(ROI)
require(ROI.plugin.glpk)
require(ROI.plugin.quadprog)
Have worked for me.
I got the same error.
You could try quadratic optimization (solve.QP). See this answer
https://stackoverflow.com/a/30650972/2824732

Resources