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("R2WinBUGS-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('R2WinBUGS') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "attach.all" > > ### * attach.all > > flush(stderr()); flush(stdout()) > > ### Name: attach.all > ### Title: Attach / detach elements of a bugs objects to search path > ### Aliases: attach.all detach.all > ### Keywords: data > > ### ** Examples > > # An example model file is given in: > model.file <- file.path(.path.package("R2WinBUGS"), "model", "schools.txt") > # Some example data (see ?schools for details): > data(schools) > J <- nrow(schools) > y <- schools$estimate > sigma.y <- schools$sd > data <- list ("J", "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") > ## Not run: > ##D ## You may need to edit "bugs.directory", > ##D ## also you need write access in the working directory: > ##D schools.sim <- bugs(data, inits, parameters, model.file, > ##D n.chains = 3, n.iter = 1000, > ##D bugs.directory = "c:/Program Files/WinBUGS14/", > ##D working.directory = NULL) > ##D > ##D # Do some inferential summaries > ##D attach.all(schools.sim) > ##D # posterior probability that the coaching program in school A > ##D # is better than in school C: > ##D print(mean(theta[,1] > theta[,3])) > ##D # 50 > ##D # and school C's program: > ##D print(quantile(theta[,1] - theta[,3], c(.25, .75))) > ##D plot(theta[,1], theta[,3]) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "bugs" > > ### * bugs > > flush(stderr()); flush(stdout()) 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.0, 1.0E-6) tau.theta <- pow(sigma.theta, -2) sigma.theta ~ dunif (0, 1000) } > > ### Name: bugs > ### Title: Run WinBUGS from R > ### Aliases: bugs > ### Keywords: interface models > > ### ** Examples > > # An example model file is given in: > model.file <- system.file(package = "R2WinBUGS", "model", "schools.txt") > # Let's take a look: > file.show(model.file) > > # Some example data (see ?schools for details): > data(schools) > schools school estimate sd 1 A 28.39 14.9 2 B 7.94 10.2 3 C -2.75 16.3 4 D 6.82 11.0 5 E -0.64 9.4 6 F 0.63 11.4 7 G 18.01 10.4 8 H 12.16 17.6 > > J <- nrow(schools) > y <- schools$estimate > sigma.y <- schools$sd > data <- list ("J", "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") > > ## Not run: > ##D ## You may need to edit "bugs.directory", > ##D ## also you need write access in the working directory: > ##D schools.sim <- bugs(data, inits, parameters, model.file, > ##D n.chains = 3, n.iter = 1000, > ##D bugs.directory = "c:/Program Files/WinBUGS14/", > ##D working.directory = NULL) > ##D print(schools.sim) > ##D plot(schools.sim) > ## End(Not run) > > > > ### *