-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
#441
Comments
Looks like it comes from these lines: datawizard/R/standardize.models.R Lines 81 to 89 in 3ff2209
library(datawizard)
data(sleep)
d <- data_modify(sleep, group = as.integer(group) - 1L)
d_wide <- data_to_wide(
d,
names_from = "group",
values_from = "extra",
names_prefix = "group"
)
model1 <- lm(d_wide$group0 - d_wide$group1 ~ 1)
stats::update(model1, data = NULL)
#>
#> Call:
#> lm(formula = d_wide$group0 - d_wide$group1 ~ 1, data = NULL)
#>
#> Coefficients:
#> (Intercept)
#> -1.58
model2 <- lm(group0 - group1 ~ 1, data = d_wide)
stats::update(model2, data = NULL)
#> Error in eval(predvars, data, env): objet 'group0' introuvable @strengejacke I think you wrote this code, do you need how to fix this? |
Minimal reprex: data(iris)
model <- lm(Sepal.Length ~ 1, data = iris)
stats::update(model, data = NULL)
#> Error in eval(predvars, data, env): object 'Sepal.Length' not found Created on 2023-07-01 with reprex v2.0.2 |
The problem for your particular example is the returned data: library(datawizard)
data(sleep)
d <- data_modify(sleep, group = as.integer(group) - 1L)
d_wide <- data_to_wide(
d,
names_from = "group",
values_from = "extra",
names_prefix = "group"
)
model2 <- lm(group0 - group1 ~ 1, data = d_wide)
head(insight::get_data(model2, source = "mf"))
#> group0 - group1
#> 1 -1.2
#> 2 -2.4
#> 3 -1.3
#> 4 -1.3
#> 5 0.0
#> 6 -1.0 we need the variables The PR at least works for these cases: library(datawizard)
data(iris)
model <- lm(Sepal.Length ~ 1, data = iris)
standardize(model)
#>
#> Call:
#> lm(formula = Sepal.Length ~ 1, data = data_std)
#>
#> Coefficients:
#> (Intercept)
#> -4.079e-16
data(sleep)
d <- data_modify(sleep, group = as.integer(group) - 1L)
d_wide <- data_to_wide(
d,
names_from = "group",
values_from = "extra",
names_prefix = "group"
)
model1 <- lm(d_wide$group0 - d_wide$group1 ~ 1)
standardize(model1)
#> Warning: Using `$` in model formulas can produce unexpected results. Specify your
#> model using the `data` argument instead.
#> Try: group0 - group1 ~ 1,
#> data =
#> Warning: Using `$` in model formulas can produce unexpected results. Specify your
#> model using the `data` argument instead.
#> Try: group0 - group1 ~ 1,
#> data =
#> Warning: Using `$` in model formulas can produce unexpected results. Specify your
#> model using the `data` argument instead.
#> Try: group0 - group1 ~ 1,
#> data =
#> Warning: Using `$` in model formulas can produce unexpected results. Specify your
#> model using the `data` argument instead.
#> Try: group0 - group1 ~ 1,
#> data =
#> Warning: Using `$` in model formulas can produce unexpected results. Specify your
#> model using the `data` argument instead.
#> Try: group0 - group1 ~ 1,
#> data =
#>
#> Call:
#> lm(formula = d_wide$group0 - d_wide$group1 ~ 1, data = data_std)
#>
#> Coefficients:
#> (Intercept)
#> -1.58
model2 <- lm(group0 - group1 ~ 1, data = d_wide)
standardize(model2)
#>
#> Call:
#> lm(formula = group0 - group1 ~ 1, data = data_std)
#>
#> Coefficients:
#> (Intercept)
#> 5.266e-17 Created on 2023-07-01 with reprex v2.0.2 |
Note that if you have a composite variable (as in this example), and you standardize each part separately, you can easily get nonsense results... |
any workaround this? |
You would probably want to compute the composite variable beforehand (not in the formula): library(datawizard)
data(sleep)
d_wide <- sleep |>
data_modify(group = as.integer(group) - 1L) |>
data_to_wide(
names_from = "group",
values_from = "extra",
names_prefix = "group"
) |>
data_modify(
diff = group0 - group1
)
model1 <- lm(diff ~ 1, data = d_wide)
standardize(model1)
#>
#> Call:
#> lm(formula = diff ~ 1, data = data_std)
#>
#> Coefficients:
#> (Intercept)
#> 1.755e-17
#> |
Does the error still persist?
Error in eval(predvars, data, env) : object '12_month_remission' not found |
Cf easystats/report#379
Created on 2023-07-01 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: