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 9 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 @@ -84,7 +84,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)"),

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

View check run for this annotation

Codecov / codecov/patch

R/standardize.models.R#L87

Added line #L87 was not covered by tests
...
)
}
Expand All @@ -96,10 +96,10 @@
weights = TRUE,
verbose = TRUE,
include_response = TRUE,
update_expr,

Check warning on line 99 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=99,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 102 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=102,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 @@ -264,10 +264,10 @@
on.exit(.update_failed())

if (isTRUE(verbose)) {
model_std <- eval(substitute(update_expr))
model_std <- eval(update_expr)

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

View check run for this annotation

Codecov / codecov/patch

R/standardize.models.R#L267

Added line #L267 was not covered by tests
} else {
utils::capture.output({
model_std <- eval(substitute(update_expr))
model_std <- eval(update_expr)

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

View check run for this annotation

Codecov / codecov/patch

R/standardize.models.R#L270

Added line #L270 was not covered by tests
})
}

Expand Down Expand Up @@ -365,7 +365,7 @@


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

Check warning on line 368 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=368,col=13,[undesirable_function_linter] Avoid undesirable function "mapply".
SIMPLIFY = FALSE,
MoreArgs = list(
y_data = y_data, m_data = m_data,
Expand All @@ -389,9 +389,9 @@
# y_data = y_data, m_data = m_data,
# y_data_std = y_data_std, m_data_std = m_data_std)
#
# control.value <- temp_vals[1]

Check warning on line 392 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=392,col=7,[commented_code_linter] Remove commented code.
# treat.value <- temp_vals[2]

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=7,[commented_code_linter] Remove commented code.
# if (verbose) insight::format_alert("control and treatment values have been rescaled to their standardized scales.")

Check warning on line 394 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=394,col=7,[commented_code_linter] Remove commented code.

Check warning on line 394 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=394,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 +402,7 @@
}


text <- utils::capture.output({

Check warning on line 405 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=405,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 @@ -447,7 +447,7 @@
# Find log-terms inside model formula, and return "clean" term names
.log_terms <- function(model, data) {
x <- insight::find_terms(model, flatten = TRUE)
# log_pattern <- "^log\\((.*)\\)"

Check warning on line 450 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=450,col=5,[commented_code_linter] Remove commented code.
log_pattern <- "(log\\(log|log|log1|log10|log1p|log2)\\(([^,\\+)]*).*"
out <- insight::trim_ws(gsub(log_pattern, "\\2", grep(log_pattern, x, value = TRUE)))
intersect(colnames(data), out)
Expand Down Expand Up @@ -476,7 +476,7 @@

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

Check warning on line 479 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=479,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 @@ test_that("standardize.lm", {
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 @@ -48,12 +57,14 @@ test_that("transformations", {
expect_equal(
effectsize::standardize_parameters(fit_exp, method = "refit")[2, 2],
unname(coef(fit_scale1)[2]),
tolerance = 1e-4
ignore_attr = TRUE
)

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 @@ -103,12 +114,14 @@ test_that("weights", {
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