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

In summary, the conversation discusses the use of the nlm function in R to minimize functions. The correct way to call nlm is to have all independent variables in an array and their starting guess values in a matching array. The conversation also includes an example of how to use nlm correctly.
  • #1
the_dane
30
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
  • #2
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 FactChecker
  • #3
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 )
 
  • #4
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:

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

1. How do I install and load the nlm package in R?

The nlm package is a built-in package in R, so you do not need to install it separately. To load the package, simply use the command library(nlm) in your R script or console.

2. What is the syntax for using the nlm function in R?

The basic syntax for the nlm function is nlm(objective, start, hessian = FALSE, ...). The "objective" argument specifies the objective function to be minimized, the "start" argument specifies the starting values for the parameters, and the "hessian" argument determines whether the Hessian matrix will be computed. Additional arguments can also be included depending on the specific objective function being used.

3. How do I specify constraints for the parameters in the nlm function?

The nlm function does not have a built-in option for specifying constraints. However, you can use the optim function in R, which allows for the specification of constraints using the "constraints" argument. You can then specify "method = "Nelder-Mead"" to use the Nelder-Mead optimization method, which is similar to the nlm function.

4. Can the nlm function be used for non-linear least squares regression?

Yes, the nlm function can be used for non-linear least squares regression. You can specify the sum of squared residuals as the objective function to be minimized in the nlm function, and the resulting parameter estimates will be the same as those obtained from the nls function in R.

5. How do I interpret the results from the nlm function in R?

The nlm function returns a list containing the estimated parameters, the value of the objective function at the estimated parameters, and the convergence code. The estimated parameters can be accessed using result$estimate, the value of the objective function at the estimated parameters can be accessed using result$objective, and the convergence code can be accessed using result$code. A code of 0 indicates successful convergence, while other codes indicate different types of issues with the optimization process.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
1
Views
802
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
752
  • Programming and Computer Science
Replies
2
Views
925
  • Programming and Computer Science
Replies
3
Views
1K
  • General Math
Replies
2
Views
743
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
22
Views
3K
Back
Top