Skip to content

Commit

Permalink
changed argument precision to resolution to close #142
Browse files Browse the repository at this point in the history
  • Loading branch information
beckyfisher committed Sep 21, 2023
1 parent 334a2ec commit 727d670
Show file tree
Hide file tree
Showing 42 changed files with 204 additions and 204 deletions.
10 changes: 5 additions & 5 deletions R/amend.R
Expand Up @@ -27,7 +27,7 @@
#'
#' @export
amend <- function(object, drop, add, loo_controls, x_range = NA,
precision = 1000, sig_val = 0.01, priors) {
resolution = 1000, sig_val = 0.01, priors) {
UseMethod("amend")
}

Expand All @@ -46,7 +46,7 @@ amend <- function(object, drop, add, loo_controls, x_range = NA,
#'
#' @export
amend.bayesmanecfit <- function(object, drop, add, loo_controls, x_range = NA,
precision = 1000, sig_val = 0.01, priors) {
resolution = 1000, sig_val = 0.01, priors) {
general_error <- paste(
"Nothing to amend, please specify a proper model to either add or drop, or",
"changes to loo_controls;\n Returning original model set."
Expand All @@ -60,7 +60,7 @@ amend.bayesmanecfit <- function(object, drop, add, loo_controls, x_range = NA,
if (!missing(drop)) {chk_character(drop)}
if (!missing(add)) {chk_character(add)}
if (!is.na(x_range[1])) {chk_numeric(x_range)}
chk_numeric(precision)
chk_numeric(resolution)
chk_numeric(sig_val)
if(!inherits(object, "bayesmanecfit")){
stop("object is not of class bayesmanecfit")
Expand Down Expand Up @@ -151,13 +151,13 @@ amend.bayesmanecfit <- function(object, drop, add, loo_controls, x_range = NA,
}
formulas <- lapply(mod_fits, extract_formula)
mod_fits <- expand_manec(mod_fits, formula = formulas, x_range = x_range,
precision = precision, sig_val = sig_val,
resolution = resolution, sig_val = sig_val,
loo_controls = loo_controls)
if (length(mod_fits) > 1) {
allot_class(mod_fits, c("bayesmanecfit", "bnecfit"))
} else {
mod_fits <- expand_nec(mod_fits[[1]], formula = formula, x_range = x_range,
precision = precision, sig_val = sig_val,
resolution = resolution, sig_val = sig_val,
loo_controls = loo_controls, model = names(mod_fits))
allot_class(mod_fits, c("bayesnecfit", "bnecfit"))
}
Expand Down
8 changes: 4 additions & 4 deletions R/average_estimates.R
Expand Up @@ -44,7 +44,7 @@
average_estimates <- function(x, estimate = "nec", ecx_val = 10,
posterior = FALSE, type = "absolute",
hormesis_def = "control", sig_val = 0.01,
precision = 1000, x_range = NA, xform = identity,
resolution = 1000, x_range = NA, xform = identity,
prob_vals = c(0.5, 0.025, 0.975)) {
if (!is.list(x) | is.null(names(x))) {
stop("Argument x must be a named list")
Expand All @@ -57,7 +57,7 @@ average_estimates <- function(x, estimate = "nec", ecx_val = 10,
chk_character(hormesis_def)
chk_numeric(ecx_val)
chk_numeric(sig_val)
chk_numeric(precision)
chk_numeric(resolution)
if (!inherits(xform, "function")) {
stop("xform must be a function.")
}
Expand All @@ -69,13 +69,13 @@ average_estimates <- function(x, estimate = "nec", ecx_val = 10,
posterior_list <- lapply(x, return_nec_post, xform = xform)
}
if (estimate == "ecx") {
posterior_list <- lapply(x, ecx, ecx_val = ecx_val, precision = precision,
posterior_list <- lapply(x, ecx, ecx_val = ecx_val, resolution = resolution,
posterior = TRUE, type = type,
hormesis_def = hormesis_def, x_range = x_range,
xform = xform)
}
if (estimate == "nsec") {
posterior_list <- lapply(x, nsec, sig_val = sig_val, precision = precision,
posterior_list <- lapply(x, nsec, sig_val = sig_val, resolution = resolution,
posterior = TRUE, hormesis_def = hormesis_def,
x_range = x_range, xform = xform)
}
Expand Down
2 changes: 1 addition & 1 deletion R/bayesmanecfit-class.R
Expand Up @@ -29,7 +29,7 @@
#' @slot w_residuals Model-weighted residual values
#' (i.e. observed - w_predicted_y).
#' @slot w_pred_vals A \code{\link[base]{list}} containing model-weighted
#' posterior predicted values based on the supplied \code{precision} and
#' posterior predicted values based on the supplied \code{resolution} and
#' \code{x_range}.
#' @slot w_nec The summary stats (median and 95% credibility intervals) of
#' w_nec_posterior.
Expand Down
2 changes: 1 addition & 1 deletion R/bayesnecfit-class.R
Expand Up @@ -23,7 +23,7 @@
#' \code{\link[stats]{formula}}.
#' @slot pred_vals A \code{\link[base]{list}} containing a
#' \code{\link[base]{data.frame}} of summary posterior predicted values
#' and a vector containing based on the supplied \code{precision} and
#' and a vector containing based on the supplied \code{resolution} and
#' \code{x_range}.
#' @slot top The estimate for parameter "top" in the fitted model.
#' @slot beta The estimate for parameter "beta" in the fitted model.
Expand Down
12 changes: 6 additions & 6 deletions R/bnec.R
Expand Up @@ -10,7 +10,7 @@
#' the \code{formula}.
#' @param x_range A range of predictor values over which to consider extracting
#' ECx.
#' @param precision The length of the predictor vector used for posterior
#' @param resolution The length of the predictor vector used for posterior
#' predictions, and over which to extract ECx values. Large values will be
#' slower but more precise.
#' @param sig_val Probability value to use as the lower quantile to test
Expand Down Expand Up @@ -191,10 +191,10 @@
#' @importFrom chk chk_number
#'
#' @export
bnec <- function(formula, data, x_range = NA, precision = 1000, sig_val = 0.01,
bnec <- function(formula, data, x_range = NA, resolution = 1000, sig_val = 0.01,
loo_controls, x_var = NULL, y_var = NULL, trials_var = NULL,
model = NULL, random = NULL, random_vars = NULL, ...) {
chk_number(precision)
chk_number(resolution)
chk_number(sig_val)

mf <- match.call(expand.dots = FALSE)
Expand Down Expand Up @@ -232,13 +232,13 @@ bnec <- function(formula, data, x_range = NA, precision = 1000, sig_val = 0.01,
}
formulas <- lapply(mod_fits, extract_formula)
mod_fits <- expand_manec(mod_fits, formula = formulas, x_range = x_range,
precision = precision, sig_val = sig_val,
resolution = resolution, sig_val = sig_val,
loo_controls = loo_controls)
if (length(mod_fits) > 1) {
allot_class(mod_fits, c("bayesmanecfit", "bnecfit"))
} else {
mod_fits <- expand_nec(mod_fits[[1]], formula = formula,
x_range = x_range, precision = precision,
x_range = x_range, resolution = resolution,
sig_val = sig_val, loo_controls = loo_controls,
model = names(mod_fits))
allot_class(mod_fits, c("bayesnecfit", "bnecfit"))
Expand All @@ -247,7 +247,7 @@ bnec <- function(formula, data, x_range = NA, precision = 1000, sig_val = 0.01,
mod_fit <- fit_bayesnec(formula = formula, data = data, model = model,
brm_args = brm_args)
mod_fit <- expand_nec(mod_fit, formula = formula, x_range = x_range,
precision = precision, sig_val = sig_val,
resolution = resolution, sig_val = sig_val,
loo_controls = loo_controls, model = model)
allot_class(mod_fit, c("bayesnecfit", "bnecfit"))
}
Expand Down
20 changes: 10 additions & 10 deletions R/bnec_newdata.R
Expand Up @@ -4,7 +4,7 @@
#'
#' @param x An object of class \code{\link{bayesnecfit}} or
#' \code{\link{bayesmanecfit}} as returned by \code{\link{bnec}}.
#' @param precision A \code{\link[base]{numeric}} vector of length 1 indicating
#' @param resolution A \code{\link[base]{numeric}} vector of length 1 indicating
#' the number of x values over which to predict values.
#' @param x_range A \code{\link[base]{numeric}} vector of length 2 indicating
#' the range of x values over which to make predictions.
Expand All @@ -15,16 +15,16 @@
#' \dontrun{
#' library(bayesnec)
#' nec4param <- pull_out(manec_example, model = "nec4param")
#' # Make fine precision, predict out of range
#' newdata <- bnec_newdata(nec4param, precision = 200, x_range = c(0, 4))
#' # Make fine resolution, predict out of range
#' newdata <- bnec_newdata(nec4param, resolution = 200, x_range = c(0, 4))
#' nrow(newdata) == 200
#' all(range(newdata$x) == c(0, 4))
#' newdata2 <- bnec_newdata(manec_example) # default size
#' nrow(newdata2) == 100
#' }
#'
#' @export
bnec_newdata <- function(x, precision = 100, x_range = NA) {
bnec_newdata <- function(x, resolution = 100, x_range = NA) {
UseMethod("bnec_newdata")
}

Expand All @@ -38,16 +38,16 @@ bnec_newdata <- function(x, precision = 100, x_range = NA) {
#' @importFrom stats model.frame
#' @noRd
#' @export
bnec_newdata.bayesnecfit <- function(x, precision = 100, x_range = NA) {
check_args_newdata(precision, x_range)
bnec_newdata.bayesnecfit <- function(x, resolution = 100, x_range = NA) {
check_args_newdata(resolution, x_range)
data <- model.frame(x$bayesnecformula, data = x$fit$data)
x_var <- attr(data, "bnec_pop")[["x_var"]]
fit <- x$fit
x_vec <- fit$data[[x_var]]
if (any(is.na(x_range))) {
x_seq <- seq(min(x_vec), max(x_vec), length = precision)
x_seq <- seq(min(x_vec), max(x_vec), length = resolution)
} else {
x_seq <- seq(min(x_range), max(x_range), length = precision)
x_seq <- seq(min(x_range), max(x_range), length = resolution)
}
newdata <- data.frame(x_seq)
names(newdata) <- x_var
Expand All @@ -68,9 +68,9 @@ bnec_newdata.bayesnecfit <- function(x, precision = 100, x_range = NA) {
#' @inherit bnec_newdata description return examples
#' @noRd
#' @export
bnec_newdata.bayesmanecfit <- function(x, precision = 100, x_range = NA) {
bnec_newdata.bayesmanecfit <- function(x, resolution = 100, x_range = NA) {
model_set <- names(x$mod_fits)
bayesnecfit_x <- pull_out(x, model = model_set[1]) |>
suppressMessages()
bnec_newdata(bayesnecfit_x, precision, x_range)
bnec_newdata(bayesnecfit_x, resolution, x_range)
}
6 changes: 3 additions & 3 deletions R/bnecfit-methods.R
Expand Up @@ -127,7 +127,7 @@ c.bnecfit <- function(x, ...) {
#'
#' @export
update.bnecfit <- function(object, newdata = NULL, recompile = NULL,
x_range = NA, precision = 1000, sig_val = 0.01,
x_range = NA, resolution = 1000, sig_val = 0.01,
loo_controls, force_fit = FALSE, ...) {
original_class <- grep("bayes", class(object), value = TRUE)
if (!original_class %in% c("bayesnecfit", "bayesmanecfit")) {
Expand Down Expand Up @@ -178,7 +178,7 @@ update.bnecfit <- function(object, newdata = NULL, recompile = NULL,
formulas <- lapply(object, extract_formula)
if (length(object) > 1) {
object <- expand_manec(object, formula = formulas, x_range = x_range,
precision = precision, sig_val = sig_val,
resolution = resolution, sig_val = sig_val,
loo_controls = loo_controls)
allot_class(object, c("bayesmanecfit", "bnecfit"))
} else if (length(object) == 1) {
Expand All @@ -187,7 +187,7 @@ update.bnecfit <- function(object, newdata = NULL, recompile = NULL,
" specified incorrect arguments? See ?update.bnecfit")
}
mod_fits <- expand_nec(object[[1]], formula = formulas[[1]],
x_range = x_range, precision = precision,
x_range = x_range, resolution = resolution,
sig_val = sig_val, loo_controls = loo_controls,
model = names(object))
allot_class(mod_fits, c("bayesnecfit", "bnecfit"))
Expand Down
8 changes: 4 additions & 4 deletions R/compare_estimates.R
Expand Up @@ -33,7 +33,7 @@
#' @export
compare_estimates <- function(x, comparison = "nec", ecx_val = 10,
type = "absolute", hormesis_def = "control",
sig_val = 0.01, precision = 100, x_range = NA) {
sig_val = 0.01, resolution = 100, x_range = NA) {
if ((comparison %in% c("nec", "ecx", "nsec")) == FALSE) {
stop("comparison must be one of nec, ecx or nsec.")
}
Expand All @@ -47,7 +47,7 @@ compare_estimates <- function(x, comparison = "nec", ecx_val = 10,
Please see ?ecx for more details.")
}
chk_numeric(sig_val)
chk_numeric(precision)
chk_numeric(resolution)
if (is.na(x_range[1])) {
x_range <- return_x_range(x)
} else {
Expand All @@ -57,12 +57,12 @@ compare_estimates <- function(x, comparison = "nec", ecx_val = 10,
posterior_list <- lapply(x, return_nec_post, xform = identity)
}
if (comparison == "ecx") {
posterior_list <- lapply(x, ecx, ecx_val = ecx_val, precision = precision,
posterior_list <- lapply(x, ecx, ecx_val = ecx_val, resolution = resolution,
posterior = TRUE, type = type,
hormesis_def = hormesis_def, x_range = x_range)
}
if (comparison == "nsec") {
posterior_list <- lapply(x, nsec, sig_val = sig_val, precision = precision,
posterior_list <- lapply(x, nsec, sig_val = sig_val, resolution = resolution,
posterior = TRUE, hormesis_def = hormesis_def,
x_range = x_range)
}
Expand Down
10 changes: 5 additions & 5 deletions R/compare_fitted.R
Expand Up @@ -7,12 +7,12 @@
#' @inheritParams compare_posterior
#' @param make_newdata Should the
#' user allow the package to create \code{newdata} for predictions?
#' If so, arguments \code{precision} and \code{x_range} will be used. Defaults
#' If so, arguments \code{resolution} and \code{x_range} will be used. Defaults
#' to TRUE. See details.
#'
#' @details The argument \code{make_newdata} is relevant to those who want the
#' package to create a data.frame from which to make predictions. This is done
#' via \code{\link{bnec_newdata}} and uses arguments \code{precision} and
#' via \code{\link{bnec_newdata}} and uses arguments \code{resolution} and
#' \code{x_range}. If \code{make_newdata = FALSE} and no additional
#' \code{newdata} argument is provided (via \code{...}), then the predictions
#' are made for the raw data. Else, to generate predictions for a specific
Expand Down Expand Up @@ -42,7 +42,7 @@
#' }
#'
#' @export
compare_fitted <- function(x, precision = 50, x_range = NA,
compare_fitted <- function(x, resolution = 50, x_range = NA,
make_newdata = TRUE, ...) {
if (is.na(x_range[1])) {
x_range <- return_x_range(x)
Expand All @@ -52,11 +52,11 @@ compare_fitted <- function(x, precision = 50, x_range = NA,
names(posterior_list) <- names(x)
for (i in seq_along(posterior_list)) {
newdata_list <- newdata_eval_fitted(
x[[i]], precision = precision, x_range = x_range,
x[[i]], resolution = resolution, x_range = x_range,
make_newdata = make_newdata, fct_eval = "compare_fitted", ...
)
x_vec <- newdata_list$x_vec
precision <- newdata_list$precision
resolution <- newdata_list$resolution
dot_list$newdata <- newdata_list$newdata
dot_list$re_formula <- newdata_list$re_formula
dot_list$object <- x[[i]]
Expand Down
18 changes: 9 additions & 9 deletions R/compare_posterior.R
Expand Up @@ -11,7 +11,7 @@
#' "nec", "nsec", "ecx" or "fitted".
#' @param make_newdata Only used if \code{comparison = "fitted"}. Should the
#' user allow the package to create \code{newdata} for predictions?
#' If so, arguments \code{precision} and \code{x_range} will be used. Defaults
#' If so, arguments \code{resolution} and \code{x_range} will be used. Defaults
#' to TRUE. See details.
#' @param ... Further arguments that control posterior predictions via
#' \code{\link[brms]{posterior_epred}}.
Expand All @@ -37,7 +37,7 @@
#' The argument \code{make_newdata} is only used if
#' \code{comparison = "fitted"}. It is relevant to those who want the package
#' to create a data.frame from which to make predictions. This is done via
#' \code{\link{bnec_newdata}} and uses arguments \code{precision} and
#' \code{\link{bnec_newdata}} and uses arguments \code{resolution} and
#' \code{x_range}. If \code{make_newdata = FALSE} and no additional
#' \code{newdata} argument is provided (via \code{...}), then the predictions
#' are made for the raw data. Else, to generate predictions for a specific
Expand Down Expand Up @@ -65,7 +65,7 @@
#' @export
compare_posterior <- function(x, comparison = "nec", ecx_val = 10,
type = "absolute", hormesis_def = "control",
sig_val = 0.01, precision, x_range = NA,
sig_val = 0.01, resolution, x_range = NA,
make_newdata = TRUE, ...) {
if (!is.list(x) | is.null(names(x))) {
stop("Argument x must be a named list.")
Expand All @@ -74,18 +74,18 @@ compare_posterior <- function(x, comparison = "nec", ecx_val = 10,
stop("Argument comparison must be a character vector.")
}
if (comparison != "fitted") {
if (missing(precision)) {
precision <- 500
if (missing(resolution)) {
resolution <- 500
}
out <- compare_estimates(x = x, comparison = comparison, ecx_val = ecx_val,
type = type, hormesis_def = hormesis_def,
sig_val = sig_val, precision = precision,
sig_val = sig_val, resolution = resolution,
x_range = x_range)
} else {
if (missing(precision)) {
precision <- 50
if (missing(resolution)) {
resolution <- 50
}
out <- compare_fitted(x = x, precision = precision, x_range = x_range,
out <- compare_fitted(x = x, resolution = resolution, x_range = x_range,
make_newdata = make_newdata, ...)
}
out
Expand Down

0 comments on commit 727d670

Please sign in to comment.