Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the seed with the `replicate()` function in R

Tags:

r

In R, there is a replicate() function that I am using to replicate multiple iterations of the same function. My function has some random number generation and I am wondering if there is a way to put in a seed number for each iteration of the replicate() function.

For example in:

replicate(10, myfunction(data))

Is there a way to put a seed number for each of the 10 replications of calling myfunction with data as input? Thanks.

like image 389
user321627 Avatar asked Oct 16 '25 22:10

user321627


2 Answers

Use set.seed() before the call on replicate.

This is worth noting link.

Expecially:

We cannot emphasize this enough: Do not set the seed too often. To see why this is such a bad idea, consider the limiting case: You set the seed, draw one pseudorandom number, reset the seed, draw again, and so continue. The pseudorandom numbers you obtain will be nothing more than the seeds you run through a mathematical function. The results you obtain will not pass for random unless the seeds you choose pass for random. If you already had such numbers, why are you even bothering to use the pseudorandom-number generator?

like image 88
RLave Avatar answered Oct 18 '25 12:10

RLave


If you want to control the seed on each iteration while holding the data argument constant, it's better to use lapply() or sapply() rather than replicate(). For example you could supply a vector of seeds to lapply() and define an inline function that binds data to myfunction(), like this:

# note: `sapply()` might be better, depending on the return value of `myfunction()`
lapply(1:10, function(seed){
  set.seed(seed)
  myfunction(data)
}

You could also define the function separately, e.g.:

# `...` is for passing (named) params other than `data` to `myfunction()`
myfunction2 <- function(data, seed, ...){
  set.seed(seed)
  myfunction(data, ...)
}

# same thing as first approach
lapply(1:10, myfunction2, data=data)

But if for whatever reason you want to generate the same values on every replication, you can wrap myfunction() in an outer function that fixes the seed, and then use replicate() to repeatedly call that function:

# wrap `myfunction()` to set seed before each call, passing params in `...`
myfunction_withseed <- function(...){ 
  set.seed(6933)
  myfunction(...)
}

# use `myfunction_withseed()` just as you would `myfunction()` 
replicate(10, myfunction_withseed(data=data))

Or you can do it inline with the original myfunction(), e,g,:

replicate(10, {set.seed(6933); myfunction(data)})
like image 22
lefft Avatar answered Oct 18 '25 13:10

lefft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!