Skip to content

Commit

Permalink
Merge pull request #2 from abmantz/plotting
Browse files Browse the repository at this point in the history
plotting options
  • Loading branch information
abmantz authored Dec 6, 2017
2 parents d959cd4 + edba0c7 commit f5da421
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
4 changes: 2 additions & 2 deletions R/rgw/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: rgw
Type: Package
Title: Goodman-Weare Affine-Invariant Sampling
Version: 0.1.0
Date: 2016-10-10
Version: 0.2.0
Date: 2017-11-16
Author: Adam Mantz
Maintainer: Adam Mantz <[email protected]>
Description: Implementation of the affine-invariant method of Goodman & Weare (2010) <DOI:10.2140/camcos.2010.5.65>, a method of producing Monte-Carlo samples from a target distribution.
Expand Down
12 changes: 11 additions & 1 deletion R/rgw/R/rgw.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,26 @@ GoodmanWeare = function(ensemble, lnpost, Nsteps, current.lnP=NULL, mc.cores=get
list(ensemble=ensemble, current.lnP=current.lnP)
}

GoodmanWeare.rem = function(post, lnpost, thin=1, mention.every=NA, save.every=NA, save.file=NA, ...) {
GoodmanWeare.rem = function(post, lnpost, thin=1, mention.every=NA, save.every=NA, save.file=NA, show.every=NA, show.params=1:dim(post)[1], show.walkers=min(dim(post)[2],8), show.pch1=1, show.pch2='.', show.pch.switch=500, ...) {
## post should be an Nparam*Nwalkers*Nsteps array with the initial walker positions
## set in post[,,1]. The ensemble will be saved in post[,,i] every thin
## iterations until post is filled.
res = list()
if (!is.na(show.every)) {
par(mfrow=c(length(show.params), 1))
}
for (i in 2:dim(post)[3]) {
res = GoodmanWeare(post[,,i-1], lnpost, thin, res$current.lnP, ...)
post[,,i] = res$ensemble
if (!is.na(mention.every) & i %% mention.every == 0) message(paste('Finished iteration', i))
if (!is.na(save.every) & !is.na(save.file) & i %% save.every == 0) save(post, file=save.file)
if (!is.na(show.every) & i %% show.every == 0) {
if (i < show.pch.switch) pch = show.pch1 else pch = show.pch2
for (j in show.params) {
plot(post[j,1,1:i], ylab=paste('parameter', j), pch=pch, col=1+1)
if (show.walkers > 1) for (k in 2:show.walkers) points(post[j,k,1:i], pch=pch, col=1+k)
}
}
}
post
}
31 changes: 28 additions & 3 deletions R/rgw/man/GoodmanWeare.rem.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Produces a Monte-Carlo Markov ensemble using the affine-invariant method of Good
}
\usage{
GoodmanWeare.rem(post, lnpost, thin=1, mention.every=NA,
save.every=NA, save.file=NA, ...)
save.every=NA, save.file=NA, show.every=NA,
show.params=1:dim(post)[1], show.walkers=min(dim(post)[2],8),
show.pch1=1, show.pch2='.', show.pch.switch=500, ...)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
Expand All @@ -30,9 +32,27 @@ save the accumulated Markov ensemble to disk every time this many iterations are
}
\item{save.file}{
filename for saving progress.
}
\item{show.every}{
plot parameter traces so far to the active graphics device periodically.
}
\item{show.params}{
(sub)set of parameter traces to plot (default is to show all).
}
\item{show.walkers}{
which walkers to plot traces of (default is first 8).
}
\item{show.pch1}{
plot symbol to use for short chains.
}
\item{show.pch2}{
plot symbol to use for long chains.
}
\item{show.pch.switch}{
chain length that distinguishes "short" and "long" chains for plotting purposes.
}
\item{...}{
additional arguments to pass to GoodmanWeare or lnpost.
additional named arguments to pass to GoodmanWeare or lnpost.
}
}
%\details{
Expand All @@ -47,7 +67,12 @@ See help for GoodmanWeare.
Adam Mantz
}
\note{
By default, the code will attempt to run in parallel (see the `parallel' package). To prevent this, pass mc.cores=1.
By default, the code will attempt to run in parallel (see the `parallel'
package). To prevent this, pass mc.cores=1.
If traces are being plotted (show.every not NA),
par(mfrow=c(length(show.params), 1)) is called on the current graphics
device.
}
%% ~Make other sections like Warning with \section{Warning }{....} ~
Expand Down
4 changes: 2 additions & 2 deletions R/rgw/man/rgw-package.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ This package implements the affine-invariant method of Goodman & Weare (2010) <D
\tabular{ll}{
Package: \tab rgw\cr
Type: \tab Package\cr
Version: \tab 0.1.0\cr
Date: \tab 2016-10-10\cr
Version: \tab 0.2.0\cr
Date: \tab 2017-11-16\cr
License: \tab MIT\cr
LazyLoad: \tab yes\cr
}
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<a href="http://ascl.net/1711.006"><img src="https://img.shields.io/badge/ascl-1711.006-blue.svg?colorB=262255" alt="ascl:1711.006" /></a>
<a href="https://cran.r-project.org/package=rgw"><img src="https://img.shields.io/cran/v/rgw.svg" alt="CRAN" /></a>
<a href="https://raw.githubusercontent.com/abmantz/rgw/master/LICENSE"><img src="https://img.shields.io/cran/l/rgw.svg" alt="MIT License" /></a>

# rgw

This package implements in [R](https://www.r-project.org/) the affine-invariant sampling method of [Goodman & Weare (2010)](http://dx.doi.org/10.2140/camcos.2010.5.65). This is a way of producing Monte-Carlo samples from a target distribution, which can be used for statistical inference.
Expand Down Expand Up @@ -45,4 +49,11 @@ plot(post[2,1,])

## Help

Use the [issues](https://github.com/abmantz/rgw/issues).
Open an [issue](https://github.com/abmantz/rgw/issues).

## History

Version | Comments
:-----: | --------
0.2.0 | Added options for plotting traces periodically during the run
0.1.0 | First release --> CRAN

0 comments on commit f5da421

Please sign in to comment.