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')
while (length (grep ('tnorm', getwd ())) > 0) setwd ("..") #devtools::document ("tnorm") #devtools::check ("tnorm") #Rcpp::compileAttributes ("tnorm") devtools::load_all ("tnorm") setwd ("./tnorm") #testthat::test_package ('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])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.