我正在尝试通过Rstudio在R中运行rjags来估计以下模型的参数alpha&beta和超参数httpBridge.start(5561,"SERVICENAME", function(request) {
:
tau.nu
有我的代码:
y_i|x_i~pois(eta_i),
eta_i=exp(alpha + beta*x_i + nu_i),
nu_i~N(0,tau.nu)
我得到奇怪的输出,我不知道哪里出了问题。MCMC在此模型中不起作用?还是我在编码中做错了什么?
答案 0 :(得分:4)
该模型无法使用标准采样器收敛。如果您使用glm
模块中的采样器,则可以。 (但这并不总是[1]的情况)
未加载glm
模块
library(rjags)
mod_sim1 <- jagsFUN(dat)
plot(mod_sim1)
load.module("glm")
mod_sim2 <- jagsFUN(dat)
plot(mod_sim2)
# function and data
# generate data
set.seed(1)
N = 50 # reduced so could run example quickly
x = rnorm(N, mean=3,sd=1)
nu = rnorm(N,0,0.01)
eta = exp(1 + 2*x + nu)
y = rpois(N,eta)
dat = data.frame(y=y,x=x)
# jags model
jagsFUN <- function(data) {
mod_string= "model {
for(i in 1:N) {
y[i] ~ dpois(eta[i])
log(eta[i]) = alpha + beta* x[i] + nu[i]
}
# moved prior outside the likelihood
for(i in 1:N){
nu[i] ~ dnorm(0,tau.nu)
}
alpha ~ dnorm(0,0.001)
beta ~ dnorm(0,0.001)
tau.nu ~ dgamma(0.001,0.001)
# return on variance scale
sig2 = 1 / tau.nu
}"
mod = jags.model(textConnection(mod_string),
data=c(as.list(data),list(N=nrow(data))),
n.chains = 3)
update(mod,1000)
mod_sim = coda.samples(model=mod,
variable.names=c("alpha","beta","sig2"),
n.iter=1e4)
return(mod_sim)
}