Skip to content

Commit

Permalink
Merge branch 'code_style' of https://github.com/easystats/bayestestR
Browse files Browse the repository at this point in the history
…into code_style
  • Loading branch information
strengejacke committed Jul 3, 2023
2 parents d9f0b05 + 1fc427c commit bdc7313
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 83 deletions.
8 changes: 5 additions & 3 deletions R/sexit_thresholds.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
#' sexit_thresholds(rnorm(1000))
#' \dontrun{
#' if (require("rstanarm")) {
#' model <- stan_glm(
#' model <- suppressWarnings(stan_glm(
#' mpg ~ wt + gear,
#' data = mtcars,
#' chains = 2,
#' iter = 200,
#' refresh = 0
#' )
#' ))
#' sexit_thresholds(model)
#'
#' model <- stan_glm(vs ~ mpg, data = mtcars, family = "binomial", refresh = 0)
#' model <- suppressWarnings(
#' stan_glm(vs ~ mpg, data = mtcars, family = "binomial", refresh = 0)
#' )
#' sexit_thresholds(model)
#' }
#'
Expand Down
41 changes: 21 additions & 20 deletions R/si.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#' updating factor greater or equal than *k*. From the perspective of the Savage-Dickey Bayes factor, testing
#' against a point null hypothesis for any value within the support interval will yield a Bayes factor smaller
#' than *1/k*.
#' \cr \cr
#' \strong{For more info, in particular on specifying correct priors for factors with more than 2 levels,
#' see [the Bayes factors vignette](https://easystats.github.io/bayestestR/articles/bayes_factors.html).}
#'
#' **For more info, in particular on specifying correct priors for factors with more than 2 levels,
#' see [the Bayes factors vignette](https://easystats.github.io/bayestestR/articles/bayes_factors.html).**
#'
#' @param BF The amount of support required to be included in the support interval.
#' @inheritParams bayesfactor_parameters
Expand All @@ -20,48 +20,49 @@
#' they should be *not flat*, and it is preferable that they be *informative* - note
#' that by default, `brms::brm()` uses flat priors for fixed-effects; see example below).
#'
#' \subsection{Choosing a value of `BF`}{
#' The choice of `BF` (the level of support) depends on what we want our interval to represent:
#' \itemize{
#' \item A `BF` = 1 contains values whose credibility is not decreased by observing the data.
#' \item A `BF` > 1 contains values who received more impressive support from the data.
#' \item A `BF` < 1 contains values whose credibility has *not* been impressively decreased by observing the data.
#' Testing against values outside this interval will produce a Bayes factor larger than 1/`BF` in support of
#' the alternative. E.g., if an SI (BF = 1/3) excludes 0, the Bayes factor against the point-null will be larger than 3.
#' }
#' }
#' @section Choosing a value of `BF`:
#' The choice of `BF` (the level of support) depends on what we want our interval
#' to represent:
#'
#' - A `BF` = 1 contains values whose credibility is not decreased by observing the data.
#' - A `BF` > 1 contains values who received more impressive support from the data.
#' - A `BF` < 1 contains values whose credibility has *not* been impressively
#' decreased by observing the data. Testing against values outside this interval
#' will produce a Bayes factor larger than 1/`BF` in support of the alternative.
#' E.g., if an SI (BF = 1/3) excludes 0, the Bayes factor against the point-null
#' will be larger than 3.
#'
#' @inheritSection bayesfactor_parameters Setting the correct `prior`
#'
#' @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}.
#'
#' @return
#' A data frame containing the lower and upper bounds of the SI.
#' \cr
#'
#' Note that if the level of requested support is higher than observed in the data, the
#' interval will be `[NA,NA]`.
#'
#' @examplesIf requireNamespace("logspline", quietly = TRUE)
#' 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)
#'
#' si(posterior, prior)
#' si(posterior, prior, verbose = FALSE)
#' \dontrun{
#' # rstanarm models
#' # ---------------
#' library(rstanarm)
#' contrasts(sleep$group) <- contr.equalprior_pairs # see vignette
#' stan_model <- stan_lmer(extra ~ group + (1 | ID), data = sleep)
#' si(stan_model)
#' si(stan_model, BF = 3)
#' si(stan_model, verbose = FALSE)
#' si(stan_model, BF = 3, verbose = FALSE)
#'
#' # emmGrid objects
#' # ---------------
#' library(emmeans)
#' group_diff <- pairs(emmeans(stan_model, ~group))
#' si(group_diff, prior = stan_model)
#' si(group_diff, prior = stan_model, verbose = FALSE)
#'
#' # brms models
#' # -----------
Expand All @@ -76,7 +77,7 @@
#' prior = my_custom_priors,
#' refresh = 0
#' ))
#' si(brms_model)
#' si(brms_model, verbose = FALSE)
#' }
#' @references
#' Wagenmakers, E., Gronau, Q. F., Dablander, F., & Etz, A. (2018, November 22).
Expand Down
6 changes: 4 additions & 2 deletions R/simulate_priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#' @inheritParams effective_sample
#' @param n Size of the simulated prior distributions.
#'
#' @seealso [unupdate()] for directly sampling from the prior
#' @seealso [`unupdate()`] for directly sampling from the prior
#' distribution (useful for complex priors and designs).
#'
#' @examples
#' \dontrun{
#' library(bayestestR)
#' if (require("rstanarm")) {
#' model <- stan_glm(mpg ~ wt + am, data = mtcars, chains = 1, refresh = 0)
#' model <- suppressWarnings(
#' stan_glm(mpg ~ wt + am, data = mtcars, chains = 1, refresh = 0)
#' )
#' simulate_prior(model)
#' }
#' }
Expand Down
6 changes: 2 additions & 4 deletions R/simulate_simpson.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
#' and statistics, in which a trend appears in several different groups of data
#' but disappears or reverses when these groups are combined.
#'
#' @param n The number of observations for each group to be generated (minimum
#' 4).
#' @param groups Number of groups (groups can be participants, clusters,
#' anything).
#' @param n The number of observations for each group to be generated (minimum 4).
#' @param groups Number of groups (groups can be participants, clusters, anything).
#' @param difference Difference between groups.
#' @param group_prefix The prefix of the group name (e.g., "G_1", "G_2", "G_3", ...).
#' @inheritParams simulate_correlation
Expand Down
4 changes: 3 additions & 1 deletion R/spi.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
#' spi(df, ci = c(0.80, 0.89, 0.95))
#' \dontrun{
#' library(rstanarm)
#' model <- stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200, refresh = 0)
#' model <- suppressWarnings(
#' stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200, refresh = 0)
#' )
#' spi(model)
#' }
#'
Expand Down
2 changes: 1 addition & 1 deletion R/unupdate.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' the goal of this function is to un-update the posteriors to obtain models
#' representing the priors. These models can then be used to examine the prior
#' predictive distribution, or to compare priors with posteriors.
#' \cr\cr
#'
#' This function in used internally to compute Bayes factors.
#'
#' @param model A fitted Bayesian model.
Expand Down
24 changes: 12 additions & 12 deletions R/weighted_posteriors.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
#' subtracting for continuous variables, and effects coding via `contr.sum` or
#' orthonormal coding via [`contr.equalprior_pairs`] for factors) can reduce this
#' issue. In any case you should be mindful of this issue.
#' \cr\cr
#'
#' See [bayesfactor_models()] details for more info on passed models.
#' \cr\cr
#'
#' Note that for `BayesFactor` models, posterior samples cannot be generated
#' from intercept only models.
#' \cr\cr
#'
#' This function is similar in function to `brms::posterior_average`.
#'
#' @note For `BayesFactor < 0.9.12-4.3`, in some instances there might be
Expand All @@ -36,24 +36,24 @@
#'
#' @return A data frame with posterior distributions (weighted across models) .
#'
#' @seealso [bayesfactor_inclusion()] for Bayesian model averaging.
#' @seealso [`bayesfactor_inclusion()`] for Bayesian model averaging.
#'
#' @examples
#' \donttest{
#' if (require("rstanarm") && require("see")) {
#' stan_m0 <- stan_glm(extra ~ 1,
#' stan_m0 <- suppressWarnings(stan_glm(extra ~ 1,
#' data = sleep,
#' family = gaussian(),
#' refresh = 0,
#' diagnostic_file = file.path(tempdir(), "df0.csv")
#' )
#' ))
#'
#' stan_m1 <- stan_glm(extra ~ group,
#' stan_m1 <- suppressWarnings(stan_glm(extra ~ group,
#' data = sleep,
#' family = gaussian(),
#' refresh = 0,
#' diagnostic_file = file.path(tempdir(), "df1.csv")
#' )
#' ))
#'
#' res <- weighted_posteriors(stan_m0, stan_m1, verbose = FALSE)
#'
Expand All @@ -73,21 +73,21 @@
#'
#' ## weighted prediction distributions via data.frames
#' if (require("rstanarm")) {
#' m0 <- stan_glm(
#' m0 <- suppressWarnings(stan_glm(
#' mpg ~ 1,
#' data = mtcars,
#' family = gaussian(),
#' diagnostic_file = file.path(tempdir(), "df0.csv"),
#' refresh = 0
#' )
#' ))
#'
#' m1 <- stan_glm(
#' m1 <- suppressWarnings(stan_glm(
#' mpg ~ carb,
#' data = mtcars,
#' family = gaussian(),
#' diagnostic_file = file.path(tempdir(), "df1.csv"),
#' refresh = 0
#' )
#' ))
#'
#' # Predictions:
#' pred_m0 <- data.frame(posterior_predict(m0))
Expand Down
8 changes: 5 additions & 3 deletions man/sexit_thresholds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 23 additions & 19 deletions man/si.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/simulate_prior.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions man/simulate_simpson.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/spi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/unupdate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bdc7313

Please sign in to comment.