R/helpers.R

Defines functions get_powerscaling_details seq_cols `%||%` stop2 require_package remove_unwanted_vars rowsums_draws SW

SW <- function(...) {
  suppressWarnings(...)
}

rowsums_draws <- function(x) {
  posterior::draws_array(
    sum = rowSums(
      posterior::as_draws_array(x),
      dims = 2
    ),
    .nchains = posterior::nchains(x)
  )
}

remove_unwanted_vars <- function(x, excluded_variables = c("lprior", "log_lik", "lp__"), regex = TRUE, ...) {

  draws <- posterior::as_draws_df(x)

  draws <- posterior::subset_draws(draws, variable = excluded_variables, exclude = TRUE, regex = regex)

  return(draws)
}

require_package <- function(package, version = NULL, message = NULL) {
  if (!requireNamespace(package, quietly = TRUE)) {
    stop2("Please install the '", package, "' package", message)
  }
  if (!is.null(version)) {
    version <- as.package_version(version)
    if (utils::packageVersion(package) < version) {
      stop2("Please install package '", package,
            "' version ", version, " or higher.")
    }
  }
  invisible(TRUE)
}

stop2 <- function(...) {
  stop(..., call. = FALSE)
}


`%||%` <- function(x, y) {
  if (is.null(x)) x <- y
  x
}

seq_cols <- function(x) {
  seq_len(NCOL(x))
}


get_powerscaling_details <- function(x) {

  attr(x, "powerscaling")

}
n-kall/priorsense documentation built on Nov. 4, 2024, 10:30 p.m.