https://github.com/mpadge/tnorm
R package to generated truncated normal distributions
https://github.com/mpadge/tnorm
Last synced: about 2 months ago
JSON representation
R package to generated truncated normal distributions
- Host: GitHub
- URL: https://github.com/mpadge/tnorm
- Owner: mpadge
- Created: 2016-08-24T12:23:42.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-11-19T08:30:11.000Z (over 5 years ago)
- Last Synced: 2025-02-14T13:23:52.495Z (3 months ago)
- Language: R
- Size: 19.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
[](https://travis-ci.org/mpadge/tnorm)
[](https://codecov.io/gh/mpadge/tnorm)# tnorm
`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 both1. [msm::rtnorm](https://cran.r-project.org/package=msm), which is entirely
R-based, and2. [truncnorm::rtruncnorm](https://cran.r-project.org/package=truncnorm), 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.--------
## Install
```{r, eval=FALSE}
devtools::install_github ('mpadge/tnorm')
```
```{r load, echo=FALSE, message=FALSE}
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')
```-----
## Use
The one and only function
```{r}
z <- tnormn (n=1e5, sd=1)
```## Compare timing with `msm::rtnorm` and `truncnorm::rtruncnorm`
```{r benchmarks}
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])
```