amices / mice

Multivariate Imputation by Chained Equations
https://amices.org/mice/
GNU General Public License v2.0
447 stars 108 forks source link

Error with `summary.mira` because of `bind_rows` #649

Closed kylebutts closed 5 months ago

kylebutts commented 5 months ago

The use of bind_rows instead of something like do.call("rbind", x) is causing errors with the marginaleffects package. Basically, broom::glance(marginaleffects::hypotheses(...)) contains the logLik column of class logLik class. Using c() on logLik converts the column to a double which vctrs reports as an error. This breaks the mira code here: https://github.com/amices/mice/blob/99bd724eac99ae44a3b7e8b2b5da0f2ddd264ec6/R/summary.R#L25-L27

A simple fix could be (but this could cause other bugs with silent column conversions):

v <- lapply(fitlist, glance, ...) %>% do.call("rbind", .) 

Alternatively, could do:

v <- lapply(fitlist, glance, ...)
v <- v %>% 
   lapply(function(x) {
      if ("logLik" %in% colnames(x)) {
         x$logLik = as.numeric(x$logLik)
      } 
      return(x)
   }) %>% bind_rows()

FYI, @vincentarelbundock. This might be a thing you could change on your end? E.g. logLik of type double and logLik_df storing attr(logLik, "df")?

Reprex:

library(mice)
#> 
#> Attaching package: 'mice'
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following objects are masked from 'package:base':
#> 
#>     cbind, rbind
library(marginaleffects)
mod <- lm(mpg ~ hp + wt + factor(cyl), data = mtcars)
mod2 <- lm(mpg ~ wt + factor(cyl), data = mtcars)
summary(mice::pool(
  list(
    x1 = hypotheses(mod, hypothesis = "exp(hp + wt) = 0.1"), 
    x2 = hypotheses(mod2, hypothesis = "wt = 0.1")
  )
))
#> Warning in get.dfcom(object, dfcom): Infinite sample size assumed.
#>                 term    estimate std.error statistic df p.value
#> 1 exp(hp + wt) = 0.1 -0.05942178        NA        NA NA      NA
#> 2           wt = 0.1 -3.30561326        NA        NA NA      NA

mice:::summary.mira(
  mice:::as.mira(list(
    x1 = hypotheses(mod, hypothesis = "exp(hp + wt) = 0.1"), 
    x2 = hypotheses(mod2, hypothesis = "wt = 0.1")
  )), 
  "glance"
)
#> Error in `bind_rows()`:
#> ! Can't combine `..1` <logLik> and `..2` <logLik>.
#> ✖ Some attributes are incompatible.
#> ℹ The author of the class should implement vctrs methods.
#> ℹ See <https://vctrs.r-lib.org/reference/faq-error-incompatible-attributes.html>.
vincentarelbundock commented 5 months ago

@kylebutts this is actually very easy for me to fix upstream. The functions in charge of extracting these statistics are in fact hosted in the modelsummary package. If you install from Github and restart R, your example should then work:

remotes::install_github("vincentarelbundock/modelsummary")

My view is that it might be best not to do a bunch of automatic type conversion in mice, so IMHO this issue can probably be closed.

kylebutts commented 5 months ago

Thanks @vincentarelbundock! Glad I pinged you :-)