1

I have found no examples of neldermead() on the internet, so I figured I would post the following. That said, I cant figure out how to control the max number of iterations of the algorithm. https://cran.r-project.org/web/packages/neldermead/neldermead.pdf

Which states the following:

optbase An object of class ’optimbase’, i.e. a list created by optimbase() and containing the following elements:

iterations The number of iterations.

Working example of using Nelder Mead to fit a parabola by minimizing the residuals

library(neldermead); library(nloptr);

##  ========= Minimizing the residuals for a 2d quadrature =========== ##
x2d = seq(-4,6,length.out=50);        ## x vector definition
ynoise = runif(n=50, min=-2, max = 2) ## noise
y2d  = 1.3 + (x2d-2.1)^2 + ynoise     ## y data for fitting


## Fitting with nelder-mead
quadmin <- function(x){ sum(  (y2d - x[1] - (x2d - x[2])^2)^2  )  }
x000 <- c(1, 2)
sol2d <- neldermead(x0 = x000, fn = quadmin)    
sol2d

yfit = sol2d[[1]][1] + (x2d - sol2d[[1]][2])^2      ## Fitted curve.   
plot(x2d, y2d); lines(x2d, yfit)    ## Plotting 

But I'm looking to do something like:

sol2d <- neldermead(x0 = x000, fn = quadmin, iterations = 200)

^^^ which doesn't work. Neither does putting it into a list:

sol2d <- neldermead(x0 = x000, fn = quadmin, optbase = list(iterations = 200))

This is a basic question about how to use these arguments, so I apologize if this isn't the right title. In advance, thank you for your help.

Community
  • 1
  • 1
Entropy
  • 133
  • 2
  • 12

1 Answers1

1

There are at least tow neldermead functions available in R. One is from the package neldermead which correspond to the documentation you link. I have not been able to make it work. It gives me back neither error or solution. The code:

library(neldermead)
library(nloptr)
##  ========= Minimizing the residuals for a 2d quadrature =========== ##
x2d = seq(-4,6,length.out=50);        ## x vector definition
ynoise = runif(n=50, min=-2, max = 2) ## noise
y2d  = 1.3 + (x2d-2.1)^2 + ynoise     ## y data for fitting


## Fitting with nelder-mead
quadmin <- function(x){ 
  sum(y2d - x[1] - (x2d - x[2])^2)^2  }
x000 <- c(1, 2)


opt <- optimbase(x0 = as.matrix(x000),fx0 = -1000,maxiter = 200,fopt = quadmin,verbose=T)
sol2d <- neldermead::neldermead(opt)    

On the other hand, package nloptr also provides a neldermedad function which sintax looks closer to you code and I have been able to run:

library(neldermead)
library(nloptr)
##  ========= Minimizing the residuals for a 2d quadrature =========== ##
x2d = seq(-4,6,length.out=50);        ## x vector definition
ynoise = runif(n=50, min=-2, max = 2) ## noise
y2d  = 1.3 + (x2d-2.1)^2 + ynoise     ## y data for fitting


## Fitting with nelder-mead
quadmin <- function(x){ 
  sum(y2d - x[1] - (x2d - x[2])^2)^2  }
x000 <- c(1, 2)


sol2d <- nloptr::neldermead(x0 = x000,fn =quadmin,control =list(maxeval=200))


yfit = sol2d[[1]][1] + (x2d - sol2d[[1]][2])^2      ## Fitted curve.   
plot(x2d, y2d); lines(x2d, yfit)

As you can see, the only issue with you code was the control part. Best!

LocoGris
  • 4,432
  • 3
  • 15
  • 30
  • 1
    The nloptr I used in my second example works properly and allows for upper and lower. See more here https://www.rdocumentation.org/packages/nloptr/versions/1.2.1/topics/neldermead – LocoGris Feb 28 '19 at 23:19