-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
using-samplers.Rmd
107 lines (76 loc) · 2.96 KB
/
using-samplers.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
---
title: "Using the rgen Samplers"
author: "James Balamuta"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Using the rgen Samplers}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
The purpose of the `rgen` package is to provide popular sampling distributions
that are not exported by [_R_'s Math API](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Numerical-analysis-subroutines) or
available in [_C++11_ and onwards](http://en.cppreference.com/w/cpp/numeric/random).
These samplers are written using [armadillo](http://arma.sourceforge.net/docs.html).
Please note, these samplers, just like the ones in [armadillo](http://arma.sourceforge.net/docs.html)
**cannot** be used in parallelized code as the underlying generation routines
rely upon _R_ calls that are single-threaded.
## Installation of `rgen`
`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("devtools")
devtools::install_github("coatless/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 (>= 0.12.10)
To use `C++11`, you **may** wish to add the following
to your `src/Makevars` and `src/Makevars.win` file:
CXX_STD = CXX11