Skip to content

Commit

Permalink
pmcmc methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kingaa committed Apr 17, 2023
1 parent ae06acb commit 5b5c803
Show file tree
Hide file tree
Showing 28 changed files with 407 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .Rprofile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
options(
help_type="html",
repos=c(
CRAN="https://repo.miserver.it.umich.edu/cran/",
CRAN="https://cloud.r-project.org/",
kingaa="https://kingaa.github.io"
),
useFancyQuotes=FALSE,
Expand Down
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: circumstance
Type: Package
Title: pomp parallelized
Version: 0.0.7.0
Date: 2023-04-14
Version: 0.0.8.0
Date: 2023-04-17
Maintainer: Aaron A. King <kingaa@umich.edu>
Description: Helper functions for parallelizing pomp computations.
Authors@R: c(person(given=c("Aaron","A."),family="King",
role=c("aut","cre"),email="kingaa@umich.edu"))
URL: https://kingaa.github.io/circumstance/
Depends: R(>= 4.1.0), pomp(>= 4.6.4)
Depends: R(>= 4.1.0), pomp(>= 5.1)
Imports: methods, foreach, grid, grDevices, graphics, utils
Suggests: doFuture, doRNG, dplyr, tidyr, ggplot2
Remotes: kingaa/pomp
Expand All @@ -25,3 +25,4 @@ Collate:
'pfilter.R'
'mif2.R'
'plot_matrix.R'
'pmcmc.R'
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(plot_matrix)
exportMethods(continue)
exportMethods(mif2)
exportMethods(pfilter)
exportMethods(pmcmc)
import(methods)
importFrom(foreach,"%dopar%")
importFrom(foreach,foreach)
Expand Down Expand Up @@ -35,5 +36,6 @@ importFrom(grid,xaxisGrob)
importFrom(grid,yaxisGrob)
importFrom(pomp,mif2)
importFrom(pomp,pfilter)
importFrom(pomp,pmcmc)
importFrom(utils,globalVariables)
importFrom(utils,head)
4 changes: 3 additions & 1 deletion R/mif2.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ setMethod(
setMethod(
"mif2",
signature=signature(data = "ANY", starts = "missing"),
definition = pomp::mif2
definition = function (data, ...) {
pomp::mif2(data,...)
}
)

##' @rdname mif2
Expand Down
4 changes: 3 additions & 1 deletion R/pfilter.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ setMethod(
setMethod(
"pfilter",
signature=signature(data = "ANY", Nrep = "missing"),
definition = pomp::pfilter
definition = function (data, ...) {
pomp::pfilter(data,...)
}
)

##' @rdname pfilter
Expand Down
108 changes: 108 additions & 0 deletions R/pmcmc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
##' Particle Markov chain Monte Carlo in parallel
##'
##' Runs multiple instances of \code{pmcmc} using \code{foreach}.
##'
##' @name pmcmc
##' @rdname pmcmc
##' @importFrom pomp pmcmc
##' @importFrom foreach foreach %dopar%
##' @include pfilter.R mif2.R
##'
##' @param data passed to \code{\link[pomp:pmcmc]{pomp::pmcmc}}
##' @param starts data frame containing parameters at which to begin iterated filtering
##' @param ... all additional arguments are passed to \code{\link[pomp:pmcmc]{pomp::pmcmc}}
##'
##' @seealso \code{\link[pomp:pmcmc]{pomp::pmcmc}}.
##'
NULL

setGeneric(
"pmcmc",
function (data, starts, ...)
standardGeneric("pmcmc")
)

##' @rdname pmcmc
##' @export
setMethod(
"pmcmc",
signature=signature(data = "ANY", starts = "data.frame"),
definition = function (data, starts, ...) {
foreach (iter_i=seq_len(nrow(starts)),.combine=c) %dopar% {
pomp::pmcmc(data,params=starts[iter_i,],...)
} -> res
names(res) <- row.names(starts)
attr(res,"doPar") <- get_doPar_info()
res
}
)

##' @rdname pmcmc
##' @export
setMethod(
"pmcmc",
signature=signature(data = "ANY", starts = "missing"),
definition = function (data, ...) {
pomp::pmcmc(data,...)
}
)

##' @rdname pmcmc
##' @export
setMethod(
"pmcmc",
signature=signature(data = "pompList", starts = "missing"),
definition = function (data, ...) {
foreach (iter_i=seq_along(data),.combine=c) %dopar% {
pomp::pmcmc(data[[iter_i]],...)
} -> res
names(res) <- names(data)
attr(res,"doPar") <- get_doPar_info()
res
}
)

##' @rdname pmcmc
##' @export
setMethod(
"pmcmc",
signature=signature(data = "pfilterList", starts = "missing"),
definition = function (data, ...) {
foreach (iter_i=seq_along(data),.combine=c) %dopar% {
pomp::pmcmc(data[[iter_i]],...)
} -> res
names(res) <- names(data)
attr(res,"doPar") <- get_doPar_info()
res
}
)

##' @rdname pmcmc
##' @export
setMethod(
"pmcmc",
signature=signature(data = "pmcmcList", starts = "missing"),
definition = function (data, ...) {
foreach (iter_i=seq_along(data),.combine=c) %dopar% {
pomp::pmcmc(data[[iter_i]],...)
} -> res
names(res) <- names(data)
attr(res,"doPar") <- get_doPar_info()
res
}
)

##' @rdname continue
##' @export
setMethod(
"continue",
signature=signature(object = "pmcmcList"),
definition = function (object, ...) {
foreach (iter_i=seq_along(object),.combine=c) %dopar% {
pomp::continue(object[[iter_i]],...)
} -> res
names(res) <- names(object)
attr(res,"doPar") <- get_doPar_info()
res
}
)
10 changes: 8 additions & 2 deletions inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
_N_e_w_s _f_o_r _p_a_c_k_a_g_e '_c_i_r_c_u_m_s_t_a_n_c_e'

_C_h_a_n_g_e_s _i_n '_c_i_r_c_u_m_s_t_a_n_c_e' _v_e_r_s_i_o_n _0._0._6:
_C_h_a_n_g_e_s _i_n '_c_i_r_c_u_m_s_t_a_n_c_e' _v_e_r_s_i_o_n _0._0._8:

• New ‘plot_matrix’ method for making scatterplot matrices.
• New parallel ‘pmcmc’ methods for parallel particle Markov
chain Monte Carlo.

_C_h_a_n_g_e_s _i_n '_c_i_r_c_u_m_s_t_a_n_c_e' _v_e_r_s_i_o_n _0._0._7:

• Information on parallel backend is now stored as an attribute
of returned objects.

_C_h_a_n_g_e_s _i_n '_c_i_r_c_u_m_s_t_a_n_c_e' _v_e_r_s_i_o_n _0._0._5:

Expand Down
9 changes: 7 additions & 2 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
\name{NEWS}
\title{News for package `circumstance'}
\section{Changes in \pkg{circumstance} version 0.0.6}{
\section{Changes in \pkg{circumstance} version 0.0.8}{
\itemize{
\item New \code{plot_matrix} method for making scatterplot matrices.
\item New parallel \code{pmcmc} methods for parallel particle Markov chain Monte Carlo.
}
}
\section{Changes in \pkg{circumstance} version 0.0.7}{
\itemize{
\item Information on parallel backend is now stored as an attribute of returned objects.
}
}
\section{Changes in \pkg{circumstance} version 0.0.5}{
Expand Down
5 changes: 4 additions & 1 deletion man/continue.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions man/pmcmc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified tests/mif2-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/mif2-03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/mif2-04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions tests/mif2.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ mfs |>

mfs |> logLik()

mfs[[1]] |> mif2()

mfs |>
continue(Nmif=5) |>
traces(pars=c("alpha_1","alpha_3","loglik")) |>
Expand Down
17 changes: 10 additions & 7 deletions tests/mif2.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Attaching package: ‘circumstance’

The following objects are masked from ‘package:pomp’:

continue, mif2, pfilter
continue, mif2, pfilter, pmcmc

> library(tidyr)
> library(dplyr)
Expand Down Expand Up @@ -104,6 +104,9 @@ Removed 3 rows containing missing values (`geom_line()`).
1 2 3
-480 -483 -477
>
> mfs[[1]] |> mif2()
<object of class ‘mif2d_pomp’>
>
> mfs |>
+ continue(Nmif=5) |>
+ traces(pars=c("alpha_1","alpha_3","loglik")) |>
Expand All @@ -121,12 +124,12 @@ Removed 3 rows containing missing values (`geom_line()`).
+ as.data.frame() |>
+ head()
.L1 time y1 y2 ess cond.logLik
1 1 1 -4.05 4.781 96.0 -4.19
2 1 2 1.83 6.273 58.8 -4.69
3 1 3 -1.32 7.556 36.5 -5.38
4 1 4 6.64 5.730 30.4 -5.35
5 1 5 6.58 1.561 121.1 -3.86
6 1 6 7.54 0.176 80.8 -4.27
1 1 1 -4.05 4.781 81.1 -4.27
2 1 2 1.83 6.273 82.8 -4.26
3 1 3 -1.32 7.556 30.3 -5.31
4 1 4 6.64 5.730 36.7 -5.25
5 1 5 6.58 1.561 108.6 -3.99
6 1 6 7.54 0.176 70.4 -4.53
>
> mfs |>
+ lapply(as_pomp) |>
Expand Down
2 changes: 1 addition & 1 deletion tests/pfilter.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Attaching package: ‘circumstance’

The following objects are masked from ‘package:pomp’:

continue, mif2, pfilter
continue, mif2, pfilter, pmcmc

> library(tidyr)
> library(dplyr)
Expand Down
Binary file modified tests/plot_matrix-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plot_matrix-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plot_matrix-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plot_matrix-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/plot_matrix-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/plot_matrix.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Expand Down Expand Up @@ -43,7 +45,7 @@ Attaching package: ‘circumstance’

The following objects are masked from ‘package:pomp’:

continue, mif2, pfilter
continue, mif2, pfilter, pmcmc

>
> png(filename="plot_matrix-%01d.png",res=100)
Expand Down
Binary file added tests/pmcmc-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/pmcmc-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/pmcmc-03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/pmcmc-04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5b5c803

Please sign in to comment.