Skip to content

Commit

Permalink
make sure we have uniform get_predicted methods
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 2, 2023
1 parent ea13b35 commit 7080574
Show file tree
Hide file tree
Showing 22 changed files with 354 additions and 48 deletions.
15 changes: 10 additions & 5 deletions R/bci.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,19 @@ bci.BFBayesFactor <- function(x, ci = 0.95, verbose = TRUE, ...) {
}


#' @rdname bci
#' @export
bci.get_predicted <- function(x, ...) {
if ("iterations" %in% names(attributes(x))) {
out <- bci(as.data.frame(t(attributes(x)$iterations)), ...)
bci.get_predicted <- function(x, ci = 0.95, use_iterations = FALSE, verbose = TRUE, ...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- bci(as.data.frame(t(attributes(x)$iterations)), ci = ci, verbose = verbose, ...)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
insight::format_error("No iterations present in the output.")
out <- bci(as.numeric(x), ci = ci, verbose = verbose, ...)
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
out
}

Expand Down
16 changes: 11 additions & 5 deletions R/eti.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,23 @@ eti.BFBayesFactor <- function(x, ci = 0.95, verbose = TRUE, ...) {
}


#' @rdname eti
#' @export
eti.get_predicted <- function(x, ...) {
if ("iterations" %in% names(attributes(x))) {
out <- eti(as.data.frame(t(attributes(x)$iterations)), ...)
eti.get_predicted <- function(x, ci = 0.95, use_iterations = FALSE, verbose = TRUE, ...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- eti(as.data.frame(t(attributes(x)$iterations)), ci = ci, verbose = verbose, ...)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
insight::format_error("No iterations present in the output.")
out <- eti(as.numeric(x), ci = ci, verbose = verbose, ...)
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
out
}


# Helper ------------------------------------------------------------------


Expand Down
19 changes: 14 additions & 5 deletions R/hdi.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#' filtered by default, so only parameters that typically appear in the
#' `summary()` are returned. Use `parameters` to select specific parameters
#' for the output.
#' @param use_iterations Logical, if `TRUE` and `x` is a `get_predicted` object,
#' (returned by [`insight::get_predicted()`]), the function is applied to the
#' iterations instead of the predictions. This only applies to models that return
#' iterations for predicted values (e.g., `brmsfit` models).
#' @param verbose Toggle off warnings.
#' @param ... Currently not used.
#'
Expand Down Expand Up @@ -353,14 +357,19 @@ hdi.BFBayesFactor <- function(x, ci = 0.95, verbose = TRUE, ...) {
}


#' @rdname hdi
#' @export
hdi.get_predicted <- function(x, ...) {
if ("iterations" %in% names(attributes(x))) {
out <- hdi(as.data.frame(t(attributes(x)$iterations)), ...)
hdi.get_predicted <- function(x, ci = 0.95, use_iterations = FALSE, verbose = TRUE, ...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- hdi(as.data.frame(t(attributes(x)$iterations)), ci = ci, verbose = verbose, ...)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
insight::format_error("No iterations present in the output.")
out <- hdi(as.numeric(x), ci = ci, verbose = verbose, ...)
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
out
}

Expand Down
32 changes: 27 additions & 5 deletions R/map_estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ map_estimate.numeric <- function(x, precision = 2^10, method = "kernel", ...) {
precision,
method = method, ...
)
out[[1]] <- NULL
attr(out, "data") <- x
out
}
Expand Down Expand Up @@ -174,13 +173,36 @@ map_estimate.emmGrid <- function(x, precision = 2^10, method = "kernel", ...) {
map_estimate.emm_list <- map_estimate.emmGrid


#' @rdname map_estimate
#' @export
map_estimate.get_predicted <- function(x, ...) {
if ("iterations" %in% names(attributes(x))) {
map_estimate(as.data.frame(t(attributes(x)$iterations)), ...)
map_estimate.get_predicted <- function(x,
precision = 2^10,
method = "kernel",
use_iterations = FALSE,
verbose = TRUE,
...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- map_estimate(
as.data.frame(t(attributes(x)$iterations)),
precision = precision,
method = method,
verbose = verbose,
...
)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
insight::format_error("No iterations present in the output.")
out <- map_estimate(as.numeric(x),
precision = precision,
method = method,
verbose = verbose,
...
)
}
out
}


Expand Down
31 changes: 26 additions & 5 deletions R/p_direction.R
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,35 @@ p_direction.BFBayesFactor <- function(x, method = "direct", null = 0, ...) {
out
}

#' @rdname p_direction
#' @export
p_direction.get_predicted <- function(x, ...) {
if ("iterations" %in% names(attributes(x))) {
out <- p_direction(as.data.frame(t(attributes(x)$iterations)), ...)
p_direction.get_predicted <- function(x,
method = "direct",
null = 0,
use_iterations = FALSE,
verbose = TRUE,
...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- p_direction(
as.data.frame(t(attributes(x)$iterations)),
method = method,
null = null,
verbose = verbose,
...
)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
insight::format_error("No iterations present in the output.")
out <- p_direction(as.numeric(x),
method = method,
null = null,
verbose = verbose,
...
)
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
out
}

Expand Down
34 changes: 32 additions & 2 deletions R/p_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,39 @@ p_map.numeric <- function(x, null = 0, precision = 2^10, method = "kernel", ...)
}


#' @rdname p_map
#' @export
p_map.get_predicted <- function(x, null = 0, precision = 2^10, method = "kernel", ...) {
p_map(as.numeric(x), null = null, precision = precision, method = method, ...)
p_map.get_predicted <- function(x,
null = 0,
precision = 2^10,
method = "kernel",
use_iterations = FALSE,
verbose = TRUE,
...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- p_map(
as.data.frame(t(attributes(x)$iterations)),
null = null,
precision = precision,
method = method,
verbose = verbose,
...
)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
out <- p_map(as.numeric(x),
null = null,
precision = precision,
method = method,
verbose = verbose,
...
)
}
out
}


Expand Down
25 changes: 23 additions & 2 deletions R/p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,30 @@ p_significance.numeric <- function(x, threshold = "default", ...) {
out
}


#' @rdname p_significance
#' @export
p_significance.get_predicted <- function(x, threshold = "default", ...) {
p_significance(as.numeric(x), threshold = threshold, ...)
p_significance.get_predicted <- function(x, threshold = "default", use_iterations = FALSE, verbose = TRUE, ...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- p_significance(
as.data.frame(t(attributes(x)$iterations)),
threshold = threshold,
verbose = verbose,
...
)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
out <- p_significance(as.numeric(x),
threshold = threshold,
verbose = verbose,
...
)
}
out
}


Expand Down
31 changes: 27 additions & 4 deletions R/point_estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,36 @@ point_estimate.matrix <- function(x, ...) {
}


#' @rdname point_estimate
#' @export
point_estimate.get_predicted <- function(x, ...) {
if ("iterations" %in% names(attributes(x))) {
point_estimate(as.data.frame(t(attributes(x)$iterations)), ...)
point_estimate.get_predicted <- function(x,
centrality = "all",
dispersion = FALSE,
use_iterations = FALSE,
verbose = TRUE,
...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- point_estimate(
as.data.frame(t(attributes(x)$iterations)),
centrality = centrality,
dispersion = dispersion,
verbose = verbose,
...
)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
as.numeric(x)
out <- point_estimate(as.numeric(x),
centrality = centrality,
dispersion = dispersion,
verbose = verbose,
...
)
}
out
}


Expand Down
27 changes: 25 additions & 2 deletions R/rope.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,31 @@ rope.numeric <- function(x, range = "default", ci = 0.95, ci_method = "ETI", ver


#' @export
rope.get_predicted <- function(x, range = "default", ci = 0.95, ci_method = "ETI", verbose = TRUE, ...) {
rope(as.numeric(x), range = range, ci = ci, ci_method = ci_method, verbose = verbose, ...)
rope.get_predicted <- function(x,
range = "default",
ci = 0.95,
ci_method = "ETI",
use_iterations = FALSE,
verbose = TRUE,
...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- rope(
as.data.frame(t(attributes(x)$iterations)),
range = range,
ci = ci,
ci_method = ci_method,
verbose = verbose,
...
)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
out <- rope(as.numeric(x), range = range, ci = ci, ci_method = ci_method, verbose = verbose, ...)
}
out
}


Expand Down
22 changes: 19 additions & 3 deletions R/si.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,26 @@ si.stanfit <- function(posterior, prior = NULL, BF = 1, verbose = TRUE, effects
out
}


#' @rdname si
#' @export
si.get_predicted <- function(posterior, ...) {
out <- si(as.data.frame(t(posterior)), ...)
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(posterior))
si.get_predicted <- function(posterior, prior = NULL, BF = 1, use_iterations = FALSE, verbose = TRUE, ...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(posterior))) {
out <- si(
as.data.frame(t(attributes(posterior)$iterations)),
prior = prior,
BF = BF,
verbose = verbose,
...
)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(posterior))
} else {
out <- si(insight::get_parameters(posterior), prior = prior, BF = BF, verbose = verbose, ...)
}
out
}

Expand Down
15 changes: 10 additions & 5 deletions R/spi.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,19 @@ spi.BFBayesFactor <- function(x, ci = 0.95, verbose = TRUE, ...) {
}


#' @rdname spi
#' @export
spi.get_predicted <- function(x, ...) {
if ("iterations" %in% names(attributes(x))) {
out <- spi(as.data.frame(t(attributes(x)$iterations)), ...)
spi.get_predicted <- function(x, ci = 0.95, use_iterations = FALSE, verbose = TRUE, ...) {
if (isTRUE(use_iterations)) {
if ("iterations" %in% names(attributes(x))) {
out <- spi(as.data.frame(t(attributes(x)$iterations)), ci = ci, verbose = verbose, ...)
} else {
insight::format_error("No iterations present in the output.")
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
} else {
insight::format_error("No iterations present in the output.")
out <- spi(as.numeric(x), ci = ci, verbose = verbose, ...)
}
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
out
}

Expand Down
Loading

0 comments on commit 7080574

Please sign in to comment.