Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 2, 2023
1 parent ac34535 commit ea13b35
Show file tree
Hide file tree
Showing 17 changed files with 196 additions and 219 deletions.
93 changes: 42 additions & 51 deletions R/bayesfactor_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#' random effects) and their `log(BF)`s (Use `as.numeric()` to extract the
#' non-log Bayes factors; see examples), that prints nicely.
#'
#' @examples
#' @examplesIf require("lme4") && require("BayesFactor") && require("rstanarm") && require("brms")
#' # With lm objects:
#' # ----------------
#' lm1 <- lm(mpg ~ 1, data = mtcars)
Expand All @@ -66,81 +66,72 @@
#' # bayesfactor_models(lm2, lm3, lm4, denominator = lm1) # same result
#' # bayesfactor_models(lm1, lm2, lm3, lm4, denominator = lm1) # same result
#'
#'
#' update(BFM, reference = "bottom")
#' as.matrix(BFM)
#' as.numeric(BFM)
#'
#'
#' lm2b <- lm(sqrt(mpg) ~ hp, data = mtcars)
#' # Set check_response = TRUE for transformed responses
#' bayesfactor_models(lm2b, denominator = lm2, check_response = TRUE)
#'
#' \donttest{
#' # With lmerMod objects:
#' # ---------------------
#' if (require("lme4")) {
#' lmer1 <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris)
#' lmer2 <- lmer(Sepal.Length ~ Petal.Length + (Petal.Length | Species), data = iris)
#' lmer3 <- lmer(Sepal.Length ~ Petal.Length + (Petal.Length | Species) + (1 | Petal.Width),
#' data = iris
#' )
#' bayesfactor_models(lmer1, lmer2, lmer3,
#' denominator = 1,
#' estimator = "REML"
#' )
#' }
#' lmer1 <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris)
#' lmer2 <- lme4::lmer(Sepal.Length ~ Petal.Length + (Petal.Length | Species), data = iris)
#' lmer3 <- lme4::lmer(
#' Sepal.Length ~ Petal.Length + (Petal.Length | Species) + (1 | Petal.Width),
#' data = iris
#' )
#' bayesfactor_models(lmer1, lmer2, lmer3,
#' denominator = 1,
#' estimator = "REML"
#' )
#'
#' # rstanarm models
#' # ---------------------
#' # (note that a unique diagnostic_file MUST be specified in order to work)
#' if (require("rstanarm")) {
#' stan_m0 <- stan_glm(Sepal.Length ~ 1,
#' data = iris,
#' family = gaussian(),
#' diagnostic_file = file.path(tempdir(), "df0.csv")
#' )
#' stan_m1 <- stan_glm(Sepal.Length ~ Species,
#' data = iris,
#' family = gaussian(),
#' diagnostic_file = file.path(tempdir(), "df1.csv")
#' )
#' stan_m2 <- stan_glm(Sepal.Length ~ Species + Petal.Length,
#' data = iris,
#' family = gaussian(),
#' diagnostic_file = file.path(tempdir(), "df2.csv")
#' )
#' bayesfactor_models(stan_m1, stan_m2, denominator = stan_m0, verbose = FALSE)
#' }
#' stan_m0 <- suppressWarnings(rstanarm::stan_glm(Sepal.Length ~ 1,
#' data = iris,
#' family = gaussian(),
#' diagnostic_file = file.path(tempdir(), "df0.csv")
#' ))
#' stan_m1 <- suppressWarnings(rstanarm::stan_glm(Sepal.Length ~ Species,
#' data = iris,
#' family = gaussian(),
#' diagnostic_file = file.path(tempdir(), "df1.csv")
#' ))
#' stan_m2 <- suppressWarnings(rstanarm::stan_glm(Sepal.Length ~ Species + Petal.Length,
#' data = iris,
#' family = gaussian(),
#' diagnostic_file = file.path(tempdir(), "df2.csv")
#' ))
#' bayesfactor_models(stan_m1, stan_m2, denominator = stan_m0, verbose = FALSE)
#'
#'
#' # brms models
#' # --------------------
#' # (note the save_pars MUST be set to save_pars(all = TRUE) in order to work)
#' if (require("brms")) {
#' brm1 <- brm(Sepal.Length ~ 1, data = iris, save_pars = save_pars(all = TRUE))
#' brm2 <- brm(Sepal.Length ~ Species, data = iris, save_pars = save_pars(all = TRUE))
#' brm3 <- brm(
#' Sepal.Length ~ Species + Petal.Length,
#' data = iris,
#' save_pars = save_pars(all = TRUE)
#' )
#' brm1 <- brms::brm(Sepal.Length ~ 1, data = iris, save_pars = save_pars(all = TRUE))
#' brm2 <- brms::brm(Sepal.Length ~ Species, data = iris, save_pars = save_pars(all = TRUE))
#' brm3 <- brms::brm(
#' Sepal.Length ~ Species + Petal.Length,
#' data = iris,
#' save_pars = save_pars(all = TRUE)
#' )
#'
#' bayesfactor_models(brm1, brm2, brm3, denominator = 1, verbose = FALSE)
#' }
#' bayesfactor_models(brm1, brm2, brm3, denominator = 1, verbose = FALSE)
#'
#'
#' # BayesFactor
#' # ---------------------------
#' if (require("BayesFactor")) {
#' data(puzzles)
#' BF <- anovaBF(RT ~ shape * color + ID,
#' data = puzzles,
#' whichRandom = "ID", progress = FALSE
#' )
#' BF
#' bayesfactor_models(BF) # basically the same
#' }
#' data(puzzles)
#' BF <- BayesFactor::anovaBF(RT ~ shape * color + ID,
#' data = puzzles,
#' whichRandom = "ID", progress = FALSE
#' )
#' BF
#' bayesfactor_models(BF) # basically the same
#' }
#'
#' @references
Expand Down
42 changes: 21 additions & 21 deletions R/p_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,41 @@
#' Testing* framework. It corresponds to the density value at the null (e.g., 0)
#' divided by the density at the Maximum A Posteriori (MAP).
#'
#' @details Note that this method is sensitive to the density estimation `method` (see the section in the examples below).
#' \subsection{Strengths and Limitations}{
#' **Strengths:** Straightforward computation. Objective property of the posterior distribution.
#' \cr \cr
#' **Limitations:** Limited information favoring the null hypothesis. Relates on density approximation. Indirect relationship between mathematical definition and interpretation. Only suitable for weak / very diffused priors.
#' }
#' @details Note that this method is sensitive to the density estimation `method`
#' (see the section in the examples below).
#'
#' ## Strengths and Limitations
#'
#' **Strengths:** Straightforward computation. Objective property of the posterior
#' distribution.
#'
#' **Limitations:** Limited information favoring the null hypothesis. Relates
#' on density approximation. Indirect relationship between mathematical
#' definition and interpretation. Only suitable for weak / very diffused priors.
#'
#' @inheritParams hdi
#' @inheritParams density_at
#' @inheritParams pd
#'
#' @examples
#' @examplesIf require("rstanarm") && require("emmeans") && require("brms") && require("BayesFactor")
#' library(bayestestR)
#'
#' p_map(rnorm(1000, 0, 1))
#' p_map(rnorm(1000, 10, 1))
#' \donttest{
#' library(rstanarm)
#' model <- suppressWarnings(
#' stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200, refresh = 0)
#' rstanarm::stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200, refresh = 0)
#' )
#' p_map(model)
#'
#' library(emmeans)
#' p_map(suppressWarnings(
#' emtrends(model, ~1, "wt", data = mtcars)
#' emmeans::emtrends(model, ~1, "wt", data = mtcars)
#' ))
#'
#' library(brms)
#' model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
#' p_map(model)
#'
#' library(BayesFactor)
#' bf <- ttestBF(x = rnorm(100, 1, 1))
#' bf <- BayesFactor::ttestBF(x = rnorm(100, 1, 1))
#' p_map(bf)
#'
#' # ---------------------------------------
Expand All @@ -49,9 +50,9 @@
#' for (iteration in 1:250) {
#' x <- rnorm(1000, 1, 1)
#' result <- data.frame(
#' "Kernel" = as.numeric(p_map(x, method = "kernel")),
#' "KernSmooth" = as.numeric(p_map(x, method = "KernSmooth")),
#' "logspline" = as.numeric(p_map(x, method = "logspline"))
#' Kernel = as.numeric(p_map(x, method = "kernel")),
#' KernSmooth = as.numeric(p_map(x, method = "KernSmooth")),
#' logspline = as.numeric(p_map(x, method = "logspline"))
#' )
#' data <- rbind(data, result)
#' }
Expand All @@ -64,10 +65,9 @@
#' }
#' @seealso [Jeff Mill's talk](https://www.youtube.com/watch?v=Ip8Ci5KUVRc)
#'
#' @references \itemize{
#' \item Makowski D, Ben-Shachar MS, Chen SHA, Lüdecke D (2019) Indices of Effect Existence and Significance in the Bayesian Framework. Frontiers in Psychology 2019;10:2767. \doi{10.3389/fpsyg.2019.02767}
#' \item Mills, J. A. (2018). Objective Bayesian Precise Hypothesis Testing. University of Cincinnati.
#' }
#' @references
#' - Makowski D, Ben-Shachar MS, Chen SHA, Lüdecke D (2019) Indices of Effect Existence and Significance in the Bayesian Framework. Frontiers in Psychology 2019;10:2767. \doi{10.3389/fpsyg.2019.02767}
#' - Mills, J. A. (2018). Objective Bayesian Precise Hypothesis Testing. University of Cincinnati.
#'
#' @export
p_map <- function(x, null = 0, precision = 2^10, method = "kernel", ...) {
Expand Down
14 changes: 6 additions & 8 deletions R/p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#'
#' @note 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}.
#'
#' @examples
#' @examplesIf require("rstanarm")
#' library(bayestestR)
#'
#' # Simulate a posterior distribution of mean 1 and SD 1
Expand All @@ -34,13 +34,11 @@
#' \donttest{
#' # rstanarm models
#' # -----------------------------------------------
#' if (require("rstanarm")) {
#' model <- rstanarm::stan_glm(mpg ~ wt + cyl,
#' data = mtcars,
#' chains = 2, refresh = 0
#' )
#' p_significance(model)
#' }
#' model <- rstanarm::stan_glm(mpg ~ wt + cyl,
#' data = mtcars,
#' chains = 2, refresh = 0
#' )
#' p_significance(model)
#' }
#' @export
p_significance <- function(x, ...) {
Expand Down
42 changes: 20 additions & 22 deletions R/p_to_bf.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,31 @@
#' and sample size: The 3p(sqrt(n)) rule. Preprint available on ArXiv:
#' https://psyarxiv.com/egydq
#'
#' @examples
#' if (requireNamespace("parameters", quietly = TRUE)) {
#' data(iris)
#' model <- lm(Petal.Length ~ Sepal.Length + Species, data = iris)
#' p_to_bf(model)
#' @examplesIf require("parameters")
#' data(iris)
#' model <- lm(Petal.Length ~ Sepal.Length + Species, data = iris)
#' p_to_bf(model)
#'
#' # Examples that demonstrate comparison between
#' # BIC-approximated and pseudo BF
#' # --------------------------------------------
#' m0 <- lm(mpg ~ 1, mtcars)
#' m1 <- lm(mpg ~ am, mtcars)
#' m2 <- lm(mpg ~ factor(cyl), mtcars)
#' # Examples that demonstrate comparison between
#' # BIC-approximated and pseudo BF
#' # --------------------------------------------
#' m0 <- lm(mpg ~ 1, mtcars)
#' m1 <- lm(mpg ~ am, mtcars)
#' m2 <- lm(mpg ~ factor(cyl), mtcars)
#'
#' # In this first example, BIC-approximated BF and
#' # pseudo-BF based on p-values are close...
#' # In this first example, BIC-approximated BF and
#' # pseudo-BF based on p-values are close...
#'
#' # BIC-approximated BF, m1 against null model
#' bic_to_bf(BIC(m1), denominator = BIC(m0))
#' # BIC-approximated BF, m1 against null model
#' bic_to_bf(BIC(m1), denominator = BIC(m0))
#'
#' # pseudo-BF based on p-values - dropping intercept
#' p_to_bf(m1)[-1, ]
#' # pseudo-BF based on p-values - dropping intercept
#' p_to_bf(m1)[-1, ]
#'
#' # The second example shows that results from pseudo-BF are less accurate
#' # and should be handled wit caution!
#' bic_to_bf(BIC(m2), denominator = BIC(m0))
#' p_to_bf(anova(m2), n_obs = nrow(mtcars))
#' }
#' # The second example shows that results from pseudo-BF are less accurate
#' # and should be handled wit caution!
#' bic_to_bf(BIC(m2), denominator = BIC(m0))
#' p_to_bf(anova(m2), n_obs = nrow(mtcars))
#'
#' @return A data frame with the p-values and pseudo-Bayes factors (against the null).
#'
Expand Down
10 changes: 3 additions & 7 deletions R/point_estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#'
#' @note 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}.
#'
#' @examples
#' @examplesIf require("rstanarm") && require("emmeans") && require("brms") && require("BayesFactor")
#' library(bayestestR)
#'
#' point_estimate(rnorm(1000))
Expand All @@ -34,31 +34,27 @@
#' \donttest{
#' # rstanarm models
#' # -----------------------------------------------
#' library(rstanarm)
#' model <- rstanarm::stan_glm(mpg ~ wt + cyl, data = mtcars)
#' point_estimate(model, centrality = "all", dispersion = TRUE)
#' point_estimate(model, centrality = c("median", "MAP"))
#'
#'
#' # emmeans estimates
#' # -----------------------------------------------
#' library(emmeans)
#' point_estimate(
#' emtrends(model, ~1, "wt", data = mtcars),
#' emmeans::emtrends(model, ~1, "wt", data = mtcars),
#' centrality = c("median", "MAP")
#' )
#'
#' # brms models
#' # -----------------------------------------------
#' library(brms)
#' model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
#' point_estimate(model, centrality = "all", dispersion = TRUE)
#' point_estimate(model, centrality = c("median", "MAP"))
#'
#' # BayesFactor objects
#' # -----------------------------------------------
#' library(BayesFactor)
#' bf <- ttestBF(x = rnorm(100, 1, 1))
#' bf <- BayesFactor::ttestBF(x = rnorm(100, 1, 1))
#' point_estimate(bf, centrality = "all", dispersion = TRUE)
#' point_estimate(bf, centrality = c("median", "MAP"))
#' }
Expand Down
2 changes: 1 addition & 1 deletion R/rope.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
#' methods for model selection. Statistics and Computing, 27(3), 711–735.
#' \doi{10.1007/s11222-016-9649-y}
#'
#' @examples
#' @examplesIf require("rstanarm") && require("emmeans") && require("brms") && require("BayesFactor")
#' library(bayestestR)
#'
#' rope(x = rnorm(1000, 0, 0.01), range = c(-0.1, 0.1))
Expand Down
2 changes: 1 addition & 1 deletion R/si.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#' Note that if the level of requested support is higher than observed in the data, the
#' interval will be `[NA,NA]`.
#'
#' @examplesIf requireNamespace("logspline", quietly = TRUE)
#' @examplesIf require("logspline") && require("rstanarm") && require("brms") && require("emmeans")
#' library(bayestestR)
#'
#' prior <- distribution_normal(1000, mean = 0, sd = 1)
Expand Down
2 changes: 1 addition & 1 deletion R/spi.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' @references
#' Liu, Y., Gelman, A., & Zheng, T. (2015). Simulation-efficient shortest probability intervals. Statistics and Computing, 25(4), 809–819. https://doi.org/10.1007/s11222-015-9563-8
#'
#' @examplesIf requireNamespace("quadprog", quietly = TRUE)
#' @examplesIf require("quadprog") && require("rstanarm")
#' library(bayestestR)
#'
#' posterior <- rnorm(1000)
Expand Down
Loading

0 comments on commit ea13b35

Please sign in to comment.