[R] solving multiple parameter starting values in nlm
spencer.graves at pdf.com
Tue Jul 8 23:27:23 CEST 2003
1. With a problem like that, I will typically try to step through my
function one line at a time until I find the line that produces the
warning, "the number of items to replace is not a multiple of
replacement length". At that point, it is typically fairly easy to
figure out how to fix the problem.
2. If that fails, could you try to strip the function down to something
that still produces the problem but is small enough that someone else
can read it in 20 seconds? Often, if you try to do this, you will find
the problem. For example, from what I read, your current problem has
nothing to do with "nlm" nor "optim"; it is "the number of items to
replace is not ... ." If so, the function "nlm" and "optim" are just
part of the forest that make it difficult for you to see the trees.
3. "q" is the name of a function in R. R is smart enough to know the
difference, but I'm not always so smart. I suggest you use a different
name, like "q." or anything else.
hope this helps. spencer graves
Michael Rennie wrote:
> Hi, there
> At 12:28 AM 7/8/03 -0700, Spencer Graves wrote:
>> 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.
> I've gone through and now attempted a number of varieties on this
> theme. Stated generally, the newest variation I am hung on is;
> f <- function (q)
> for i in (1:length(day))
> .........I then use x and y in my loop for daily iterations, where each
> element is dependent upon the value of the previous iteration, and
> obtain values for c, d, and try to minimize the following function........
> f <- ((a^2 -c^2)^2 + (b^2 - d^2)^2)^2/2 ; f
> nlm (f, 1, r=1)
> I've squared all the results, the differences, and the overall value as
> the function to minimize such that the differences between the user
> input values (a, b) and the model output (b,d) are exaggerated so as to
> obtain a sufficient minimization. This function works on it's own when
> performed after the daily iteration loop, but it doesn't even get that
> far in this version of the nlm loop. Also, the loop with only my daily
> iterations works fine (without the nlm loop), no warnings, all is good.
> But, once I embed it within the loop for function f, my program stalls
> on the daily iterations. I get error messages that seem to indicate
> that it isn't even going through the first iteration properly.
> If, however, I code my input variables as q and q in the loop,
> instead of x, y, it solves the loop correctly, but comes back with a
> billion warnings that "the number of items to replace is not a multiple
> of replacement length". I think that it wants my q vector to be the
> same length as the iterations in the daily loop, 365, but gets mad when
> it isn't, because it possesses only 2 elements, (x ,y) or (q, q).
> As well, when I run the daily iteration loops OUTSIDE the nlm loop with
> the q and q coding, I get the same error messages ("the number of
> items to replace is not a multiple of replacement length"). Despite
> this, it goes through the program and reports a solution.
> My fear is that even though I am getting the right answers from the
> daily loop in this last version, if I am getting errors, then I
> shouldn't just simply ignore them and continue. Or should I? Are the
> errors simply a result of a technicality? Personally, I would feel
> better if I could do it without them.
>> 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.
> Yes, or at least a modified version of it. see above.
>> 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" does.
> Yes, and "optim" is stalling in the same places and generating the same
> errors as 'nlm'.
> Any other thoughts or suggestions for what I'm doing wrong?
>> 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 and q 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?
> Michael Rennie
> M.Sc. Candidate
> University of Toronto at Mississauga
> 3359 Mississauga Rd. N.
> Mississauga, ON L5L 1C6
> Ph: 905-828-5452 Fax: 905-828-3792
More information about the R-help