amices / mice

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

Does argument `parallelseed` in `futuremice()` work as expected? #514

Closed stefvanbuuren closed 1 year ago

stefvanbuuren commented 1 year ago

I am unsure whether I understand the intended behaviour of parallelseed. I would think that specifying the same parallelseed at two identical calls would yield the same imputations, as happens with seed in mice().

The test script below for mice 3.14.9 shows that imputes are different. Is my understanding correct? Any ideas how to fix?

library(mice, warn.conflicts = FALSE)
library(testthat)

context("mice: seed")

imp1 <- mice(nhanes2, m = 2, seed = 123, print = FALSE)
imp2 <- mice(nhanes2, m = 2, seed = 123, print = FALSE)
test_that("same seed yields same imputations", {
  expect_identical(imp1[-11], imp2[-11])
})
#> Test passed 🎉

context("futuremice: parallelseed")

imp1 <- futuremice(nhanes2, m = 4, n.core = 2, parallelseed = 1)
imp2 <- futuremice(nhanes2, m = 4, n.core = 2, parallelseed = 1)
test_that("same parallelseed yields same imputations", {
  expect_identical(imp1[-11], imp2[-11])
})
#> ── Failure ('<text>:17'): same parallelseed yields same imputations ────────────
#> imp1[-11] not identical to imp2[-11].
#> Component "imp": Component "bmi": Component "1": Mean relative difference: 0.1554468
#> Component "imp": Component "bmi": Component "2": Mean relative difference: 0.1348936
#> Component "imp": Component "bmi": Component "3": Mean relative difference: 0.1516
#> Component "imp": Component "bmi": Component "4": Mean relative difference: 0.1243956
#> Component "imp": Component "hyp": Component "1": 2 string mismatches
#> Component "imp": Component "hyp": Component "2": 4 string mismatches
#> Component "imp": Component "hyp": Component "3": 2 string mismatches
#> Component "imp": Component "hyp": Component "4": 4 string mismatches
#> Component "imp": Component "chl": Component "1": Mean relative difference: 0.2008149
#> ...
#> Error in `reporter$stop_if_needed()`:
#> ! Test failed

#> Backtrace:
#>      â–†
#>   1. └─testthat::test_that(...)
#>   2.   └─withr (local) `<fn>`(`<env>`)
#>   3.     ├─base::tryCatch(...)
#>   4.     │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   5.     │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   6.     │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   7.     └─base::eval(handler$expr, handler$envir)
#>   8.       └─base::eval(handler$expr, handler$envir)
#>   9.         └─reporter$stop_if_needed()
#>  10.           └─rlang::abort("Test failed")

Created on 2022-11-09 with reprex v2.0.2

gerkovink commented 1 year ago

Nope. Does not seem to be platform dependent. Check fail on:

gerkovink commented 1 year ago

Will pick this up

gerkovink commented 1 year ago

Argument parallelseed experienced override by the internal mice call to set.seed(NULL) introduced in v3.14.4. Fixed in #515

gerkovink commented 1 year ago

Reopening till #515 is merged

stefvanbuuren commented 1 year ago

Ah, thanks for the diagnosis. Let's first solve #515 then.