From 2741cdc5c86dd2e1f47e45d5bf9db8d5c9db1c91 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 21 Nov 2024 19:55:19 +0100 Subject: [PATCH] Make `standardize()` error messages clearer (#562) * Warn user for invalif formula * add tests * fix tests --- DESCRIPTION | 2 +- R/standardize.models.R | 8 ++++++ tests/testthat/test-standardize_models.R | 32 ++++++++++++++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ba821b0ba..2325c062d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.13.0.12 +Version: 0.13.0.13 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531")), diff --git a/R/standardize.models.R b/R/standardize.models.R index a92ffe243..cf6062c78 100644 --- a/R/standardize.models.R +++ b/R/standardize.models.R @@ -78,6 +78,14 @@ standardize.default <- function(x, return(x) } + # check model formula. Some notations don't work when standardizing data + insight::formula_ok( + x, + action = "error", + prefix_msg = "Model cannot be standardized.", + verbose = verbose + ) + data_std <- NULL # needed to avoid note .standardize_models(x, robust = robust, two_sd = two_sd, diff --git a/tests/testthat/test-standardize_models.R b/tests/testthat/test-standardize_models.R index 706a4e6e7..d61caf450 100644 --- a/tests/testthat/test-standardize_models.R +++ b/tests/testthat/test-standardize_models.R @@ -31,6 +31,29 @@ test_that("standardize | errors", { }) +test_that("standardize | problematic formulas", { + data(mtcars) + m <- lm(mpg ~ hp, data = mtcars) + expect_equal( + coef(standardise(m)), + c(`(Intercept)` = -3.14935717633686e-17, hp = -0.776168371826586), + tolerance = 1e-4 + ) + + colnames(mtcars)[1] <- "1_mpg" + m <- lm(`1_mpg` ~ hp, data = mtcars) + expect_error(standardise(m), regex = "Looks like") + + # works interactive only + # data(mtcars) + # m <- lm(mtcars$mpg ~ mtcars$hp) + # expect_error(standardise(m), regex = "model formulas") + + m <- lm(mtcars[, 1] ~ hp, data = mtcars) + expect_error(standardise(m), regex = "indexed data") +}) + + # Transformations --------------------------------------------------------- test_that("transformations", { skip_if_not_installed("effectsize") @@ -206,15 +229,14 @@ test_that("standardize non-Gaussian response", { # variables evaluated in the environment $$$ ------------------------------ test_that("variables evaluated in the environment", { m <- lm(mtcars$mpg ~ mtcars$cyl + am, data = mtcars) - w <- capture_warnings(standardize(m)) - expect_true(any(grepl("mtcars$mpg", w, fixed = TRUE))) + w <- capture_error(standardize(m)) + expect_true(any(grepl("Using `$`", w, fixed = TRUE))) ## Note: # No idea why this is suddenly not giving a warning on older R versions. m <- lm(mtcars$mpg ~ mtcars$cyl + mtcars$am, data = mtcars) - warns <- capture_warnings(standardize(m)) - expect_true(any(grepl("mtcars$mpg", warns, fixed = TRUE))) - expect_true(any(grepl("No variables", warns, fixed = TRUE))) + w <- capture_error(standardize(m)) + expect_true(any(grepl("Using `$`", w, fixed = TRUE))) })