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("eco-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('eco') Loading required package: MASS eco: R Package for Fitting Bayesian Models of Ecological Inference in 2x2 Tables Version 2.1-1 > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "eco" > > ### * eco > > flush(stderr()); flush(stdout()) > > ### Name: eco > ### Title: Fitting the Parametric Bayesian Model of Ecological Inference in > ### 2x2 Tables > ### Aliases: eco > ### Keywords: models > > ### ** Examples > > > ## load the registration data > data(reg) > > ## NOTE: convergence has not been properly assessed for the following > ## examples. See Imai and Lu (2004) for more complete examples. > > ## fit the parametric model with the default prior specification > res <- eco(Y ~ X, data = reg, verbose = TRUE) 10 percent done. 20 percent done. 30 percent done. 40 percent done. 50 percent done. 60 percent done. 70 percent done. 80 percent done. 90 percent done. 100 percent done. > ## summarize the results > summary(res) Call: eco(formula = Y ~ X, data = reg, verbose = TRUE) Parameter Estimates: mean std.dev 2.5 % 97.5 % E(W1) 0.467241 0.067260 0.349181 0.611 E(W2) 0.951948 0.008893 0.933071 0.967 mu1 -0.132895 0.275547 -0.622641 0.450 mu2 3.002927 0.192229 2.634854 3.375 Sigma11 6.305958 0.835710 4.864044 8.053 Sigma12 2.787046 0.552663 1.590832 3.800 Sigma22 3.589549 0.495152 2.736656 4.669 Aggregate Estimates: mean std.dev 2.5 % 97.5 % W1 0.46139 0.02601 0.42203 0.52 W2 0.88597 0.00901 0.86568 0.90 Number of Units: 275 Number of Monte Carlo Draws: 5000 > > ## obtain out-of-sample prediction > out <- predict(res, verbose=TRUE) 10 percent done. 20 percent done. 30 percent done. 40 percent done. 50 percent done. 60 percent done. 70 percent done. 80 percent done. 90 percent done. 100 percent done. > ## summarize the results > summary(out) Out-of-sample Prediction: mean std.dev 2.5 % 97.5 % W1 0.479196 0.344356 0.005138 0.991 W2 0.876160 0.177246 0.314998 0.999 Number of Monte Carlo Draws: 5000 > > > > > cleanEx(); ..nameEx <- "ecoBD" > > ### * ecoBD > > flush(stderr()); flush(stdout()) > > ### Name: ecoBD > ### Title: Calculating the Bounds for Ecological Inference in RxC Tables > ### Aliases: ecoBD > ### Keywords: models > > ### ** Examples > > > ## load the registration data > data(reg) > > ## calculate the bounds > res <- ecoBD(Y ~ X, N = N, data = reg) > ## print the results > print(res) Call: ecoBD(formula = Y ~ X, data = reg, N = N) Aggregate Lower Bounds (Proportions): c1 c2 r1 0.047909 0.549623 r2 0.005402 0.061693 Aggregate Upper Bounds (Proportions): c1 c2 r1 0.2156 0.7173 r2 0.1731 0.2294 Aggregate Lower Bounds (Counts): c1 c2 r1 427500 4904400 r2 48200 550500 Aggregate Upper Bounds (Counts): c1 c2 r1 1923800 6400700 r2 1544500 2046800 > > > > cleanEx(); ..nameEx <- "ecoNP" > > ### * ecoNP > > flush(stderr()); flush(stdout()) > > ### Name: ecoNP > ### Title: Fitting the Nonparametric Bayesian Model of Ecological Inference > ### in 2x2 Tables > ### Aliases: ecoNP > ### Keywords: models > > ### ** Examples > > > ## load the registration data > data(reg) > > ## NOTE: convergence has not been properly assessed for the following > ## example. See Imai and Lu (2004) for more complete examples. > > ## fit the nonparametric model to give in-sample predictions > ## store the parameters to make population inference later > res <- ecoNP(Y ~ X, data = reg, n.draws = 500, param = TRUE, verbose = TRUE) 10 percent done. 20 percent done. 30 percent done. 40 percent done. 50 percent done. 60 percent done. 70 percent done. 80 percent done. 90 percent done. 100 percent done. > ##summarize the results > summary(res) Call: ecoNP(formula = Y ~ X, data = reg, parameter = TRUE, n.draws = 500, verbose = TRUE) Aggregate Estimates: mean std.dev 2.5 % 97.5 % W1 0.59502 0.03401 0.53756 0.663 W2 0.83969 0.01178 0.81602 0.860 Number of Units: 275 Number of Monte Carlo Draws: 500 > > ## obtain out-of-sample prediction > out <- predict(res, verbose=TRUE) 10 percent done. 20 percent done. 30 percent done. 40 percent done. 50 percent done. 60 percent done. 70 percent done. 80 percent done. 90 percent done. 100 percent done. > ## summarize the results > summary(out) Out-of-sample Prediction: mean std.dev 2.5 % 97.5 % W1 0.60619 0.25420 0.07147 0.995 W2 0.82720 0.19910 0.29364 1.000 Number of Monte Carlo Draws: 137500 > > ## density plots of the out-of-sample predictions > par(mfrow=c(2,1)) > plot(density(out[,1]), main="W1") > plot(density(out[,2]), main="W2") > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > ### *