-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
123 lines (89 loc) · 3.91 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
<!-- badges: start -->
[![R-CMD-check](https://github.com/coatless-rpkg/rgen/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/coatless-rpkg/rgen/actions/workflows/R-CMD-check.yaml)
[![Downloads](https://cranlogs.r-pkg.org/badges/rgen?color=brightgreen)](https://www.r-pkg.org/pkg/rgen) [![CRAN](http://www.r-pkg.org/badges/version/rgen)](https://cran.r-project.org/package=rgen) [![License](https://img.shields.io/badge/license-GPL%20%28%3E=%202%29-brightgreen.svg?style=flat)](https://www.gnu.org/licenses/gpl-2.0.html)
<!-- badges: end -->
# `rgen` - C++ Headers for Sampling Distributions
The repository houses random distribution sampling routines based in [`armadillo`](https://github.com/conradsnicta/armadillo-code).
These routines connect into *R*'s seed generator using
[`RcppArmadillo`](https://github.com/RcppCore/RcppArmadillo). This package
was spun off from the [`r-to-armadillo`](https://github.com/coatless-rpkg/r-to-armadillo)
project as it contained more direct references to internal _R_ seeds.
## Supported Distributions
Presently, `rgen` provides random sample functionality from:
- [dirichlet](https://en.wikipedia.org/wiki/Dirichlet_distribution)
- [multinomial](https://en.wikipedia.org/wiki/Multinomial_distribution)
- [wishart](https://en.wikipedia.org/wiki/Wishart_distribution)
- [inverse wishart](https://en.wikipedia.org/wiki/Inverse-Wishart_distribution)
- [multivariate normal](https://en.wikipedia.org/wiki/Multivariate_normal_distribution)
- [matrix normal](https://en.wikipedia.org/wiki/Matrix_normal_distribution)
Most notably, this is a header-only collection of functions. Therefore, this
package can be linked to a pre-existing package instead of having to copy
and paste the header files directly into your project's source.
`rgen` is available on CRAN and GitHub.
To install the package, you must first have a compiler on your system that is compatible with R.
For help on obtaining a compiler consult:
- [OS X](http://thecoatlessprofessor.com/programming/r-compiler-tools-for-rcpp-on-os-x/)
- [Windows](http://thecoatlessprofessor.com/programming/rcpp/install-rtools-for-rcpp/)
With a compiler in hand, install the package from CRAN with:
```r
install.packages("rgen")
```
or from GitHub by:
```r
install.packages("remotes")
remotes::install_github("coatless-rpkg/rgen")
```
## Using `rgen`
There are two ways to use `rgen`. The first is to use `rgen` in a standalone
script. The script is typically built using `sourceCpp()`. The second approach
allows for `rgen` to be used within an R package.
### Standalone file usage
Within the `C++` file, the `rgen` package provides an Rcpp plugins'
depends statement that must be included after `rgen.h` header. This plugin
statement indicates that a dependency is `rgen`.
```cpp
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
#include <rgen.h>
// [[Rcpp::depends(rgen)]]
```
**Note:** Since `rgen` relies upon `RcppArmadillo`, you must include
the `RcppArmadillo.h` header _and_ include the traditional Rcpp dependency
attribute, e.g. `// [[Rcpp::depends(RcppArmadillo)]]`.
For example, the following would allow for you to sample from an inverse
wishart distribution:
```cpp
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
#include <rgen.h>
// [[Rcpp::depends(rgen)]]
// Surface the riwishart function in the rgen package into R.
// [[Rcpp::export]]
arma::mat riwishart(unsigned int df, const arma::mat& S) {
return rgen::riwishart(df, S);
}
/*** R
# Set seed for reproducibility
set.seed(111)
# Call the C++ function from R
riwishart(3, diag(2))
*/
```
### Package usage
To use `rgen` in your R package, modify the `DESCRIPTION` file by adding:
```
LinkingTo: Rcpp, RcppArmadillo, rgen
Imports:
Rcpp
```