Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using argmax or something simpler in R

Tags:

function

r

max

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:

g0<-function(k){sample(0:1,k,replace=T)}

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.

X1 design matrix

Xg<-function(g){
  Xg<-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<-Xg(g0)

reduced design matrix for g0

c<-1:100000

mp<-function(g){
  mp<-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)
}

this is my function.

Therefore if I have mp(g) and mp(g*), for 2 inputs g and g*, such that the max is mp(g*) how can I return g*?

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.

like image 910
Jonny Phelps Avatar asked Jun 22 '13 16:06

Jonny Phelps


1 Answers

Like this:

inputs     <- list(g, g2)
outputs    <- sapply(inputs, mp)
best.input <- inputs[which.max(outputs)]
like image 159
flodel Avatar answered Nov 03 '22 02:11

flodel