I am trying run rjags in R (via Rstudio) to estimate parameters alpha&beta and hyperparameter tau.nu
of the model following:
y_i|x_i~pois(eta_i),
eta_i=exp(alpha + beta*x_i + nu_i),
nu_i~N(0,tau.nu)
there is my code:
#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)
the I get weird output,I don't konw where I get wrong.Does MCMC not work in this model?Or I just do something wrong in coding?
nu
outside the likelihood, increased burnin and iterations -- these made no difference. After loading theglm
module (load.module("glm")
) it converged as expected. (caveat, I usedN=100
as it would take too long otherwise) – Cotemporaryglm
module that you suggested,it converged,but i still have problems:I set iterations as 5e4,why the range of x-axis is 0~1.5e5 ?And the value of estimation of tau.mu is far away from true value 0.01. – Fluoroscopytau.nu
is the precision. to return the variance add a line under the priors,sig2 = 1 / tau.nu
, Then followsig2
instead oftau.nu
. From your example, the iterations on the x-axis depend the n.adapt (1000) from jags.model + 5000 burnin + n.iter (2e4) from coda.samples, so I'd expect the x-axis to start at 6000, and go to 26000 inplot(mod_sim)
- which is what I think you really want. As you are combining the chains you get 3*5e4 = 1.5e5 -- so one after the other. – Cotemporarybayesplot
package ,it made problems when I try to add output of other approach.So I tried jags.hist function inR2jags
package, it errors with 'not found jags.hist function',even I run the example case 1.of course,I did installR2jags
– Fluoroscopylibrary(data.table) ; chn <- rbindlist(lapply(mod_sim2, as.data.table), idcol="chain") ; mchn <- melt(chn, id.vars = "chain") ; library(ggplot2) ; ggplot(mchn, aes(value)) + geom_histogram() + facet_wrap(~variable, scales="free")
(ps density plots are definitely the better option compared to histograms) – Cotemporaryrjags.mat = as.matrix(mod_csim);rjags.dat = as.data.frame(rjags.mat) ;hist.sig2=ggplot() + geom_histogram(data=rjags.dat,aes(x=sig2,y=..density..), binwidth=.1, colour="gray", fill="white") + geom_density(data = dens.inla,aes(x=sig2),col='black')
I tired like this.And thank you sooooooo much. – Fluoroscopy