How to solve Non linear equation in R [closed]

That’s pretty easy with the numerical non-linear minimization function nlm(). You define a loss function that should be minimized. In your example that would be eg

f <- function(a) mean((y - x^a)^2)

This is a simple sum of squares loss function. You have to square the difference between y and x^a, otherwise the minimum would be -Inf.

Now you can just use nlm() to find the minimum:

x <- runif(1000)
y <- x^(0.8) + rnorm(1000,0,0.1)

nlm(f, p=1)

gives:

$minimum
[1] 0.01004981

$estimate
[1] 0.8033611

$gradient
[1] 6.322894e-08

$code
[1] 1

$iterations
[1] 6

So the estimate is exactly what you would think.

Other possibilities include optimize() for one-dimensional fitting, but for this you have to specify an interval in which to search:

> optimize(f, interval =c(-100,100))
$minimum
[1] 0.8033645

$objective
[1] 0.01004981

The more general approach would be to use the function optim() that allows you to minimize over multiple parameters:

> optim( 0, f)
$par
[1] 0.8035156

$value
[1] 0.01004981
...

Note that this will result in a warning. This function is not optimized for one-dimensional optimization. You can specify method = "Brent but then you need to set the lower and upper limits.

So tons of possibilities in R to do this 🙂

Leave a Comment