2
votes

I am attempting to use optim() in R to solve for lambda in the following equation:

lambda/sigma^2 - ln(lambda/sigma^2) = 1 + 1/Q

subject to constraint :

lambda > sigma^2.

I am not sure how one goes about setting up this in R.

I am open to alternative optimization routines as well although the equation seems convex and therefore optim should be a fine choice.

Thank you!

2
Is lambda the only variable? If you check the "note" in ?optim for one-dimensional problems optimize() is recommended instead. And when you say "solve for lambda", is there an equality (or inequality) missing from your equation? Or do you want to minimize or maximize your expression? - Gregor Thomas
I updated the equation (I had a typo). Yes, it is a one-dimensional problem. I will explore optimize() - thanks for the lead! - Ram Ahluwalia

2 Answers

3
votes

You are trying to solve an equation. Whether or not the constraint is met, can only be decided ex post. You can use uniroot as follows

f <- function(x,sigma=1,Q=1) {x/sigma^2 - log(x/sigma^2) - 1 - 1/Q}
uniroot(f,c(1,5))

giving

$root
[1] 3.146198

$f.root
[1] 3.552369e-06

$iter
[1] 5

$estim.prec
[1] 6.103516e-05
1
votes

Decided this is more an answer than a comment.

Both optim and optimize minimize functions, so what you want to do is write an error function that returns, say, the squared error for a given lambda (se(lambda, sigma^2, Q), make sure your lambda is the first argument). Then call optim(f = se, lower = sigma^2, sigma^2, Q) and it will return the value of lambda that minimizes your error function. If you have multiple data points (Q, sigma^2 pairs) then make your function a sum of squared errors or try using nls().