[Pomp-commits] r220 - in pkg: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon May 10 15:25:56 CEST 2010
Author: kingaa
Date: 2010-05-10 15:25:56 +0200 (Mon, 10 May 2010)
New Revision: 220
Added:
pkg/R/plugins.R
pkg/man/plugins.Rd
Removed:
pkg/R/euler.R
pkg/man/euler.Rd
Log:
- rename these files
Deleted: pkg/R/euler.R
===================================================================
--- pkg/R/euler.R 2010-05-06 12:12:28 UTC (rev 219)
+++ pkg/R/euler.R 2010-05-10 13:25:56 UTC (rev 220)
@@ -1,123 +0,0 @@
-onestep.simulate <- function (xstart, times, params,
- step.fun, ...,
- statenames = character(0),
- paramnames = character(0),
- covarnames = character(0),
- zeronames = character(0),
- tcovar, covar, PACKAGE)
-{
- if (is.character(step.fun)) {
- efun <- try(
- getNativeSymbolInfo(step.fun,PACKAGE)$address,
- silent=FALSE
- )
- if (inherits(efun,'try-error')) {
- stop("no symbol named ",step.fun," in package ",PACKAGE)
- }
- } else if (is.function(step.fun)) {
- if (!all(c('x','t','params','delta.t','...')%in%names(formals(step.fun))))
- stop(sQuote("step.fun")," must be a function of prototype ",sQuote("step.fun(x,t,params,delta.t,...)"))
- efun <- step.fun
- } else {
- stop(sQuote("step.fun")," must be either a function or the name of a compiled routine")
- }
-
- .Call(
- euler_model_simulator,
- func=efun,
- xstart=xstart,
- times=times,
- params=params,
- dt=0,
- method=1L,
- statenames=statenames,
- paramnames=paramnames,
- covarnames=covarnames,
- zeronames=zeronames,
- tcovar=tcovar,
- covar=covar,
- args=pairlist(...)
- )
-}
-
-euler.simulate <- function (xstart, times, params,
- step.fun, delta.t, ...,
- statenames = character(0),
- paramnames = character(0),
- covarnames = character(0),
- zeronames = character(0),
- tcovar, covar, PACKAGE)
-{
- if (is.character(step.fun)) {
- efun <- try(
- getNativeSymbolInfo(step.fun,PACKAGE)$address,
- silent=FALSE
- )
- if (inherits(efun,'try-error')) {
- stop("no symbol named ",step.fun," in package ",PACKAGE)
- }
- } else if (is.function(step.fun)) {
- if (!all(c('x','t','params','delta.t','...')%in%names(formals(step.fun))))
- stop(sQuote("step.fun")," must be a function of prototype ",sQuote("step.fun(x,t,params,delta.t,...)"))
- efun <- step.fun
- } else {
- stop(sQuote("step.fun")," must be either a function or the name of a compiled routine")
- }
-
- .Call(
- euler_model_simulator,
- func=efun,
- xstart=xstart,
- times=times,
- params=params,
- dt=delta.t,
- method=0L,
- statenames=statenames,
- paramnames=paramnames,
- covarnames=covarnames,
- zeronames=zeronames,
- tcovar=tcovar,
- covar=covar,
- args=pairlist(...)
- )
-}
-
-onestep.density <- function (x, times, params,
- dens.fun, ...,
- statenames = character(0),
- paramnames = character(0),
- covarnames = character(0),
- tcovar, covar, log = FALSE,
- PACKAGE)
-{
- if (is.character(dens.fun)) {
- efun <- try(
- getNativeSymbolInfo(dens.fun,PACKAGE)$address,
- silent=FALSE
- )
- if (inherits(efun,'try-error')) {
- stop("no symbol named ",dens.fun," in package ",PACKAGE)
- }
- } else if (is.function(dens.fun)) {
- if (!all(c('x1','x2','t1','t2','params','...')%in%names(formals(dens.fun))))
- stop(sQuote("dens.fun")," must be a function of prototype ",sQuote("dens.fun(x1,x2,t1,t2,params,...)"))
- efun <- dens.fun
- } else {
- stop(sQuote("dens.fun")," must be either a function or the name of a compiled routine")
- }
-
- .Call(
- euler_model_density,
- efun,
- x,
- times,
- params,
- statenames,
- paramnames,
- covarnames,
- tcovar,
- covar,
- log,
- args=pairlist(...)
- )
-}
Copied: pkg/R/plugins.R (from rev 219, pkg/R/euler.R)
===================================================================
--- pkg/R/plugins.R (rev 0)
+++ pkg/R/plugins.R 2010-05-10 13:25:56 UTC (rev 220)
@@ -0,0 +1,130 @@
+onestep.sim <- function (step.fun, PACKAGE) {
+ efun <- pomp.fun(
+ f=step.fun,
+ PACKAGE=PACKAGE,
+ proto="step.fun(x,t,params,delta.t,...)"
+ )
+ function (xstart, times, params, ...,
+ statenames = character(0),
+ paramnames = character(0),
+ covarnames = character(0),
+ zeronames = character(0),
+ tcovar, covar) {
+ .Call(
+ euler_model_simulator,
+ func=efun,
+ xstart=xstart,
+ times=times,
+ params=params,
+ dt=0,
+ method=1L,
+ statenames=statenames,
+ paramnames=paramnames,
+ covarnames=covarnames,
+ zeronames=zeronames,
+ tcovar=tcovar,
+ covar=covar,
+ args=pairlist(...)
+ )
+ }
+}
+
+euler.sim <- function (step.fun, delta.t, PACKAGE) {
+ efun <- pomp.fun(
+ f=step.fun,
+ PACKAGE=PACKAGE,
+ proto="step.fun(x,t,params,delta.t,...)"
+ )
+ function (xstart, times, params, ...,
+ statenames = character(0),
+ paramnames = character(0),
+ covarnames = character(0),
+ zeronames = character(0),
+ tcovar, covar) {
+ .Call(
+ euler_model_simulator,
+ func=efun,
+ xstart=xstart,
+ times=times,
+ params=params,
+ dt=delta.t,
+ method=0L,
+ statenames=statenames,
+ paramnames=paramnames,
+ covarnames=covarnames,
+ zeronames=zeronames,
+ tcovar=tcovar,
+ covar=covar,
+ args=pairlist(...)
+ )
+ }
+}
+
+onestep.dens <- function (dens.fun, PACKAGE) {
+ efun <- pomp.fun(
+ f=dens.fun,
+ PACKAGE=PACKAGE,
+ proto="dens.fun(x1,x2,t1,t2,params,...)"
+ )
+ function (x, times, params, ...,
+ statenames = character(0),
+ paramnames = character(0),
+ covarnames = character(0),
+ tcovar, covar, log = FALSE) {
+ .Call(
+ euler_model_density,
+ func=efun,
+ x=x,
+ times=times,
+ params=params,
+ statenames=statenames,
+ paramnames=paramnames,
+ covarnames=covarnames,
+ tcovar=tcovar,
+ covar=covar,
+ log=log,
+ args=pairlist(...)
+ )
+ }
+}
+
+gillespie.sim <- function (rate.fun, v, d, PACKAGE) {
+ if (!(is.matrix(d)&&is.matrix(v))) {
+ stop(sQuote("v")," and ",sQuote("d")," must be matrices")
+ }
+ nvar <- nrow(v)
+ nevent <- ncol(v)
+ if ((nvar!=nrow(d))||(nevent!=ncol(d)))
+ stop(sQuote("v")," and ",sQuote("d")," must agree in dimension")
+
+ efun <- pomp.fun(
+ f=rate.fun,
+ PACKAGE=PACKAGE,
+ proto="rate.fun(j,x,t,params,...)"
+ )
+ function (xstart, times, params,
+ statenames = character(0),
+ paramnames = character(0),
+ covarnames = character(0),
+ zeronames = character(0),
+ tcovar, covar, ...) {
+ .Call(
+ SSA_simulator,
+ func=efun,
+ mflag=0L, ## Gillespie's algorithm
+ xstart=xstart,
+ times=times,
+ params=params,
+ e=rep(0,nvar),
+ vmatrix=v,
+ dmatrix=d,
+ tcovar=tcovar,
+ covar=covar,
+ statenames=statenames,
+ paramnames=paramnames,
+ covarnames=covarnames,
+ zeronames=zeronames,
+ args=pairlist(...)
+ )
+ }
+}
Deleted: pkg/man/euler.Rd
===================================================================
--- pkg/man/euler.Rd 2010-05-06 12:12:28 UTC (rev 219)
+++ pkg/man/euler.Rd 2010-05-10 13:25:56 UTC (rev 220)
@@ -1,117 +0,0 @@
-\name{euler}
-\alias{euler}
-\alias{euler.simulate}
-\alias{onestep.simulate}
-\alias{onestep.density}
-\title{Plug-ins for dynamical models based on stochastic Euler algorithms}
-\description{
- Plug-in facilities for implementing discrete-time Markov processes and continuous-time Markov processes using the Euler algorithm.
- These can be used in the \code{rprocess} and \code{dprocess} slots of \code{pomp}.
-}
-\usage{
-euler.simulate(xstart, times, params, step.fun, delta.t, \dots,
- statenames = character(0), paramnames = character(0),
- covarnames = character(0), zeronames = character(0),
- tcovar, covar, PACKAGE)
-onestep.simulate(xstart, times, params, step.fun, \dots,
- statenames = character(0), paramnames = character(0),
- covarnames = character(0), zeronames = character(0),
- tcovar, covar, PACKAGE)
-onestep.density(x, times, params, dens.fun, \dots,
- statenames = character(0), paramnames = character(0),
- covarnames = character(0), tcovar, covar, log = FALSE,
- PACKAGE)
-}
-\arguments{
- \item{xstart}{
- Matrix (dimensions \code{nvar} x \code{nrep}) of states at initial time \code{times[1]}.
- }
- \item{x}{
- Matrix (dimensions \code{nvar} x \code{nrep} x \code{ntimes}) of states at times \code{times}.
- }
- \item{times}{
- Vector of times (length \code{ntimes}) at which states are required or given.
- }
- \item{params}{
- Matrix containing parameters of the model.
- The \code{nrep} columns of \code{params} correspond to those of \code{xstart}.
- }
- \item{step.fun}{
- This can be either an R function or the name of a compiled, dynamically loaded native function containing the model simulator.
- It should be written to take a single Euler step from a single point in state space.
- If it is a native function, it must be of type \dQuote{pomp_onestep_sim} as defined in the header \dQuote{pomp.h}, which is included with the package.
- For details on how to write such codes, see Details.
- }
- \item{dens.fun}{
- This can be either an R function or a compiled, dynamically loaded native function containing the model transition log probability density function.
- This function will be called to compute the log likelihood of the actual Euler steps.
- It must be of type \dQuote{pomp_onestep_pdf} as defined in the header \dQuote{pomp.h}, which is included with the package.
- For details on how to write such codes, see Details.
- }
- \item{delta.t}{
- Time interval of Euler steps.
- }
- \item{statenames, paramnames, covarnames}{
- Names of state variables, parameters, covariates, in the order they will be expected by the routine named in \code{step.fun} and \code{dens.fun}.
- This information is only used when the latter are implemented as compiled native functions.
- }
- \item{zeronames}{
- Names of additional variables which will be zeroed before each time in \code{times}.
- These are useful, e.g., for storing accumulations of state variables.
- }
- \item{covar, tcovar}{
- Matrix of covariates and times at which covariates are measured.
- }
- \item{log}{
- logical; if TRUE, log probabilities are given.
- }
- \item{\dots}{
- if \code{step.fun} (or \code{dens.fun}) is an R function, then additional arguments will be passed to it.
- If \code{step.fun} (or \code{dens.fun}) is a native routine, then additional arguments are ignored.
- }
- \item{PACKAGE}{
- an optional argument that specifies to which dynamically loaded library we restrict the search for the native routines.
- If this is \dQuote{base}, we search in the R executable itself.
- }
-}
-\details{
- \code{onestep.simulate} assumes that a single call to \code{step.fun} will advance the state process from one time to the next.
- \code{euler.simulate} will take multiple Euler steps, each of size at most \code{delta.t} (see below for information on how the actual Euler step size is chosen) to get from one time to the next.
-
- \code{onestep.density} assumes that no state transitions occure between consecutive times.
-
- If \code{step.fun} is written as an R function, it must have at least the arguments \code{x}, \code{t}, \code{params}, \code{delta.t}, and \code{\dots}.
- On a call to this function, \code{x} will be a named vector of state variables, \code{t} a scalar time, and \code{params} a named vector of parameters.
- The length of the Euler step will be \code{delta.t}.
- If the argument \code{covars} is included and a covariate table has been included in the \code{pomp} object, then on a call to this function, \code{covars} will be filled with the values, at time \code{t}, of the covariates.
- This is accomplished via interpolation of the covariate table.
- Additional arguments may be given: these will be filled by the correspondingly-named elements in the \code{userdata} slot of the \code{pomp} object (see \code{\link{pomp}}).
-
- If \code{step.fun} is written in a native language, it must be a function of type "pomp_onestep_sim" as specified in the header "pomp.h" included with the package (see the directory "include" in the installed package directory).
-
- If \code{dens.fun} is written as an R function, it must have at least the arguments \code{x1}, \code{x2}, \code{t1}, \code{t2}, \code{params}, and \code{\dots}.
- On a call to this function, \code{x1} and \code{x2} will be named vectors of state variables at times \code{t1} and \code{t2}, respectively.
- The named vector \code{params} contains the parameters.
- If the argument \code{covars} is included and a covariate table has been included in the \code{pomp} object, then on a call to this function, \code{covars} will be filled with the values, at time \code{t1}, of the covariates.
- This is accomplished via interpolation of the covariate table.
- As above, any additional arguments will be filled by the correspondingly-named elements in the \code{userdata} slot of the \code{pomp} object (see \code{\link{pomp}}).
-
- If \code{dens.fun} is written in a native language, it must be a function of type "pomp_onestep_pdf" as defined in the header "pomp.h" included with the package (see the directory "include" in the installed package directory).
-}
-\value{
- \code{euler.simulate} and \code{onestep.simulate} each return a \code{nvar} x \code{nrep} x \code{ntimes} array, where \code{nvar} is the number of state variables, \code{nrep} is the number of replicate simulations (= number of columns of \code{xstart} and \code{params}), and \code{ntimes} is the length of \code{times}.
- If \code{x} is this array, \code{x[,,1]} will be identical to \code{xstart}; the rownames of \code{x} and \code{xstart} will also coincide.
-
- \code{onestep.density} returns a \code{nrep} x \code{ntimes-1} array.
- If \code{f} is this array, \code{f[i,j]} is the likelihood of a transition from \code{x[,i,j]} to \code{x[,i,j+1]} in exactly one Euler step of duration \code{times[j+1]-times[j]}.
-}
-\author{Aaron A. King \email{kingaa at umich dot edu}}
-\seealso{\code{\link{eulermultinom}}, \code{\link{pomp}}}
-\examples{
-## an example showing how to use these functions to implement a seasonal SIR model is contained
-## in the 'examples' directory
-\dontrun{
-edit(file=system.file("examples/euler_sir.R",package="pomp"))
-}
-}
-\keyword{models}
Copied: pkg/man/plugins.Rd (from rev 219, pkg/man/euler.Rd)
===================================================================
--- pkg/man/plugins.Rd (rev 0)
+++ pkg/man/plugins.Rd 2010-05-10 13:25:56 UTC (rev 220)
@@ -0,0 +1,102 @@
+\name{plugins}
+\alias{plugins}
+\alias{onestep.sim}
+\alias{euler.sim}
+\alias{onestep.dens}
+\alias{gillespie.sim}
+\title{Plug-ins for dynamical models based on stochastic Euler algorithms}
+\description{
+ Plug-in facilities for implementing discrete-time Markov processes and continuous-time Markov processes using the Euler algorithm.
+ These can be used in the \code{rprocess} and \code{dprocess} slots of \code{pomp}.
+}
+\usage{
+onestep.sim(step.fun, PACKAGE)
+euler.sim(step.fun, delta.t, PACKAGE)
+gillespie.sim(rate.fun, v, d, PACKAGE)
+onestep.dens(dens.fun, PACKAGE)
+}
+\arguments{
+ \item{step.fun}{
+ This can be either an R function or the name of a compiled, dynamically loaded native function containing the model simulator.
+ It should be written to take a single Euler step from a single point in state space.
+ If it is an R function, it should be of the form \code{step.fun(x,t,params,delta.t,...)}.
+ Here, \code{x} is a named vector containing the value of the state process at time \code{t},
+ \code{params} is a named vector containing parameters,
+ and \code{delta.t} is the length of the Euler time-step.
+ If \code{step.fun} is the name of a native function, it must be of type \dQuote{pomp_onestep_sim} as defined in the header \dQuote{pomp.h}, which is included with the \pkg{pomp} package.
+ For details on how to write such codes, see Details.
+ }
+ \item{rate.fun}{
+ This can be either an R function or the name of a compiled, dynamically loaded native function that computes the transition rates.
+ If it is an R function, it should be of the form \code{rate.fun(j,x,t,params,...)}.
+ Here, \code{j} is the number of the event,
+ \code{x} is a named vector containing the value of the state process at time \code{t},
+ \code{params} is a named vector containing parameters,
+ and \code{delta.t} is the length of the Euler time-step.
+ If \code{rate.fun} is a native function, it must be of type \dQuote{pomp_ssa_rate_fn} as defined in the header \dQuote{pomp.h}, which is included with the package.
+ For details on how to write such codes, see Details.
+ }
+ \item{v, d}{
+ Matrices that specify the continuous-time Markov process in terms of its elementary events.
+ Each should have dimensions \code{nvar} x \code{nevent}, where \code{nvar} is the number of state variables and \code{nevent} is the number of elementary events.
+ \code{v} describes the changes that occur in each elementary event:
+ it will usually comprise the values 1, -1, and 0 according to whether a state variable is incremented, decremented, or unchanged in an elementary event.
+ \code{d} is a binary matrix that describes the dependencies of elementary event rates on state variables:
+ \code{d[i,j]} will have value 1 if event rate \code{j} must be updated as a result of a change in state variable \code{i} and 0 otherwise
+ }
+ \item{dens.fun}{
+ This can be either an R function or a compiled, dynamically loaded native function containing the model transition log probability density function.
+ If it is an R function, it should be of the form \code{dens.fun(x1,x2,t1,t2,params,...)}.
+ Here, \code{x1} and \code{x2} are named vectors containing the values of the state process at times \code{t1} and \code{t2},
+ \code{params} is a named vector containing parameters.
+ If \code{dens.fun} is the name of a native function, it should be of type \dQuote{pomp_onestep_pdf} as defined in the header \dQuote{pomp.h}, which is included with the \pkg{pomp} package.
+ This function should return the log likelihood of a transition from \code{x1} at time \code{t1} to \code{x2} at time \code{t2}, assuming that no intervening transitions have occurred.
+ For details on how to write such codes, see Details.
+ }
+ \item{delta.t}{
+ Size of Euler time-steps.
+ }
+ \item{PACKAGE}{
+ an optional argument that specifies to which dynamically loaded library we restrict the search for the native routines.
+ If this is \dQuote{base}, we search in the R executable itself.
+ }
+}
+\details{
+ \code{onestep.sim} assumes that a single call to \code{step.fun} will advance the state process from one time to the next.
+ \code{euler.sim} will take multiple Euler steps, each of size at most \code{delta.t} (see below for information on how the actual Euler step size is chosen) to get from one time to the next.
+
+ In writing \code{onestep.dens}, you can assume that no state transitions have occurred between \code{t1} and \code{t2}.
+
+ If \code{step.fun} is written as an R function, it must have at least the arguments \code{x}, \code{t}, \code{params}, \code{delta.t}, and \code{\dots}.
+ On a call to this function, \code{x} will be a named vector of state variables, \code{t} a scalar time, and \code{params} a named vector of parameters.
+ The length of the Euler step will be \code{delta.t}.
+ If the argument \code{covars} is included and a covariate table has been included in the \code{pomp} object, then on a call to this function, \code{covars} will be filled with the values, at time \code{t}, of the covariates.
+ This is accomplished via interpolation of the covariate table.
+ Additional arguments may be given: these will be filled by the correspondingly-named elements in the \code{userdata} slot of the \code{pomp} object (see \code{\link{pomp}}).
+
+ If \code{step.fun} is written in a native language, it must be a function of type "pomp_onestep_sim" as specified in the header "pomp.h" included with the package (see the directory "include" in the installed package directory).
+
+ If \code{dens.fun} is written as an R function, it must have at least the arguments \code{x1}, \code{x2}, \code{t1}, \code{t2}, \code{params}, and \code{\dots}.
+ On a call to this function, \code{x1} and \code{x2} will be named vectors of state variables at times \code{t1} and \code{t2}, respectively.
+ The named vector \code{params} contains the parameters.
+ If the argument \code{covars} is included and a covariate table has been included in the \code{pomp} object, then on a call to this function, \code{covars} will be filled with the values, at time \code{t1}, of the covariates.
+ This is accomplished via interpolation of the covariate table.
+ As above, any additional arguments will be filled by the correspondingly-named elements in the \code{userdata} slot of the \code{pomp} object (see \code{\link{pomp}}).
+
+ If \code{dens.fun} is written in a native language, it must be a function of type "pomp_onestep_pdf" as defined in the header "pomp.h" included with the package (see the directory "include" in the installed package directory).
+}
+\value{
+ \code{onestep.sim} and \code{euler.sim} each return functions suitable for use as the argument \code{rprocess} argument in \code{\link{pomp}}.
+
+ \code{onestep.dens} returns a function suitable for use as the argument \code{dprocess} in \code{\link{pomp}}.
+}
+\author{Aaron A. King \email{kingaa at umich dot edu}}
+\seealso{\code{\link{eulermultinom}}, \code{\link{pomp}}}
+\examples{
+## an example showing how to use these functions to implement a seasonal SIR model is contained
+## in the 'examples' directory
+\dontrun{
+edit(file=system.file("examples/sir.R",package="pomp"))
+}
+}
+\keyword{models}
More information about the pomp-commits
mailing list