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

report.lm: "Warning: Using $ in model formulas can produce unexpected results", but doing so errors #379

Open
rempsyc opened this issue Jul 1, 2023 · 3 comments

Comments

@rempsyc
Copy link
Sponsor Member

rempsyc commented Jul 1, 2023

It would be nice to get rid of the several warnings in R CMD check, but the proposed fix leads to an error:

library(report)

data(sleep)
d <- datawizard::data_modify(sleep, group = as.integer(group) - 1L)
d_wide <<- datawizard::data_to_wide(
  d,
  names_from = "group",
  values_from = "extra",
  names_prefix = "group"
)

model_io <- lm(d_wide$group0 - d_wide$group1 ~ 1)
out <- report(model_io)
#> 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 =

#> 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 =

#> 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 =

model_io <- lm(group0 - group1 ~ 1, data = d_wide)
out <- report(model_io)
#> Error in eval(predvars, data, env): object 'group0' not found
#> Error: Unable to refit the model with standardized data.
#>   Try instead to standardize the data (standardize(data)) and refit the
#>   model manually.

Created on 2023-07-01 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.0 (2023-04-21 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_Canada.utf8
#>  ctype    English_Canada.utf8
#>  tz       America/Toronto
#>  date     2023-07-01
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version  date (UTC) lib source
#>  bayestestR     0.13.1.2 2023-07-01 [1] https://easystats.r-universe.dev (R 4.3.1)
#>  cli            3.6.1    2023-03-23 [1] CRAN (R 4.3.0)
#>  coda           0.19-4   2020-09-30 [1] CRAN (R 4.3.0)
#>  codetools      0.2-19   2023-02-01 [2] CRAN (R 4.3.0)
#>  datawizard     0.8.0.4  2023-06-28 [1] https://easystats.r-universe.dev (R 4.3.1)
#>  digest         0.6.31   2022-12-11 [1] CRAN (R 4.3.0)
#>  effectsize     0.8.3.11 2023-06-30 [1] https://easystats.r-universe.dev (R 4.3.1)
#>  emmeans        1.8.6    2023-05-11 [1] CRAN (R 4.3.0)
#>  estimability   1.4.1    2022-08-05 [1] CRAN (R 4.3.0)
#>  evaluate       0.21     2023-05-05 [1] CRAN (R 4.3.0)
#>  fastmap        1.1.1    2023-02-24 [1] CRAN (R 4.3.0)
#>  fs             1.6.2    2023-04-25 [1] CRAN (R 4.3.0)
#>  glue           1.6.2    2022-02-24 [1] CRAN (R 4.3.0)
#>  highr          0.10     2022-12-22 [1] CRAN (R 4.3.0)
#>  htmltools      0.5.5    2023-03-23 [1] CRAN (R 4.3.0)
#>  insight        0.19.3   2023-06-29 [1] https://easystats.r-universe.dev (R 4.3.1)
#>  knitr          1.40     2022-08-24 [1] CRAN (R 4.3.0)
#>  lattice        0.21-8   2023-04-05 [2] CRAN (R 4.3.0)
#>  lifecycle      1.0.3    2022-10-07 [1] CRAN (R 4.3.0)
#>  magrittr       2.0.3    2022-03-30 [1] CRAN (R 4.3.0)
#>  MASS           7.3-58.4 2023-03-07 [2] CRAN (R 4.3.0)
#>  Matrix         1.5-4    2023-04-04 [2] CRAN (R 4.3.0)
#>  multcomp       1.4-23   2023-03-09 [1] CRAN (R 4.3.0)
#>  mvtnorm        1.2-2    2023-06-08 [1] CRAN (R 4.3.0)
#>  parameters     0.21.1.2 2023-06-28 [1] https://easystats.r-universe.dev (R 4.3.1)
#>  performance    0.10.4.1 2023-06-22 [1] https://easystats.r-universe.dev (R 4.3.1)
#>  purrr          1.0.1    2023-01-10 [1] CRAN (R 4.3.0)
#>  R.cache        0.16.0   2022-07-21 [1] CRAN (R 4.3.0)
#>  R.methodsS3    1.8.2    2022-06-13 [1] CRAN (R 4.3.0)
#>  R.oo           1.25.0   2022-06-12 [1] CRAN (R 4.3.0)
#>  R.utils        2.12.2   2022-11-11 [1] CRAN (R 4.3.0)
#>  report       * 0.5.7.4  2023-05-20 [1] local
#>  reprex         2.0.2    2022-08-17 [1] CRAN (R 4.3.0)
#>  rlang          1.1.1    2023-04-28 [1] CRAN (R 4.3.0)
#>  rmarkdown      2.22     2023-06-01 [1] CRAN (R 4.3.0)
#>  rstudioapi     0.14     2022-08-22 [1] CRAN (R 4.3.0)
#>  sandwich       3.0-2    2022-06-15 [1] CRAN (R 4.3.0)
#>  sessioninfo    1.2.2    2021-12-06 [1] CRAN (R 4.3.0)
#>  stringi        1.7.12   2023-01-11 [1] CRAN (R 4.3.0)
#>  stringr        1.5.0    2022-12-02 [1] CRAN (R 4.3.0)
#>  styler         1.9.1    2023-03-04 [1] CRAN (R 4.3.0)
#>  survival       3.5-5    2023-03-12 [2] CRAN (R 4.3.0)
#>  TH.data        1.1-2    2023-04-17 [1] CRAN (R 4.3.0)
#>  vctrs          0.6.3    2023-06-14 [1] CRAN (R 4.3.0)
#>  withr          2.5.0    2022-03-03 [1] CRAN (R 4.3.0)
#>  xfun           0.39     2023-04-20 [1] CRAN (R 4.3.0)
#>  xtable         1.8-4    2019-04-21 [1] CRAN (R 4.3.0)
#>  yaml           2.3.7    2023-01-23 [1] CRAN (R 4.3.0)
#>  zoo            1.8-12   2023-04-13 [1] CRAN (R 4.3.0)
#> 
#>  [1] C:/Users/there/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.0/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@rempsyc
Copy link
Sponsor Member Author

rempsyc commented Jul 1, 2023

Some additional info:

Warnings come from: parameters::model_parameters() and insight::get_data()

library(report)
data(sleep)
d <- datawizard::data_modify(sleep, group = as.integer(group) - 1L)
d_wide <<- datawizard::data_to_wide(
  d,
  names_from = "group",
  values_from = "extra",
  names_prefix = "group"
)
x <- lm(d_wide$group0 - d_wide$group1 ~ 1)
params <- parameters::model_parameters(x, ci_random = FALSE)
#> 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 =
out <- insight::get_data(x)
#> 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: Could not recover model data from environment. Please make sure your
#>   data is available in your workspace.
#>   Trying to retrieve data from the model frame now.

Created on 2023-07-01 with reprex v2.0.2

Error comes from: report_effectsize()

library(report)
data(sleep)
d <- datawizard::data_modify(sleep, group = as.integer(group) - 1L)
d_wide <<- datawizard::data_to_wide(
  d,
  names_from = "group",
  values_from = "extra",
  names_prefix = "group"
)
x <- lm(group0 - group1 ~ 1, data = d_wide)
params <- parameters::model_parameters(x, ci_random = FALSE)
effsize <- report_effectsize(x)
#> Error in eval(predvars, data, env): object 'group0' not found
#> Error: Unable to refit the model with standardized data.
#>   Try instead to standardize the data (standardize(data)) and refit the
#>   model manually.

Created on 2023-07-01 with reprex v2.0.2

@etiennebacher do you think these are coming from datawizard or insight::get_data() somehow? Do you have a suggestion to where to start looking to fix this issue?

@etiennebacher
Copy link
Member

Looks like it comes from datawizard::standardize(), I'll open an issue:

library(report)
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)
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 =
#> 
#> Call:
#> lm(formula = d_wide$group0 - d_wide$group1 ~ 1, data = data_std)
#> 
#> Coefficients:
#> (Intercept)  
#>       -1.58

model1 <- lm(group0 - group1 ~ 1, data = d_wide)
standardize(model1)
#> Error in eval(predvars, data, env): objet 'group0' introuvable
#> Error: Unable to refit the model with standardized data.
#>   Try instead to standardize the data (standardize(data)) and refit the
#>   model manually.

Created on 2023-07-01 with reprex v2.0.2

@mattansb
Copy link
Member

mattansb commented Jul 2, 2023

Note that standardizing the components of a composite variable will probably lead to nonsense results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants