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

Ben Bolker bbolker at gmail.com
Mon Aug 2 20:50:41 CEST 2010


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 ...



More information about the R-help mailing list