From 81fd43233cb60bcca3857c98e22a5d1e640d5431 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 17 Sep 2024 10:12:17 +0200 Subject: [PATCH] DRY --- R/equivalence_test.R | 11 ++--------- R/p_significance.R | 28 +++++++++++++++++++--------- R/rope.R | 11 ++--------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/R/equivalence_test.R b/R/equivalence_test.R index 589b8d7fc..fa089ed75 100644 --- a/R/equivalence_test.R +++ b/R/equivalence_test.R @@ -167,16 +167,9 @@ equivalence_test.data.frame <- function(x, range = "default", ci = 0.95, rvar_co # multiple ranges for the parameters - iterate over parameters and range if (is.list(range)) { - if (length(range) != ncol(x)) { - insight::format_error("Length of `range` (i.e. number of ROPE limits) should match the number of parameters.") - } # check if list of values contains only valid values - checks <- vapply(range, function(r) { - !all(r == "default") || !all(is.numeric(r)) || length(r) != 2 - }, logical(1)) - if (!all(checks)) { - insight::format_error("`range` should be 'default' or a vector of 2 numeric values (e.g., c(-0.1, 0.1)).") - } + .check_list_range(range, x) + # apply thresholds to each column l <- insight::compact_list(mapply( function(p, r) { equivalence_test( diff --git a/R/p_significance.R b/R/p_significance.R index c00917612..af075cef7 100644 --- a/R/p_significance.R +++ b/R/p_significance.R @@ -137,16 +137,9 @@ p_significance.data.frame <- function(x, threshold = "default", rvar_col = NULL, if (ncol(x) == 1) { ps <- .p_significance(x[, 1], threshold = threshold, ...) } else if (is.list(threshold)) { - if (length(threshold) != ncol(x)) { - insight::format_error("Length of `threshold` should match the number of parameters.") - } # check if list of values contains only valid values - checks <- vapply(threshold, function(r) { - !all(r == "default") || !all(is.numeric(r)) || length(r) > 2 - }, logical(1)) - if (!all(checks)) { - insight::format_error("`threshold` should be 'default' or a vector of 2 numeric values (e.g., c(-0.1, 0.1)).") - } + .check_list_range(threshold, x, larger_two = TRUE) + # apply thresholds to each column ps <- mapply( function(p, thres) { .p_significance( @@ -422,3 +415,20 @@ as.double.p_significance <- as.numeric.p_significance } threshold } + +.check_list_range <- function(range, params, larger_two = FALSE) { + if (length(range) != ncol(params)) { + insight::format_error("Length of `range` (i.e. number of ROPE limits) should match the number of parameters.") + } + # check if list of values contains only valid values + checks <- vapply(range, function(r) { + if (larger_two) { + !all(r == "default") || !all(is.numeric(r)) || length(r) > 2 + } else { + !all(r == "default") || !all(is.numeric(r)) || length(r) != 2 + } + }, logical(1)) + if (!all(checks)) { + insight::format_error("`range` should be 'default' or a vector of 2 numeric values (e.g., c(-0.1, 0.1)).") + } +} diff --git a/R/rope.R b/R/rope.R index 38f02f53c..a07726504 100644 --- a/R/rope.R +++ b/R/rope.R @@ -612,16 +612,9 @@ rope.sim <- function(x, range = "default", ci = 0.95, ci_method = "ETI", paramet #' @keywords internal .prepare_rope_df <- function(parms, range, ci, ci_method, verbose) { if (is.list(range)) { - if (length(range) != ncol(parms)) { - insight::format_error("Length of `range` (i.e. number of ROPE limits) should match the number of parameters.") - } # check if list of values contains only valid values - checks <- vapply(range, function(r) { - !all(r == "default") || !all(is.numeric(r)) || length(r) != 2 - }, logical(1)) - if (!all(checks)) { - insight::format_error("`range` should be 'default' or a vector of 2 numeric values (e.g., c(-0.1, 0.1)).") - } + .check_list_range(range, params) + # apply thresholds to each column tmp <- mapply( function(p, r) { rope(