diff --git a/DESCRIPTION b/DESCRIPTION index 2b95bc028..173e34b8a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: bayestestR Title: Understand and Describe Bayesian Models and Posterior Distributions -Version: 0.14.0.11 +Version: 0.15.0 Authors@R: c(person(given = "Dominique", family = "Makowski", @@ -66,8 +66,8 @@ Description: Provides utilities to describe posterior Depends: R (>= 3.6) Imports: - insight (>= 0.20.4), - datawizard (>= 0.12.3), + insight (>= 0.20.5), + datawizard (>= 0.13.0), graphics, methods, stats, @@ -81,6 +81,7 @@ Suggests: blavaan, bridgesampling, brms, + collapse, curl, effectsize, emmeans, @@ -88,7 +89,6 @@ Suggests: ggdist, ggplot2, glmmTMB, - httr, httr2, KernSmooth, knitr, @@ -112,7 +112,8 @@ Suggests: rstanarm, see (>= 0.8.5), testthat, - tweedie + tweedie, + withr License: GPL-3 URL: https://easystats.github.io/bayestestR/ BugReports: https://github.com/easystats/bayestestR/issues @@ -127,4 +128,3 @@ Config/testthat/parallel: true Config/rcmdcheck/ignore-inconsequential-notes: true Config/Needs/website: easystats/easystatstemplate Config/Needs/check: stan-dev/cmdstanr -Remotes: easystats/insight, easystats/datawizard diff --git a/NAMESPACE b/NAMESPACE index 0151ef477..125750df7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -112,7 +112,6 @@ S3method(ci,sim.merMod) S3method(ci,slopes) S3method(ci,stanfit) S3method(ci,stanreg) -S3method(cwi,data.frame) S3method(describe_posterior,BFBayesFactor) S3method(describe_posterior,BGGM) S3method(describe_posterior,MCMCglmm) @@ -642,7 +641,6 @@ export(contr.orthonorm) export(convert_bayesian_as_frequentist) export(convert_p_to_pd) export(convert_pd_to_p) -export(cwi) export(density_at) export(describe_posterior) export(describe_prior) diff --git a/NEWS.md b/NEWS.md index c3232190d..1af4ab5a5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# bayestestR (development version) +# bayestestR 0.15.0 ## Changes diff --git a/R/bayesfactor.R b/R/bayesfactor.R index 989806241..feb8623b4 100644 --- a/R/bayesfactor.R +++ b/R/bayesfactor.R @@ -2,11 +2,11 @@ #' #' This function compte the Bayes factors (BFs) that are appropriate to the #' input. For vectors or single models, it will compute [`BFs for single -#' parameters()`][bayesfactor_parameters], or is `hypothesis` is specified, -#' [`BFs for restricted models()`][bayesfactor_restricted]. For multiple models, +#' parameters`][bayesfactor_parameters], or is `hypothesis` is specified, +#' [`BFs for restricted models`][bayesfactor_restricted]. For multiple models, #' it will return the BF corresponding to [`comparison between -#' models()`][bayesfactor_models] and if a model comparison is passed, it will -#' compute the [`inclusion BF()`][bayesfactor_inclusion]. +#' models`][bayesfactor_models] and if a model comparison is passed, it will +#' compute the [`inclusion BF`][bayesfactor_inclusion]. #' \cr\cr #' For a complete overview of these functions, read the [Bayes factor vignette](https://easystats.github.io/bayestestR/articles/bayes_factors.html). #' @@ -17,19 +17,20 @@ #' @inheritParams bayesfactor_models #' @inheritParams bayesfactor_inclusion #' -#' @return Some type of Bayes factor, depending on the input. See [bayesfactor_parameters()], [bayesfactor_models()] or [bayesfactor_inclusion()] +#' @return Some type of Bayes factor, depending on the input. See +#' [`bayesfactor_parameters()`], [`bayesfactor_models()`] or [`bayesfactor_inclusion()`]. #' #' @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}. #' #' @examplesIf require("rstanarm") && require("logspline") +#' \dontrun{ #' library(bayestestR) #' #' prior <- distribution_normal(1000, mean = 0, sd = 1) -#' posterior <- distribution_normal(1000, mean = .5, sd = .3) +#' posterior <- distribution_normal(1000, mean = 0.5, sd = 0.3) #' #' bayesfactor(posterior, prior = prior, verbose = FALSE) #' -#' \donttest{ #' # rstanarm models #' # --------------- #' model <- suppressWarnings(rstanarm::stan_lmer(extra ~ group + (1 | ID), data = sleep)) diff --git a/R/bic_to_bf.R b/R/bic_to_bf.R index 306b5e750..f8e6ae832 100644 --- a/R/bic_to_bf.R +++ b/R/bic_to_bf.R @@ -27,8 +27,8 @@ bic_to_bf <- function(bic, denominator, log = FALSE) { delta <- (denominator - bic) / 2 if (log) { - return(delta) + delta } else { - return(exp(delta)) + exp(delta) } } diff --git a/R/describe_posterior.R b/R/describe_posterior.R index 715501f31..73b41aa83 100644 --- a/R/describe_posterior.R +++ b/R/describe_posterior.R @@ -6,18 +6,16 @@ #' **bayestestR** supports a wide range of models (see `methods("describe_posterior")`) #' and not all of those are documented in the 'Usage' section, because methods #' for other classes mostly resemble the arguments of the `.numeric` method. -#' @param ci_method The type of index used for Credible Interval. Can be -#' `"ETI"` (default, see [bayestestR::eti()]), `"HDI"` -#' (see [bayestestR::hdi()]), `"BCI"` (see -#' [bayestestR::bci()]), `"SPI"` (see [bayestestR::spi()]), or -#' `"SI"` (see [bayestestR::si()]). +#' @param ci_method The type of index used for Credible Interval. Can be `"ETI"` +#' (default, see [`eti()`]), `"HDI"` (see [`hdi()`]), `"BCI"` (see [`bci()`]), +#' `"SPI"` (see [`spi()`]), or `"SI"` (see [`si()`]). #' @param test The indices of effect existence to compute. Character (vector) or #' list with one or more of these options: `"p_direction"` (or `"pd"`), #' `"rope"`, `"p_map"`, `"equivalence_test"` (or `"equitest"`), -#' `"bayesfactor"` (or `"bf"`) or `"all"` to compute all tests. -#' For each "test", the corresponding \pkg{bayestestR} function is called -#' (e.g. [bayestestR::rope()] or [bayestestR::p_direction()]) and its results -#' included in the summary output. +#' `"bayesfactor"` (or `"bf"`) or `"all"` to compute all tests. For each +#' "test", the corresponding \pkg{bayestestR} function is called (e.g. +#' [`rope()`] or [`p_direction()`]) and its results included in the summary +#' output. #' @param rope_range ROPE's lower and higher bounds. Should be a vector of two #' values (e.g., `c(-0.1, 0.1)`), `"default"` or a list of numeric vectors of #' the same length as numbers of parameters. If `"default"`, the bounds are @@ -27,7 +25,7 @@ #' @param keep_iterations If `TRUE`, will keep all iterations (draws) of #' bootstrapped or Bayesian models. They will be added as additional columns #' named `iter_1, iter_2, ...`. You can reshape them to a long format by -#' running [bayestestR::reshape_iterations()]. +#' running [`reshape_iterations()`]. #' @param bf_prior Distribution representing a prior for the computation of #' Bayes factors / SI. Used if the input is a posterior, otherwise (in the #' case of models) ignored. diff --git a/R/describe_prior.R b/R/describe_prior.R index 7f94a68ea..54b140c83 100644 --- a/R/describe_prior.R +++ b/R/describe_prior.R @@ -63,7 +63,7 @@ describe_prior.brmsfit <- function(model, # If the prior scale has been adjusted, it is the actual scale that was used. if ("Prior_Adjusted_Scale" %in% names(priors)) { - priors$Prior_Scale[!is.na(priors$Prior_Adjusted_Scale)] <- priors$Prior_Adjusted_Scale[!is.na(priors$Prior_Adjusted_Scale)] + priors$Prior_Scale[!is.na(priors$Prior_Adjusted_Scale)] <- priors$Prior_Adjusted_Scale[!is.na(priors$Prior_Adjusted_Scale)] # nolint priors$Prior_Adjusted_Scale <- NULL } @@ -85,7 +85,7 @@ describe_prior.brmsfit <- function(model, colnames(priors)[1] <- "Cleaned_Parameter" out <- merge(cp, priors, by = "Cleaned_Parameter", all = TRUE) out <- out[!duplicated(out$Parameter), ] - priors <- out[intersect(colnames(out), c("Parameter", "Prior_Distribution", "Prior_df", "Prior_Location", "Prior_Scale", "Response"))] + priors <- out[intersect(colnames(out), c("Parameter", "Prior_Distribution", "Prior_df", "Prior_Location", "Prior_Scale", "Response"))] # nolint } priors diff --git a/R/diagnostic_draws.R b/R/diagnostic_draws.R index f1b1ddc48..30858f453 100644 --- a/R/diagnostic_draws.R +++ b/R/diagnostic_draws.R @@ -27,17 +27,26 @@ diagnostic_draws <- function(posterior, ...) { diagnostic_draws.brmsfit <- function(posterior, ...) { insight::check_if_installed("brms") - data <- brms::nuts_params(posterior) - data$idvar <- paste0(data$Chain, "_", data$Iteration) + nuts_parameters <- brms::nuts_params(posterior) + nuts_parameters$idvar <- paste0( + nuts_parameters$Chain, + "_", + nuts_parameters$Iteration + ) out <- stats::reshape( - data, + nuts_parameters, v.names = "Value", idvar = "idvar", timevar = "Parameter", direction = "wide" ) out$idvar <- NULL - out <- merge(out, brms::log_posterior(posterior), by = c("Chain", "Iteration"), sort = FALSE) + out <- merge( + out, + brms::log_posterior(posterior), + by = c("Chain", "Iteration"), + sort = FALSE + ) # Rename names(out)[names(out) == "Value.accept_stat__"] <- "Acceptance_Rate" diff --git a/R/distribution.R b/R/distribution.R index 97c347ddd..d89e05548 100644 --- a/R/distribution.R +++ b/R/distribution.R @@ -33,21 +33,21 @@ distribution <- function(type = "normal", ...) { ) switch(match.arg(arg = type, choices = basr_r_distributions), - "beta" = distribution_beta(...), - "binom" = , - "binomial" = distribution_binomial(...), - "cauchy" = distribution_cauchy(...), - "chisq" = , - "chisquared" = distribution_chisquared(...), - "gamma" = distribution_gamma(...), - "gaussian" = , - "normal" = distribution_normal(...), - "nbinom" = distribution_nbinom(...), - "poisson" = distribution_poisson(...), - "t" = , - "student" = , - "student_t" = distribution_student(...), - "uniform" = distribution_uniform(...), + beta = distribution_beta(...), + binom = , + binomial = distribution_binomial(...), + cauchy = distribution_cauchy(...), + chisq = , + chisquared = distribution_chisquared(...), + gamma = distribution_gamma(...), + gaussian = , + normal = distribution_normal(...), + nbinom = distribution_nbinom(...), + poisson = distribution_poisson(...), + t = , + student = , + student_t = distribution_student(...), + uniform = distribution_uniform(...), distribution_custom(type = type, ...) ) } @@ -148,7 +148,7 @@ distribution_mixture_normal <- function(n, mean = c(-3, 3), sd = 1, random = FAL n <- round(n / length(mean)) sd <- sd if (length(sd) != length(mean)) { - sd <- rep(sd, length.out = length(mean)) + sd <- rep_len(sd, length(mean)) } diff --git a/R/effective_sample.R b/R/effective_sample.R index fa81bd5d5..0b5c6df7c 100644 --- a/R/effective_sample.R +++ b/R/effective_sample.R @@ -16,8 +16,10 @@ #' information there is in autocorrelated chains} (*Kruschke 2015, p182-3*). #' #' @references -#' - Kruschke, J. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan. Academic Press. -#' - Bürkner, P. C. (2017). brms: An R package for Bayesian multilevel models using Stan. Journal of Statistical Software, 80(1), 1-28 +#' - Kruschke, J. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, +#' and Stan. Academic Press. +#' - Bürkner, P. C. (2017). brms: An R package for Bayesian multilevel models +#' using Stan. Journal of Statistical Software, 80(1), 1-28 #' #' @examplesIf require("rstanarm") #' \donttest{ @@ -82,7 +84,7 @@ effective_sample.brmsfit <- function(model, #' @export effective_sample.stanreg <- function(model, effects = c("fixed", "random", "all"), - component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), + component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), # nolint parameters = NULL, ...) { # check arguments @@ -112,7 +114,7 @@ effective_sample.stanreg <- function(model, #' @export effective_sample.stanmvreg <- function(model, effects = c("fixed", "random", "all"), - component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), + component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), # nolint parameters = NULL, ...) { # check arguments diff --git a/R/equivalence_test.R b/R/equivalence_test.R index 638af6102..aafd098bf 100644 --- a/R/equivalence_test.R +++ b/R/equivalence_test.R @@ -31,8 +31,8 @@ #' to zero the better). #' \cr \cr #' Some attention is required for finding suitable values for the ROPE limits -#' (argument `range`). See 'Details' in [`rope_range()`][rope_range] -#' for further information. +#' (argument `range`). See 'Details' in [`rope_range()`] for further +#' information. #' \cr \cr #' **Multicollinearity: Non-independent covariates** #' \cr \cr diff --git a/R/hdi.R b/R/hdi.R index 0662a2c52..fa31b23bd 100644 --- a/R/hdi.R +++ b/R/hdi.R @@ -82,7 +82,8 @@ #' @inherit ci return #' #' @family ci -#' @seealso Other interval functions, such as [hdi()], [eti()], [bci()], [spi()], [si()], [cwi()]. +#' @seealso Other interval functions, such as [`hdi()`], [`eti()`], [`bci()`], +#' [`spi()`], [`si()`]. #' #' @examplesIf require("rstanarm") && require("brms") && require("emmeans") && require("BayesFactor") #' library(bayestestR) diff --git a/R/model_to_priors.R b/R/model_to_priors.R index 29634e547..20dfb9d1a 100644 --- a/R/model_to_priors.R +++ b/R/model_to_priors.R @@ -35,12 +35,12 @@ model_to_priors.brmsfit <- function(model, scale_multiply = 3, ...) { for (p in priors_params$Parameter) { if (p %in% params$Parameter) { - subset <- params[params$Parameter == p, ] + param_subset <- params[params$Parameter == p, ] priors$prior[priors_params$Parameter == p] <- paste0( "normal(", - insight::format_value(subset$Mean), + insight::format_value(param_subset$Mean), ", ", - insight::format_value(subset$SD * scale_multiply), + insight::format_value(param_subset$SD * scale_multiply), ")" ) } diff --git a/R/overlap.R b/R/overlap.R index 36faf6899..8fdb29737 100644 --- a/R/overlap.R +++ b/R/overlap.R @@ -1,6 +1,8 @@ #' Overlap Coefficient #' -#' A method to calculate the overlap coefficient between two empirical distributions (that can be used as a measure of similarity between two samples). +#' A method to calculate the overlap coefficient between two empirical +#' distributions (that can be used as a measure of similarity between two +#' samples). #' #' @param x Vector of x values. #' @param y Vector of x values. @@ -17,32 +19,56 @@ #' overlap(x, y) #' plot(overlap(x, y)) #' @export -overlap <- function(x, y, method_density = "kernel", method_auc = "trapezoid", precision = 2^10, extend = TRUE, extend_scale = 0.1, ...) { +overlap <- function(x, + y, + method_density = "kernel", + method_auc = "trapezoid", + precision = 2^10, + extend = TRUE, + extend_scale = 0.1, + ...) { # Generate densities - dx <- estimate_density(x, method = method_density, precision = precision, extend = extend, extend_scale = extend_scale, ...) - dy <- estimate_density(y, method = method_density, precision = precision, extend = extend, extend_scale = extend_scale, ...) + dx <- estimate_density( + x, + method = method_density, + precision = precision, + extend = extend, + extend_scale = extend_scale, + ... + ) + dy <- estimate_density( + y, + method = method_density, + precision = precision, + extend = extend, + extend_scale = extend_scale, + ... + ) # Create density estimation functions fx <- stats::approxfun(dx$x, dx$y, method = "linear", rule = 2) fy <- stats::approxfun(dy$x, dy$y, method = "linear", rule = 2) x_axis <- seq(min(c(dx$x, dy$x)), max(c(dx$x, dy$x)), length.out = precision) - data <- data.frame(x = x_axis, y1 = fx(x_axis), y2 = fy(x_axis)) - + approx_data <- data.frame(x = x_axis, y1 = fx(x_axis), y2 = fy(x_axis)) # calculate intersection densities - data$intersection <- pmin(data$y1, data$y2) - data$exclusion <- pmax(data$y1, data$y2) + approx_data$intersection <- pmin(approx_data$y1, approx_data$y2) + approx_data$exclusion <- pmax(approx_data$y1, approx_data$y2) # integrate areas under curves - area_intersection <- area_under_curve(data$x, data$intersection, method = method_auc) + area_intersection <- area_under_curve( + approx_data$x, + approx_data$intersection, + method = method_auc + ) # area_exclusion <- area_under_curve(data$x, data$exclusion, method = method_auc) # compute overlap coefficient overlap <- area_intersection - attr(overlap, "data") <- data + attr(overlap, "data") <- approx_data class(overlap) <- c("overlap", class(overlap)) overlap @@ -59,7 +85,7 @@ print.overlap <- function(x, ...) { #' @export plot.overlap <- function(x, ...) { # Can be improved through see - data <- attributes(x)$data - graphics::plot(data$x, data$exclusion, type = "l") - graphics::polygon(data$x, data$intersection, col = "red") + plot_data <- attributes(x)$data + graphics::plot(plot_data$x, plot_data$exclusion, type = "l") + graphics::polygon(plot_data$x, plot_data$intersection, col = "red") } diff --git a/R/p_significance.R b/R/p_significance.R index 12a692c1b..c38a3842a 100644 --- a/R/p_significance.R +++ b/R/p_significance.R @@ -285,7 +285,7 @@ p_significance.predictions <- p_significance.slopes p_significance.stanreg <- function(x, threshold = "default", effects = c("fixed", "random", "all"), - component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), + component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), # nolint parameters = NULL, verbose = TRUE, ...) { diff --git a/R/rope.R b/R/rope.R index 24c3a9b4a..675bd3581 100644 --- a/R/rope.R +++ b/R/rope.R @@ -24,7 +24,7 @@ #' @param ci The Credible Interval (CI) probability, corresponding to the #' proportion of HDI, to use for the percentage in ROPE. #' @param ci_method The type of interval to use to quantify the percentage in -#' ROPE. Can be 'HDI' (default) or 'ETI'. See [ci()]. +#' ROPE. Can be 'HDI' (default) or 'ETI'. See [`ci()`]. #' #' @inheritParams hdi #' @@ -46,7 +46,7 @@ #' Kruschke (2010, 2011, 2014) suggests using the proportion of the `95%` #' (or `89%`, considered more stable) [HDI][hdi] that falls within the #' ROPE as an index for "null-hypothesis" testing (as understood under the -#' Bayesian framework, see [`equivalence_test()`][equivalence_test]). +#' Bayesian framework, see [`equivalence_test()`]). #' #' @section Sensitivity to parameter's scale: #' It is important to consider the unit (i.e., the scale) of the predictors diff --git a/R/cwi.R b/WIP/cwi.R similarity index 100% rename from R/cwi.R rename to WIP/cwi.R diff --git a/_pkgdown.yml b/_pkgdown.yml index 2e890f27b..e270eb8ff 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -68,7 +68,6 @@ reference: - reshape_iterations - diagnostic_draws - model_to_priors - - cwi - disgust # Keep articles organized diff --git a/man/bayesfactor.Rd b/man/bayesfactor.Rd index bed0803d1..71f072d3e 100644 --- a/man/bayesfactor.Rd +++ b/man/bayesfactor.Rd @@ -47,14 +47,15 @@ the denominator, or a model to be used as a denominator. Ignored for \verb{BayesFactor::priorOdds<-}.} } \value{ -Some type of Bayes factor, depending on the input. See \code{\link[=bayesfactor_parameters]{bayesfactor_parameters()}}, \code{\link[=bayesfactor_models]{bayesfactor_models()}} or \code{\link[=bayesfactor_inclusion]{bayesfactor_inclusion()}} +Some type of Bayes factor, depending on the input. See +\code{\link[=bayesfactor_parameters]{bayesfactor_parameters()}}, \code{\link[=bayesfactor_models]{bayesfactor_models()}} or \code{\link[=bayesfactor_inclusion]{bayesfactor_inclusion()}}. } \description{ This function compte the Bayes factors (BFs) that are appropriate to the -input. For vectors or single models, it will compute \code{\link[=bayesfactor_parameters]{BFs for single parameters()}}, or is \code{hypothesis} is specified, -\code{\link[=bayesfactor_restricted]{BFs for restricted models()}}. For multiple models, -it will return the BF corresponding to \code{\link[=bayesfactor_models]{comparison between models()}} and if a model comparison is passed, it will -compute the \code{\link[=bayesfactor_inclusion]{inclusion BF()}}. +input. For vectors or single models, it will compute \code{\link[=bayesfactor_parameters]{BFs for single parameters}}, or is \code{hypothesis} is specified, +\code{\link[=bayesfactor_restricted]{BFs for restricted models}}. For multiple models, +it will return the BF corresponding to \code{\link[=bayesfactor_models]{comparison between models}} and if a model comparison is passed, it will +compute the \code{\link[=bayesfactor_inclusion]{inclusion BF}}. \cr\cr For a complete overview of these functions, read the \href{https://easystats.github.io/bayestestR/articles/bayes_factors.html}{Bayes factor vignette}. } @@ -63,14 +64,14 @@ There is also a \href{https://easystats.github.io/see/articles/bayestestR.html}{ } \examples{ \dontshow{if (require("rstanarm") && require("logspline")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontrun{ library(bayestestR) prior <- distribution_normal(1000, mean = 0, sd = 1) -posterior <- distribution_normal(1000, mean = .5, sd = .3) +posterior <- distribution_normal(1000, mean = 0.5, sd = 0.3) bayesfactor(posterior, prior = prior, verbose = FALSE) -\donttest{ # rstanarm models # --------------- model <- suppressWarnings(rstanarm::stan_lmer(extra ~ group + (1 | ID), data = sleep)) diff --git a/man/bayestestR-package.Rd b/man/bayestestR-package.Rd index 3552e5bdb..c61f4e45c 100644 --- a/man/bayestestR-package.Rd +++ b/man/bayestestR-package.Rd @@ -33,15 +33,15 @@ Useful links: } \author{ -\strong{Maintainer}: Dominique Makowski \email{dom.makowski@gmail.com} (\href{https://orcid.org/0000-0001-5375-9967}{ORCID}) (@Dom_Makowski) +\strong{Maintainer}: Dominique Makowski \email{dom.makowski@gmail.com} (\href{https://orcid.org/0000-0001-5375-9967}{ORCID}) Authors: \itemize{ - \item Daniel Lüdecke \email{d.luedecke@uke.de} (\href{https://orcid.org/0000-0002-8895-3206}{ORCID}) (@strengejacke) - \item Mattan S. Ben-Shachar \email{matanshm@post.bgu.ac.il} (\href{https://orcid.org/0000-0002-4287-4801}{ORCID}) (@mattansb) - \item Indrajeet Patil \email{patilindrajeet.science@gmail.com} (\href{https://orcid.org/0000-0003-1995-6531}{ORCID}) (@patilindrajeets) + \item Daniel Lüdecke \email{d.luedecke@uke.de} (\href{https://orcid.org/0000-0002-8895-3206}{ORCID}) + \item Mattan S. Ben-Shachar \email{matanshm@post.bgu.ac.il} (\href{https://orcid.org/0000-0002-4287-4801}{ORCID}) + \item Indrajeet Patil \email{patilindrajeet.science@gmail.com} (\href{https://orcid.org/0000-0003-1995-6531}{ORCID}) \item Micah K. Wilson \email{micah.k.wilson@curtin.edu.au} (\href{https://orcid.org/0000-0003-4143-7308}{ORCID}) - \item Brenton M. Wiernik \email{brenton@wiernik.org} (\href{https://orcid.org/0000-0001-9560-6336}{ORCID}) (@bmwiernik) + \item Brenton M. Wiernik \email{brenton@wiernik.org} (\href{https://orcid.org/0000-0001-9560-6336}{ORCID}) } Other contributors: diff --git a/man/bci.Rd b/man/bci.Rd index 172d51655..3e2f66a42 100644 --- a/man/bci.Rd +++ b/man/bci.Rd @@ -174,7 +174,6 @@ Statistical Science. 11(3): 189–212. 10.1214/ss/1032280214 \seealso{ Other ci: \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{hdi}()}, \code{\link{si}()}, diff --git a/man/ci.Rd b/man/ci.Rd index 1ef0d7948..92c7a9ae4 100644 --- a/man/ci.Rd +++ b/man/ci.Rd @@ -155,7 +155,6 @@ intervals"? BMJ 2019;l5381. 10.1136/bmj.l5381 \seealso{ Other ci: \code{\link{bci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{hdi}()}, \code{\link{si}()}, diff --git a/man/cwi.Rd b/man/cwi.Rd deleted file mode 100644 index 6b3db0ae0..000000000 --- a/man/cwi.Rd +++ /dev/null @@ -1,95 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/cwi.R -\name{cwi} -\alias{cwi} -\alias{cwi.data.frame} -\title{Curvewise Intervals (CWI)} -\usage{ -cwi(x, ...) - -\method{cwi}{data.frame}(x, ci = 0.95, ...) -} -\arguments{ -\item{x}{Vector representing a posterior distribution, or a data frame of such -vectors. Can also be a Bayesian model. \strong{bayestestR} supports a wide range -of models (see, for example, \code{methods("hdi")}) and not all of those are -documented in the 'Usage' section, because methods for other classes mostly -resemble the arguments of the \code{.numeric} or \code{.data.frame}methods.} - -\item{...}{Currently not used.} - -\item{ci}{Value or vector of probability of the (credible) interval - CI -(between 0 and 1) to be estimated. Default to \code{.95} (\verb{95\%}).} -} -\value{ -A data frame with following columns: -\itemize{ -\item \code{Parameter} The model parameter(s), if \code{x} is a model-object. If \code{x} is a -vector, this column is missing. -\item \code{CI} The probability of the credible interval. -\item \code{CI_low}, \code{CI_high} The lower and upper credible interval limits for the parameters. -} -} -\description{ -Compute the \strong{Curvewise interval (CWI)} (also called the "simultaneous interval" or "joint interval") of posterior distributions using \code{ggdist::curve_interval()}. -Whereas the more typical "pointwise intervals" contain xx\% of the posterior for a single parameter, -joint/curvewise intervals contain xx\% of the posterior distribution for \strong{all} parameters. -} -\details{ -Applied model predictions, pointwise intervals contain xx\% of the predicted response values \strong{conditional} on specific predictor values. -In contrast, curvewise intervals contain xx\% of the predicted response values across all predictor values. -Put another way, curvewise intervals contain xx\% of the full \strong{prediction lines} from the model. - -For more details, see the \href{https://mjskay.github.io/ggdist/articles/lineribbon.html#curve-boxplots-aka-lineribbons-with-joint-intervals-or-curvewise-intervals-}{\emph{ggdist} documentation on curvewise intervals}. -} -\examples{ -\donttest{ -library(bayestestR) - -if (require("ggplot2") && require("rstanarm") && require("ggdist")) { - # Generate data ============================================= - k <- 11 # number of curves (iterations) - n <- 201 # number of rows - data <- data.frame(x = seq(-15, 15, length.out = n)) - - # Simulate iterations as new columns - for (i in 1:k) { - data[paste0("iter_", i)] <- dnorm(data$x, seq(-5, 5, length.out = k)[i], 3) - } - - # Note: first, we need to transpose the data to have iters as rows - iters <- datawizard::data_transpose(data[paste0("iter_", 1:k)]) - - # Compute Median - data$Median <- point_estimate(iters)[["Median"]] - - # Compute Credible Intervals ================================ - - # Compute ETI (default type of CI) - data[c("ETI_low", "ETI_high")] <- eti(iters, ci = 0.5)[c("CI_low", "CI_high")] - - # Compute CWI - # ggdist::curve_interval(reshape_iterations(data), iter_value .width = 0.5) - - # Visualization ============================================= - ggplot(data, aes(x = x, y = Median)) + - geom_ribbon(aes(ymin = ETI_low, ymax = ETI_high), fill = "red", alpha = 0.3) + - geom_line(linewidth = 1) + - geom_line( - data = reshape_iterations(data), - aes(y = iter_value, group = iter_group), - alpha = 0.3 - ) -} -} -} -\seealso{ -Other ci: -\code{\link{bci}()}, -\code{\link{ci}()}, -\code{\link{eti}()}, -\code{\link{hdi}()}, -\code{\link{si}()}, -\code{\link{spi}()} -} -\concept{ci} diff --git a/man/describe_posterior.Rd b/man/describe_posterior.Rd index a81b821fa..717288520 100644 --- a/man/describe_posterior.Rd +++ b/man/describe_posterior.Rd @@ -107,19 +107,17 @@ Dispersion is not available for \code{"MAP"} or \code{"mode"} centrality indices \item{ci}{Value or vector of probability of the CI (between 0 and 1) to be estimated. Default to \code{0.95} (\verb{95\%}).} -\item{ci_method}{The type of index used for Credible Interval. Can be -\code{"ETI"} (default, see \code{\link[=eti]{eti()}}), \code{"HDI"} -(see \code{\link[=hdi]{hdi()}}), \code{"BCI"} (see -\code{\link[=bci]{bci()}}), \code{"SPI"} (see \code{\link[=spi]{spi()}}), or -\code{"SI"} (see \code{\link[=si]{si()}}).} +\item{ci_method}{The type of index used for Credible Interval. Can be \code{"ETI"} +(default, see \code{\link[=eti]{eti()}}), \code{"HDI"} (see \code{\link[=hdi]{hdi()}}), \code{"BCI"} (see \code{\link[=bci]{bci()}}), +\code{"SPI"} (see \code{\link[=spi]{spi()}}), or \code{"SI"} (see \code{\link[=si]{si()}}).} \item{test}{The indices of effect existence to compute. Character (vector) or list with one or more of these options: \code{"p_direction"} (or \code{"pd"}), \code{"rope"}, \code{"p_map"}, \code{"equivalence_test"} (or \code{"equitest"}), -\code{"bayesfactor"} (or \code{"bf"}) or \code{"all"} to compute all tests. -For each "test", the corresponding \pkg{bayestestR} function is called -(e.g. \code{\link[=rope]{rope()}} or \code{\link[=p_direction]{p_direction()}}) and its results -included in the summary output.} +\code{"bayesfactor"} (or \code{"bf"}) or \code{"all"} to compute all tests. For each +"test", the corresponding \pkg{bayestestR} function is called (e.g. +\code{\link[=rope]{rope()}} or \code{\link[=p_direction]{p_direction()}}) and its results included in the summary +output.} \item{rope_range}{ROPE's lower and higher bounds. Should be a vector of two values (e.g., \code{c(-0.1, 0.1)}), \code{"default"} or a list of numeric vectors of diff --git a/man/effective_sample.Rd b/man/effective_sample.Rd index 50c8aac01..7ca6d5745 100644 --- a/man/effective_sample.Rd +++ b/man/effective_sample.Rd @@ -70,7 +70,9 @@ effective_sample(model) } \references{ \itemize{ -\item Kruschke, J. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan. Academic Press. -\item Bürkner, P. C. (2017). brms: An R package for Bayesian multilevel models using Stan. Journal of Statistical Software, 80(1), 1-28 +\item Kruschke, J. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, +and Stan. Academic Press. +\item Bürkner, P. C. (2017). brms: An R package for Bayesian multilevel models +using Stan. Journal of Statistical Software, 80(1), 1-28 } } diff --git a/man/equivalence_test.Rd b/man/equivalence_test.Rd index 27fc778f7..2e2281f66 100644 --- a/man/equivalence_test.Rd +++ b/man/equivalence_test.Rd @@ -132,8 +132,8 @@ of the posterior within the ROPE is smaller than to \verb{2.5\%} or greater than to zero the better). \cr \cr Some attention is required for finding suitable values for the ROPE limits -(argument \code{range}). See 'Details' in \code{\link[=rope_range]{rope_range()}} -for further information. +(argument \code{range}). See 'Details' in \code{\link[=rope_range]{rope_range()}} for further +information. \cr \cr \strong{Multicollinearity: Non-independent covariates} \cr \cr diff --git a/man/eti.Rd b/man/eti.Rd index 4b116beb7..b81a35a60 100644 --- a/man/eti.Rd +++ b/man/eti.Rd @@ -171,7 +171,6 @@ eti(bf, ci = c(0.80, 0.89, 0.95)) Other ci: \code{\link{bci}()}, \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{hdi}()}, \code{\link{si}()}, \code{\link{spi}()} diff --git a/man/hdi.Rd b/man/hdi.Rd index 927d4f3da..62ffc2e67 100644 --- a/man/hdi.Rd +++ b/man/hdi.Rd @@ -177,12 +177,12 @@ examples in R and Stan. Chapman and Hall/CRC. } } \seealso{ -Other interval functions, such as \code{\link[=hdi]{hdi()}}, \code{\link[=eti]{eti()}}, \code{\link[=bci]{bci()}}, \code{\link[=spi]{spi()}}, \code{\link[=si]{si()}}, \code{\link[=cwi]{cwi()}}. +Other interval functions, such as \code{\link[=hdi]{hdi()}}, \code{\link[=eti]{eti()}}, \code{\link[=bci]{bci()}}, +\code{\link[=spi]{spi()}}, \code{\link[=si]{si()}}. Other ci: \code{\link{bci}()}, \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{si}()}, \code{\link{spi}()} diff --git a/man/overlap.Rd b/man/overlap.Rd index d3a170c29..86448f776 100644 --- a/man/overlap.Rd +++ b/man/overlap.Rd @@ -34,7 +34,9 @@ means that the x axis will be extended by \code{1/10} of the range of the data.} \item{...}{Currently not used.} } \description{ -A method to calculate the overlap coefficient between two empirical distributions (that can be used as a measure of similarity between two samples). +A method to calculate the overlap coefficient between two empirical +distributions (that can be used as a measure of similarity between two +samples). } \examples{ library(bayestestR) diff --git a/man/si.Rd b/man/si.Rd index 0ca1492af..a066e987c 100644 --- a/man/si.Rd +++ b/man/si.Rd @@ -226,7 +226,6 @@ The Support Interval. \doi{10.31234/osf.io/zwnxb} Other ci: \code{\link{bci}()}, \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{hdi}()}, \code{\link{spi}()} diff --git a/man/spi.Rd b/man/spi.Rd index 6212d9343..b1bf0b560 100644 --- a/man/spi.Rd +++ b/man/spi.Rd @@ -127,7 +127,6 @@ Liu, Y., Gelman, A., & Zheng, T. (2015). Simulation-efficient shortest probabili Other ci: \code{\link{bci}()}, \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{hdi}()}, \code{\link{si}()} diff --git a/revdep/cran.md b/revdep/cran.md index c00eb8384..948cf06b1 100644 --- a/revdep/cran.md +++ b/revdep/cran.md @@ -1,12 +1 @@ -## revdepcheck results - -We checked 22 reverse dependencies, comparing R CMD check results across CRAN and dev versions of this package. - - * We saw 0 new problems - * We failed to check 1 packages - -Issues with CRAN packages are summarised below. - -### Failed to check - -* snSMART (NA) +Maintainance release. Also required for the upcoming release of the 'parameters' package. diff --git a/tests/testthat/test-bayesfactor_models.R b/tests/testthat/test-bayesfactor_models.R index a17c29976..22a0912e2 100644 --- a/tests/testthat/test-bayesfactor_models.R +++ b/tests/testthat/test-bayesfactor_models.R @@ -17,9 +17,13 @@ test_that("bayesfactor_models BIC", { BFM3 <- bayesfactor_models(mo2, mo3, mo4, mo1, denominator = mo1) BFM4 <<- bayesfactor_models(mo2, mo3, mo4, mo5, mo1, denominator = mo1) - expect_equal(BFM1, BFM2) - expect_equal(BFM1, BFM3) - expect_equal(BFM1, bayesfactor_models(list(mo2 = mo2, mo3 = mo3, mo4 = mo4, mo1 = mo1), denominator = 4)) + expect_equal(BFM1, BFM2, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal(BFM1, BFM3, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal( + BFM1, + bayesfactor_models(list(mo2 = mo2, mo3 = mo3, mo4 = mo4, mo1 = mo1), denominator = 4), + tolerance = 1e-4, ignore_attr = TRUE + ) # only on same data! expect_warning(bayesfactor_models(mo1, mo2, mo4_e)) @@ -104,19 +108,19 @@ test_that("bayesfactor_models STAN", { set.seed(333) suppressMessages({ - expect_warning( + expect_warning({ stan_models <- bayesfactor_models(stan_bf_0, stan_bf_1) - ) + }) }) expect_s3_class(stan_models, "bayesfactor_models") - expect_equal(length(stan_models$log_BF), 2) + expect_length(stan_models$log_BF, 2) expect_equal(stan_models$log_BF[2], log(bridge_BF$bf), tolerance = 0.1) }) test_that("bayesfactor_models BRMS", { # Checks for brms models skip_on_cran() - skip_on_ci() + # skip_on_ci() skip_if_not_or_load_if_installed("bridgesampling") skip_if_not_or_load_if_installed("brms") @@ -143,7 +147,9 @@ test_that("bayesfactor_models BRMS", { set.seed(444) suppressWarnings(suppressMessages( expect_message( - bfm <- bayesfactor_models(stan_brms_model_0, stan_brms_model_1), + { + bfm <- bayesfactor_models(stan_brms_model_0, stan_brms_model_1) + }, regexp = "marginal" ) )) @@ -163,7 +169,12 @@ test_that("bayesfactor_models BRMS", { silent = 2 ) - suppressWarnings(expect_message(bfmwc <- bayesfactor_models(stan_brms_model_0wc, stan_brms_model_1wc), regexp = NA)) + suppressWarnings(expect_message( + { + bfmwc <- bayesfactor_models(stan_brms_model_0wc, stan_brms_model_1wc) + }, + regexp = NA + )) expect_equal(bfmwc$log_BF, bfm$log_BF, tolerance = 0.01) }) @@ -179,7 +190,8 @@ test_that("bayesfactor_inclusion | BayesFactor", { BF_ToothGrowth <- BayesFactor::anovaBF(len ~ dose * supp, ToothGrowth) expect_equal( bayesfactor_inclusion(BF_ToothGrowth), - bayesfactor_inclusion(bayesfactor_models(BF_ToothGrowth)) + bayesfactor_inclusion(bayesfactor_models(BF_ToothGrowth)), + tolerance = 1e-4, ignore_attr = TRUE ) }) diff --git a/tests/testthat/test-bayesfactor_parameters.R b/tests/testthat/test-bayesfactor_parameters.R index ff821f432..feef8baf6 100644 --- a/tests/testthat/test-bayesfactor_parameters.R +++ b/tests/testthat/test-bayesfactor_parameters.R @@ -57,7 +57,7 @@ test_that("bayesfactor_parameters RSTANARM", { skip_if_offline() skip_if_not_or_load_if_installed("logspline", "2.1.21") skip_if_not_or_load_if_installed("rstanarm") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") fit <- suppressMessages(stan_glm(mpg ~ ., data = mtcars, refresh = 0)) @@ -93,7 +93,7 @@ test_that("bayesfactor_parameters RSTANARM", { test_that("bayesfactor_parameters BRMS", { skip_if_offline() skip_if_not_or_load_if_installed("logspline", "2.1.21") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("brms") skip_if_not_or_load_if_installed("cmdstanr") skip_if_not(dir.exists(cmdstanr::cmdstan_default_install_path())) diff --git a/tests/testthat/test-brms.R b/tests/testthat/test-brms.R index 28c517b17..2c5cae012 100644 --- a/tests/testthat/test-brms.R +++ b/tests/testthat/test-brms.R @@ -2,7 +2,7 @@ test_that("brms", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") set.seed(333) model <- insight::download_model("brms_mixed_1") @@ -10,14 +10,14 @@ test_that("brms", { expect_s3_class(hdi(model), "data.frame") expect_s3_class(ci(model), "data.frame") expect_s3_class(rope(model, verbose = FALSE), "data.frame") - expect_true(inherits(equivalence_test(model), "equivalence_test")) + expect_s3_class(equivalence_test(model), "equivalence_test") expect_s3_class(map_estimate(model), "data.frame") expect_s3_class(p_map(model), "data.frame") expect_s3_class(p_direction(model), "data.frame") - expect_identical(colnames(hdi(model)), c("Parameter", "CI", "CI_low", "CI_high", "Effects", "Component")) - expect_identical(colnames(hdi(model, effects = "all")), c("Parameter", "CI", "CI_low", "CI_high", "Effects", "Component")) - expect_equal(nrow(equivalence_test(model)), 2L) + expect_named(hdi(model), c("Parameter", "CI", "CI_low", "CI_high", "Effects", "Component")) + expect_named(hdi(model, effects = "all"), c("Parameter", "CI", "CI_low", "CI_high", "Effects", "Component")) + expect_identical(nrow(equivalence_test(model)), 2L) out <- describe_posterior(model, effects = "all", component = "all", centrality = "mean") suppressWarnings({ @@ -38,7 +38,7 @@ test_that("brms", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") set.seed(333) model <- insight::download_model("brms_1") @@ -57,7 +57,7 @@ test_that("brms", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") set.seed(333) model <- insight::download_model("brms_mv_2") @@ -81,7 +81,7 @@ test_that("brms", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") set.seed(333) model <- insight::download_model("brms_2") diff --git a/tests/testthat/test-check_prior.R b/tests/testthat/test-check_prior.R index c01edab87..68ff465b5 100644 --- a/tests/testthat/test-check_prior.R +++ b/tests/testthat/test-check_prior.R @@ -90,13 +90,14 @@ test_that("check_prior - brms (linux)", { ) )) - expect_warning(expect_identical( - check_prior(model2, method = "lakeland")$Prior_Quality, - c( - "informative", "misinformative", "informative", "informative", - "informative", "not determinable", "not determinable", "not determinable" - ) - )) + ## FIXME: this test returns inconsistent results across platforms and OSs + # expect_warning(expect_identical( + # check_prior(model2, method = "lakeland")$Prior_Quality, + # c( + # "informative", "misinformative", "informative", "informative", + # "informative", "not determinable", "not determinable", "not determinable" + # ) + # )) }) test_that("check_prior - brms (not linux or windows)", { diff --git a/tests/testthat/test-ci.R b/tests/testthat/test-ci.R index fd9884967..2fdf1d3ca 100644 --- a/tests/testthat/test-ci.R +++ b/tests/testthat/test-ci.R @@ -32,7 +32,7 @@ test_that("ci", { test_that("ci", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("brms") m <- insight::download_model("stanreg_merMod_5") @@ -49,7 +49,7 @@ test_that("ci", { test_that("rope", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("brms") m <- insight::download_model("brms_zi_3") diff --git a/tests/testthat/test-data.frame-with-rvar.R b/tests/testthat/test-data.frame-with-rvar.R index 220c534bd..4399c75b0 100644 --- a/tests/testthat/test-data.frame-with-rvar.R +++ b/tests/testthat/test-data.frame-with-rvar.R @@ -1,5 +1,5 @@ test_that("data.frame w/ rvar_col descrive_posterior etc", { - skip_on_ci() + # skip_on_ci() skip_on_cran() skip_if_not_installed("posterior") @@ -63,7 +63,7 @@ test_that("data.frame w/ rvar_col descrive_posterior etc", { }) test_that("data.frame w/ rvar_col bayesfactors", { - skip_on_ci() + # skip_on_ci() skip_on_cran() skip_if_not_installed("posterior") skip_if_not_installed("logspline") diff --git a/tests/testthat/test-describe_posterior.R b/tests/testthat/test-describe_posterior.R index db265a679..de9db998e 100644 --- a/tests/testthat/test-describe_posterior.R +++ b/tests/testthat/test-describe_posterior.R @@ -3,7 +3,7 @@ test_that("describe_posterior", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") skip_on_os("linux") @@ -113,7 +113,7 @@ test_that("describe_posterior", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") set.seed(333) @@ -250,7 +250,7 @@ test_that("describe_posterior", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") m <- insight::download_model("stanreg_merMod_5") @@ -267,7 +267,7 @@ test_that("describe_posterior", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") m <- insight::download_model("brms_zi_3") @@ -286,7 +286,7 @@ test_that("describe_posterior w/ BF+SI", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") x <- insight::download_model("stanreg_lm_1") @@ -322,7 +322,7 @@ test_that("describe_posterior: BayesFactor", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") set.seed(123) diff --git a/tests/testthat/test-describe_prior.R b/tests/testthat/test-describe_prior.R index 8ba26521f..011f251d9 100644 --- a/tests/testthat/test-describe_prior.R +++ b/tests/testthat/test-describe_prior.R @@ -2,7 +2,7 @@ test_that("describe_prior", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") skip_on_os("linux") diff --git a/tests/testthat/test-hdi.R b/tests/testthat/test-hdi.R index 84785b364..367f2e179 100644 --- a/tests/testthat/test-hdi.R +++ b/tests/testthat/test-hdi.R @@ -3,7 +3,7 @@ test_that("hdi", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") expect_equal(hdi(distribution_normal(1000), ci = 0.90)$CI_low[1], -1.64, tolerance = 0.02) @@ -25,7 +25,7 @@ test_that("ci", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") m <- insight::download_model("stanreg_merMod_5") @@ -43,7 +43,7 @@ test_that("rope", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") m <- insight::download_model("brms_zi_3") @@ -61,7 +61,7 @@ test_that("ci - BayesFactor", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") mod_bf <- proportionBF(y = 15, N = 25, p = 0.5) diff --git a/tests/testthat/test-marginaleffects.R b/tests/testthat/test-marginaleffects.R index a1a3dcb3f..a49c16f4d 100644 --- a/tests/testthat/test-marginaleffects.R +++ b/tests/testthat/test-marginaleffects.R @@ -1,80 +1,84 @@ -test_that("marginaleffects descrive_posterior", { - skip_on_ci() - skip_on_cran() +skip_on_cran() +skip_if_not_installed("withr") +skip_if_not_installed("rstanarm") +skip_if_not_installed("marginaleffects") +skip_if_not_installed("collapse") - skip_if_not_installed("rstanarm") - skip_if_not_installed("marginaleffects") +withr::with_environment( + new.env(), + test_that("marginaleffects descrive_posterior", { + # skip_on_ci() - data("mtcars") - mtcars$cyl <- factor(mtcars$cyl) - mod <- rstanarm::stan_glm(mpg ~ cyl + hp * am, data = mtcars, refresh = 0) + data("mtcars") + mtcars$cyl <- factor(mtcars$cyl) + mod <- rstanarm::stan_glm(mpg ~ cyl + hp * am, data = mtcars, refresh = 0) - mfx <- marginaleffects::avg_slopes(mod, by = "am") - mfx_samps <- as.data.frame(t(attr(mfx, "posterior_draws"))) + mfx <- marginaleffects::avg_slopes(mod, by = "am") + mfx_samps <- as.data.frame(t(attr(mfx, "posterior_draws"))) - results <- describe_posterior(mfx, - centrality = "MAP", ci_method = "hdi", - test = c("pd", "rope", "p_map", "equivalence_test") - ) - results_draws <- describe_posterior(mfx_samps, - centrality = "MAP", ci_method = "hdi", - test = c("pd", "rope", "p_map", "equivalence_test") - ) + results <- describe_posterior(mfx, + centrality = "MAP", ci_method = "hdi", + test = c("pd", "rope", "p_map", "equivalence_test") + ) + results_draws <- describe_posterior(mfx_samps, + centrality = "MAP", ci_method = "hdi", + test = c("pd", "rope", "p_map", "equivalence_test") + ) - expect_true(all(c("term", "contrast") %in% colnames(results))) - expect_equal(results[setdiff(colnames(results), c("term", "contrast"))], - results_draws[setdiff(colnames(results_draws), "Parameter")], - ignore_attr = TRUE - ) + expect_true(all(c("term", "contrast") %in% colnames(results))) + expect_equal(results[setdiff(colnames(results), c("term", "contrast"))], + results_draws[setdiff(colnames(results_draws), "Parameter")], + ignore_attr = TRUE + ) - # estimate_density - mfx <- marginaleffects::comparisons(mod, - variables = "cyl", - newdata = marginaleffects::datagrid(hp = 100, am = 0) - ) - samps <- insight::get_parameters(mod)[c("cyl6", "cyl8")] + # estimate_density + mfx <- marginaleffects::comparisons(mod, + variables = "cyl", + newdata = marginaleffects::datagrid(hp = 100, am = 0) + ) + samps <- insight::get_parameters(mod)[c("cyl6", "cyl8")] - res <- estimate_density(mfx) - resref <- estimate_density(samps) - expect_equal(res[intersect(colnames(res), colnames(resref))], - resref[intersect(colnames(res), colnames(resref))], - ignore_attr = TRUE - ) -}) + res <- estimate_density(mfx) + resref <- estimate_density(samps) + expect_equal(res[intersect(colnames(res), colnames(resref))], + resref[intersect(colnames(res), colnames(resref))], + ignore_attr = TRUE + ) + }) +) -test_that("marginaleffects bayesfactors", { - skip_on_ci() - skip_on_cran() +withr::with_environment( + new.env(), + test_that("marginaleffects bayesfactors", { + # skip_on_ci() - skip_if_not_installed("rstanarm") - skip_if_not_installed("marginaleffects") + data("mtcars") + mtcars$cyl <- factor(mtcars$cyl) + mod <- rstanarm::stan_glm(mpg ~ cyl + hp * am, data = mtcars, refresh = 0) + modp <- unupdate(mod, verbose = FALSE) - data("mtcars") - mtcars$cyl <- factor(mtcars$cyl) - mod <- rstanarm::stan_glm(mpg ~ cyl + hp * am, data = mtcars, refresh = 0) - modp <- unupdate(mod, verbose = FALSE) + mfx <- marginaleffects::avg_slopes(mod, by = "am") + mfxp <- marginaleffects::avg_slopes(modp, by = "am") - mfx <- marginaleffects::avg_slopes(mod, by = "am") - mfxp <- marginaleffects::avg_slopes(modp, by = "am") + mfx_samps <- as.data.frame(t(attr(mfx, "posterior_draws"))) + mfxp_samps <- as.data.frame(t(attr(mfxp, "posterior_draws"))) - mfx_samps <- as.data.frame(t(attr(mfx, "posterior_draws"))) - mfxp_samps <- as.data.frame(t(attr(mfxp, "posterior_draws"))) + # SI + outsi <- si(mfx, prior = mfxp, verbose = FALSE) + outsiref <- si(mfx_samps, prior = mfxp_samps, verbose = FALSE) - # SI - outsi <- si(mfx, prior = mfxp, verbose = FALSE) - outsiref <- si(mfx_samps, prior = mfxp_samps, verbose = FALSE) + expect_true(all(c("term", "contrast") %in% colnames(outsi))) + expect_equal(outsi[setdiff(colnames(outsi), c("term", "contrast"))], + outsiref[setdiff(colnames(outsiref), "Parameter")], + ignore_attr = TRUE + ) - expect_true(all(c("term", "contrast") %in% colnames(outsi))) - expect_equal(outsi[setdiff(colnames(outsi), c("term", "contrast"))], - outsiref[setdiff(colnames(outsiref), "Parameter")], - ignore_attr = TRUE - ) - - # bayesfactor_parameters - bfp <- bayesfactor_parameters(mfx, prior = mfxp, verbose = FALSE) - bfpref <- bayesfactor_parameters(mfx_samps, prior = mfxp_samps, verbose = FALSE) - expect_equal(bfp[setdiff(colnames(bfp), c("term", "contrast"))], - bfpref[setdiff(colnames(bfpref), "Parameter")], - ignore_attr = TRUE - ) -}) + # bayesfactor_parameters + bfp <- bayesfactor_parameters(mfx, prior = mfxp, verbose = FALSE) + bfpref <- bayesfactor_parameters(mfx_samps, prior = mfxp_samps, verbose = FALSE) + expect_equal(bfp[setdiff(colnames(bfp), c("term", "contrast"))], + bfpref[setdiff(colnames(bfpref), "Parameter")], + ignore_attr = TRUE + ) + }) +) diff --git a/tests/testthat/test-point_estimate.R b/tests/testthat/test-point_estimate.R index 873e35ee2..769ee71d5 100644 --- a/tests/testthat/test-point_estimate.R +++ b/tests/testthat/test-point_estimate.R @@ -2,7 +2,7 @@ test_that("point_estimate: stanreg", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") m <- insight::download_model("stanreg_merMod_5") p <- insight::get_parameters(m, effects = "all") @@ -18,7 +18,7 @@ test_that("point_estimate: brms", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") m <- insight::download_model("brms_zi_3") p <- insight::get_parameters(m, effects = "all", component = "all") diff --git a/tests/testthat/test-print.R b/tests/testthat/test-print.R index d353c6601..653814629 100644 --- a/tests/testthat/test-print.R +++ b/tests/testthat/test-print.R @@ -2,7 +2,7 @@ test_that("print.describe_posterior", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") m <- insight::download_model("brms_zi_3") expect_snapshot(describe_posterior(m, verbose = FALSE), variant = "windows") diff --git a/tests/testthat/test-rstanarm.R b/tests/testthat/test-rstanarm.R index 75546cc27..868303a6b 100644 --- a/tests/testthat/test-rstanarm.R +++ b/tests/testthat/test-rstanarm.R @@ -2,7 +2,7 @@ test_that("rstanarm", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") set.seed(333) model <- insight::download_model("stanreg_lm_1") @@ -50,7 +50,7 @@ test_that("rstanarm", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") set.seed(333) model <- insight::download_model("stanreg_glm_3") @@ -69,7 +69,7 @@ test_that("rstanarm", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") set.seed(333) model <- insight::download_model("stanreg_merMod_3") @@ -89,7 +89,7 @@ test_that("rstanarm", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") set.seed(333) model <- insight::download_model("stanmvreg_1") @@ -109,7 +109,7 @@ test_that("rstanarm", { skip_on_cran() skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") set.seed(333) model <- insight::download_model("stanmvreg_1") diff --git a/tests/testthat/test-spi.R b/tests/testthat/test-spi.R index 41d377106..f6b86dad7 100644 --- a/tests/testthat/test-spi.R +++ b/tests/testthat/test-spi.R @@ -3,7 +3,7 @@ test_that("spi", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") expect_equal(spi(distribution_normal(1000), ci = 0.90)$CI_low[1], -1.65, tolerance = 0.02) @@ -25,7 +25,7 @@ test_that("ci", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") m <- insight::download_model("stanreg_merMod_5") @@ -42,7 +42,7 @@ test_that("spi brms", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") m <- insight::download_model("brms_zi_3") @@ -60,7 +60,7 @@ test_that("ci - BayesFactor", { skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("brms") - skip_if_not_or_load_if_installed("httr") + skip_if_not_or_load_if_installed("httr2") skip_if_not_or_load_if_installed("BayesFactor") mod_bf <- proportionBF(y = 15, N = 25, p = 0.5) diff --git a/tests/testthat/test-weighted_posteriors.R b/tests/testthat/test-weighted_posteriors.R index 33b3a2649..36ba8f9c1 100644 --- a/tests/testthat/test-weighted_posteriors.R +++ b/tests/testthat/test-weighted_posteriors.R @@ -27,7 +27,7 @@ test_that("weighted_posteriors for BayesFactor", { test_that("weighted_posteriors for BayesFactor (intercept)", { # fails for win old-release - skip_on_ci() + # skip_on_ci() skip_on_cran() skip_if_not_or_load_if_installed("BayesFactor") @@ -41,7 +41,7 @@ test_that("weighted_posteriors for BayesFactor (intercept)", { BFmods <- regressionBF(y ~ x1 + x2, data = dat, progress = FALSE) res <- weighted_posteriors(BFmods) - expect_equal(attr(res, "weights")$weights, c(1032, 805, 1388, 775)) + expect_equal(attr(res, "weights")$weights, c(1032, 805, 1388, 775), ignore_attr = TRUE) wHDI <- hdi(res[c("x1", "x2")], ci = 0.9) expect_equal(wHDI$CI_low, c(-0.519, -0.640), tolerance = 0.01) @@ -64,7 +64,7 @@ test_that("weighted_posteriors for nonlinear BayesFactor", { res <- weighted_posteriors(BFS) - expect_equal(attributes(res)$weights$weights, c(113, 3876, 11)) + expect_equal(attributes(res)$weights$weights, c(113, 3876, 11), ignore_attr = TRUE) }) test_that("weighted_posteriors vs posterior_average", { @@ -87,7 +87,9 @@ test_that("weighted_posteriors vs posterior_average", { ) set.seed(444) - expect_warning(res_BT <- weighted_posteriors(fit1, fit2)) + expect_warning({ + res_BT <- weighted_posteriors(fit1, fit2) + }) set.seed(444) res_brms <- brms::posterior_average(fit1, fit2, weights = "bma", missing = 0) @@ -96,8 +98,8 @@ test_that("weighted_posteriors vs posterior_average", { res_BT1 <- eti(res_BT) res_brms1 <- eti(res_brms) - expect_equal(res_BT1$Parameter, res_brms1$Parameter) - expect_equal(res_BT1$CI, res_brms1$CI) - expect_equal(res_BT1$CI_low, res_brms1$CI_low) - expect_equal(res_BT1$CI_high, res_brms1$CI_high) + expect_equal(res_BT1$Parameter, res_brms1$Parameter, tolerance = 1e-4) + expect_equal(res_BT1$CI, res_brms1$CI, tolerance = 1e-4) + expect_equal(res_BT1$CI_low, res_brms1$CI_low, tolerance = 1e-4) + expect_equal(res_BT1$CI_high, res_brms1$CI_high, tolerance = 1e-4) }) diff --git a/vignettes/mediation.Rmd b/vignettes/mediation.Rmd index 70bc70a9c..ae9eb5d0f 100644 --- a/vignettes/mediation.Rmd +++ b/vignettes/mediation.Rmd @@ -1,6 +1,6 @@ --- title: "Mediation Analysis using Bayesian Regression Models" -output: +output: rmarkdown::html_vignette: toc: true fig_width: 10.08 @@ -10,7 +10,7 @@ vignette: > \usepackage[utf8]{inputenc} %\VignetteIndexEntry{Mediation Analysis using Bayesian Regression Models} %\VignetteEngine{knitr::rmarkdown} -editor_options: +editor_options: chunk_output_type: console bibliography: bibliography.bib csl: apa.csl @@ -31,7 +31,7 @@ options(width = 80) if (!requireNamespace("mediation", quietly = TRUE) || !requireNamespace("curl", quietly = TRUE) || - !requireNamespace("httr", quietly = TRUE) || + !requireNamespace("httr2", quietly = TRUE) || !requireNamespace("lavaan", quietly = TRUE) || !requireNamespace("brms", quietly = TRUE) || !requireNamespace("rstanarm", quietly = TRUE) ||