vincentarelbundock / modelsummary

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

No output when multisession plan already loaded using furrr #623

Closed chrishanretty closed 1 year ago

chrishanretty commented 1 year ago

Modelsummary complains that it could not extracted the required information from a model of class "mipo" after a multisession plan had been initiated using furrr. Modelsummary can extract the required information when no plan has been started, or using plan(sequential).

Minimal reproducible example:

library(modelsummary)
library(mice)
library(Amelia)

url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat <- read.csv(url)[, c('Clergy', 'Commerce', 'Literacy')]

# Insert missing values
dat$Clergy[sample(1:nrow(dat), 10)] <- NA
dat$Commerce[sample(1:nrow(dat), 10)] <- NA
dat$Literacy[sample(1:nrow(dat), 10)] <- NA

# Impute with `mice` and `Amelia`
dat_mice <- mice(dat, m = 5, printFlag = FALSE)
dat_amelia <- amelia(dat, m = 5, p2s = 0)$imputations

# Estimate models
mod <- list()
mod[['Listwise deletion']] <- lm(Clergy ~ Literacy + Commerce, dat)
mod[['Mice']] <- with(dat_mice, lm(Clergy ~ Literacy + Commerce)) 
mod[['Amelia']] <- lapply(dat_amelia, function(x) lm(Clergy ~ Literacy + Commerce, x))

# Pool results
mod[['Mice']] <- mice::pool(mod[['Mice']])
mod[['Amelia']] <- mice::pool(mod[['Amelia']])

### WORKS
modelsummary(mod, output = "markdown")

library(furrr)
nWorkers <- parallel::detectCores() - 1
plan(multisession, workers = nWorkers)

### DOES NOT WORK
modelsummary(mod, output = "markdown")

### WORKS
plan(sequential)
modelsummary(mod, output = "markdown")

I have not yet been able to replicate this with objects of other model classes, so this may be a complicated interaction between Amelia and modelsummary.

Output of sessionInfo():

R version 4.2.2 (2022-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] furrr_0.3.1        future_1.30.0      Amelia_1.8.1       Rcpp_1.0.10       
[5] mice_3.15.0        modelsummary_1.3.0

loaded via a namespace (and not attached):
 [1] svglite_2.1.1       mvtnorm_1.1-3       lattice_0.20-45    
 [4] tidyr_1.3.0         listenv_0.9.0       zoo_1.8-11         
 [7] digest_0.6.31       lmtest_0.9-40       utf8_1.2.3         
[10] parallelly_1.33.0   R6_2.5.1            backports_1.4.1    
[13] coda_0.19-4         evaluate_0.19       highr_0.10         
[16] httr_1.4.5          pillar_1.9.0        rlang_1.1.0        
[19] multcomp_1.4-20     rstudioapi_0.14     performance_0.10.2 
[22] Matrix_1.5-3        checkmate_2.1.0     rmarkdown_2.19     
[25] splines_4.2.2       webshot_0.5.4       stringr_1.5.0      
[28] foreign_0.8-84      munsell_0.5.0       broom_1.0.4        
[31] compiler_4.2.2      xfun_0.36           systemfonts_1.0.4  
[34] pkgconfig_2.0.3     parameters_0.20.1   globals_0.16.2     
[37] htmltools_0.5.4     insight_0.18.8      tidyselect_1.2.0   
[40] tibble_3.2.1        codetools_0.2-18    viridisLite_0.4.1  
[43] fansi_1.0.4         dplyr_1.1.0         tables_0.9.10      
[46] MASS_7.3-58.1       grid_4.2.2          xtable_1.8-4       
[49] lifecycle_1.0.3     magrittr_2.0.3      bayestestR_0.13.0  
[52] scales_1.2.1        datawizard_0.6.5    stringi_1.7.12     
[55] estimability_1.4.1  future.apply_1.10.0 cli_3.6.1          
[58] xml2_1.3.3          generics_0.1.3      vctrs_0.6.1        
[61] sandwich_3.0-2      TH.data_1.1-1       kableExtra_1.3.4   
[64] tools_4.2.2         glue_1.6.2          purrr_1.0.1        
[67] emmeans_1.8.3       parallel_4.2.2      fastmap_1.1.0      
[70] survival_3.5-0      colorspace_2.1-0    rvest_1.0.3        
[73] knitr_1.41         
vincentarelbundock commented 1 year ago

@chrishanretty thanks a lot for the report and detailed example. I can reproduce the issue, but I’m not sure what’s causing it. I have spent a bunch of time trying to figure it out, but I’m not sure I’m getting any closer. I’ll keep trying, but I’m not sure when I’ll have a solution.

In the meantime, the best I can do is offer a global option to disable future in modelsummary. This will allow you to use furrr for other applications, but will make modelsummary run sequentially. Of course, you can still use the mc.cores trick described in the documentation.

Using 1.3.0.9018 from Github:

options(modelsummary_future = FALSE)

library(modelsummary)
library(mice)
library(Amelia)
url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat <- read.csv(url)[, c('Clergy', 'Commerce', 'Literacy')]
dat$Clergy[sample(1:nrow(dat), 10)] <- NA
dat$Commerce[sample(1:nrow(dat), 10)] <- NA
dat$Literacy[sample(1:nrow(dat), 10)] <- NA
dat_mice <- mice(dat, m = 5, printFlag = FALSE)
dat_amelia <- amelia(dat, m = 5, p2s = 0)$imputations
mod <- list()
mod[['Listwise deletion']] <- lm(Clergy ~ Literacy + Commerce, dat)
mod[['Mice']] <- with(dat_mice, lm(Clergy ~ Literacy + Commerce)) 
mod[['Amelia']] <- lapply(dat_amelia, function(x) lm(Clergy ~ Literacy + Commerce, x))
mod[['Mice']] <- mice::pool(mod[['Mice']])
mod[['Amelia']] <- mice::pool(mod[['Amelia']])
library(furrr)
nWorkers <- parallel::detectCores() - 1
plan(multisession, workers = nWorkers)
modelsummary(mod, output = "markdown")
Listwise deletion Mice Amelia
(Intercept) 80.554 79.365 79.100
(14.931) (14.751) (13.222)
Literacy -0.547 -0.541 -0.532
(0.236) (0.226) (0.219)
Commerce -0.367 -0.373 -0.378
(0.159) (0.144) (0.133)
:———— ——————: ———: ———:
Num.Obs. 58 86 86
Num.Imp. 5 5
R2 0.106 0.122 0.123
R2 Adj. 0.073 0.100 0.101
AIC 536.9
BIC 545.1
Log.Lik. -264.431
F 3.261
RMSE 23.11