简体   繁体   中英

R using set.seed() within a purrr::map iteration structure

When generating new data in R, I can use set.seed() to ensure that I get the same data sets every time the code is run:

set.seed(12345)
a <- rnorm(500, mean = 50, sd = 10)
set.seed(12345)
b <- rnorm(500, mean = 50, sd = 10)
identical(a, b)
# TRUE

If I comment out the set.seed() lines, identical(a,b) returns FALSE .

Now I want to use a purrr::map() structure to generate multiple data sets with slightly different parameters:

library(tidyverse)

means <- c(40, 50, 60)
sds <- c(9, 10, 11)

set.seed(12345)
data <- map2(
  means,
  sds,
  ~
    rnorm(500, mean = .x, sd = .y)
    )

The map2() call generates a list of three data frames. With this relatively simple operation, I get identical data frames every time I run the code. But I'm finding that with more complex, longer functional pipelines involving certain packages (eg, bestNormalize ), I'm not getting identical output when the set.seed() command is outside the iterative looping structure of map() .

I'm at a loss for how to bring set.seed() within the map() iteration structure so that it is called anew at the beginning of each iteration. To be clear, the larger goal is to be able to iterate over functions that use random number generation, and to get identical results every time. Perhaps there's a better way to accomplish this in the tidyverse that doesn't depend on set.seed() . Thanks in advance for any help!

I hope this solves your question as how to position the seed inside the map call:

means <- c(40, 50, 60)
sds <- c(9, 10, 11)

myfun <- function(means, sds){
    set.seed(12345) # set it before each call
    ret <- rnorm(500, mean = means, sd = sds)
    return(ret)
}

data <- purrr::map2(means,
                    sds,
                    ~ myfun(.x, .y))

As a followup, here is the most concise way to solve my original problem:

library(tidyverse)

means <- c(40, 50, 60)
sds <- c(9, 10, 11)

data <- map2(
  means,
  sds,
  ~ {
    set.seed(12345)
    rnorm(500, mean = .x, sd = .y)
  }
)

This code returns identical results each time it is run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM