I'm currently writing a MCMC procedure in R for estimation of Rasch model parameters. To do this I use a metropolis-hastings algorithm in a Gibbs sampler.
In the code below a part of the proposal function for the item parameters is given.
y <- rnorm(1,delta[i],sd) #proposal value
# delta[i] is the current delta value from which new proposal is simulated
# calculate log-probability for each person with proposal value
for(p in 1:length(theta)){
z[i] = log(exp(x[p,i]*(theta[p]-y))/(1 + (exp(x[p,i]*(theta[p]-y)))))
}
# sum log-prob values and add log of f(y)
d.l.p[i] <- sum(z)+log(dnorm(y,0,1)) # d.l.p = delta likelihood proposal
## Is this correct?
if(runif(1)<= exp(d.l.p[i]-d.l.c[i])){ # d.l.c = delta likelihood current value
delta[i] <- y
d.l.c[i] <- d.l.p[i]
}
My question is whether using if(runif(1)<= exp(d.l.p[i]-d.l.c[i]))
to determine whether the proposal value should be accepted is correct? I know that in the case of non log-likelihood, you can use if(runif(1)<= d.l.p[i]/d.l.c[i])
to determine the acceptation of the new value.
Since this is more of a conceptual question than a coding question, I left out the rest of the code. However, if all code is required I'll gladly provide it.
Tnx in advance! Joost