vincentarelbundock / modelsummary

Beautiful and customizable model summaries in R.
http://modelsummary.com
Other
896 stars 74 forks source link

Error on `vcov="bootstrap"` #744

Closed joshuafayallen closed 3 months ago

joshuafayallen commented 3 months ago

Hi Vincent, I keep getting this error from when using vcov to correct the standard errors and I keep getting this error when trying to bootstrap the standard errors.

library(palmerpenguins)
library(modelsummary)
#> Warning: package 'modelsummary' was built under R version 4.3.3
#> `modelsummary` 2.0.0 now uses `tinytable` as its default table-drawing
#>   backend. Learn more at: https://vincentarelbundock.github.io/tinytable/
#> 
#> Revert to `kableExtra` for one session:
#> 
#>   options(modelsummary_factory_default = 'kableExtra')
#> 
#> Change the default backend persistently:
#> 
#>   config_modelsummary(factory_default = 'gt')
#> 
#> Silence this message forever:
#> 
#>   config_modelsummary(startup_message = FALSE)

test_mod = lm(body_mass_g ~ bill_length_mm, data = penguins)

modelsummary(test_mod, vcov = 'bootstrap', cluster = 'species')
#> Error: Unable to extract a variance-covariance matrix from model of class `lm`.

However, it seems to work with other arguments

library(palmerpenguins)
library(modelsummary)
#> Warning: package 'modelsummary' was built under R version 4.3.3

first_mod = lm(body_mass_g ~ flipper_length_mm, data = penguins)

second_mod = lm(body_mass_g ~ bill_depth_mm,data = penguins)

mods_list = list(first_mod, second_mod)

modelsummary(mods_list, vcov = ~island)
(Intercept) -5780.831 7488.652
(282.805) (1026.894)
flipper_length_mm 49.686
(1.702)
bill_depth_mm -191.643
(48.100)
Num.Obs. 342 342
R2 0.759 0.223
R2 Adj. 0.758 0.220
AIC 5062.9 5463.3
BIC 5074.4 5474.8
Log.Lik. -2528.427 -2728.667
RMSE 393.12 706.00
Std.Errors by: island by: island

Created on 2024-04-05 with reprex v2.1.0

Created on 2024-04-05 with reprex v2.1.0

Here is the `sessioninfo:

sessionInfo()
#> R version 4.3.2 (2023-10-31)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Running under: macOS Sonoma 14.0
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> time zone: America/New_York
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] styler_1.10.2     digest_0.6.35     fastmap_1.1.1     xfun_0.43        
#>  [5] magrittr_2.0.3    glue_1.7.0        R.utils_2.12.3    knitr_1.45       
#>  [9] htmltools_0.5.8   rmarkdown_2.26    lifecycle_1.0.4   cli_3.6.2        
#> [13] R.methodsS3_1.8.2 vctrs_0.6.5       reprex_2.1.0      withr_3.0.0      
#> [17] compiler_4.3.2    R.oo_1.26.0       R.cache_0.16.0    purrr_1.0.2      
#> [21] rstudioapi_0.15.0 tools_4.3.2       evaluate_0.23     yaml_2.3.8       
#> [25] rlang_1.1.3       fs_1.6.3

Created on 2024-04-05 with reprex v2.1.0

vincentarelbundock commented 3 months ago

Thanks a lot for the report!

Using the development versions of both insight and modelsummary should return a more informative error. It looks like the problem had to do with missing values. You can pass a vector instead of the name of the variable to the cluster argument:

remotes::install_github("easystats/insight")
remotes::install_github("vincentarelbundock/modelsummary")
library(palmerpenguins)
library(modelsummary)
test_mod = lm(body_mass_g ~ bill_length_mm, data = penguins)

modelsummary(test_mod, vcov = 'bootstrap', cluster = penguins$species)
(Intercept) 362.307
(1621.185)
bill_length_mm 87.415
(41.620)
Num.Obs. 342
R2 0.354
R2 Adj. 0.352
AIC 5400.0
BIC 5411.5
Log.Lik. -2696.987
RMSE 643.54
Std.Errors Bootstrap
joshuafayallen commented 2 months ago

Got it thank you so much!