An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/mpadge/tnorm.svg?branch=master)](https://travis-ci.org/mpadge/tnorm)
[![codecov](https://codecov.io/gh/mpadge/tnorm/branch/master/graph/badge.svg)](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 both

1. [msm::rtnorm](https://cran.r-project.org/package=msm), which is entirely
R-based, and

2. [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])
```