I use both packages in order to do Bayesian analysis but there are some differences that I don't understand:
First of all the package rjags
allows the adaptation phase, with the jags.model
function, while the package r2jags
does not have this phase, and with the function jags
(or jags.parallel
) begin to sample from the posteriori distribution. Is the adaptive phase included in that function, or the package r2jags
does not consider it?
Secondly, in rjags
, could I say that these two chunks of code are similar?
jmod <- jags.model(file="somefile.txt", data = data, n.chains=3)
update(jmod,100)
jsample <- coda.samples(jmod, n.iter=100, variable.names=par)
and
jmod <- jags.model(file="somefile.txt", data = data, n.chains=3)
jsample <- coda.samples(jmod, n.iter=200,n.burnin=100, variable.names=par)
that is, the burn-in phase with update
function can be also done in coda.samples
function? Thank you.
R2jags
is an over-the-top function for running rjags
. It is meant to make it a bit easier to do some things as described in the package description like running until converged, or parallelising MCMC chains.
If you look at the jags
function in R2jags
(e.g. by looking at the source code or just entering jags
without brackets into your R console), you will find the following calls near the end of the function (lines 151–177 on linked github version):
m <- jags.model(model.file,
data = data,
inits = init.values,
n.chains = n.chains,
n.adapt = 0 )
adapt( m,
n.iter = n.adapt,
by = refresh,
progress.bar = progress.bar,
end.adaptation = TRUE )
samples <- coda.samples( model = m,
variable.names = parameters.to.save,
n.iter = ( n.iter - n.burnin ),
thin = n.thin,
by = refresh,
progress.bar = progress.bar )
So R2jags::jags
is compiling the model with jags.model
, adapting it with adapt
, then iterating and sampling from the posterior with coda.samples
Your two calls aren't exactly equivalent. In the first you:
jags.model
,update
, thencoda.samples
.In the second you
jags.model
,coda.samples
.I.e., you're posterior sample is from more iterations but there is no additional "burn-in" phase in method 2 after the adaptation implicit in jags.model
. There is no use for n.burnin
in rjags
, only in R2jags
; see the coda.samples
call in the jags
function code above, while earlier on line 77, that function assigns n.adapt <- n.burnin
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With