R:is.nloptr(ret)中的错误:x0中的目标返回NA [英] R: Error in is.nloptr(ret) : objective in x0 returns NA

查看:438
本文介绍了R:is.nloptr(ret)中的错误:x0中的目标返回NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用nloptr包来找到使非线性函数F = b0 + b1 * x + b2 * x ^ 2 + b3 * x ^ 3最大化的最佳x值.

I am trying to use the nloptr package to find the optimal x value that maximized the non-linear function F=b0+b1*x+b2*x^2+b3*x^3.

我正在将以下代码与apply()函数一起使用,以使其在回归数据帧的每一行中循环,并为每一行获取该函数的最优值:

I am using the following code with apply() function in order to loop it through each individual row of the Regression data frame and get the optimal value of the function for each individual row:

F <- function(x,b0,b1,b2,b3){return(b0+b1*x+b2*x^2+b3*x^3)}
Optimal <- apply(Regression,1,function(i){
                  nloptr( x0 <- c(0)
                         ,eval_f <- F
                         ,eval_g_ineq = NULL
                         ,eval_g_eq = NULL
                         ,eval_grad_f = NULL
                         ,eval_jac_g_ineq = NULL
                         ,eval_jac_g_eq = NULL
                         ,lb <- c(-Inf)
                         ,ub <- c(Inf)
                         ,opts <- list( "algorithm" = "NLOPT_LD_AUGLAG",
                                        "xtol_rel" = 1.0e-7,
                                        "maxeval" = 1000)
                         ,b0=Regression$b0[i]
                         ,b1=Regression$b1[i]
                         ,b2=Regression$b2[i]
                         ,b3=Regression$b3[i])})

代码要求b0,b1,b2,b3值的回归数据帧具有以下格式:

The Regression data frame which the code calls for the b0,b1,b2,b3 values has the following format:

Tag bo b1 b2 b3
A   5  6  1  3
B   8  8  7  3
C   9  2  7  5
D   1  6  1  3
E   3  6  2  1
..  .. .. .. ..

运行脚本时出现以下错误:

I am getting the following error when running the script:

Error in is.nloptr(ret) : objective in x0 returns NA
In addition: Warning message:
In if (is.na(f0)) { :

推荐答案

如果您还打算访问该函数中的项目,则不应使用apply传递回归"行.当apply强制Regression为单一类型时,也会出现问题.这将是字符而不是数字.相反,它应该是:

You should NOT be passing rows of "Regression" using apply if you are also intending to access items inside the function. There's also going to be a problem when apply coerces Regression to a single type. It will be character rather than numeric. Instead, it should be:

library(nloptr)
F <- function(x,b0,b1,b2,b3){return(b0+b1*x+b2*x^2+b3*x^3)}
Optimal <- apply(Regression[-1],     #removes first column
                                 1, function(i){   # i-variable gets values
                  nloptr( x0 <- c(0)
                         ,eval_f <- F
                         ,eval_g_ineq = NULL
                         ,eval_g_eq = NULL
                         ,eval_grad_f = NULL
                         ,eval_jac_g_ineq = NULL
                         ,eval_jac_g_eq = NULL
                         ,lb <- c(-Inf)
                         ,ub <- c(Inf)
                         ,opts <- list( "algorithm" = "NLOPT_LD_AUGLAG",
                                        "xtol_rel" = 1.0e-7,
                                        "maxeval" = 1000)
                         ,b0=i[1]
                         ,b1=i[2]
                         ,b2=i[3]
                         ,b3=i[4])})

使用您的回归"对象进行了测试. (我担心尝试三次多项式时会出现最小值还是最大值.)不幸的是,您选择的参数不一致:

Tested with your "Regression"-object. (I have concerns about whether there will be a minimum or a maximum when attempting to work with a cubic polynomial.) Unfortunately you have chosen parameters that are inconsistent:

Error in is.nloptr(ret) : 
  A gradient for the objective function is needed by algorithm NLOPT_LD_AUGLAG 
but was not supplied.

尽管如此,应该可以很容易地计算出多项式的梯度.

It should be possible to calculate a gradient of a polynomial without too much difficulty, though.

构造梯度函数后,我得到:

After constructing a gradient function I now get:

grad_fun <- function(x,b0,b1,b2,b3) { b1 + x*b2/3 +x^2*b3/3 }
> F <- function(x, b0,b1,b2,b3){return(b0+b1*x+b2*x^2+b3*x^3)}
> Optimal <- apply(Regression[-1],     
+                                  1, function(i){   
+                   nloptr( x0 <- c(0)
+                          ,eval_f <- F
+                          ,eval_g_ineq = NULL
+                          ,eval_g_eq = NULL
+                          ,eval_grad_f = grad_fun
+                          ,eval_jac_g_ineq = NULL
+                          ,eval_jac_g_eq = NULL
+                          ,lb <- c(-Inf)
+                          ,ub <- c(Inf)
+                          ,opts <- list( "algorithm" = "NLOPT_LD_AUGLAG",
+                                         "xtol_rel" = 1.0e-7,
+                                         "maxeval" = 1000)
+                          ,b0=i[1]
+                          ,b1=i[2]
+                          ,b2=i[3]
+                          ,b3=i[4])})
Error in is.nloptr(ret) : 
  The algorithm NLOPT_LD_AUGLAG needs a local optimizer; specify an algorithm and termination condition in local_opts

在我看来,我已经使您跨过了几个障碍,因此,这还不是真正的答案,但它似乎很有用,并且发表评论的时间太长了.

Seemed to me that I've gotten you past several hurdles, so this is not yet really an answer but it seems useful and was far too long for a comment.

编辑;将算法更改为"algorithm" = "NLOPT_LD_LBFGS"的进一步实验使代码运行无误,但据我所知,4运行了所有使用$ message : chr "NLOPT_FAILURE: Generic failure code."返回的列表.我的猜测是,优化三次多项式通常会在没有约束的情况下失败,而我在您的问题说明中没有发现任何问题.

Edit; Further experiments with changing the algorithm to "algorithm" = "NLOPT_LD_LBFGS" gets the code to run without error but as far as I can see the 4 runs all returned list with $ message : chr "NLOPT_FAILURE: Generic failure code.". My guess is that optimizing cubic polynomials will generally fail without constraints and I see none in your problem specification.

这篇关于R:is.nloptr(ret)中的错误:x0中的目标返回NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆