title: using argmax or something simpler in R
body: <p>I am trying to set up a Gibbs sampler in R where I update my value at each step.
  I have a function in R that I want to maximise for 2 values; my previous value and a new one. 
  So I know the maximum outcome from the function applied to both values. But then how do I select the best input without doing it manually? (I need to do a lot of iterations). Here is an idea of the code and the variables:</p>
  
  <pre><code>g0&lt;-function(k){sample(0:1,k,replace=T)}
  </code></pre>
  
  <p>this is a k dimensional vector with entries 1 or 0 uniformly. Initial starting point for my chain. If i=1 then include the i'th variable in the design matrix.</p>
  
  <p><code>X1</code> design matrix</p>
  
  <pre><code>Xg&lt;-function(g){
    Xg&lt;-cbind(X1[,1]*g[1],X1[,2]*g[2],X1[,3]*g[3],X1[,4]*g[4],X1[,5]*g[5],X1[,6]*g[6],X1[,7]*g[7])
    return(Xg[,which(!apply(Xg,2,FUN = function(x){all(x == 0)}))])
  }
  Xg0&lt;-Xg(g0)
  </code></pre>
  
  <p>reduced design matrix for <code>g0</code></p>
  
  <pre><code>c&lt;-1:100000
  
  mp&lt;-function(g){
    mp&lt;-sum((1/(c*(c+1)^-((q+1)/2)))*
    (t(Y)%*%Y-(c/(c+1))*t(Y)%*%Xg(g)%*%solve(t(Xg(g))%*%Xg(g))%*%t(Xg(g))%*%Y)^(-27/2))
    return(mp)
  }
  </code></pre>
  
  <p>this is my function. </p>
  
  <p>Therefore if I have <code>mp(g)</code> and <code>mp(g*)</code>, for 2 inputs <code>g</code> and <code>g*</code>, such that the max is <code>mp(g*)</code> how can I return <code>g*</code>?</p>
  
  <p>Thanks for any help and if you have any queries just ask. sorry about the messy code as well; I have not used this site before.</p>