Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare CRAN submission #678

Merged
merged 19 commits into from
Oct 17, 2024
Merged
12 changes: 6 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -81,14 +81,14 @@ Suggests:
blavaan,
bridgesampling,
brms,
collapse,
curl,
effectsize,
emmeans,
gamm4,
ggdist,
ggplot2,
glmmTMB,
httr,
httr2,
KernSmooth,
knitr,
Expand All @@ -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
Expand All @@ -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
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# bayestestR (development version)
# bayestestR 0.15.0

## Changes

Expand Down
15 changes: 8 additions & 7 deletions R/bayesfactor.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#'
#' 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).

Check warning on line 11 in R/bayesfactor.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/bayesfactor.R,line=11,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 148 characters.

Check warning on line 11 in R/bayesfactor.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor.R,line=11,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 148 characters.
#'
#' @param ... A numeric vector, model object(s), or the output from
#' `bayesfactor_models`.
Expand All @@ -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}.

Check warning on line 23 in R/bayesfactor.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/bayesfactor.R,line=23,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 179 characters.

Check warning on line 23 in R/bayesfactor.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor.R,line=23,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 179 characters.
#'
#' @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))
Expand Down
4 changes: 2 additions & 2 deletions R/bic_to_bf.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
18 changes: 8 additions & 10 deletions R/describe_posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -48,7 +46,7 @@
#' - Makowski, D., Ben-Shachar, M. S., Chen, S. H. A., and 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}
#' - [Region of Practical Equivalence (ROPE)](https://easystats.github.io/bayestestR/articles/region_of_practical_equivalence.html)

Check warning on line 49 in R/describe_posterior.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/describe_posterior.R,line=49,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 131 characters.
#' - [Bayes factors](https://easystats.github.io/bayestestR/articles/bayes_factors.html)
#'
#' @examples
Expand Down Expand Up @@ -122,7 +120,7 @@


#' @keywords internal
.describe_posterior <- function(x,

Check warning on line 123 in R/describe_posterior.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/describe_posterior.R,line=123,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 68 to at most 40.
centrality = "median",
dispersion = FALSE,
ci = 0.95,
Expand Down
4 changes: 2 additions & 2 deletions R/describe_prior.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
17 changes: 13 additions & 4 deletions R/diagnostic_draws.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Diagnostic values for each iteration
#'
#' Returns the accumulated log-posterior, the average Metropolis acceptance rate, divergent transitions, treedepth rather than terminated its evolution normally.

Check warning on line 3 in R/diagnostic_draws.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/diagnostic_draws.R,line=3,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 161 characters.
#' @inheritParams diagnostic_posterior
#'
#' @examples
Expand All @@ -27,17 +27,26 @@
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"
Expand Down
32 changes: 16 additions & 16 deletions R/distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@
)

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, ...)
)
}
Expand Down Expand Up @@ -148,7 +148,7 @@
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))
}


Expand Down Expand Up @@ -229,8 +229,8 @@
#' @export
distribution_tweedie <- function(n,
xi = NULL,
mu,

Check warning on line 232 in R/distribution.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/distribution.R,line=232,col=34,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
phi,

Check warning on line 233 in R/distribution.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/distribution.R,line=233,col=34,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
power = NULL,
random = FALSE,
...) {
Expand Down
10 changes: 6 additions & 4 deletions R/effective_sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions R/equivalence_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,9 +53,9 @@
#'
#'
#' @references
#' - Kruschke, J. K. (2018). Rejecting or accepting parameter values in Bayesian estimation. Advances in Methods and Practices in Psychological Science, 1(2), 270-280. \doi{10.1177/2515245918771304}

Check warning on line 56 in R/equivalence_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/equivalence_test.R,line=56,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 198 characters.
#' - Kruschke, J. K. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan. Academic Press
#' - Piironen, J., & Vehtari, A. (2017). Comparison of Bayesian predictive methods for model selection. Statistics and Computing, 27(3), 711–735. \doi{10.1007/s11222-016-9649-y}

Check warning on line 58 in R/equivalence_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/equivalence_test.R,line=58,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 177 characters.
#'
#' @return A data frame with following columns:
#'
Expand Down Expand Up @@ -172,7 +172,7 @@
# check if list of values contains only valid values
range <- .check_list_range(range, x)
# apply thresholds to each column
l <- insight::compact_list(mapply(

Check warning on line 175 in R/equivalence_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/equivalence_test.R,line=175,col=32,[undesirable_function_linter] Avoid undesirable function "mapply".
function(p, r) {
equivalence_test(
p,
Expand Down
3 changes: 2 additions & 1 deletion R/hdi.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions R/model_to_priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
")"
)
}
Expand Down
Loading
Loading