title: R optim vector to maximise user defined function
body: <p>I have the following dataframes and I would like to find the global maximum of the optimised_vector</p>
  
  <pre><code>set.seed(1)
  xxx&lt;-data.frame(C1 = c("obs1","obs2","obs3","obs4","obs5","obs6"),
                  X1 = round(runif(6, 1, 6),digits=1),
                  X2 = round(runif(6, 1, 6),digits=1),
                  X3 = round(runif(6, 1, 6),digits=1),
                  X4 = round(runif(6, 1, 6),digits=1),
                  X5 = round(runif(6, 1, 6),digits=1)
  )
  
  yyy&lt;-data.frame(C1 = c("obs1","obs2","obs3","obs4","obs5","obs6"),
                  YY = c(1,2,3,4,5,6)
  )
  
  #optimised_vector&lt;-c(1,1,1,1,1,1)
  optimised_vector&lt;-c("coef1","coef2","coef3","coef4","coef5","coef6")
  </code></pre>
  
  <p>So I've built these functions to try to find the maximum correlation between my dummy data. But I would like to be able to impose some constraints, like non-negativity and max of 0.75 for each of the elements of the optimised_vector</p>
  
  <pre><code>inner_function &lt;- function(xxx,optimised_vector) {
  
     scoring &lt;- rowSums(xxx[,2:6] * optimised_vector)
     return (scoring)
  }
  
  maximiser &lt;- function(optimised_vector) {   
     1-cor(
        yyy$YY, 
        inner_function(xxx,optimised_vector),
        method="kendall", use="pairwise")
     }
  </code></pre>
  
  <p>And then I run optim to get the results...</p>
  
  <pre><code>optim(par = c(1,1,1,1,1,1),
        fn  = maximiser)
  </code></pre>
  
  <p>Is this correct, does it even find the global convergence points and how can I impose constraints?</p>