Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more informative check_arg #1031

Merged
merged 9 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,4 @@ Config/testthat/edition: 3
Config/testthat/parallel: true
Config/Needs/website: easystats/easystatstemplate
Config/rcmdcheck/ignore-inconsequential-notes: true
Remotes: easystats/insight#938
2 changes: 1 addition & 1 deletion R/2_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ ci.glm <- function(x,
vcov_args = NULL,
verbose = TRUE,
...) {
method <- match.arg(method, choices = c("profile", "wald", "normal", "residual"))
method <- insight::validate_argument(method, c("profile", "wald", "normal", "residual"))

# No robust vcov for profile method
if (method == "profile") {
Expand Down
6 changes: 3 additions & 3 deletions R/bootstrap_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bootstrap_model.default <- function(model,

insight::check_if_installed("boot")

type <- match.arg(type, choices = c("ordinary", "parametric", "balanced", "permutation", "antithetic"))
type <- insight::validate_argument(type, c("ordinary", "parametric", "balanced", "permutation", "antithetic"))
parallel <- match.arg(parallel)

model_data <- data <- insight::get_data(model, verbose = FALSE) # nolint
Expand Down Expand Up @@ -156,7 +156,7 @@ bootstrap_model.merMod <- function(model,
...) {
insight::check_if_installed("lme4")

type <- match.arg(type, choices = c("parametric", "semiparametric"))
type <- insight::validate_argument(type, c("parametric", "semiparametric"))
parallel <- match.arg(parallel)

boot_function <- function(model) {
Expand Down Expand Up @@ -228,7 +228,7 @@ bootstrap_model.nestedLogit <- function(model,
...) {
insight::check_if_installed("boot")

type <- match.arg(type, choices = c("ordinary", "balanced", "permutation", "antithetic"))
type <- insight::validate_argument(type, c("ordinary", "balanced", "permutation", "antithetic"))
parallel <- match.arg(parallel)

model_data <- data <- insight::get_data(model, verbose = FALSE) # nolint
Expand Down
13 changes: 8 additions & 5 deletions R/ci_generic.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
if (is.null(method)) {
method <- "wald"
}
method <- match.arg(tolower(method), choices = c(
"wald", "ml1", "betwithin", "kr",
"satterthwaite", "kenward", "boot",
"profile", "residual", "normal"
))
method <- tolower(method)
method <- insight::validate_argument(
method,
c(
"wald", "ml1", "betwithin", "kr", "satterthwaite", "kenward", "boot",
"profile", "residual", "normal"
)
)

effects <- match.arg(effects)
component <- match.arg(component)
Expand Down
6 changes: 3 additions & 3 deletions R/extract_random_variances.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

# check for errors
if (is.null(out) && isTRUE(verbose)) {
insight::format_warning("Something went wrong when calculating random effects parameters. Only showing model's fixed effects now. You may use `effects=\"fixed\"` to speed up the call to `model_parameters()`.")
insight::format_warning("Something went wrong when calculating random effects parameters. Only showing model's fixed effects now. You may use `effects=\"fixed\"` to speed up the call to `model_parameters()`.") # nolint
}

out
Expand All @@ -45,7 +45,7 @@
ci_random = NULL,
verbose = FALSE,
...) {
component <- match.arg(component, choices = c("all", "conditional", "zero_inflated", "zi", "dispersion"))
component <- insight::validate_argument(component, c("all", "conditional", "zero_inflated", "zi", "dispersion"))

out <- suppressWarnings(
.extract_random_variances_helper(
Expand Down Expand Up @@ -208,7 +208,7 @@
)

# fix names for uncorrelated slope-intercepts
pattern <- paste0("(", paste0(insight::find_random(model, flatten = TRUE), collapse = "|"), ")\\.\\d+$")
pattern <- paste0("(", paste(insight::find_random(model, flatten = TRUE), collapse = "|"), ")\\.\\d+$")
out$Group <- gsub(pattern, "\\1", out$Group)

# remove non-used columns
Expand Down Expand Up @@ -336,7 +336,7 @@

# extract CI for random SD ------------------------

.random_sd_ci <- function(model,

Check warning on line 339 in R/extract_random_variances.R

View workflow job for this annotation

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

file=R/extract_random_variances.R,line=339,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 63 to at most 40.
out,
ci_method,
ci, ci_random,
Expand Down
16 changes: 8 additions & 8 deletions R/methods_glmmTMB.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @inheritParams simulate_model
#' @rdname model_parameters.merMod
#' @export
model_parameters.glmmTMB <- function(model,

Check warning on line 10 in R/methods_glmmTMB.R

View workflow job for this annotation

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

file=R/methods_glmmTMB.R,line=10,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 73 to at most 40.
ci = 0.95,
ci_method = "wald",
ci_random = NULL,
Expand Down Expand Up @@ -47,8 +47,8 @@
ci_method <- .check_df_method(ci_method)

# which components to return?
effects <- match.arg(effects, choices = c("fixed", "random", "all"))
component <- match.arg(component, choices = c("all", "conditional", "zi", "zero_inflated", "dispersion"))
effects <- insight::validate_argument(effects, c("fixed", "random", "all"))
component <- insight::validate_argument(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))

# standardize only works for fixed effects...
if (!is.null(standardize) && standardize != "refit") {
Expand Down Expand Up @@ -268,8 +268,8 @@
verbose = TRUE,
...) {
method <- tolower(method)
method <- match.arg(method, choices = c("wald", "normal", "ml1", "betwithin", "profile", "uniroot", "robust"))
component <- match.arg(component, choices = c("all", "conditional", "zi", "zero_inflated", "dispersion"))
method <- insight::validate_argument(method, c("wald", "normal", "ml1", "betwithin", "profile", "uniroot", "robust"))
component <- insight::validate_argument(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))

if (is.null(.check_component(x, component, verbose = verbose))) {
return(NULL)
Expand Down Expand Up @@ -315,8 +315,8 @@
component = "all",
verbose = TRUE,
...) {
component <- match.arg(component, choices = c("all", "conditional", "zi", "zero_inflated", "dispersion"))
effects <- match.arg(effects, choices = c("fixed", "random"))
component <- insight::validate_argument(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
effects <- insight::validate_argument(effects, c("fixed", "random"))

dot_args <- .check_dots(
dots = list(...),
Expand Down Expand Up @@ -374,10 +374,10 @@
#' @export
simulate_model.glmmTMB <- function(model,
iterations = 1000,
component = c("all", "conditional", "zi", "zero_inflated", "dispersion"),
component = "all",
verbose = FALSE,
...) {
component <- match.arg(component)
component <- insight::validate_argument(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
info <- insight::model_info(model, verbose = FALSE)

## TODO remove is.list() when insight 0.8.3 on CRAN
Expand Down
14 changes: 7 additions & 7 deletions R/methods_lme4.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,22 @@ model_parameters.merMod <- function(model,
ci_method <- tolower(ci_method)

if (isTRUE(bootstrap)) {
ci_method <- match.arg(
ci_method <- insight::validate_argument(
ci_method,
choices = c("hdi", "quantile", "ci", "eti", "si", "bci", "bcai")
c("hdi", "quantile", "ci", "eti", "si", "bci", "bcai")
)
} else {
ci_method <- match.arg(
ci_method <- insight::validate_argument(
ci_method,
choices = c(
c(
"wald", "normal", "residual", "ml1", "betwithin", "satterthwaite",
"kenward", "kr", "boot", "profile", "uniroot"
)
)
}

# which component to return?
effects <- match.arg(effects, choices = c("fixed", "random", "all"))
effects <- insight::validate_argument(effects, c("fixed", "random", "all"))
params <- params_random <- params_variance <- NULL

# post hoc standardize only works for fixed effects...
Expand Down Expand Up @@ -343,7 +343,7 @@ ci.merMod <- function(x,
iterations = 500,
...) {
method <- tolower(method)
method <- match.arg(method, choices = c(
method <- insight::validate_argument(method, c(
"wald", "ml1", "betwithin", "kr",
"satterthwaite", "kenward", "boot",
"profile", "residual", "normal"
Expand Down Expand Up @@ -379,7 +379,7 @@ standard_error.merMod <- function(model,
vcov_args = NULL,
...) {
dots <- list(...)
effects <- match.arg(effects, choices = c("fixed", "random"))
effects <- insight::validate_argument(effects, c("fixed", "random"))

if (effects == "random") {
out <- .standard_errors_random(model)
Expand Down
1 change: 1 addition & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
ifnotfound
}


.deprecated_warning <- function(old, new, verbose = TRUE) {
if (verbose) {
insight::format_warning(paste0(
Expand Down
2 changes: 1 addition & 1 deletion man/simulate_model.Rd

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

Loading