[R] Including a text file (JAGS model) in an R package

Emmanuel Charpentier charpent at bacbuc.dyndns.org
Mon Aug 16 11:24:42 CEST 2010


Sorry for chiming in a tad late (I was incommunicado for two weeks)

Le lundi 02 août 2010 à 18:50 +0000, Ben Bolker a écrit :
> Prof Brian Ripley <ripley <at> stats.ox.ac.uk> writes:
> 
> > 
> > Here is how I do it:
> > 
> > write("
> > model {
> >    for (year in 1:N) {
> >         D[year] ~ dpois(mu[year])
> >         log(mu[year]) <- b[1] + step(year - changeyear) * b[2]
> >    }
> >    for (j in 1:2) { b[j] ~ dnorm(0.0, 1.0E-6) }
> >    changeyear ~ dunif(1,N)
> > }
> > ", "coalmining.jags")
> > 
> > which has the advantage that the model description is right there in 
> > the .Rd file.
> > 
> > If you want a file to be part of the package, put it in e.g.
> > 
> > <pkg source>/inst/JAGSmodels/model.txt
> > 
> > and in the example use
> > 
> > system.file("JAGSmodels", "model.txt", package="whatever")
> 
>   What Prof Ripley said, but also: for JAGS and WinBUGS models you can
> actually specify
> 
> coalmining <- function() {
>  for (year in 1:N) {
>          D[year] ~ dpois(mu[year])
>          log(mu[year]) <- b[1] + step(year - changeyear) * b[2]
>     }
>     for (j in 1:2) { b[j] ~ dnorm(0.0, 1.0E-6) }
>     changeyear ~ dunif(1,N)
> }
> 
> which is even more nicely integrated into the R code ...

... but will fail with BUGS truncation/censoring notation, which cannot
pass for R syntax.

The R2WinBUGS package has a write.model() function, which I shoplifted,
simplified (JAGS doesn't seem to be as picky as WinBUGS in tne numerical
notation department), extended (to take advantage of the JAGS' "data
transform" feature) for use with JAGS (which is much more convenient to
use nowadays with R on a Unix-like platform than either WinBUGS or
OpenBUGS). I planned to submit it to Martyn Plummer, but got
interrupted...

HTH,

						Emmanuel Charpentier



More information about the R-help mailing list