Skip to content

standardize() fails in some cases #442

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

Open
wants to merge 11 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,7 +104,7 @@
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 / lint

file=R/standardize.models.R,line=107,col=33,[function_argument_linter] Arguments without defaults should come before arguments with defaults.

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, ...)
model_data <- insight::get_data(x, source = "mf", verbose = FALSE)
Expand Down Expand Up @@ -263,10 +263,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 @@ -364,7 +364,7 @@


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

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

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/standardize.models.R,line=367,col=13,[undesirable_function_linter] Avoid undesirable function "mapply".

Check warning on line 367 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=367,col=13,[undesirable_function_linter] Avoid undesirable function "mapply".
SIMPLIFY = FALSE,
MoreArgs = list(
y_data = y_data, m_data = m_data,
Expand All @@ -390,7 +390,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 393 in R/standardize.models.R

View workflow job for this annotation

GitHub Actions / lint / lint

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

Check warning on line 393 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=393,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 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 @@ -122,12 +133,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