write.model: Creating a WinBUGS model file

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Convert R / S-PLUS function to a WinBUGS model file

Usage

1
write.model(model, con = "model.bug", digits = 5)

Arguments

model

R / S-PLUS function containing the BUGS model in the BUGS model language, for minor differences see Section Details.

con

passed to writeLines which actually writes the model file

digits

number of significant digits used for WinBUGS input, see formatC

Details

BUGS models follow closely S syntax. It is therefore possible to write most BUGS models as R functions.

As a difference, BUGS syntax allows truncation specification like this: dnorm(...) I(...) but this is illegal in R and S-PLUS. To overcome this incompatibility, use dummy operator %_% before I(...): dnorm(...) %_% I(...). The dummy operator %_% will be removed before the BUGS code is saved.

In S-PLUS, a warning is generated when the model function is defined if the last statement in the model is an assignment. To avoid this warning, add the line "invisible()" to the end of the model definition. This line will be removed before the BUGS code is saved.

Value

Nothing, but as a side effect, the model file is written

Author(s)

original idea by Jouni Kerman, modified by Uwe Ligges

See Also

bugs

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## Same "schoolsmodel" that is used in the examples in ?bugs:
schoolsmodel <- function(){
    for (j in 1:J){
        y[j] ~ dnorm (theta[j], tau.y[j])
        theta[j] ~ dnorm (mu.theta, tau.theta)
        tau.y[j] <- pow(sigma.y[j], -2)
    }
    mu.theta ~ dnorm (0.0, 1.0E-6)
    tau.theta <- pow(sigma.theta, -2)
    sigma.theta ~ dunif (0, 1000)
}

if (is.R()){ # for R
    ## some temporary filename:
    filename <- file.path(tempdir(), "schoolsmodel.bug")
} else{ # for S-PLUS
     ## put the file in the working directory:
     filename <- "schoolsmodel.bug"
}

## write model file:
write.model(schoolsmodel, filename)
## and let's take a look:
file.show(filename)

Example output

Loading required package: coda
Loading required package: boot
model
{
    for (j in 1:J) {
        y[j] ~ dnorm(theta[j], tau.y[j])
        theta[j] ~ dnorm(mu.theta, tau.theta)
        tau.y[j] <- pow(sigma.y[j], -2)
    }
    mu.theta ~ dnorm(0.00000E+00, 1.00000E-06)
    tau.theta <- pow(sigma.theta, -2)
    sigma.theta ~ dunif(0.00000E+00, 1000)
}

R2WinBUGS documentation built on May 2, 2019, 1:45 p.m.