Skip to content

Commit

Permalink
Merge branch 'issue-322': closes #322
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinsimpson committed Dec 14, 2024
2 parents 405f5fd + dcaaf35 commit 0672df9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: gratia
Version: 0.9.2.9013
Version: 0.9.2.9014
Title: Graceful 'ggplot'-Based Graphics and Other Functions for GAMs Fitted Using 'mgcv'
Authors@R: c(person(given = "Gavin L.", family = "Simpson",
email = "ucfagls@gmail.com",
Expand Down Expand Up @@ -33,7 +33,8 @@ Imports:
cli,
nlme,
ggokabeito,
withr
withr,
scales
Suggests:
gamm4,
lme4,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ export(variance_comp)
export(vars_from_label)
export(which_smooths)
export(worm_plot)
importFrom(cli,format_error)
importFrom(cli,pluralize)
importFrom(cli,qty)
importFrom(cli,style_dim)
importFrom(cli,symbol)
importFrom(dplyr,"%>%")
Expand Down Expand Up @@ -533,6 +535,7 @@ importFrom(rlang,"!!")
importFrom(rlang,"%||%")
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,abort)
importFrom(rlang,dots_list)
importFrom(rlang,enexpr)
importFrom(rlang,enquo)
Expand All @@ -546,6 +549,7 @@ importFrom(rlang,has_name)
importFrom(rlang,inject)
importFrom(rlang,parse_expr)
importFrom(rlang,set_names)
importFrom(scales,ordinal_format)
importFrom(stats,IQR)
importFrom(stats,as.formula)
importFrom(stats,binomial)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

* `conditional_values()` and its `draw()` method compute and plot predictions
from a fitted GAM that are conditional on one or more covariates. The
function is a wrapper around `fitted_values()` but allows the user simple ways to specify which covariates to condition on and at what values those covariates should take. It provides similar functionality to
function is a wrapper around `fitted_values()` but allows the user simple ways
to specify which covariates to condition on and at what values those
covariates should take. It provides similar functionality to
`marginaleffects::plot_predictions()`, but is simpler. See #300.

* `penalty()` and `basis()` can now allow the smooth to be reparameterized such
Expand Down
48 changes: 48 additions & 0 deletions R/conditional-values.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,61 @@
range(x, na.rm = TRUE)
}

#' @importFrom scales ordinal_format
#' @importFrom cli format_error qty
#' @importFrom rlang abort
`process_condition` <- function(condition, data, variables, n_vals = 100) {
# condition: the thing passed as condition to conditional_values
# data: a data frame to use for evaluating the fitted values
# variables: the variables used in the model
# n_vals: how many values of the x condition to create
c_list <- is.list(condition)
nms <- names(condition)
nms_len <- nchar(nms)
# check if any names are "" == 0 characters, for those elements with such
# names, check if that element of conditions is the name of a variable in the
# model. If it is we don't need to do anything. If it isn't, check if it is a
# helper: if it is a helper, throw an error but prompt user to check if they
# forgot to name that element. If it is'nt a helper, throw a less specific
# error
no_nms <- nms_len == 0
if (any(no_nms)) {
is_zero <- which(no_nms)
# check if the unnamed element contains the name of a variable in the model
nm_in_model <- condition[is_zero] %in% variables
# drop any that are in models
no_nms[is_zero[which(nm_in_model)]] <- FALSE
is_zero <- is_zero[!nm_in_model]
# check if any remaining unnamed elements contain helpers
allowed <- c("threenum", "fivenum", "quartile", "minmax", "decile")
is_allowed <- condition[is_zero] %in% allowed
# report this problem
msg_allowed <- NULL
if (any(is_allowed)) {
which_allowed <- is_zero[is_allowed]
n_allowed <- length(which_allowed)
str_allowed <- scales::ordinal_format()(which_allowed)
msg_allowed <- "The {str_allowed} element{?s} of {.var condition} {?is/are} unnamed, but {?contains a/contains} function name{?s}."
}
# drop any that are helpers
no_nms[is_zero] <- FALSE
is_zero <- is_zero[!is_allowed]
# if there are any remaining, then format a different message
msg_wrong <- NULL
if ((n_wrong <- length(is_zero)) > 0L) {
str_wrong <- scales::ordinal_format()(is_zero)
msg_wrong <- "The {str_wrong} element{?s} of {.var condition} {?is/are} unnamed but {?does/do} not refer to {?a variable/variables} in the model."
}
msg_qty <- length(c(msg_wrong, msg_allowed))
msg <- c("{.strong Error in the supplied {.var condition}:}",
"x" = msg_wrong,
"x" = msg_allowed,
"i" = "Did you forget to name {qty(msg_qty)} {?this/these} element{?s}?"
)
if (!is.null(msg_allowed) | !is.null(msg_wrong)) {
rlang::abort(format_error(msg))
}
}
if (is.null(nms)) {
nms <- rep(list(NULL), 4)
}
Expand Down

0 comments on commit 0672df9

Please sign in to comment.