Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid changing global .Random.seed #426

Closed
weiyaw opened this issue Aug 19, 2021 · 2 comments
Closed

Avoid changing global .Random.seed #426

weiyaw opened this issue Aug 19, 2021 · 2 comments

Comments

@weiyaw
Copy link

weiyaw commented Aug 19, 2021

Hi,

Would you consider using withr::local_seed in mice() rather than set.seed?

Currently set.seed sets .Random.seed in the users' global environment which can result in unintended results and bugs.

Regards,

Kenyon

@stefvanbuuren
Copy link
Member

Thanks for your suggestion of a neater alternative.

I created a branch randomseed that isolates the seed streams used in the mice() and mice.mids() functions. The random number state is reset to the initial state on completion of mice(...) or mice.mids(...), so both functions do not interfere anymore with the calling code, e.g.,

context("mice: local random stream")

set.seed(1)
a <- runif(10)

set.seed(1)
b1 <- runif(6)

imp <- mice(nhanes, maxit = 0, seed = 82, print = FALSE)
imp2 <- mice.mids(imp, maxit = 1, print = FALSE)

b2 <- runif(4)

test_that("mice and mice.mids use a local random stream", {
  expect_equal(a, c(b1, b2))
})

As long as the branch exists, install by

devtools::install_github(repo = "amices/mice@randomseed")

I did not look at the parallel functionality in the package.

Does this work for you?

@stefvanbuuren
Copy link
Member

PR #432 implements the local seed in the master version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants