
Extract a model into the apabayes tidy contract
Source:R/extract-bayesfactor.R, R/extract-blavaan.R, R/extract-brms.R, and 8 more
apa_tidy.Rdapa_tidy() turns a fitted model, a set of posterior draws or a
comparison object into an apabayes_tidy table: fixed column names,
the numbers computed by easystats, no formatting. It is the whole of
the extract layer; everything apabayes reports is built from its
output.
Usage
# S3 method for class 'BFBayesFactor'
apa_tidy(x, ...)
# S3 method for class 'BFBayesFactorList'
apa_tidy(x, ...)
# S3 method for class 'BFmcmc'
apa_tidy(x, ...)
# S3 method for class 'bayesfactor_inclusion'
apa_tidy(x, ...)
# S3 method for class 'blavaan'
apa_tidy(
x,
variables = NULL,
labels = NULL,
component = "all",
standardize = FALSE,
centrality = c("median", "mean"),
ci = c("eti", "hdi"),
ci_level = 0.95,
rope = NULL,
rope_ci = 0.95,
diagnostics = TRUE,
...
)
# S3 method for class 'brmsfit'
apa_tidy(
x,
variables = NULL,
labels = NULL,
effects = c("fixed", "all", "random"),
component = "all",
centrality = c("median", "mean"),
ci = c("eti", "hdi"),
ci_level = 0.95,
rope = NULL,
rope_ci = 0.95,
diagnostics = TRUE,
...
)
# S3 method for class 'brmshypothesis'
apa_tidy(x, directional = NULL, ...)
# S3 method for class 'compare.loo'
apa_tidy(x, weights = NULL, reference = NULL, ...)
# S3 method for class 'bayesfactor_models'
apa_tidy(x, ...)
# S3 method for class 'emmGrid'
apa_tidy(
x,
centrality = c("median", "mean"),
ci = c("hdi", "eti"),
ci_level = 0.95,
rope = NULL,
rope_ci = 0.95,
...
)
# S3 method for class 'emm_list'
apa_tidy(x, ...)
# S3 method for class 'easycorrelation'
apa_tidy(x, centrality = c("median", "mean"), ci = c("hdi", "eti"), ...)
apa_tidy(x, ...)
# Default S3 method
apa_tidy(x, ...)
# S3 method for class 'runjags'
apa_tidy(x, ...)
# S3 method for class 'draws'
apa_tidy(
x,
variables = NULL,
labels = NULL,
centrality = c("median", "mean"),
ci = c("eti", "hdi"),
ci_level = 0.95,
rope = NULL,
rope_ci = 0.95,
diagnostics = TRUE,
...
)
# S3 method for class 'lavaan'
apa_tidy(
x,
variables = NULL,
labels = NULL,
component = "all",
standardize = FALSE,
ci_level = 0.95,
...
)
# S3 method for class 'estimate_contrasts'
apa_tidy(x, centrality = NULL, ci = NULL, ...)
# S3 method for class 'estimate_means'
apa_tidy(x, centrality = NULL, ci = NULL, ...)
# S3 method for class 'parameters_model'
apa_tidy(x, ...)
# S3 method for class 'describe_posterior'
apa_tidy(x, variables = NULL, labels = NULL, centrality = NULL, ...)
# S3 method for class 'stanreg'
apa_tidy(
x,
variables = NULL,
labels = NULL,
effects = c("fixed", "all", "random"),
component = "all",
centrality = c("median", "mean"),
ci = c("eti", "hdi"),
ci_level = 0.95,
rope = NULL,
rope_ci = 0.95,
diagnostics = TRUE,
...
)Arguments
- x
The object to extract.
- ...
Passed to the method.
- variables
Character vector of draws variables to report, in the order given, or
NULLfor every variable that is not internal. A variable is internal when its name ends in__(the Stan convention for sampler quantities such aslp__), is exactlylprior, or begins withprior_(the brms log-prior and prior-draw variables, present whenever a fit was sampled withsample_prior = "yes"). The rule is a default, not a filter: naming a variable invariablesreports it, andvariables = posterior::variables(x)reports everything.- labels
Named character vector of display labels, e.g.
c(b_wt = "Weight"). Draws objects carry no formula, solabelequalstermfor every variable you do not name.- component
Which model component to report, passed to
parameters::model_parameters()."all"is the easystats default.- standardize
FALSEfor the unstandardized solution,TRUEfor the completely standardized one ("std.all"), or one of"std.all","std.lv"and"std.nox"as inlavaan::standardizedSolution(). Thestdcolumn records which.- centrality
"median"(the default ofparametersfor brms and blavaan) or"mean". On the result-object method it defaults toNULL, meaning the centrality the table already holds: there is nothing left to choose, and a table computed withcentrality = "all"must be told which of the two to report.- ci
"eti", the equal-tailed interval reported as CrI, or"hdi", the highest-density interval.- ci_level
The interval mass, a number strictly between 0 and 1.
- rope
NULL, or the two bounds of a region of practical equivalence. The ROPE percentage is opt-in; the bounds are kept in therope_rangeattribute so a table note can state them.- rope_ci
The share of the posterior the ROPE percentage is computed on, passed to bayestestR as
rope_ci;1uses the whole posterior, the default0.95the central 95%.- diagnostics
FALSEleavesrhat,ess_bulkandess_tailasNAinstead of computing them.- effects
Which parameters to report, passed to
parameters::model_parameters():"fixed"(the easystats default, population-level parameters and the distributional ones such assigma),"all"(adds the group-level standard deviations and correlations) or"random". Asking for"random"from a model that has no random effects is an error, decided byinsight::is_mixed_model()before easystats is called.- directional
NULLto read fromxwhich rows test a directional hypothesis, or a logical vector of length 1 or one per row saying so.brms::hypothesis()records the operator only in theHypothesisstring, and replaces that string with the user's name when the hypothesis was named, so the derivation reads the interval instead: brms uses the quantiles atalphaand1 - alphafor a directional hypothesis and atalpha / 2and1 - alpha / 2for a point one. A row where neither rule fits, or both (a posterior with no spread), is an error rather than a missing Bayes factor.- weights
NULL, or model weights to report in theweightcolumn: the result ofloo::loo_model_weights()(its kind,"stacking","pseudo-BMA+"or"pseudo-BMA", becomes theweight_methodattribute), or a numeric vector named by model. The weights are matched to rows by model name, never by position, and must name exactly the models ofx. The comparison object carries no weights;performance::compare_performance()'sLOOIC_wtis the stacking weight.- reference
NULLfor loo's own reference — the model in the first row, the one with the highest ELPD, against whichloo::loo_compare()signs every difference — or the name of another model ofxto take the differences from, so that a model behind the reference prints a positiveelpd_diff. Naming loo's own reference changes nothing: the object's numbers are returned untouched.Under another reference
elpd_diffbecomeselpdminus the reference'selpd— a difference of two numbers loo reported, computed by apabayes, which is why it is named here — and the standard error of the difference survives only where loo measured it:0on the reference row, loo's ownse_diffon the model that was loo's reference (the same pair, read the other way round), andNAon every other row, because acompare.looobject does not carry the pointwise ELPDs another pair would need. Runloo::loo_compare()on two models to get that standard error.p_worseanddiag_diffqualify a difference against loo's reference and becomeNAfor the same reason; every column that describes a model rather than a pair is unchanged, as is the row order.
Value
An apabayes_tidy tibble.
Methods (by class)
apa_tidy(BFBayesFactor): ABFBayesFactorobject from BayesFactor (BayesFactor::anovaBF(),BayesFactor::lmBF(),BayesFactor::ttestBF(),BayesFactor::correlationBF(), ...), as abf_modelstable read from the object: the denominator model first, then every numerator in the object's order, each named by BayesFactor's short name (model, made unique as BayesFactor names its rows:"wt","wt #1") and long name (name).log_bfis BayesFactor's own natural-log Bayes factor,bfits exponential,errorits proportional numerical error (NAon the denominator row and where BayesFactor records none, 0 for the exact families), andmethodthe prior family ("JZS (BayesFactor)","Jeffreys-beta* (BayesFactor)", ...).post_probis computed at equal prior odds, as on thebayesfactor_modelsroute. A numerator identical to the denominator (bf / bf[1]) is not repeated. ABFBayesFactorList(bf / bf) holds several denominators and is refused; report one column of it,x[, j]. Parameter estimates come from the draws:apa_tidy(BayesFactor::posterior(bf, iterations = ))reads aBFmcmcobject through the draws route, and takes that route's arguments.apa_tidy(BFBayesFactorList): ABFBayesFactorListis refused: it holds Bayes factors against several denominators, and a table has one.apa_tidy(BFmcmc): ABFmcmcobject fromBayesFactor::posterior()is read by the draws route, with every argument of that route. An object with several numerators needsindex =inBayesFactor::posterior().apa_tidy(bayesfactor_inclusion): The output ofbayestestR::bayesfactor_inclusion(), as abf_inclusiontable: one row perterm, with the prior and posterior inclusion probabilities (p_prior,p_posterior), the inclusion Bayes factorbfand its loglog_bf. A term in every model has no inclusion Bayes factor (NA, where bayestestR hasNaN); a posterior inclusion probability that rounds to 1 gives an infinite one, which is kept. Theaveragingattribute says whether the models were averaged over"all"or"matched"models,prior_oddswhether the prior odds were"equal"or"custom"(the values inprior_odds_values). bayestestR does not record how the models' Bayes factors were computed, sobf_methodisNA.apa_tidy(blavaan): Ablavaanfit fromblavaan::bcfa(),blavaan::bsem()orblavaan::bgrowth(). Estimates, the credible interval and pd come fromparameters::model_parameters(), which reports the free parameters only — the fixed marker loading is absent, andpisNAthroughout.termis blavaan's own parameter name (visual=~x1,x1~~x1) andlabelthe same with spaces. Herecomponentis"all","latent"or"residual", blavaan's own vocabulary rather than lavaan's six names.R-hat and both ESS columns come from
posterior::summarise_draws()overblavaan::blavInspect(x, "mcmc"), and blavaan's own are never computed. They therefore differ from whatblavaan::summary()prints, which reportsblavInspect(x, "rhat")and"neff": one ESS where the contract has two, from an estimator that is neither the bulk nor the tail ESS the "greater than 400" rule of thumb is defined for.standardizereadsblavaan::standardizedPosterior()instead of easystats, which cannot standardize a blavaan fit at all. The standardized solution covers the whole parameter table, so its rows are a superset of the unstandardized ones: the fixed markers appear, with a real interval, and under"std.all"the latent variances are exactly 1. Those rows carry no component information, socomponentisNAthere and cannot be combined withstandardize; select rows withvariablesinstead. A multi-group fit is refused.apa_tidy(brmsfit): Abrmsfit, and by inheritance abmmfit. Estimates, interval, pd and the ROPE percentage come fromparameters::model_parameters(), R-hat and both ESS columns frombayestestR::diagnostic_posterior(); apabayes computes no summary of its own. A bmm model parameter is a brms distributional parameter, socomponentcarries it (drift,kappa, ...).One shape is out of reach upstream: a model whose response is a matrix with a
trials()term (family = multinomial(), which is every bmm M3 fit) fails insideinsight::get_data()on R 4.3 or newer, before any number is computed. The method then aborts namingapa_tidy(brms::as_draws_df(fit)), which reads the draws directly;apa_tidy_diagnostics()andapa_convergence()do not go through easystats and are unaffected.apa_tidy(brmshypothesis): The output ofbrms::hypothesis(). Estimates, interval, evidence ratio and posterior probability are brms's own; apabayes addsbf10(1 / evid_ratiofor a point hypothesis, the evidence ratio itself for a directional one) anddirectional. easystats has no method for this class, so no easystats function is called, andpackage_versionscreditsbrmsalone.apa_tidy(compare.loo): The output ofloo::loo_compare(), of either shape: the data frame loo 2.10.0 and later return, or the matrix of earlier versions. One row per model, best first, with loo's own numbers: the ELPD difference to the best model and its standard error, the model's ELPD,p_looand LOOIC. No loo function is called;loomust be installed to record its version. apabayes never turns an ELPD difference into a Bayes factor or a weight into evidence (ARCHITECTURE decision 17).apa_tidy(bayesfactor_models): The output ofbayestestR::bayesfactor_models().bfisexp(log_BF), kept next tolog_bfbecause the exponential overflows above a log Bayes factor of about 709;denominatormarks the row every Bayes factor is taken against;methodis how they were computed (bridge sampling, the BIC approximation, BayesFactor's JZS).post_probis the posterior probability of each model under equal prior odds, the assumption theprior_oddsattribute records.modelis the model as bayestestR names it (the formula's right-hand side for most classes) and the extra columnnamethe argument it was passed as.apa_tidy(emmGrid): AnemmGridfromemmeans::emmeans()oremmeans::contrast()on a Bayesian fit: one row per grid row, the contrast string or, on a grid of marginal means, the row's label as emmeans writes it (cyl_f4,cyl_f4 auto), in thecontrastcolumn. The estimate, interval, pd and ROPE share come fromparameters::model_parameters().ci = "hdi", the default, is the highest-density intervalsummary(emmGrid)prints (emmeans calls it HPD; the numbers are identical) and is labelled HDI;ci = "eti"gives the equal-tailed interval. Abyvariable's values go into thegroupcolumn and its name into thebyattribute; every grid variable is kept as an extra column. The grid is reported as it is: subset it with[first to report fewer rows. A grid without posterior draws (from a frequentist fit) is refused, and so is anemm_list(emmeans(fit, pairwise ~ f)), which holds two tables of different kinds — pass one of its parts.apa_tidy(emm_list): Anemm_list, whichemmeans::emmeans()returns for a two-sided formula (pairwise ~ f), is refused: it holds a table of means and a table of contrasts, and one tidy table reports one kind. Passx$emmeansorx$contrasts.apa_tidy(easycorrelation): A table of Bayesian correlations fromcorrelation::correlation()orcorrelation::cor_test()withbayesian = TRUE, as acorrelationstable: one row per pair, namedvar1~~var2, with the posterior estimate of the correlation, its interval, pd, the ROPE share, the Bayes factor and the pairwise n, all read from the table. The table does not record which interval or which centrality it holds, sociandcentralitymust name what it was computed with; they default to correlation's own defaults (bayesian_ci_method = "hdi", the median) and apabayes cannot check them. A table computed withcentrality = "all"has norhocolumn and is read from the columncentralitynames. correlation writes itsciargument into the table without applying it (the bounds are 95 % whateverciwas), so a table labelled with any other level is refused. The ROPE share is reported without bounds, which the table does not carry. Frequentist tables and tables with diagonal rows (redundant = TRUE) are refused.apa_tidy(default): Anythingposterior::as_draws_df()accepts —stanfit,CmdStanFit,mcmc,mcmc.list, a draws matrix or a data frame of draws — is converted and handed to thedrawsmethod. A data frame, tibble (grouped or not) or data.table counts as draws only when its columns are all numeric; a summary table (one with a class of its own, or with a label column) is refused rather than read as draws. The class of the object you passed is kept as thesource_classattribute.apa_tidy(runjags): Arunjagsobject keeps its chains in$mcmc;posteriorhas no method for the object itself.apa_tidy(draws): Posterior draws. Estimates, interval, pd and the ROPE percentage come frombayestestR::describe_posterior(), R-hat and ESS fromposterior::summarise_draws(); apabayes computes no summary of its own.apa_tidy(lavaan): Alavaanfit fromlavaan::cfa(),lavaan::sem()orlavaan::growth(). Estimates, standard errors, the confidence interval and the p value come fromparameters::model_parameters(), which readslavaan::parameterEstimates()or, withstandardize,lavaan::standardizedSolution().termis lavaan's own parameter name (visual=~x1,x1~~x1,dem60~ind60,x1~1;.g2appended in the second group of a multi-group fit) andlabelthe same with spaces. Herecomponentis"all"or one or more of"loading","regression","correlation","variance","mean"and"defined".centralityisNAon the result — the estimate is a maximum-likelihood point estimate — andci_methodis"wald", or"boot"(percentile bootstrap) for the unstandardized solution of a fit withse = "bootstrap". A fixed parameter (the marker loading) hasp = NA, as lavaan reports it. Ablavaanfit is refused: this method reports maximum-likelihood fits.apa_tidy(estimate_contrasts): A table of contrasts frommodelbased::estimate_contrasts()on a Bayesian fit, as acontraststable. A row is named as modelbased names it,Level1 - Level2("6 - 4", or"6, auto - 4, auto"for two variables), or by itsParameterfor a custom comparison; abyvariable's values go into thegroupcolumn. The numbers are read from the table, not recomputed, and so is the ROPE share with its bounds when the table has one. The table records its interval only in its call: with noci_methodthere it is modelbased's default equal-tailed interval, otherwise the one named. When the call givesci_methodas a variable, or the table has lost its call, name the interval withci = "eti"orci = "hdi"; acithat contradicts the call is refused. Tables computed withbackend = "emmeans"are refused, since their estimate column does not say which centrality it holds: pass the emmeans grid toapa_tidy()instead.apa_tidy(estimate_means): A table of marginal means frommodelbased::estimate_means()on a Bayesian fit, as acontraststable whose rows are named by the values of theirbyvariables ("4","4, auto"). Everything else is read as forestimate_contraststables.apa_tidy(parameters_model): Aparameters_modeltable fromparameters::model_parameters(). A table of a Bayesian model inheritsdescribe_posteriorand is handled by that method; a table that does not — a frequentist model, or one computed with severalcilevels — is refused here rather than coerced to draws.apa_tidy(describe_posterior): Adescribe_posteriortable frombayestestR::describe_posterior(), or fromparameters::model_parameters()on a Bayesian model. The numbers are read from the table, soci,ci_level,ropeanddiagnosticsare not arguments: they were settled when the table was computed, andci_methodand the interval level are read from it.centrality = NULLreports whichever of the median and the mean the table holds, and asks you to name one when it holds both.rhatandess_tailare read when present;ess_bulkis alwaysNA, because no easystats table carries it — pass the fit for both ESS columns.bfisexp(log_BF)when the table was computed withtest = "bf". Thepackage_versionsattribute records the versions installed whenapa_tidy()ran, not necessarily those that computed the table.apa_tidy(stanreg): Astanregobject from rstanarm. Estimates, interval, pd and the ROPE percentage come fromparameters::model_parameters()withpriors = FALSE; R-hat and both ESS columns fromposterior::summarise_draws()over the fit's draws, which cover every reported parameter includingsigma. apabayes computes no summary of its own.
See also
apabayes_tidy() for the contract.
Examples
# Deterministic draws, so the example does not depend on RNG state:
# normal quantiles, reordered by a fixed rule so the chain is not
# monotone and the diagnostics are meaningful.
z <- stats::qnorm(stats::ppoints(400))
z <- z[order(sin(seq_along(z)))]
draws <- posterior::as_draws_df(
data.frame(mu = 2 + z, sigma = exp(0.3 * z))
)
apa_tidy(draws)
#> # apabayes tidy table: parameters (2 rows)
#> # median, 95% CrI (equal-tailed); source: draws_df
#> # A tibble: 2 × 18
#> term label estimate ci_low ci_high ci_method ci_level pd rope_pct rhat
#> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 mu mu 2 0.0599 3.94 eti 0.95 0.978 NA 0.998
#> 2 sigma sigma 1.00 0.559 1.79 eti 0.95 1 NA 0.998
#> # ℹ 8 more variables: ess_bulk <dbl>, ess_tail <dbl>, bf <dbl>,
#> # component <chr>, group <chr>, effects <chr>, std <lgl>, p <dbl>
apa_tidy(draws, variables = "mu", ci = "hdi", ci_level = 0.9)
#> # apabayes tidy table: parameters (1 row)
#> # median, 90% HDI; source: draws_df
#> # A tibble: 1 × 18
#> term label estimate ci_low ci_high ci_method ci_level pd rope_pct rhat
#> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 mu mu 2 0.343 3.63 hdi 0.9 0.978 NA 0.998
#> # ℹ 8 more variables: ess_bulk <dbl>, ess_tail <dbl>, bf <dbl>,
#> # component <chr>, group <chr>, effects <chr>, std <lgl>, p <dbl>
# An easystats table you already computed is reported as it stands:
# the interval, its method and the ROPE are read off the object, not
# recomputed, so those are not arguments here.
apa_tidy(bayestestR::describe_posterior(draws))
#> # apabayes tidy table: parameters (2 rows)
#> # median, 95% CrI (equal-tailed); source: describe_posterior
#> # A tibble: 2 × 18
#> term label estimate ci_low ci_high ci_method ci_level pd rope_pct rhat
#> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 mu mu 2 0.0599 3.94 eti 0.95 0.978 0.00263 NA
#> 2 sigma sigma 1.00 0.559 1.79 eti 0.95 1 0 NA
#> # ℹ 8 more variables: ess_bulk <dbl>, ess_tail <dbl>, bf <dbl>,
#> # component <chr>, group <chr>, effects <chr>, std <lgl>, p <dbl>
# A lavaan fit: a maximum-likelihood estimate with a Wald interval and
# a p value, so `centrality` is NA and `ci_method` is "wald".
fit <- lavaan::cfa(
"visual =~ x1 + x2 + x3",
data = lavaan::HolzingerSwineford1939
)
apa_tidy(fit, component = "loading", standardize = TRUE)
#> # apabayes tidy table: parameters (3 rows)
#> # 95% CI (Wald); source: lavaan
#> # A tibble: 3 × 18
#> term label estimate ci_low ci_high ci_method ci_level pd rope_pct rhat
#> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 visual=… visu… 0.621 0.489 0.753 wald 0.95 NA NA NA
#> 2 visual=… visu… 0.479 0.356 0.602 wald 0.95 NA NA NA
#> 3 visual=… visu… 0.710 0.570 0.850 wald 0.95 NA NA NA
#> # ℹ 8 more variables: ess_bulk <dbl>, ess_tail <dbl>, bf <dbl>,
#> # component <chr>, group <chr>, effects <chr>, std <lgl>, p <dbl>
# `brms::hypothesis()` reads a plain data frame of draws as well as a
# fitted model, so the hypothesis route needs no Stan here. The first
# row is directional (a 90% interval, `bf10` the posterior odds), the
# second a point hypothesis whose evidence ratio needs prior draws
# this data frame does not carry.
q <- stats::qnorm(stats::ppoints(400))
draws <- data.frame(b_wt = -5 + q, b_am = 0.2 + 2 * q)
apa_tidy(brms::hypothesis(draws, c("b_wt < 0", "b_am = 0")))
#> # apabayes tidy table: hypotheses (2 rows)
#> # mean; source: brmshypothesis
#> # A tibble: 2 × 11
#> hypothesis group estimate ci_low ci_high ci_method ci_level evid_ratio
#> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
#> 1 (b_wt) < 0 NA -5 -6.63 -3.37 eti 0.9 Inf
#> 2 (b_am) = 0 NA 0.2 -3.68 4.08 eti 0.95 NA
#> # ℹ 3 more variables: post_prob <dbl>, bf10 <dbl>, directional <lgl>
# An emmeans grid from a Bayesian fit carries the coefficient draws;
# `emmeans::qdrg()` builds one from a draws matrix directly, which is
# what a brms or rstanarm fit would supply. The interval is the HDI,
# emmeans's own HPD interval; `ci = "eti"` asks for the equal-tailed one.
q <- stats::qnorm(stats::ppoints(400))
coefs <- cbind(
`(Intercept)` = 26.7 + q, cyl_f6 = -6.9 + 1.5 * q,
cyl_f8 = -11.6 + 1.3 * q
)
cars <- transform(mtcars, cyl_f = factor(cyl))
grid <- emmeans::qdrg(~cyl_f, data = cars, mcmc = coefs)
means <- emmeans::emmeans(grid, ~cyl_f)
apa_tidy(means)
#> # apabayes tidy table: contrasts (3 rows)
#> # median, 95% HDI; source: emmGrid
#> # A tibble: 3 × 10
#> contrast group estimate ci_low ci_high ci_method ci_level pd rope_pct cyl_f
#> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <chr>
#> 1 cyl_f4 NA 26.7 24.8 28.7 hdi 0.95 1 NA 4
#> 2 cyl_f6 NA 19.8 15.0 24.8 hdi 0.95 1 NA 6
#> 3 cyl_f8 NA 15.1 10.6 19.7 hdi 0.95 1 NA 8
apa_tidy(emmeans::contrast(means, "pairwise"), rope = c(-1, 1))
#> # apabayes tidy table: contrasts (3 rows)
#> # median, 95% HDI; source: emmGrid
#> # A tibble: 3 × 9
#> contrast group estimate ci_low ci_high ci_method ci_level pd rope_pct
#> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 cyl_f4 - cyl_… NA 6.9 3.93 9.81 hdi 0.95 1 0
#> 2 cyl_f4 - cyl_… NA 11.6 9.02 14.1 hdi 0.95 1 0
#> 3 cyl_f6 - cyl_… NA 4.7 4.30 5.09 hdi 0.95 1 0
if (FALSE) { # rlang::is_installed(c("modelbased", "rstanarm", "marginaleffects", "collapse"))
# Four packages are guarded for, not one: `estimate_contrasts()` does
# its work through marginaleffects, a Suggests of modelbased rather
# than a dependency, and marginaleffects does this path's through
# collapse. Measured on CI, one run apart. The version modelbased
# asks of marginaleffects is in `DESCRIPTION`.
# A modelbased table is reported as it stands. It records its interval
# only in its call: none named there is modelbased's equal-tailed
# default, and a variable there has to be named with `ci =`.
# \donttest{
cars <- transform(mtcars, cyl_f = factor(cyl))
fit <- rstanarm::stan_glm(
mpg ~ wt + cyl_f,
data = cars, chains = 2, iter = 1000, seed = 1, refresh = 0
)
apa_tidy(modelbased::estimate_contrasts(fit, contrast = "cyl_f"))
method <- "hdi"
means <- modelbased::estimate_means(fit, by = "cyl_f", ci_method = method)
apa_tidy(means, ci = "hdi")
# }
}
# Bayesian correlations. The table records neither its interval nor
# its centrality, so `ci` and `centrality` name them; the defaults are
# correlation's own.
r <- correlation::correlation(
mtcars[, c("mpg", "wt", "hp")],
bayesian = TRUE
)
apa_tidy(r)
#> # apabayes tidy table: correlations (3 rows)
#> # median, 95% HDI; source: easycorrelation
#> # A tibble: 3 × 17
#> term var1 var2 group estimate ci_low ci_high ci_method ci_level pd
#> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
#> 1 mpg~~wt mpg wt NA -0.812 -0.907 -0.661 hdi 0.95 1
#> 2 mpg~~hp mpg hp NA -0.709 -0.851 -0.514 hdi 0.95 1
#> 3 wt~~hp wt hp NA 0.586 0.369 0.774 hdi 0.95 1
#> # ℹ 7 more variables: rope_pct <dbl>, bf <dbl>, n <dbl>, method <chr>,
#> # prior_distribution <chr>, prior_location <dbl>, prior_scale <dbl>
r_eti <- correlation::correlation(
mtcars[, c("mpg", "wt")],
bayesian = TRUE, bayesian_ci_method = "eti"
)
apa_tidy(r_eti, ci = "eti")
#> # apabayes tidy table: correlations (1 row)
#> # median, 95% CrI (equal-tailed); source: easycorrelation
#> # A tibble: 1 × 17
#> term var1 var2 group estimate ci_low ci_high ci_method ci_level pd
#> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
#> 1 mpg~~wt mpg wt NA -0.820 -0.905 -0.667 eti 0.95 1
#> # ℹ 7 more variables: rope_pct <dbl>, bf <dbl>, n <dbl>, method <chr>,
#> # prior_distribution <chr>, prior_location <dbl>, prior_scale <dbl>
# A BayesFactor object is read as a table of model comparisons, with
# BayesFactor's own names, prior family and numerical error; its
# inclusion Bayes factors come from bayestestR; its estimates from the
# draws of `BayesFactor::posterior()`.
cars <- transform(mtcars, am_f = factor(am), vs_f = factor(vs))
bf <- BayesFactor::anovaBF(mpg ~ am_f * vs_f, data = cars, progress = FALSE)
apa_tidy(bf)
#> # apabayes tidy table: bf_models (5 rows)
#> # source: BFBayesFactor
#> # A tibble: 5 × 8
#> model bf log_bf denominator method post_prob error name
#> <chr> <dbl> <dbl> <lgl> <chr> <dbl> <dbl> <chr>
#> 1 Intercept only 1 e0 0 TRUE JZS (… 3.13e-6 NA Inte…
#> 2 am_f 8.66e1 4.46 FALSE JZS (… 2.71e-4 1.88e-8 mpg …
#> 3 vs_f 5.29e2 6.27 FALSE JZS (… 1.66e-3 1.46e-9 mpg …
#> 4 am_f + vs_f 1.93e5 12.2 FALSE JZS (… 6.05e-1 9.60e-3 mpg …
#> 5 am_f + vs_f + am_f:… 1.26e5 11.7 FALSE JZS (… 3.93e-1 3.44e-2 mpg …
apa_tidy(bayestestR::bayesfactor_inclusion(bf))
#> # apabayes tidy table: bf_inclusion (3 rows)
#> # source: bayestestRBF
#> # A tibble: 3 × 5
#> term p_prior p_posterior bf log_bf
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 am_f 0.6 0.998 401. 5.99
#> 2 vs_f 0.6 1.000 2430. 7.80
#> 3 am_f:vs_f 0.2 0.393 2.59 0.953
bf_t <- BayesFactor::ttestBF(formula = mpg ~ am_f, data = cars)
apa_tidy(BayesFactor::posterior(bf_t, iterations = 500, progress = FALSE))
#> # apabayes tidy table: parameters (5 rows)
#> # median, 95% CrI (equal-tailed); source: BFmcmc
#> # A tibble: 5 × 18
#> term label estimate ci_low ci_high ci_method ci_level pd rope_pct rhat
#> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 mu mu 20.8 18.8 22.4 eti 0.95 1 NA 1.000
#> 2 beta (… beta… -6.62 -10.3 -2.50 eti 0.95 0.998 NA 0.999
#> 3 sig2 sig2 25.4 15.8 45.4 eti 0.95 1 NA 1.00
#> 4 delta delta -1.30 -2.14 -0.433 eti 0.95 0.998 NA 1.00
#> 5 g g 1.60 0.211 38.8 eti 0.95 1 NA 0.998
#> # ℹ 8 more variables: ess_bulk <dbl>, ess_tail <dbl>, bf <dbl>,
#> # component <chr>, group <chr>, effects <chr>, std <lgl>, p <dbl>