R/MCMCglmm_tidiers.R
, R/mcmc_tidiers.R
mcmc_tidiers.Rd
Tidying methods for MCMC (Stan, JAGS, etc.) fits
# S3 method for MCMCglmm
tidy(x, effects = c("fixed", "ran_pars"), scales = NULL, ...)
tidyMCMC(
x,
pars,
robust = FALSE,
conf.int = FALSE,
conf.level = 0.95,
conf.method = c("quantile", "HPDinterval"),
drop.pars = c("lp__", "deviance"),
rhat = FALSE,
ess = FALSE,
index = FALSE,
...
)
# S3 method for rjags
tidy(
x,
robust = FALSE,
conf.int = FALSE,
conf.level = 0.95,
conf.method = "quantile",
...
)
# S3 method for stanfit
tidy(
x,
pars,
robust = FALSE,
conf.int = FALSE,
conf.level = 0.95,
conf.method = c("quantile", "HPDinterval"),
drop.pars = c("lp__", "deviance"),
rhat = FALSE,
ess = FALSE,
index = FALSE,
...
)
# S3 method for mcmc
tidy(
x,
pars,
robust = FALSE,
conf.int = FALSE,
conf.level = 0.95,
conf.method = c("quantile", "HPDinterval"),
drop.pars = c("lp__", "deviance"),
rhat = FALSE,
ess = FALSE,
index = FALSE,
...
)
# S3 method for mcmc.list
tidy(
x,
pars,
robust = FALSE,
conf.int = FALSE,
conf.level = 0.95,
conf.method = c("quantile", "HPDinterval"),
drop.pars = c("lp__", "deviance"),
rhat = FALSE,
ess = FALSE,
index = FALSE,
...
)
a model fit to be converted to a data frame
which effects (fixed, random, etc.) to return
scales on which to report results
mostly unused; for tidy.MCMCglmm
, these represent options
passed through to tidy.mcmc
(e.g. robust
, conf.int
, conf.method
, ...)
(character) specification of which parameters to include
use mean and standard deviation (if FALSE) or median and mean absolute deviation (if TRUE) to compute point estimates and uncertainty?
(logical) include confidence interval?
probability level for CI
method for computing confidence intervals ("quantile" or "HPDinterval")
Parameters not to include in the output (such as log-probability information)
(logical) include Rhat and/or effective sample size estimates?
Add index column, remove index from term. For example,
term a[13]
becomes term a
and index 13
.
if (require("MCMCglmm")) {
## original model
if (FALSE) {
mm0 <- MCMCglmm(Reaction ~ Days,
random = ~Subject, data = sleepstudy,
nitt=4000,
pr = TRUE
)
}
## load stored object
load(system.file("extdata","MCMCglmm_example.rda",
package="broom.mixed"))
tidy(mm0)
tidy(mm1)
tidy(mm2)
tail(tidy(mm0,effects="ran_vals"))
}
#> Loading required package: MCMCglmm
#> Warning: there is no package called ‘MCMCglmm’
# Using example from "RStan Getting Started"
# https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
model_file <- system.file("extdata", "8schools.stan", package = "broom.mixed")
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18))
## original model
if (FALSE) {
set.seed(2015)
rstan_example <- rstan::stan(file = model_file, data = schools_dat,
iter = 1000, chains = 2, save_dso = FALSE)
}
if (require(rstan)) {
## load stored object
rstan_example <- readRDS(system.file("extdata", "rstan_example.rds", package = "broom.mixed"))
tidy(rstan_example)
tidy(rstan_example, conf.int = TRUE, pars = "theta")
td_mean <- tidy(rstan_example, conf.int = TRUE)
td_median <- tidy(rstan_example, conf.int = TRUE, robust = TRUE)
if (require(dplyr) && require(ggplot2)) {
tds <- (dplyr::bind_rows(list(mean=td_mean, median=td_median), .id="method")
%>% mutate(type=ifelse(grepl("^theta",term),"theta",
ifelse(grepl("^eta",term),"eta",
"other")))
)
ggplot(tds, aes(estimate, term)) +
geom_errorbarh(aes(xmin = conf.low, xmax = conf.high),height=0) +
geom_point(aes(color = method))+
facet_wrap(~type,scale="free",ncol=1)
} ## require(dplyr,ggplot2)
} ## require(rstan)
#> Loading required package: rstan
#> Warning: there is no package called ‘rstan’
if (require(R2jags)) {
## see help("jags",package="R2jags")
## and example("jags",package="R2jags")
## for details
## load stored object
R2jags_example <- readRDS(system.file("extdata", "R2jags_example.rds", package = "broom.mixed"))
tidy(R2jags_example)
tidy(R2jags_example, conf.int=TRUE, conf.method="quantile")
}
#> Loading required package: R2jags
#> Warning: there is no package called ‘R2jags’