Differences between rjags and r2jags
Asked Answered
W

1

5

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.

Wooley answered 8/6, 2018 at 10:32 Comment(0)
K
7

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:

  1. compile and adapt with jags.model,
  2. update for 100 iterations with update, then
  3. update and sample from the posterior for 100 iterations with coda.samples.

In the second you

  1. compile and adapt with jags.model,
  2. update and sample from the posterior for 200 iterations with 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.

Knop answered 14/6, 2018 at 4:32 Comment(2)
So it's just an ensamble of functions. Thank you, very clear.Wooley
One of the great things about R is that it's pretty easy to look inside of functions and work out what they might be doing. Don't be scared to just plug a function name directly into the console to see the underlying codeKnop

© 2022 - 2024 — McMap. All rights reserved.