Care should be taken with this method not to encounter the
birthday problem, described
https://www.johndcook.com/blog/2016/01/29/random-number-generator-seed-mistakes/.
Since the sitmo
threefry
, this currently generates
one random deviate from the uniform distribution to seed the
engine threefry
and then run the code.
Arguments
- size
target for number of successful trials, or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive, need not be integer.
- prob
probability of success in each trial.
0 < prob <= 1
.- n
number of observations. If
length(n) > 1
, the length is taken to be the number required.- ncores
Number of cores for the simulation
rxnorm
simulates using the threefry sitmo generator.rxnormV
used to simulate with the vandercorput simulator, but since it didn't satisfy the normal properties it was changed to simple be an alias ofrxnorm
. It is no longer supported inrxode2({})
blocks- mu
alternative parametrization via mean: see ‘Details’.
Value
negative binomial random deviates. Note that rxbinom2
uses the mu
parameterization an the rxbinom
uses the prob
parameterization (mu=size/(prob+size)
)
Details
Therefore, a simple call to the random number generated followed by a second call to random number generated may have identical seeds. As the number of random number generator calls are increased the probability that the birthday problem will increase.
The key to avoid this problem is to either run all simulations in the
rxode2
environment once (therefore one seed or series of seeds
for the whole simulation), pre-generate all random variables
used for the simulation, or seed the rxode2 engine with rxSetSeed()
Internally each ID is seeded with a unique number so that the results do not depend on the number of cores used.
Examples
# \donttest{
## Use threefry engine
rxnbinom(10, 0.9, n = 10) # with rxbinom you have to explicitly state n
#> [1] 9 10 8 9 9 10 9 9 8 9
rxnbinom(3, 0.5, n = 10, ncores = 2) # You can parallelize the simulation using openMP
#> [1] 2 2 2 3 2 3 3 1 3 3
rxnbinom(4, 0.7)
#> [1] 4
# use mu parameter
rxnbinomMu(40, 40, n=10)
#> [1] 17 23 23 16 19 21 21 25 17 20
## This example uses `rxbinom` directly in the model
rx <- function() {
model({
a <- rxnbinom(10, 0.5)
})
}
et <- et(1, id = 1:100)
s <- rxSolve(rx, et)
#>
#>
#> ℹ parameter labels from comments are typically ignored in non-interactive mode
#> ℹ Need to run with the source intact to parse comments
#>
#>
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
rx <- function() {
model({
a <- rxnbinomMu(10, 40)
})
}
s <- rxSolve(rx, et)
#>
#>
#> ℹ parameter labels from comments are typically ignored in non-interactive mode
#> ℹ Need to run with the source intact to parse comments
#>
#>
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
# }