13

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!

Enzo
  • 969
  • 1
  • 8
  • 23

1 Answers1

12

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.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • 3
    no need for this extra function (currying); you can simply pass the extra arguments through `...`: `o = optimize(myFunction, 2, 3, interval=c(-100, 100))` – baptiste Jun 09 '13 at 20:12
  • I am sorry one thing I forgot to mention was what should I do if my first argument is a vector? It seems that `optimize()` expects all arguments to be real numbers. – Enzo Jun 09 '13 at 22:50
  • @Enzo: to be clear, you then want to optimize over the entire vector, as in find the vector of that length that minimizes the output (at which point it is no longer 1d optimization)? Or do you want to run the optimization once for each of the values in that vector? – David Robinson Jun 10 '13 at 04:30
  • @DavidRobinson Yes, it's not a 1d optimization problem. I want to minimize the function over an entire vector, as in minimizing the cost function for a learning algorithm. – Enzo Jun 10 '13 at 15:07
  • @Enzo: See the update to my answer- I believe the section at the top (using `optim`) is what you want. – David Robinson Jun 10 '13 at 17:11