title: Optim with multiple parameters - define function argument type
body: <p>I want to minimize the first 11 (or any large number) parameters of a function, which takes 15 (or any number greater than 11 / the first number stated) parameters in total. The problem is that when I use the <code>optim</code> function with <code>par=c(...)</code> for the first 11 values, the function cannot be evaluated as it takes all 11 values for its first argument only. Is there any way to force each input argument to be a scalar / single number? </p>
  
  <p>To be more specific: </p>
  
  <pre><code>fun_to_optim &lt;- function(m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,p1,p2,p3,p4)
  {
     # I want to make sure m1 to m11 are single numbers and no vectors
     ...
  }
  # optimization
  optim(par=c(0,0,0,0,0,0,0,0,0,0,0),fn=fun_to_optim,p1=data,p2=opt,p3=data,p4=1)    
  </code></pre>
  
  <p>Trying to run this code always ends with the error message: <b> Error in fn(par, ...) : argument "m1" is missing, with no default.</b> By printing out the arguments, I figured that the function assumes that all values from <code>par</code> are for the input variable <code>m1</code>. So <code>m1</code> is taken to be a vector with the initial values given through <code>par</code>. </p>
  
  <p>Please note: I am aware of the possibility to simply put the first 11 parameters into a vector (let's say <code>para_to_optim</code>) and then call the <code>optim</code> function as I did. However, I explicitly do not want to do this because I need to optimize the parameters iteratively, i.e. starting with just 1 parameter and ending will all 11. Writing the function 11 times with different input argument structures seems a bit inefficient. </p>
  
  <p>I have already checked similar post for function arguments such as <a href="https://stackoverflow.com/questions/7139778/forcing-specific-data-types-as-arguments-to-a-function">Forcing specific data types as arguments to a function</a> and <a href="https://stackoverflow.com/questions/1745514/how-to-define-argument-types-for-r-functions">How to define argument types for R functions?</a>, but it would not be possible in my case to check the type of the argument within the function and wrapper or S3 classes are also kind of problematic. Is there any other way to solve this?</p>