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

standardize() fails in some cases #442

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions R/standardize.models.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
weights = weights,
verbose = verbose,
include_response = include_response,
update_expr = stats::update(x, data = data_std),
update_expr = str2lang("stats::update(x, data = data_std)"),
...
)
}
Expand All @@ -104,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 @@ -272,10 +272,10 @@
on.exit(.update_failed())

if (isTRUE(verbose)) {
model_std <- eval(substitute(update_expr))
model_std <- eval(update_expr)
} else {
utils::capture.output({
model_std <- eval(substitute(update_expr))
model_std <- eval(update_expr)
})
}

Expand Down Expand Up @@ -373,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 @@ -399,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 @@ -410,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 @@ -484,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
15 changes: 14 additions & 1 deletion tests/testthat/test-standardize_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
m0 <- lm(Sepal.Length ~ Species * Petal.Width, data = iris_z)
m1 <- lm(Sepal.Length ~ Species * Petal.Width, data = iris2)
model <- standardize(m1)
expect_identical(coef(m0), coef(model))
expect_equal(coef(m0), coef(model), tolerance = 1e-5)
})

test_that("standardize.lm, edge case (intercept only)", {
iris2 <- na.omit(iris)
iris_z <- standardize(iris2)
m0 <- lm(Sepal.Length ~ 1, data = iris_z)
m1 <- lm(Sepal.Length ~ 1, data = iris2)
model <- standardize(m1)
expect_equal(coef(m0), coef(model), tolerance = 1e-5)
})

test_that("standardize, mlm", {
Expand Down Expand Up @@ -55,7 +64,7 @@


# Transformations ---------------------------------------------------------
test_that("transformations", {

Check warning on line 67 in tests/testthat/test-standardize_models.R

View workflow job for this annotation

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

file=tests/testthat/test-standardize_models.R,line=67,col=30,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.
skip_if_not_installed("effectsize")
# deal with log / sqrt terms
expect_message(standardize(lm(mpg ~ sqrt(cyl) + log(hp), mtcars)))
Expand All @@ -71,12 +80,14 @@
expect_equal(
effectsize::standardize_parameters(fit_exp, method = "refit")[2, 2],
unname(coef(fit_scale1)[2]),
tolerance = 1e-4
ignore_attr = TRUE

Check warning on line 84 in tests/testthat/test-standardize_models.R

View workflow job for this annotation

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

file=tests/testthat/test-standardize_models.R,line=84,col=5,[error] unexpected symbol
)

expect_equal(
effectsize::standardize_parameters(fit_exp, method = "basic")[2, 2],
unname(coef(fit_scale2)[2]),
tolerance = 1e-4
ignore_attr = TRUE
)

Expand Down Expand Up @@ -125,12 +136,14 @@
expect_equal(
stdREFIT[[2]],
effectsize::standardize_parameters(m, method = "posthoc")[[2]],
tolerance = 1e-4
ignore_attr = TRUE
)

expect_equal(
stdREFIT[[2]],
effectsize::standardize_parameters(m, method = "basic")[[2]],
tolerance = 1e-4
ignore_attr = TRUE
)
})
Expand Down
Loading