easystats / report

:scroll: :tada: Automated reporting of objects in R
https://easystats.github.io/report/
Other
694 stars 69 forks source link

Error: bad 'data': object 'data_std' not found #398

Closed alexisdmacintyre closed 1 year ago

alexisdmacintyre commented 1 year ago

_RStudio Version 2023.09.0+463 R version 4.3.1 Platform: x8664-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19042)

df <- tibble::tibble(
  name = rep(c("name1",
               "name2",
               "name3",
               "name4",
               "name5"),
             times = 200),
  Y = sample(x = 1000000:10000000, size = 1000),
  A = rep(c(0, 1), times = 500),
  B = rep(c(1, 2, 3, 4, 5,6,7,8,9,10), times = 100),
  C = rep(c(10, 20, 30, 40, 50,60,70,80,90,100), times = 100))

myModel <- lmer(Y ~ 1 + A + C + (1 + A | B),
              data = df, REML = FALSE, control = lmerControl(
                optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))

report_table(myModel, ci_method = "kenward")

Returns:

Error: bad 'data': object 'data_std' not found

alexisdmacintyre commented 1 year ago

It looks like this is due to REML = FALSE. My understanding is that easystats functions would allow models fit with ML but issue a warning.

strengejacke commented 1 year ago

Kenward-Rogers approximation does not work with ML-fit. If the model is fit with ML instead of REML, internally, stats::update(myModel, . ~ ., REML = TRUE) is called. This doesn't seem to work with report(), maybe it's a scoping issue where update() can't find the model/data in the environment. However, it works with model_parameters():

library(lme4)
#> Loading required package: Matrix
library(report)

df <- tibble::tibble(
  name = rep(c("name1",
               "name2",
               "name3",
               "name4",
               "name5"),
             times = 200),
  Y = sample(x = 1000000:10000000, size = 1000),
  A = rep(c(0, 1), times = 500),
  B = rep(c(1, 2, 3, 4, 5,6,7,8,9,10), times = 100),
  C = rep(c(10, 20, 30, 40, 50,60,70,80,90,100), times = 100))

myModel <- lmer(Y ~ 1 + A + C + (1 + A | B),
              data = df, REML = FALSE, control = lmerControl(
                optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
#> Loading required namespace: optimx
#> boundary (singular) fit: see help('isSingular')

parameters::model_parameters(myModel, ci = 0.95, ci_method = "kr")
#> boundary (singular) fit: see help('isSingular')
#> # Fixed Effects
#> 
#> Parameter   | Coefficient |       SE |                 95% CI |     t |   df |      p
#> -------------------------------------------------------------------------------------
#> (Intercept) |    5.43e+06 | 2.23e+05 | [  4.89e+06, 5.98e+06] | 24.42 | 5.98 | < .001
#> A           |    2.39e+05 | 1.70e+05 | [ -1.62e+05, 6.41e+05] |  1.41 | 7.04 | 0.202 
#> C           |    -1616.79 |  3782.73 | [ -10550.67,  7317.09] | -0.43 | 7.04 | 0.682 
#> 
#> # Random Effects
#> 
#> Parameter            | Coefficient
#> ----------------------------------
#> SD (Intercept: B)    |        0.00
#> SD (A: B)            |    5.35e-03
#> Cor (Intercept~A: B) |            
#> SD (Residual)        |    2.62e+06
#> 
#> Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
#>   using a Wald t-distribution with Kenward-Roger approximation.
#>   Uncertainty intervals for random effect variances computed using a Wald
#>   z-distribution approximation.

Created on 2023-10-17 with reprex v2.0.2

Maybe this can also be solved for report?

mattansb commented 1 year ago

This is also a scoping issue in parameters: KR needs to refit the model to REML, but since the model was internally standardized, the dataframe with the standardized data (called data_std) cannot be found and we get an error.