Skip to content

Commit

Permalink
rd cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jul 2, 2023
1 parent 9b8fe34 commit fe68024
Show file tree
Hide file tree
Showing 29 changed files with 314 additions and 283 deletions.
14 changes: 7 additions & 7 deletions R/bayesfactor_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#' @param ... Fitted models (see details), all fit on the same data, or a single
#' `BFBayesFactor` object (see 'Details'). Ignored in `as.matrix()`,
#' `update()`. If the following named arguments are present, they are passed
#' to [insight::get_loglikelihood] (see details):
#' to [`insight::get_loglikelihood()`] (see details):
#' - `estimator` (defaults to `"ML"`)
#' - `check_response` (defaults to `FALSE`)
#' @param denominator Either an integer indicating which of the models to use as
#' the denominator, or a model to be used as a denominator. Ignored for
#' `BFBayesFactor`.
#' @param object,x A [bayesfactor_models()] object.
#' @param object,x A [`bayesfactor_models()`] object.
#' @param subset Vector of model indices to keep or remove.
#' @param reference Index of model to reference to, or `"top"` to
#' reference to the best model, or `"bottom"` to reference to the worst
Expand All @@ -26,9 +26,9 @@
#' implemented in the \href{https://easystats.github.io/see/}{\pkg{see}-package}.
#'
#' @details
#' If the passed models are supported by \pkg{insight} the DV of all models will be tested for equality
#' (else this is assumed to be true), and the models' terms will be extracted (allowing for follow-up
#' analysis with `bayesfactor_inclusion`).
#' If the passed models are supported by **insight** the DV of all models will
#' be tested for equality (else this is assumed to be true), and the models'
#' terms will be extracted (allowing for follow-up analysis with `bayesfactor_inclusion`).
#'
#' - For `brmsfit` or `stanreg` models, Bayes factors are computed using the \CRANpkg{bridgesampling} package.
#' - `brmsfit` models must have been fitted with `save_pars = save_pars(all = TRUE)`.
Expand All @@ -44,9 +44,9 @@
#' testing is substantially larger than for estimation (the default of 4000
#' samples may not be enough in many cases). A conservative rule of thumb is to
#' obtain 10 times more samples than would be required for estimation
#' (\cite{Gronau, Singmann, & Wagenmakers, 2017}). If less than 40,000 samples
#' (_Gronau, Singmann, & Wagenmakers, 2017_). If less than 40,000 samples
#' are detected, `bayesfactor_models()` gives a warning.
#' \cr \cr
#'
#' See also [the Bayes factors vignette](https://easystats.github.io/bayestestR/articles/bayes_factors.html).
#'
#' @inheritSection bayesfactor_parameters Interpreting Bayes Factors
Expand Down
58 changes: 24 additions & 34 deletions R/ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@
#' (SI) for Bayesian and frequentist models. The Documentation is accessible
#' for:
#'
#' \itemize{
#' \item [Bayesian models](https://easystats.github.io/bayestestR/articles/credible_interval.html)
#' \item [Frequentist models](https://easystats.github.io/parameters/reference/ci.default.html)
#' }
#' - [Bayesian models](https://easystats.github.io/bayestestR/articles/credible_interval.html)
#' - [Frequentist models](https://easystats.github.io/parameters/reference/ci.default.html)
#'
#' @param x A `stanreg` or `brmsfit` model, or a vector representing a posterior distribution.
#' @param method Can be ['ETI'][eti] (default), ['HDI'][hdi], ['BCI'][bci], ['SPI'][spi] or ['SI'][si].
#' @param x A `stanreg` or `brmsfit` model, or a vector representing a posterior
#' distribution.
#' @param method Can be ["ETI"][eti] (default), ["HDI"][hdi], ["BCI"][bci],
#' ["SPI"][spi] or ["SI"][si].
#' @param ci Value or vector of probability of the CI (between 0 and 1)
#' to be estimated. Default to `.95` (`95%`).
#' to be estimated. Default to `0.95` (`95%`).
#' @inheritParams hdi
#' @inheritParams si
#' @inherit hdi seealso
#' @family ci
#'
#' @return A data frame with following columns:
#' \itemize{
#' \item `Parameter` The model parameter(s), if `x` is a model-object. If `x` is a vector, this column is missing.
#' \item `CI` The probability of the credible interval.
#' \item `CI_low`, `CI_high` The lower and upper credible interval limits for the parameters.
#' }
#'
#' - `Parameter` The model parameter(s), if `x` is a model-object. If `x` is a
#' vector, this column is missing.
#' - `CI` The probability of the credible interval.
#' - `CI_low`, `CI_high` The lower and upper credible interval limits for the parameters.
#'
#' @note When it comes to interpretation, we recommend thinking of the CI in terms of
#' an "uncertainty" or "compatibility" interval, the latter being defined as
#' \dQuote{Given any value in the interval and the background assumptions,
#' the data should not seem very surprising} (\cite{Gelman & Greenland 2019}).
#' \cr \cr
#' There is also a [`plot()`-method](https://easystats.github.io/see/articles/bayestestR.html) implemented in the \href{https://easystats.github.io/see/}{\pkg{see}-package}.
#' "Given any value in the interval and the background assumptions,
#' the data should not seem very surprising" (_Gelman & Greenland 2019_).
#'
#' @references Gelman A, Greenland S. Are confidence intervals better termed "uncertainty intervals"? BMJ 2019;l5381. 10.1136/bmj.l5381
#' There is also a [`plot()`-method](https://easystats.github.io/see/articles/bayestestR.html) implemented in the \href{https://easystats.github.io/see/}{\pkg{see}-package}.
#'
#' @references
#' Gelman A, Greenland S. Are confidence intervals better termed "uncertainty
#' intervals"? BMJ 2019;l5381. 10.1136/bmj.l5381
#'
#' @examplesIf require("rstanarm", quietly = TRUE)
#' library(bayestestR)
Expand Down Expand Up @@ -138,9 +139,12 @@ ci <- function(x, ...) {
)
)
} else {
insight::format_error(
"`method` should be 'ETI' (for equal-tailed interval),'HDI' (for highest density interval), 'BCI' (for bias corrected and accelerated bootstrap intervals), 'SPI' (for shortest probability interval) or 'SI' (for support interval)."
)
insight::format_error(paste0(
"`method` should be 'ETI' (for equal-tailed interval), ",
"'HDI' (for highest density interval), 'BCI' (for bias corrected and ",
"accelerated bootstrap intervals), 'SPI' (for shortest probability ",
"interval) or 'SI' (for support interval)."
))
}
}

Expand All @@ -157,7 +161,6 @@ ci.numeric <- function(x, ci = 0.95, method = "ETI", verbose = TRUE, BF = 1, ...
ci.data.frame <- ci.numeric



#' @export
ci.draws <- function(x, ci = 0.95, method = "ETI", verbose = TRUE, BF = 1, ...) {
.ci_bayesian(.posterior_draws_to_df(x), ci = ci, method = method, verbose = verbose, BF = BF, ...)
Expand Down Expand Up @@ -185,7 +188,6 @@ ci.emmGrid <- function(x, ci = NULL, ...) {
ci.emm_list <- ci.emmGrid



#' @rdname ci
#' @export
ci.sim.merMod <- function(x,
Expand All @@ -207,7 +209,6 @@ ci.sim.merMod <- function(x,
}



#' @rdname ci
#' @export
ci.sim <- function(x,
Expand All @@ -227,7 +228,6 @@ ci.sim <- function(x,
}



#' @rdname ci
#' @export
ci.stanreg <- function(x,
Expand Down Expand Up @@ -285,22 +285,17 @@ ci.brmsfit <- function(x,
)
}


#' @export
ci.stanfit <- ci.stanreg


#' @export
ci.blavaan <- ci.stanreg


#' @rdname ci
#' @export
ci.BFBayesFactor <- ci.numeric




#' @rdname ci
#' @export
ci.MCMCglmm <- function(x, ci = 0.95, method = "ETI", verbose = TRUE, ...) {
Expand Down Expand Up @@ -338,22 +333,17 @@ ci.bcplm <- function(x, ci = 0.95, method = "ETI", verbose = TRUE, ...) {
ci(insight::get_parameters(x), ci = ci, method = method, verbose = verbose, ...)
}


#' @export
ci.blrm <- ci.bcplm


#' @export
ci.mcmc <- ci.bcplm


#' @export
ci.mcmc.list <- ci.bcplm


#' @export
ci.BGGM <- ci.bcplm


#' @export
ci.get_predicted <- ci.data.frame
2 changes: 1 addition & 1 deletion R/describe_posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
#'
#' # emmeans estimates
#' # -----------------------------------------------
#' describe_posterior(suppressWarnings(emtrends(model, ~1, "wt")))
#' describe_posterior(emtrends(model, ~1, "wt"))
#' }
#'
#' # BayesFactor objects
Expand Down
4 changes: 1 addition & 3 deletions R/equivalence_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@
#' plot(test)
#'
#' library(emmeans)
#' equivalence_test(
#' suppressWarnings(emtrends(model, ~1, "wt", data = mtcars))
#' )
#' equivalence_test(emtrends(model, ~1, "wt", data = mtcars))
#'
#' library(brms)
#' model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
Expand Down
4 changes: 1 addition & 3 deletions R/estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@
#' head(estimate_density(model))
#'
#' library(emmeans)
#' head(estimate_density(suppressWarnings(
#' emtrends(model, ~1, "wt", data = mtcars)
#' )))
#' head(estimate_density(emtrends(model, ~1, "wt", data = mtcars)))
#'
#' # brms models
#' # -----------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion R/eti.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' eti(model, ci = c(0.80, 0.89, 0.95))
#'
#' library(emmeans)
#' eti(suppressWarnings(emtrends(model, ~1, "wt", data = mtcars)))
#' eti(emtrends(model, ~1, "wt", data = mtcars))
#'
#' library(brms)
#' model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
Expand Down
28 changes: 14 additions & 14 deletions R/hdi.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@
#' from each tail of the distribution and always include the median, the HDI is
#' *not* equal-tailed and therefore always includes the mode(s) of posterior
#' distributions. While this can be useful to better represent the credibility
#' mass of a distribution, the HDI also has some limitations. See [spi()] for
#' mass of a distribution, the HDI also has some limitations. See [`spi()`] for
#' details.
#' \cr \cr
#'
#' The [`95%` or `89%` Credible Intervals (CI)](https://easystats.github.io/bayestestR/articles/credible_interval.html)
#' are two reasonable ranges to characterize the uncertainty related to the estimation (see [here](https://easystats.github.io/bayestestR/articles/credible_interval.html) for a discussion about the differences between these two values).
#' \cr
#' are two reasonable ranges to characterize the uncertainty related to the
#' estimation (see [here](https://easystats.github.io/bayestestR/articles/credible_interval.html)
#' for a discussion about the differences between these two values).
#'
#' The `89%` intervals (`ci = 0.89`) are deemed to be more stable than, for
#' instance, `95%` intervals (\cite{Kruschke, 2014}). An effective sample size
#' instance, `95%` intervals (_Kruschke, 2014_). An effective sample size
#' of at least 10.000 is recommended if one wants to estimate `95%` intervals
#' with high precision (\cite{Kruschke, 2014, p. 183ff}). Unfortunately, the
#' with high precision (_Kruschke, 2014, p. 183ff_). Unfortunately, the
#' default number of posterior samples for most Bayes packages (e.g., `rstanarm`
#' or `brms`) is only 4.000 (thus, you might want to increase it when fitting
#' your model). Moreover, 89 indicates the arbitrariness of interval limits -
#' its only remarkable property is being the highest prime number that does not
#' exceed the already unstable `95%` threshold (\cite{McElreath, 2015}).
#' \cr
#' exceed the already unstable `95%` threshold (_McElreath, 2015_).
#'
#' However, `95%` has some [advantages
#' too](https://easystats.github.io/blog/posts/bayestestr_95/). For instance, it
#' shares (in the case of a normal posterior distribution) an intuitive
Expand All @@ -55,17 +57,17 @@
#' makes analyses more conservative (i.e., the probability of covering 0 is
#' larger for the `95%` CI than for lower ranges such as `89%`), which is a good
#' thing in the context of the reproducibility crisis.
#' \cr \cr
#'
#' A `95%` equal-tailed interval (ETI) has `2.5%` of the distribution on either
#' side of its limits. It indicates the 2.5th percentile and the 97.5h
#' percentile. In symmetric distributions, the two methods of computing credible
#' intervals, the ETI and the [HDI][hdi], return similar results.
#' \cr \cr
#'
#' This is not the case for skewed distributions. Indeed, it is possible that
#' parameter values in the ETI have lower credibility (are less probable) than
#' parameter values outside the ETI. This property seems undesirable as a summary
#' of the credible values in a distribution.
#' \cr \cr
#'
#' On the other hand, the ETI range does change when transformations are applied
#' to the distribution (for instance, for a log odds scale to probabilities):
#' the lower and higher bounds of the transformed distribution will correspond
Expand Down Expand Up @@ -96,9 +98,7 @@
#' bayestestR::hdi(model, ci = c(0.80, 0.90, 0.95))
#'
#' library(emmeans)
#' bayestestR::hdi(suppressWarnings(
#' emtrends(model, ~1, "wt", data = mtcars)
#' ))
#' bayestestR::hdi(emtrends(model, ~1, "wt", data = mtcars))
#'
#' library(brms)
#' model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
Expand Down
4 changes: 1 addition & 3 deletions R/p_direction.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@
#' # emmeans
#' # -----------------------------------------------
#' if (require("emmeans")) {
#' p_direction(suppressWarnings(
#' emtrends(model, ~1, "wt", data = mtcars)
#' ))
#' p_direction(emtrends(model, ~1, "wt", data = mtcars))
#' }
#'
#' # brms models
Expand Down
2 changes: 1 addition & 1 deletion R/point_estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#' # -----------------------------------------------
#' library(emmeans)
#' point_estimate(
#' suppressWarnings(emtrends(model, ~1, "wt", data = mtcars)),
#' emtrends(model, ~1, "wt", data = mtcars),
#' centrality = c("median", "MAP")
#' )
#'
Expand Down
Loading

0 comments on commit fe68024

Please sign in to comment.