tnorm
is an R package for generating random samples from truncated
normal distributions. It exists solely as a faster replacement for the
core functionality of other packages, and it useful in simulation
contexts where speed is important and samples from truncated normal
distributions are required. This functionality already exists in both
msm::rtnorm, which is entirely R-based, and
truncnorm::rtruncnorm, which has core code in C.
Even the latter of these is, however, slower than tnorm
at generating
random values. tnorm
currently generates distributions centred around
a mean value of 1, truncated to between 0 and 2. Values can be readily
re-scaled to desired mean values and limits.
devtools::install_github ('mpadge/tnorm')
The one and only function
z <- tnormn (n=1e5, sd=1)
msm::rtnorm
and truncnorm::rtruncnorm
library (rbenchmark)
n <- 1e6
mn <- sd <- 1
knitr::kable (benchmark (
tnorm::tnormn (n = n, sd = sd),
msm::rtnorm (n = n, mean = mn, sd = sd, lower = 0, upper = 2),
truncnorm::rtruncnorm (n = n, sd = sd, a = 0, b = 2),
replications = 10,
order = "relative") [, 1:4])
| | test | replications| elapsed| relative| |-----|:-------------------------------------------------------------|-------------:|--------:|---------:| | 1 | tnorm::tnormn(n = n, sd = sd) | 10| 0.753| 1.000| | 3 | truncnorm::rtruncnorm(n = n, sd = sd, a = 0, b = 2) | 10| 1.317| 1.749| | 2 | msm::rtnorm(n = n, mean = mn, sd = sd, lower = 0, upper = 2) | 10| 5.709| 7.582|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.