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

Warn user for invalid formula #562

Merged
merged 3 commits into from
Nov 21, 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
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 @@
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 All @@ -96,10 +104,10 @@
weights = TRUE,
verbose = TRUE,
include_response = TRUE,
update_expr,

Check warning on line 107 in R/standardize.models.R

View workflow job for this annotation

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

file=R/standardize.models.R,line=107,col=33,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
...) {
m_info <- .get_model_info(x, ...)
data <- insight::get_data(x, source = "mf", verbose = FALSE)

Check warning on line 110 in R/standardize.models.R

View workflow job for this annotation

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

file=R/standardize.models.R,line=110,col=3,[object_overwrite_linter] 'data' is an exported object from package 'utils'. Avoid re-using such symbols.

if (isTRUE(attr(data, "is_subset"))) {
insight::format_error("Cannot standardize a model fit with a 'subset = '.")
Expand Down Expand Up @@ -365,7 +373,7 @@


if (!is.null(covs)) {
covs <- mapply(.rescale_fixed_values, covs, names(covs),

Check warning on line 376 in R/standardize.models.R

View workflow job for this annotation

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

file=R/standardize.models.R,line=376,col=13,[undesirable_function_linter] Avoid undesirable function "mapply".
SIMPLIFY = FALSE,
MoreArgs = list(
y_data = y_data, m_data = m_data,
Expand All @@ -391,7 +399,7 @@
#
# control.value <- temp_vals[1]
# treat.value <- temp_vals[2]
# if (verbose) insight::format_alert("control and treatment values have been rescaled to their standardized scales.")

Check warning on line 402 in R/standardize.models.R

View workflow job for this annotation

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

file=R/standardize.models.R,line=402,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 121 characters.
# }

if (verbose && !all(c(control.value, treat.value) %in% c(0, 1))) {
Expand All @@ -402,7 +410,7 @@
}


text <- utils::capture.output({

Check warning on line 413 in R/standardize.models.R

View workflow job for this annotation

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

file=R/standardize.models.R,line=413,col=3,[object_overwrite_linter] 'text' is an exported object from package 'graphics'. Avoid re-using such symbols.
model_std <- stats::update(x,
model.y = y_std, model.m = m_std,
# control.value = control.value, treat.value = treat.value
Expand Down Expand Up @@ -476,7 +484,7 @@

# check if model has a response variable that should not be standardized.
info$is_linear &&
!info$family == "inverse.gaussian" &&

Check warning on line 487 in R/standardize.models.R

View workflow job for this annotation

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

file=R/standardize.models.R,line=487,col=5,[comparison_negation_linter] Use x != y, not !(x == y).
!info$is_survival &&
!info$is_censored

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
Loading