R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("rbugs-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('rbugs') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "format4Bugs" > > ### * format4Bugs > > flush(stderr()); flush(stdout()) > > ### Name: format4Bugs > ### Title: Format a Data List as BUGS Data Input > ### Aliases: format4Bugs > ### Keywords: utilities interface > > ### ** Examples > > dat <- list(a = runif(1), b=rnorm(2), c=matrix(rexp(4), 2, 2)) > format4Bugs(dat) [1] "list(a=2.65509E-01, b=c(-3.26233E-01, 1.32980E+00), c= structure(.Data= c(4.58030E-01, 2.89497E+00, 2.58228E-01, 1.22956E+00), .Dim=c(2, 2)))" > > > > cleanEx(); ..nameEx <- "genBugsScript" > > ### * genBugsScript > > flush(stderr()); flush(stdout()) > > ### Name: genBugsScript > ### Title: Generating Script File for BUGS > ### Aliases: genBugsScript > ### Keywords: utilities interface > > ### ** Examples > > ## no tested examples for mac-os. > ## Not run: > ##D script.file <- paste(tempfile("script"), ".txt", sep="") > ##D genBugsScript(paramSet=c("alpha", "theta"), n.chains=2, n.keep=1500, > ##D n.burnin=500, n.thin=1, > ##D model.file="/var/scratch/c/tmp/model.txt", > ##D data.file="/var/scratch/c/tmp/data.txt", > ##D inits.file=c("/var/scratch/c/tmp/init1.txt", > ##D "/var/scratch/c/tmp/init2.txt"), > ##D workingDir="/var/scratch/c/tmp", > ##D bugsWorkingDir="c:/tmp", > ##D script=script.file, > ##D debug=FALSE, useWine=TRUE) > ##D file.show(script.file) > ##D unlink(script.file) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "genDataFile" > > ### * genDataFile > > flush(stderr()); flush(stdout()) list(a=2.65509E-01, b=c(-3.26233E-01, 1.32980E+00), c= structure(.Data= c(4.58030E-01, 2.89497E+00, 2.58228E-01, 1.22956E+00), .Dim=c(2, 2))) > > ### Name: genDataFile > ### Title: Generating the Data File for BUGS > ### Aliases: genDataFile > ### Keywords: utilities > > ### ** Examples > > dat <- list(a = runif(1), b=rnorm(2), c=matrix(rexp(4), 2, 2)) > genDataFile(dat, "foo.txt") > file.show("foo.txt") > unlink("foo.txt") > > > > cleanEx(); ..nameEx <- "genInitsFile" > > ### * genInitsFile > > flush(stderr()); flush(stdout()) list(alpha=1.00000E+00, beta=2.00000E+00) list(alpha=4.00000E+00, beta=4.00000E+00) list(alpha=c(-6.26454E+01, 1.83643E+01), beta=1.59797E-04) list(alpha=c(3.29508E+01, -8.20468E+01), beta=3.36210E+01) > > ### Name: genInitsFile > ### Title: Generating Initial Value Files for BUGS > ### Aliases: genInitsFile > ### Keywords: utilities > > ### ** Examples > > ## when inits is a list: > inits <- list(list(alpha=1, beta=2), list(alpha=4, beta=4)) > genInitsFile(2, inits, "foo.init") > file.show("foo.init1.txt") > file.show("foo.init2.txt") > ## when inits is a function: > inits <- function() list(alpha=rnorm(2, sd=100), + beta=rgamma(1, 0.1, 0.001)) > genInitsFile(2, inits, "foo.init") > file.show("foo.init1.txt") > file.show("foo.init2.txt") > unlink("foo.init1.txt") > unlink("foo.init2.txt") > > > > cleanEx(); ..nameEx <- "pumps" > > ### * pumps > > flush(stderr()); flush(stdout()) model { for (i in 1 : N) { theta[i] ~ dgamma(alpha, beta) lambda[i] <- theta[i] * t[i] x[i] ~ dpois(lambda[i]) } alpha ~ dexp(1) beta ~ dgamma(0.1, 1.0) } list(alpha = 1, beta = 1) > > ### Name: pumps > ### Title: 10 Power Plant Pumps > ### Aliases: pumps > ### Keywords: datasets > > ### ** Examples > > data(pumps) > pumps.data <- list(t = pumps$t, x = pumps$x, N = nrow(pumps)) > pumps.model <- file.path(.path.package("rbugs"), "bugs/model", "pumps.bug") > file.show(pumps.model) > pumps.inits <- file.path(.path.package("rbugs"), "bugs/inits", "pumps.txt") > file.show(pumps.inits) > inits <- list(dget(pumps.inits)) > parameters <- c("theta", "alpha", "beta") > > ## Not run: > ##D ## The workingDir must be a directory which can be seen by wine > ##D ## The nominalWorkingDir gives the directory name recoganizable for > ##D ## Windows. > ##D ## The full name of the bugs and wine executables need to set. > ##D ## In .Renviron, add lines similar to these: > ##D ## BUGS="c:/program files/winbugs14/winbugs14.exe" > ##D ## WINE="/var/scratch/jyan/wine-20040408/wine" > ##D > ##D pumps.sim <- rbugs(data = pumps.data, inits, parameters, > ##D pumps.model, n.chains = 1, n.iter = 1000, > ##D workingDir="/var/scratch/jyan/c/tmp", > ##D bugsWorkingDir="c:/tmp", > ##D useWine=TRUE) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "rbugs" > > ### * rbugs > > flush(stderr()); flush(stdout()) > > ### Name: rbugs > ### Title: Run BUGS from R in a BATCH > ### Aliases: rbugs > ### Keywords: interface > > ### ** Examples > > ## see examples in this-is-escaped-code{ and this-is-escaped-codenormal-bracket42bracket-normal > > > > cleanEx(); ..nameEx <- "schools" > > ### * schools > > flush(stderr()); flush(stdout()) # Bugs model file for 8 schools analysis from Section 5.5 of "Bayesian Data # Analysis". Save this into the file "schools.txt" in your R working directory. model { for (j in 1:J){ # J=8, the number of schools y[j] ~ dnorm (theta[j], tau.y[j]) # data model: the likelihood tau.y[j] <- pow(sigma.y[j], -2) # tau = 1/sigma^2 } for (j in 1:J){ theta[j] ~ dnorm (mu.theta, tau.theta) # hierarchical model for theta } tau.theta <- pow(sigma.theta, -2) # tau = 1/sigma^2 mu.theta ~ dnorm (0.0, 1.0E-6) # noninformative prior on mu sigma.theta ~ dunif (0, 1000) # noninformative prior on sigma } > > ### Name: schools > ### Title: 8 schools > ### Aliases: schools > ### Keywords: datasets > > ### ** Examples > > data(schools) > J <- nrow(schools) > y <- schools$estimate > y <- rnorm(length(y)) > sigma.y <- schools$sd > schools.data <- list ("J", "y", "sigma.y") > ## schools.data <- list(J=J, y=y, sigma.y=sigma.y) > inits <- function() {list (theta=rnorm(J,0,100), + mu.theta=rnorm(1,0,100), + sigma.theta=runif(1,0,100))} > parameters <- c("theta", "mu.theta", "sigma.theta") > schools.bug <- file.path(.path.package("rbugs"), "bugs/model", "schools.bug") > file.show(schools.bug) > ## Not run: > ##D ## no tested examples for mac-os. > ##D schools.sim <- rbugs(data=schools.data, inits, parameters, > ##D schools.bug, n.chains=3, n.iter=1000, > ##D workingDir="/var/scratch/jyan/c/tmp", > ##D bugsWorkingDir="c:/tmp", > ##D useWine=TRUE, > ##D wine="/var/scratch/jyan/wine-20040408/wine", > ##D debug=TRUE) > ## End(Not run) > > > > ### *