Change objective function in nls.lm () to "R"

I am using the nls.lm {package: minpack.lm} function to optimize a parameter for a hydrological model. The function works pretty well, but I want to use another one objective function (OF)

. Usually, the function fn "ob" in nls.lm is defined as

A function that returns a vector of residuals, the sum square of which 
is to be minimized. The first argument of fn must be par.

      

Now I want to use Nash-Sutcliff-Efficiency

which is defined as

NSE <- 1 - (sum((obs - sim)^2) / sum((obs - mean(obs))^2))

      

or another OF. The problem is that it nls.lm

minimizes the expression sum(x)^2

, and only x

can be changed. I know that best fit NSE = 1

. Thus, 1 - NSE

creates a real minimization problem.

By the way: example 1 from the man page nls.lm

is a good example; there

observed - getPred(p,xx)

      

is minimized, which actually means that

sum ( observed - getPred(p,xx) )^2

      

is minimized by the function nls.lm

, whereas it getPred(p,xx)

returns sim

.

Any suggestion would be helpful. Thank you in advance. Micah

+3


source to share


1 answer


nls.lm (and related functions nls and nlsLM) are designed to minimize the squared sum of residuals. For the problem you are trying to solve, I would try a generic minimizer.

If the problem is "not too hard" (ie has one global minimum, is smooth), you can try to apply "optim" to it (I would try "Nelder-Mead" and "BFGS" options) or the "bobyqa "from the" minqa "package, among other functions.



If a problem requires a global optimizer, you can try the GenSA function from the GenSA package, the genoud function from the rgenoud package, or the DEoptim function from the DEoptim package, among other options. An overview of "Global Optimization in R" is presented in the Journal of Statistical Software, which will give a fuller overview of the applicable functions.

+1


source