我正在尝试在 R 中运行 rjags(通过 Rstudio)来估计tau.nu
以下模型的参数 alpha&beta 和超参数:
y_i|x_i~pois(eta_i),
eta_i=exp(alpha + beta*x_i + nu_i),
nu_i~N(0,tau.nu)
有我的代码:
#generating data
N = 1000
x = rnorm(N, mean=3,sd=1)
nu = rnorm(N,0,0.01)
eta = exp(1 + 2*x + nu)
y = rpois(N,eta)
data=data.frame(y=y,x=x)
###MCMC
library(rjags)
library(coda)
mod_string= "model {
for(i in 1:1000) {
y[i]~dpois(eta[i])
eta[i]=exp(alpha+beta*x[i]+nu[i])
nu[i]~dnorm(0,tau.nu)
}
alpha ~ dnorm(0,0.001)
beta ~ dnorm(0,0.001)
tau.nu ~ dgamma(0.01,0.01)
}"
params = c("alpha","beta","tau.nu")
inits = function() {
inits = list("alpha"=rnorm(1,0,100),"beta"=rnorm(1,0,80),"tau.nu"=rgamma(1,1,1))
}
mod = jags.model(textConnection(mod_string), data=data, inits=inits, n.chains =3)
update(mod,5000)
mod_sim = coda.samples(model=mod,
variable.names=params,
n.iter=2e4)
mod_csim = as.mcmc(do.call(rbind, mod_sim))
plot(mod_csim)
我得到奇怪的输出,我不知道我哪里出错了。MCMC 在这个模型中不起作用吗?或者我只是在编码中做错了什么?