Skip to content

Commit

Permalink
map, point_est, eti, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattansb committed Sep 5, 2024
1 parent 2164d40 commit b238567
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 11 deletions.
2 changes: 1 addition & 1 deletion 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.5
Version: 0.14.0.6
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
7 changes: 4 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Changes

* Support for `posterior::rvar`-type column in data frames.
For example, a data frame `df` with an `rvar` column `".pred"` can now be
called directly via `p_direction(df, rvar_col = ".pred")`.

* Added support for `{marginaleffects}`

* Results from objects generated by `{emmeans}` (`emmGrid`/`emm_list`) now
Expand All @@ -17,9 +21,6 @@
- `p_direction()` gets a `remove_na` argument, which defaults to `TRUE`, to
remove `NA` values from the input before calculating the pd-values.

- The `data.frame` method for `p_direction()` gets an `rvar_col` argument, to
specify the column that contains the `rvar` objects.

- Besides the existing `as.numeric()` method, `p_direction()` now also has an
`as.vector()` method.

Expand Down
21 changes: 19 additions & 2 deletions R/eti.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,26 @@ eti.numeric <- function(x, ci = 0.95, verbose = TRUE, ...) {


#' @export
eti.data.frame <- function(x, ci = 0.95, verbose = TRUE, ...) {
#' @rdname eti
#' @inheritParams p_direction
eti.data.frame <- function(x, ci = 0.95, verbose = TRUE, rvar_col = NULL, ...) {
obj_name <- insight::safe_deparse_symbol(substitute(x))

if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {

Check warning on line 74 in R/eti.R

View workflow job for this annotation

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

file=R/eti.R,line=74,col=14,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
cl <- match.call()
cl[[1]] <- eti
cl$x <- x_rvar
cl$rvar_col <- NULL
out <- eval.parent(cl)

attr(out, "object_name") <- sprintf('%s[["%s"]]', obj_name, rvar_col)

return(.append_datagrid(out, x))
}


dat <- .compute_interval_dataframe(x = x, ci = ci, verbose = verbose, fun = "eti")
attr(dat, "object_name") <- insight::safe_deparse_symbol(substitute(x))
attr(dat, "object_name") <- obj_name
dat
}

Expand Down
16 changes: 15 additions & 1 deletion R/map_estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,22 @@ map_estimate.brmsfit <- function(x, precision = 2^10, method = "kernel", effects


#' @rdname map_estimate
#' @inheritParams p_direction
#' @export
map_estimate.data.frame <- function(x, precision = 2^10, method = "kernel", ...) {
map_estimate.data.frame <- function(x, precision = 2^10, method = "kernel", rvar_col = NULL, ...) {
if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {

Check warning on line 154 in R/map_estimate.R

View workflow job for this annotation

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

file=R/map_estimate.R,line=154,col=14,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
cl <- match.call()
cl[[1]] <- map_estimate
cl$x <- x_rvar
cl$rvar_col <- NULL
out <- eval.parent(cl)

obj_name <- insight::safe_deparse_symbol(substitute(x))
attr(out, "object_name") <- sprintf('%s[["%s"]]', obj_name, rvar_col)

return(.append_datagrid(out, x))
}

.map_estimate_models(x, precision = precision, method = method)
}

Expand Down
11 changes: 11 additions & 0 deletions R/p_direction.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
#' df <- data.frame(replicate(4, rnorm(100)))
#' p_direction(df)
#' p_direction(df, method = "kernel")
#'
#' \donttest{
#' # rstanarm models
#' # -----------------------------------------------
Expand All @@ -148,6 +149,14 @@
#' p_direction(bf)
#' p_direction(bf, method = "kernel")
#' }
#'
#' @examplesIf requireNamespace("posterior", quietly = TRUE)
#' # Using "rvar_col"
#' x <- data.frame(mu = c(0, 0.5, 1), sigma = c(1, 0.5, 0.25))
#' x$my_rvar <- posterior::rvar_rng(rnorm, 3, mean = x$mu, sd = x$sigma)
#' x
#' p_direction(x, rvar_col = "my_rvar")
#'
#' @export
p_direction <- function(x, ...) {
UseMethod("p_direction")
Expand Down Expand Up @@ -187,6 +196,8 @@ p_direction.numeric <- function(x,


#' @rdname p_direction
#' @param rvar_col Possibly a single character - the name of an `rvar` column in
#' the data frame, to be processed. See example in [p_direction()].
#' @export
p_direction.data.frame <- function(x,
method = "direct",
Expand Down
18 changes: 17 additions & 1 deletion R/point_estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,23 @@ point_estimate.numeric <- function(x, centrality = "all", dispersion = FALSE, th


#' @export
point_estimate.data.frame <- function(x, centrality = "all", dispersion = FALSE, threshold = 0.1, ...) {
#' @rdname point_estimate
#' @inheritParams p_direction
point_estimate.data.frame <- function(x, centrality = "all", dispersion = FALSE, threshold = 0.1, rvar_col = NULL, ...) {
if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
cl <- match.call()
cl[[1]] <- point_estimate
cl$x <- x_rvar
cl$rvar_col <- NULL
out <- eval.parent(cl)

obj_name <- insight::safe_deparse_symbol(substitute(x))
attr(out, "object_name") <- sprintf('%s[["%s"]]', obj_name, rvar_col)

return(.append_datagrid(out, x))
}


x <- .select_nums(x)

if (ncol(x) == 1) {
Expand Down
6 changes: 6 additions & 0 deletions man/eti.Rd

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

5 changes: 4 additions & 1 deletion man/map_estimate.Rd

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

12 changes: 10 additions & 2 deletions man/p_direction.Rd

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

13 changes: 13 additions & 0 deletions man/point_estimate.Rd

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

0 comments on commit b238567

Please sign in to comment.