Skip to content

Commit

Permalink
Prepare CRAN submission (#678)
Browse files Browse the repository at this point in the history
* Prepare CRAN submission

* check if tests work

* check tests

* test

* test

* tests

* fix tests

* includ withr

* lintr

* fix

* use httr2 instead of httr

* cran comments

* lintr, style, remove cwi

* fix

* use latest datawizard

* docs

* just skip (dont run) some examples

* put everything in dontrun

* minor docs style
  • Loading branch information
strengejacke authored Oct 17, 2024
1 parent ba01f93 commit 080f664
Show file tree
Hide file tree
Showing 48 changed files with 293 additions and 348 deletions.
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,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).

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.
#'
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 / 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 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
Expand Up @@ -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"
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 @@ 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, ...)
)
}
Expand Down Expand Up @@ -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))
}


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 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

0 comments on commit 080f664

Please sign in to comment.