mice icon indicating copy to clipboard operation
mice copied to clipboard

pool() on lavaan fit objects gives error: coef() not available on S4 object

Open Gootjes opened this issue 5 months ago • 2 comments

Before submitting the issue

  • Check whether a bug report is the right format for your issue. For questions about using the mice package, see Discussions. For theoretical background about the mice methods, see Flexible Imputation of Missing Data.
  • Please make sure that you are using the latest version of mice (e.g. with the packageVersion() or sessionInfo() function).
  • Please verify whether the bug has already been addressed (e.g. on GitHub or Stack Overflow.

Describe the bug The error happens in get.dfcom on this line https://github.com/amices/mice/blob/c924a7473ea2a95a551ca54479557877defb6423/R/get.df.R#L24
The package lavaan implements coef as an S4 function (compare lavaan::coef to stats::coef), however, this function is not available at this point, so the regular coef function is called on the lavaan fit object, leading to an error.

To Reproduce

library(lavaan)
#> This is lavaan 0.6-15
#> lavaan is FREE software! Please report any bugs.
library(mice)
#> 
#> Attaching package: 'mice'
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following objects are masked from 'package:base':
#> 
#>     cbind, rbind
pool(list(sem("mpg ~ cyl", data = mtcars[1:10,]), sem("mpg ~ cyl", data = mtcars[1:20,])))
#> Error in object$coefficients: $ operator not defined for this S4 class

Created on 2024-01-14 with reprex v2.0.2

Expected behavior That coef() works.

A solution would be to make get.dfcom a S3method, such that get.dfcom.default is the current function, and custom functions are implemented for lavaan and coxph (and in the future others?)

Gootjes avatar Jan 14 '24 18:01 Gootjes