This package implements in R the affine-invariant sampling method of Goodman & Weare (2010). This is a way of producing Monte-Carlo samples from a target distribution, which can be used for statistical inference.
This R implementation is based on the very clear description given by Foreman-Mackey et al. (2012), who provide an implementation in python.
In R, run install.packages("rgw")
. Note that the version hosted on CRAN may lag behind this one (see VERSION.md).
- Clone this repository.
- In a terminal, navigate to the
<repository base>/R/
. - Run
R CMD install rgw
. Alternatively, in an R session, runinstall.packages("rgw", repos=NULL)
.
Here's the simple example that appears in the documentation:
# In this example, we'll sample from a simple 2D Gaussian.
# Define the log-posterior function
lnP = function(x) sum( dnorm(x, c(0,1), c(pi, exp(0.5)), log=TRUE) )
# Initialize an ensemble of 100 walkers. We'll take 100 steps, saving the ensemble after each.
nwalk = 100
post = array(NA, dim=c(2, nwalk, 101))
post[1,,1] = rnorm(nwalk, 0, 0.1)
post[2,,1] = rnorm(nwalk, 1, 0.1)
# Run
post = GoodmanWeare.rem(post, lnP)
# Plot the final ensemble
plot(post[1,,101], post[2,,101])
# Look at the trace of each parameter for one of the walkers.
plot(post[1,1,])
plot(post[2,1,])
# Go on to get confidence intervals, make niftier plots, etc.
Open an issue.