Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minimize a function over one input parameter in R

Tags:

r

minimize

Suppose I have a function that is defined as following

myFunction = function(input1, input2, input3) {
    # do something with input and then return
}

and now I want to minimize myFunction over only the first input, namely, input1, while fixing the other parameters.

In R, it seems that there are some prepacked functions like nlm, optim, etc. But the documentation doesn't really explain how to do the problem above. For example, it seems that optim can only minimize a function over only 1 input? I am probably wrong, but please correct me, and show me the advised way of doing this kind of minimizing problem.

Thank you very much!

like image 588
Enzo Avatar asked Jun 09 '13 19:06

Enzo


1 Answers

For minimizing the output when the input is a vector, you can use optim.

myFunction = function(input1, input2, input3) sum(abs(input1 - 3))
o = optim(7:10, myFunction, input2=5, input3=6)
o$par
# [1] 2.999989 2.999995 3.000000 3.000001

The first argument to optim (7:10 in my example) is the starting value for input1 (a guess of where to start the optimization). The second is the function, and you can then pass in the fixed parameters (like input2 and input3).

In this example, the minimum turns out to be a vector of only 3s (as that minimizes sum(abs(input1 - 3))).


If you were performing just a 1-dimensional minimization, you would use the optimize function, and pass the other inputs to it after passing the function:

myFunction = function(input1, input2, input3) {
    return(abs(input1 - 3) + input2 + input3)
}

o = optimize(myFunction, 2, 3, interval=c(-100, 100))
print(o$minimum)
# [1] 3.000003

If you want to minimize based on input2 or input3 (one that isn't the first argument to the function), you can give the others as named arguments:

myFunction = function(input1, input2, input3) {
    return(abs(input1 - 3) + abs(input2 - 12) + input3)
}

print(optimize(myFunction, input1=2, input3=3, interval=c(-100, 100)))
print(o$minimum)
[1] 11.99998

If you're wondering why this is possible, it's because optimize is defined as:

function (f, interval, ..., lower = min(interval), upper = max(interval), 
          maximum = FALSE, tol = .Machine$double.eps^0.25) 

That ... allows the use of any number of additional parameters that it will then pass to myFunction.

like image 136
David Robinson Avatar answered Oct 19 '22 03:10

David Robinson