Skip to content

Commit

Permalink
Make standardize() error messages clearer (#562)
Browse files Browse the repository at this point in the history
* Warn user for invalif formula

* add tests

* fix tests
  • Loading branch information
strengejacke authored Nov 21, 2024
1 parent 9baa22b commit 2741cdc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531")),
Expand Down
8 changes: 8 additions & 0 deletions R/standardize.models.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 27 additions & 5 deletions tests/testthat/test-standardize_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)))
})


Expand Down

0 comments on commit 2741cdc

Please sign in to comment.