title: R: Using optim() to set variable weights
body: <p>I have the following data:</p>
  
  <pre><code>set.seed(1)
  data &lt;- data.frame(matrix(rexp(45), 5))
  data &lt;- cbind(data, c(1,2,2,3,1))
  colnames(data) &lt;- c("model1_a","model1_b","model1_c","model2_a","model2_b","model2_c","model3_a","model3_b","model3_c", "true_max")
  </code></pre>
  
  <p>That is:</p>
  
  <pre><code>   model1_a  model1_b  model1_c  model2_a  model2_b   model2_c   model3_a  model3_b  model3_c true_max
  1 0.7551818 2.8949685 1.3907351 1.0352439 2.3645153 0.05943916 1.43528534 1.0227259 1.0798811        1
  2 1.1816428 1.2295621 0.7620299 1.8760352 0.6418926 0.57871246 0.03726853 0.3017409 1.0282469        2
  3 0.1457067 0.5396828 1.2376036 0.6547466 0.2941204 3.95893285 0.32401015 0.7252143 1.2922616        2
  4 0.1397953 0.9565675 4.4239342 0.3369335 0.5658655 1.17331211 1.32046793 0.7515427 1.2531054        3
  5 0.4360686 0.1470460 1.0545432 0.5884797 0.1060726 0.99681296 0.20351035 0.2350275 0.5546414        1
  </code></pre>
  
  <p>Using this data, I need to find the optimal weights for combining the results from each model. Without weights, these sums are:</p>
  
  <pre><code>  cbind(
      "a_total" = data$model1_a+data$model2_a+data$model3_a,
      "b_total" = data$model1_b+data$model2_b+data$model3_b,
      "c_total" = data$model1_c+data$model2_c+data$model3_c
    )
  
        a_total   b_total  c_total
  [1,] 3.225711 6.2822097 2.530055
  [2,] 3.094946 2.1731956 2.368989
  [3,] 1.124464 1.5590175 6.488798
  [4,] 1.797197 2.2739757 6.850352
  [5,] 1.228059 0.4881461 2.605998
  </code></pre>
  
  <p>Using <code>which.max()</code> yields the estimated max of <code>2 1 3 3 3</code>. 
  How do I use <code>optim()</code> to set weights to each column (or individual value)? The data contains the true max.</p>