我目前正在用R编写一个MCMC过程,用于估计Rasch模型参数。为此,我在Gibbs采样器中使用metropolis hastings算法。
在下面的代码中,给出了项目参数建议函数的一部分。
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]
}我的问题是,使用if(runif(1)<= exp(d.l.p[i]-d.l.c[i]))来确定是否应该接受建议值是否正确?我知道在非对数似然的情况下,您可以使用if(runif(1)<= d.l.p[i]/d.l.c[i])来确定新值的可接受性。
由于这更多的是一个概念问题,而不是一个编码问题,所以我省略了其余的代码。但是,如果需要所有代码,我会很乐意提供。
提前使用Tnx!Joost
发布于 2015-08-19 02:00:58
我一直在取随机制服的log,如下所示:if((lpnew-lpold)>log(runif(1)
https://stackoverflow.com/questions/28992255
复制相似问题