These methods tidy the estimates from
brmsfit-objects
(fitted model objects from the brms package) into a summary.
# S3 method for class 'brmsfit'
tidy(
x,
parameters = NA,
effects = c("fixed", "ran_pars"),
robust = FALSE,
conf.int = TRUE,
conf.level = 0.95,
conf.method = c("quantile", "HPDinterval"),
rhat = FALSE,
ess = FALSE,
fix.intercept = TRUE,
exponentiate = FALSE,
...
)
# S3 method for class 'brmsfit'
glance(x, looic = FALSE, ...)
# S3 method for class 'brmsfit'
augment(x, data = stats::model.frame(x), newdata = NULL, se.fit = TRUE, ...)Fitted model object from the brms package. See
brmsfit-class.
Names of parameters for which a summary should be
returned, as given by a character vector or regular expressions.
If NA (the default) summarized parameters are specified
by the effects argument.
A character vector including one or more of "fixed",
"ran_vals", or "ran_pars".
See the Value section for details.
Whether to use median and median absolute deviation of the posterior distribution, rather than mean and standard deviation, to derive point estimates and uncertainty
If TRUE columns for the lower (conf.low)
and upper bounds (conf.high) of posterior uncertainty intervals are included.
Defines the range of the posterior uncertainty conf.int,
such that 100 * conf.level% of the parameter's posterior distributio
lies within the corresponding interval.
Only used if conf.int = TRUE.
method for computing confidence intervals ("quantile" or "HPDinterval")
whether to calculate the *Rhat* convergence metric
(FALSE by default)
whether to calculate the *effective sample size* (ESS) convergence metric
(FALSE by default)
rename "Intercept" parameter to "(Intercept)", to match behaviour of other model types?
whether to exponentiate the fixed-effect coefficient estimates and confidence intervals (common for logistic regression); if TRUE, also scales the standard errors by the exponentiated coefficient, transforming them to the new scale
Extra arguments, not used
Should the LOO Information Criterion (and related info) be
included? See loo.stanfit for details. (This
can be slow for models fit to large datasets.)
data frame
new data frame
return standard errors of fit?
All tidying methods return a data.frame without rownames.
The structure depends on the method chosen.
When parameters = NA, the effects argument is used
to determine which parameters to summarize.
Generally, tidy.brmsfit returns
one row for each coefficient, with at least three columns:
The name of the model parameter.
A point estimate of the coefficient (mean or median).
A standard error for the point estimate (sd or mad).
When effects = "fixed", only population-level
effects are returned.
When effects = "ran_vals", only group-level effects are returned.
In this case, two additional columns are added:
The name of the grouping factor.
The name of the level of the grouping factor.
Specifying effects = "ran_pars" selects the
standard deviations and correlations of the group-level parameters.
If conf.int = TRUE, columns for the lower and
upper bounds of the posterior conf.int computed.
The names ‘fixed’, ‘ran_pars’, and ‘ran_vals’ (corresponding to "non-varying", "hierarchical", and "varying" respectively in previous versions of the package), while technically inappropriate in a Bayesian setting where "fixed" and "random" effects are not well-defined, are used for compatibility with other (frequentist) mixed model types.
At present, the components of parameter estimates are separated by parsing the column names of as_draws (e.g. r_patient[1,Intercept] for the random effect on the intercept for patient 1, or b_Trt1 for the fixed effect Trt1. We try to detect underscores in parameter names and warn, but detection may be imperfect.
## original model
if (FALSE) { # \dontrun{
brms_crossedRE <- brm(mpg ~ wt + (1|cyl) + (1+wt|gear), data = mtcars,
iter = 500, chains = 2)
} # }
# \donttest{
## too slow for CRAN (>5 seconds)
## load stored object
load(system.file("extdata", "brms_example.rda", package="broom.mixed"))
fit <- brms_crossedRE
tidy(fit)
#> Loading required package: rstan
#> Loading required package: StanHeaders
#> Error: package or namespace load failed for ‘rstan’ in .doLoadActions(where, attach):
#> error in load action .__A__.1 for package rstan: Rcpp::loadModule(module = "class_model_base", what = TRUE, env = ns, : Unable to load module "class_model_base": attempt to apply non-function
#> Error in .requirePackage(package): unable to find required package ‘rstan’
tidy(fit, parameters = "^sd_", conf.int = FALSE)
#> # A tibble: 3 × 4
#> component term estimate std.error
#> <chr> <chr> <dbl> <dbl>
#> 1 cond sd_cyl__(Intercept) 3.04 1.99
#> 2 cond sd_gear__(Intercept) 3.91 3.46
#> 3 cond sd_gear__wt 1.38 0.922
tidy(fit, effects = "fixed", conf.method="HPDinterval")
#> # A tibble: 2 × 7
#> effect component term estimate std.error conf.low conf.high
#> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 fixed cond (Intercept) 33.4 3.31 28.3 41.1
#> 2 fixed cond wt -4.24 1.04 -6.31 -2.57
tidy(fit, effects = "ran_vals")
#> # A tibble: 9 × 9
#> effect component group level term estimate std.error conf.low conf.high
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 ran_vals cond cyl 4 (Interce… 1.97 1.83 -1.72 6.39
#> 2 ran_vals cond cyl 6 (Interce… -0.584 1.76 -4.89 2.22
#> 3 ran_vals cond cyl 8 (Interce… -1.85 1.85 -6.86 0.840
#> 4 ran_vals cond gear 3 (Interce… -2.47 3.43 -10.8 1.35
#> 5 ran_vals cond gear 4 (Interce… 1.89 2.77 -2.52 8.40
#> 6 ran_vals cond gear 5 (Interce… 0.684 2.51 -3.36 6.73
#> 7 ran_vals cond gear 3 wt 0.839 1.12 -0.634 3.45
#> 8 ran_vals cond gear 4 wt -0.407 1.02 -2.66 1.24
#> 9 ran_vals cond gear 5 wt -0.471 1.06 -2.83 1.34
tidy(fit, effects = "ran_pars", robust = TRUE)
#> # A tibble: 5 × 8
#> effect component group term estimate std.error conf.low conf.high
#> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 ran_pars cond cyl sd__(Interc… 2.60 1.52 0.834 8.76
#> 2 ran_pars cond gear sd__(Interc… 2.98 2.88 0.310 12.9
#> 3 ran_pars cond gear sd__wt 1.38 0.911 0.0326 3.24
#> 4 ran_pars cond gear cor__(Inter… -0.623 0.483 -0.988 0.724
#> 5 ran_pars cond Residual sd__Observa… 2.56 0.447 1.96 3.48
if (require("posterior")) {
tidy(fit, effects = "ran_pars", rhat = TRUE, ess = TRUE)
}
#> Loading required package: posterior
#> This is posterior version 1.6.1
#>
#> Attaching package: ‘posterior’
#> The following objects are masked from ‘package:stats’:
#>
#> mad, sd, var
#> The following objects are masked from ‘package:base’:
#>
#> %in%, match
#> # A tibble: 5 × 10
#> effect component group term estimate std.error conf.low conf.high rhat ess
#> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 ran_p… cond cyl sd__… 3.04 1.99 0.834 8.76 1.01 79.5
#> 2 ran_p… cond gear sd__… 3.91 3.46 0.310 12.9 1.01 50.8
#> 3 ran_p… cond gear sd__… 1.38 0.922 0.0326 3.24 1.02 38.4
#> 4 ran_p… cond gear cor_… -0.455 0.508 -0.988 0.724 1.02 61.5
#> 5 ran_p… cond Resi… sd__… 2.57 0.393 1.96 3.48 1.03 69.4
# glance method
glance(fit)
#> # A tibble: 1 × 4
#> algorithm pss nobs sigma
#> <chr> <dbl> <int> <dbl>
#> 1 sampling 100 32 2.56
## this example will give a warning that it should be run with
## reloo=TRUE; however, doing this will fail
## because the \code{fit} object has been stripped down to save space
suppressWarnings(glance(fit, looic = TRUE, cores = 1))
#> # A tibble: 1 × 7
#> algorithm pss nobs sigma looic elpd_loo p_loo
#> <chr> <dbl> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 sampling 100 32 2.56 157. -78.4 5.81
head(augment(fit))
#> # A tibble: 6 × 7
#> mpg wt cyl gear .fitted .se.fit .resid
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 2.62 6 4 22.7 2.87 -1.41
#> 2 21 2.88 6 4 21.6 3.10 -0.534
#> 3 22.8 2.32 4 4 26.3 2.86 -2.77
#> 4 21.4 3.22 6 3 19.9 2.88 2.27
#> 5 18.7 3.44 8 3 16.9 2.66 1.17
#> 6 18.1 3.46 6 3 18.4 2.87 -0.425
# }