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

support rvar columns in data.frames #673

Merged
merged 24 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions R/bayesfactor_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@
#' to match the `posterior`-type input:
#'
#' - **A numeric vector** - `prior` should also be a _numeric vector_, representing the prior-estimate.
#' - **A data frame** - `prior` should also be a _data frame_, representing the prior-estimates, in matching column order.

Check warning on line 92 in R/bayesfactor_parameters.R

View workflow job for this annotation

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

file=R/bayesfactor_parameters.R,line=92,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.

Check warning on line 92 in R/bayesfactor_parameters.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_parameters.R,line=92,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
#' - If `rvar_col` is specified, `prior` should be _the name of an `rvar` column_ that represents the prior-estimates.
#' - **Supported Bayesian model (`stanreg`, `brmsfit`, etc.)**
#' - `prior` should be _a model an equivalent model with MCMC samples from the priors *only*_. See [unupdate()].
#' - If `prior` is set to `NULL`, [unupdate()] is called internally (not supported for `brmsfit_multiple` model).
#' - **Output from a `{marginaleffects}` function** - `prior` should also be _an equivalent output_ from a `{marginaleffects}` function based on a prior-model

Check warning on line 97 in R/bayesfactor_parameters.R

View workflow job for this annotation

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

file=R/bayesfactor_parameters.R,line=97,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 158 characters.

Check warning on line 97 in R/bayesfactor_parameters.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_parameters.R,line=97,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 158 characters.
#' (See [unupdate()]).
#' - **Output from an `{emmeans}` function**
#' - `prior` should also be _an equivalent output_ from an `{emmeans}` function based on a prior-model (See [unupdate()]).

Check warning on line 100 in R/bayesfactor_parameters.R

View workflow job for this annotation

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

file=R/bayesfactor_parameters.R,line=100,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.

Check warning on line 100 in R/bayesfactor_parameters.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_parameters.R,line=100,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.
#' - `prior` can also be _the original (posterior) model_, in which case the function
#' will try to "unupdate" the estimates (not supported if the estimates have undergone
#' any transformations -- `"log"`, `"response"`, etc. -- or any `regrid`ing).
Expand Down Expand Up @@ -185,7 +185,12 @@
#' @author Mattan S. Ben-Shachar
#'
#' @export
bayesfactor_parameters <- function(posterior, ...) {
bayesfactor_parameters <- function(posterior,
prior = NULL,
direction = "two-sided",
null = 0,
...,
verbose = TRUE) {
strengejacke marked this conversation as resolved.
Show resolved Hide resolved
UseMethod("bayesfactor_parameters")
}

Expand All @@ -195,8 +200,8 @@
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
...) {
...,
verbose = TRUE) {
if (length(null) > 1L && verbose) {
insight::format_alert("`null` is a range - computing a ROPE based Bayes factor.")
}
Expand All @@ -217,8 +222,8 @@
prior = NULL,
direction = "two-sided",
null = rope_range(posterior, verbose = FALSE),
verbose = TRUE,
...) {
...,
verbose = TRUE) {
if (length(null) < 2 && verbose) {
insight::format_alert("'null' is a point - computing a Savage-Dickey (point null) Bayes factor.")
}
Expand Down Expand Up @@ -251,15 +256,15 @@
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
...) {
...,
verbose = TRUE) {
# nm <- insight::safe_deparse(substitute(posterior)

if (is.null(prior)) {
prior <- posterior
if (verbose) {
insight::format_warning(
"Prior not specified! Please specify a prior (in the form 'prior = distribution_normal(1000, 0, 1)') to get meaningful results."

Check warning on line 267 in R/bayesfactor_parameters.R

View workflow job for this annotation

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

file=R/bayesfactor_parameters.R,line=267,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 136 characters.

Check warning on line 267 in R/bayesfactor_parameters.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_parameters.R,line=267,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 136 characters.
)
}
}
Expand All @@ -284,11 +289,11 @@
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
effects = c("fixed", "random", "all"),
component = c("conditional", "location", "smooth_terms", "sigma", "zi", "zero_inflated", "all"),

Check warning on line 293 in R/bayesfactor_parameters.R

View workflow job for this annotation

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

file=R/bayesfactor_parameters.R,line=293,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 139 characters.

Check warning on line 293 in R/bayesfactor_parameters.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_parameters.R,line=293,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 139 characters.
parameters = NULL,
...) {
...,
verbose = TRUE) {
cleaned_parameters <- insight::clean_parameters(posterior)
effects <- match.arg(effects)
component <- match.arg(component)
Expand Down Expand Up @@ -330,8 +335,8 @@
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
...) {
...,
verbose = TRUE) {
cleaned_parameters <- insight::clean_parameters(posterior)

samps <- .clean_priors_and_posteriors(posterior, prior,
Expand Down Expand Up @@ -363,8 +368,8 @@
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
...) {
...,
verbose = TRUE) {
samps <- .clean_priors_and_posteriors(
posterior,
prior,
Expand Down Expand Up @@ -404,8 +409,8 @@
direction = "two-sided",
null = 0,
rvar_col = NULL,
verbose = TRUE,
...) {
...,
verbose = TRUE) {
x_rvar <- .possibly_extract_rvar_col(posterior, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
Expand Down Expand Up @@ -467,7 +472,7 @@
class(bf_val)
))

attr(bf_val, "hypothesis") <- null # don't change the name of this attribute - it is used only internally for "see" and printing

Check warning on line 475 in R/bayesfactor_parameters.R

View workflow job for this annotation

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

file=R/bayesfactor_parameters.R,line=475,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 130 characters.

Check warning on line 475 in R/bayesfactor_parameters.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_parameters.R,line=475,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 130 characters.
attr(bf_val, "direction") <- direction
attr(bf_val, "plot_data") <- .make_BF_plot_data(posterior, prior, direction, null, ...)

Expand All @@ -480,8 +485,8 @@
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
...) {
...,
verbose = TRUE) {
bayesfactor_parameters(
.posterior_draws_to_df(posterior),
prior = if (!is.null(prior)) .posterior_draws_to_df(prior),
Expand Down
60 changes: 37 additions & 23 deletions man/bayesfactor_parameters.Rd

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

Loading