How to properly use the nlm function in R for minimizing functions?

Click For Summary

Discussion Overview

The discussion focuses on the proper usage of the nlm function in R for minimizing functions, particularly addressing how to handle multiple variables in the function being minimized. Participants explore the correct structure of the function and the input parameters required for nlm to work effectively.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares an initial attempt to minimize a function with two variables using nlm but only receives an estimate for one variable, questioning how to obtain estimates for both x and y.
  • Another participant suggests that the parameter p must be an array rather than a single number, indicating a misunderstanding of how nlm operates with multiple variables.
  • A similar point is reiterated by another participant, emphasizing that the independent input values of the function must match the dimensions of p.
  • A different participant critiques the initial code, stating that the function is incorrectly defined for nlm and provides a corrected version that uses an array for the function's input.

Areas of Agreement / Disagreement

Participants express differing views on the correct implementation of the nlm function, with no consensus reached on the best approach to define the function and its parameters.

Contextual Notes

There are unresolved aspects regarding the proper structure of the function and the handling of multiple variables, as well as the specific requirements for input parameters in nlm.

the_dane
Messages
29
Reaction score
0
I try to find out how to minimize functions i R by using nlm function:
> f<-function(x,y){x^2+y^2+10-5*x-y}
> nlm(f,0.1,0.1)

That only gives me an estimate for x. How would write the code to get x and y?
 
Technology news on Phys.org
From
nlm(f, p, ..., hessian = FALSE, typsize = rep(1, length(p)),
fscale = 1, print.level = 0, ndigit = 12, gradtol = 1e-6,
stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000),
steptol = 1e-6, iterlim = 100, check.analyticals = TRUE)

I gather p must be an array, not just a number like 0.1

(as becomes a good fortran programmer, I know nothing of R... :rolleyes: )
 
  • Like
Likes   Reactions: FactChecker
BvU said:
From
nlm(f, p, ..., hessian = FALSE, typsize = rep(1, length(p)),
fscale = 1, print.level = 0, ndigit = 12, gradtol = 1e-6,
stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000),
steptol = 1e-6, iterlim = 100, check.analyticals = TRUE)

I gather p must be an array, not just a number like 0.1

(as becomes a good fortran programmer, I know nothing of R... :rolleyes: )
Also the independent input values of the function f must be an array x of dimension the same as p. (see https://stat.ethz.ch/R-manual/R-devel/library/stats/html/nlm.html )
 
Your code is calling nlm incorrectly. And the function is defined incorrectly for using nlm.

All the independent variables of f that nlm should manipulate should be in the array x and all their starting guess values should be in a matching array p. There are no constant parameters to pass to f.

Try something like this:
Code:
# Define a function of array x.
 f<-function(x){x[1]^2+x[2]^2+10-5*x[1]-x[2]}

# Initial guess values for x
 p = array( c( 1, 0), dim=c(2,1) )

# Call nlm
 ans <- nlm(f,p)

# print answer
 print(ans)
 
Last edited:

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
7K