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 2 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
6 changes: 6 additions & 0 deletions R/standardize.models.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
return(x)
}

# check model formula. Some notations don't work when standardizing data
if (!insight::formula_ok(x, verbose = verbose)) {
insight::format_alert(insight::color_text("Model cannot be standardized.", "red"))
return(x)
}
strengejacke marked this conversation as resolved.
Show resolved Hide resolved

data_std <- NULL # needed to avoid note
.standardize_models(x,
robust = robust, two_sd = two_sd,
Expand All @@ -96,10 +102,10 @@
weights = TRUE,
verbose = TRUE,
include_response = TRUE,
update_expr,

Check warning on line 105 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=105,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 108 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=108,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 +371,7 @@


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

Check warning on line 374 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=374,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 +397,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 400 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=400,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 +408,7 @@
}


text <- utils::capture.output({

Check warning on line 411 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=411,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 +482,7 @@

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

Check warning on line 485 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=485,col=5,[comparison_negation_linter] Use x != y, not !(x == y).
!info$is_survival &&
!info$is_censored

Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-standardize_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ 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_message(expect_warning(standardise(m), regex = "Looks like"))

data(mtcars)
m <- lm(mtcars$mpg ~ mtcars$hp)
expect_message(expect_warning(standardise(m), regex = "model formulas"))

m <- lm(mtcars[, 1] ~ hp, data = mtcars)
expect_message(expect_warning(standardise(m), regex = "indexed data"))
})
etiennebacher marked this conversation as resolved.
Show resolved Hide resolved


# Transformations ---------------------------------------------------------
test_that("transformations", {
skip_if_not_installed("effectsize")
Expand Down
Loading