[R] specifying multiple parameter starting values in nlm

Spencer Graves spencer.graves at pdf.com
Tue Jul 8 09:28:40 CEST 2003

	  Did you work through the examples at the end of the "nlm" 
documentation?  The first example there shows that "nlm" expects a 
single vector argument over which f is to be minimized.  This suggests 
that your first construction should not work, while your second should 
be closer.

	  Also, did you try running your function by itself, checking to make 
sure it computed correctly?  The final line for the second function as I 
read it below appears OUTSIDE the function definition as a stand-alone 
expression to be executed once between the function definition and the 
"nlm" call.

	  Also, have you tried "optim"?  It seems to be more general and 
robust, requiring fewer assumptions to function.  The default method for 
"optim" does not require the function to be differentiable, while "nlm" 

hope this helps.  spencer graves

Michael Rennie wrote:
> Hi there
> I am having trouble figuring out how to get an nlm function to report estimates 
> for two parameter values in an estimation.
> The way I've got it goes something like this:
> f <- function (q, r)
> {
> here, I have a second loop which uses q, r to give me values for c, d below.  a 
> and b are already specified; this loop is a mass-balance function where I am 
> trying to find values of q, r to give me c and d. I want c and d (mass-balance 
> model outputs) to approximate a and b (known endpoints that I want the model to 
> attain), respecitvely.  Thus, the function I want to minimize is:
> fu <- ((a^2 - c^2) + (b^2 - d^2))/2 ; fu
> }
> nlm (f, 1, r=1)
> This doesn't return estimates for each parameter, and I get error messages 
> saying something like "repleaced missing value with maximum" or something.  All 
> I did here was try (and fail, obviously) to model my needs against the examples 
> given in help(nlm), but they are using vectors as inputs, and I only need 2 
> values input, and 2 returned.  
> I've also tried specifying the function as 
> f <- function (q)
> {
> mass-balance loop
> }
> fu <- ((a^2 - c^2) + (b^2 - d^2))/2 ; fu
> nlm(f, c(1,1))
> Specifying 1 as the starting values, and coding q[1] and q[2] in my mass 
> balance loop instead of q, r.
> But this doesn't work either, and I get messages that I think are indicating 
> that my mass balance loop isn't computing properly ("number of items to replace 
> is not a multiple of replacement length"- an error I don't get when I run the 
> mass-balance loop outside the nlm function and just specify q, r).
> Does anyone know how to specify two parameters in the function, and ask for 
> estimates of both back from nlm? Alternatively, any hunches on how to make this 
> thing do what I want it to?  
> Thanks, 
> Mike

More information about the R-help mailing list