Title: | Collocation Inference for Dynamic Systems |
---|---|
Description: | These functions implement collocation-inference for continuous-time and discrete-time stochastic processes. They provide model-based smoothing, gradient-matching, generalized profiling and forwards prediction error methods. |
Authors: | Giles Hooker [aut, cre], Luo Xiao [aut], James Ramsay [ctb] |
Maintainer: | Giles Hooker <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0.5 |
Built: | 2025-03-06 03:48:19 UTC |
Source: | https://github.com/cran/CollocInfer |
Functions carry out collocation inference method for nonlinear continuous-time dynamic systems. These are based on basis-expansion representations for the state of the system. Gradient-matching, profiling and EM algorithms are supported.
Package: | CollocInfer |
Type: | Package |
Version: | 2.1.0 |
Date: | 2009-08-19 |
License: | GPL-2 |
LazyLoad: | yes |
Giles Hooker, Luo Xiao
Maintainer: Giles Hooker <[email protected]>
Ramsay, James O., Giles Hooker, Jiguo Cao and David Campbell (2007), "Parameter Estimation in Ordinary Differential Equations: A Generalized Smoothing Approach", Journal of the Royal Statistical Society, 69
Ramsay, James O., and Silverman, Bernard W. (2006), Functional Data Analysis, 2nd ed., Springer, New York.
Five-species Chemostat Model
ChemoData
ChemoData
A 61 by 2 matrix of data observed in a chemostat.
A vector of 61 observation times corresponding to ChemoData.
Named parameter vector as a starting point for estimation ChemoData
.
c('N','C1','C2','B','S')
: the state variable names for the chemostat system.
parameter names for the chemostat system.
Yoshida, T., L. E. Jones, S. P. Ellner, G. F. Fussmann and N. G. Hairston, 2003, "Rapid evolution drives ecological dynamics in a predator-prey system", Nature, 424, pp. 303-306.
Two-Species Rosenzweig-MacArthur Model
ChemoRMData
ChemoRMData
A 108 by 2 matrix of data observed in a chemostat.
Named parameter vector as a starting point for estimation in ChemoRMData
.
A vector of 108 observation times corresponding to ChemoData.
parameter names for the Rosenzweig-MacArthur system.
the state variable names for the Rosenzweig-MacArthur system.
Becks, L., S. P. Ellner, L. E. Jones, and N. G. Hairston, 2010, "Reduction of adaptive genetic diversity radically alters eco-evolutionary community dynamics", Ecology Letters, 13, pp. 989-997.
Diagnostic Plots on the Results of CollocInfer
CollocInferPlots(coefs,pars,lik,proc,times=NULL,data=NULL, cols=NULL,datacols=NULL,datanames=NULL,ObsPlot=TRUE,DerivPlot=TRUE, cex.axis=1.5,cex.lab=1.5,cex=1.5,lwd=2)
CollocInferPlots(coefs,pars,lik,proc,times=NULL,data=NULL, cols=NULL,datacols=NULL,datanames=NULL,ObsPlot=TRUE,DerivPlot=TRUE, cex.axis=1.5,cex.lab=1.5,cex=1.5,lwd=2)
coefs |
Vector giving the current estimate of the coefficients. |
pars |
Vector of estimated parameters. |
lik |
|
proc |
|
times |
Vector observation times for the data. |
data |
Matrix of observed data values. |
cols |
Optional vector specifying a color for each state variable. |
datacols |
Optional vector specifying a color for each observation dimension. |
datanames |
Optional character vector specifying a glyph to plot the data. Taken from the column-names
of |
ObsPlot |
Should a plot of predictions and observations be given? |
DerivPlot |
Should derivative diagnostics be produced? |
cex.axis |
Axis font size. |
cex.lab |
Label font size. |
cex |
Plotting point font size |
lwd |
Plotting line width |
Timevec is taken to be the quadrature values. Three plots can be produced:
If ObsPlot=TRUE
a plot is given of the predicted values of the observations along with
the observations themselves (if given).
If DerivPlot=TRUE
two plots are produced. The first gives the value of the derivative of
the estimated trajectory (dashed) and the value of the right-hand-side of the ordinary differential equation
in proc
(hence the predicted derivative) (solid). The second plot gives their difference in the first panel as well as the estimated trajectory in the second panel.
A list containing elements used in plotting:
timevec |
Times at which the trajectories etc were evaluated. |
traj |
Estimated value of the trajectory. |
dtraj |
Derivative of the estimated trajectory. |
ftraj |
Value of the derivative of the trajectory predicted by |
otraj |
Predicted values of the observations from |
Data generated for FitzHugh-Nagumo Examples
FhNdata
FhNdata
A 41 by 2 matrix of data generated from the FitzHugh Nagumo equations.
A vector of 41 observation times corresponding to FhNdata.
Named parameter vector used to generate FhNdata
.
c('V','R')
: the state variable names for the FitzHugh Nagumo system.
c('a','b','c')
parameter names for the FitzHugh Nagumo system.
James Ramsay, Giles Hooker David Campbell and Jiguo Cao, 2007. "Parameter Estimation for Differential Equations: A Generalized Smoothing Approach". Journal of the Royal Statistical Society Vol 69 No 5.
Parameters Estimated for FhN Data – used to speed up examples
FhNestPars
FhNestPars
Estimated parameters for the FhN Data example.
Estimated coefficients for the FhN Data example.
James Ramsay, Giles Hooker David Campbell and Jiguo Cao, 2007. "Parameter Estimation for Differential Equations: A Generalized Smoothing Approach". Journal of the Royal Statistical Society Vol 69 No 5.
Estimating hidden states to maximize agreement with the process.
FitMatchOpt(coefs,which,pars,proc,meth='nlminb',control=list()) FitMatchErr(coefs,allcoefs,which,pars,proc,sgn=1) FitMatchDC(coefs,allcoefs,which,pars,proc,sgn=1) FitMatchDC2(coefs,allcoefs,which,pars,proc,sgn=1) FitMatchList(coefs,allcoefs,which,pars,proc,sgn=1)
FitMatchOpt(coefs,which,pars,proc,meth='nlminb',control=list()) FitMatchErr(coefs,allcoefs,which,pars,proc,sgn=1) FitMatchDC(coefs,allcoefs,which,pars,proc,sgn=1) FitMatchDC2(coefs,allcoefs,which,pars,proc,sgn=1) FitMatchList(coefs,allcoefs,which,pars,proc,sgn=1)
coefs |
Vector giving the current estimate of the coefficients for the hidden states. |
allcoefs |
Matrix giving the coefficients of all the states including initial values for |
which |
Vector of indices of states to be estimated. |
pars |
Parameters to be used for the processes. |
proc |
|
sgn |
Is the minimizing (1) or maximizing (0)? |
meth |
Optimization function currently one of 'nlminb', 'MaxNR', 'optim' or 'trust'. |
control |
Control object for optimization function. |
These routines allow the values of coefficients for some states to be optimized relative to the others. That is, the
objective defined by proc
is minimized over those states specified in which
leaving the others constant. This
would be typically done, for example, a smooth is taken to estimate some states non-parametrically, but data is not available on all
of them.
A number of optimization routines have been implemented in FitMatchOpt
, some experimentation is advised.
FitMatchOpt |
A list containing
|
FitMatchErr |
The value of the process likelihood at the current estimated states. |
FitMatchDC |
The derivative of |
FitMatchDC2 |
The second derivative of |
FitMatchList |
Returns a list with elements |
ParsMatchErr
, SplineCoefsErr
, inneropt
############################### #### Some Data ##### ############################### data(FhNdata) # And parameter estimates data(FhNest) ############################### #### Basis Object ####### ############################### knots = seq(0,20,0.2) norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) # Initial values for coefficients will be obtained by smoothing fd.data = FhNdata[,1] DEfd = smooth.basis(FhNtimes,fd.data,fdPar(bbasis,1,0.5)) coefs = cbind(DEfd$fd$coefs,rep(0,nbasis)) colnames(coefs) = FhNvarnames ############################################################# ### If We Only Observe One State, We Can Re-Smooth Others ### ############################################################# profile.obj = LS.setup(pars=FhNpars,coefs=coefs,fn=make.fhn(), basisvals=bbasis,lambda=1000,times=FhNtimes) lik = profile.obj$lik proc= profile.obj$proc # DD = Matrix(diag(1,200),sparse=TRUE) # tDD = t(DD) fres = FitMatchOpt(coefs=coefs,which=2,pars=FhNpars,proc) plot(fd(fres$coefs,bbasis))
############################### #### Some Data ##### ############################### data(FhNdata) # And parameter estimates data(FhNest) ############################### #### Basis Object ####### ############################### knots = seq(0,20,0.2) norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) # Initial values for coefficients will be obtained by smoothing fd.data = FhNdata[,1] DEfd = smooth.basis(FhNtimes,fd.data,fdPar(bbasis,1,0.5)) coefs = cbind(DEfd$fd$coefs,rep(0,nbasis)) colnames(coefs) = FhNvarnames ############################################################# ### If We Only Observe One State, We Can Re-Smooth Others ### ############################################################# profile.obj = LS.setup(pars=FhNpars,coefs=coefs,fn=make.fhn(), basisvals=bbasis,lambda=1000,times=FhNtimes) lik = profile.obj$lik proc= profile.obj$proc # DD = Matrix(diag(1,200),sparse=TRUE) # tDD = t(DD) fres = FitMatchOpt(coefs=coefs,which=2,pars=FhNpars,proc) plot(fd(fres$coefs,bbasis))
Forward prediction error objective for choice of lambda in square error criteria.
forward.prediction.error(times,data,coefs,lik,proc,pars,whichtimes=NULL)
forward.prediction.error(times,data,coefs,lik,proc,pars,whichtimes=NULL)
times |
Vector observation times for the data. |
data |
Matrix of observed data values. |
coefs |
Vector giving the current estimate of the coefficients in the spline. |
lik |
|
proc |
|
pars |
Initial values of parameters to be estimated processes. |
whichtimes |
Specifies the start and end times for forward prediction, given by indeces of
If left NULL, |
Forward prediction error can be used to choose values of lambda
in the profiled
estimation routines. The ordinary differential equation is solved starting from the starting
times specified in whichtimes
and measured at the corresponding measurement times. The error is then recorded.
This should then be minimized by a grid search.
The forwards prediction error from the estimates.
Estmates coefficients given parameters.
inneropt(data,times,pars,coefs,lik,proc,in.meth='nlminb',control.in=list())
inneropt(data,times,pars,coefs,lik,proc,in.meth='nlminb',control.in=list())
data |
Matrix of observed data values. |
times |
Vector observation times for the data. |
pars |
Initial values of parameters to be estimated processes. |
coefs |
Vector giving the current estimate of the coefficients in the spline. |
lik |
|
proc |
|
in.meth |
Inner optimization function currently one of 'nlminb', 'maxNR', 'optim', 'trust' or 'SplineEst'.
The last calls |
control.in |
Control object for inner optimization function. |
This minimizes the objective function defined by the addition of the lik
and proc
objectives with respect to the coefficients. A number of generic
optimization routines can be used and some experimentation is recommended.
A list with elements
coefs |
A matrix giving he optimized coefficients. |
res |
The results of the inner optimization function. |
outeropt
, Smooth.LS
,LS.setup
, multinorm.setup
, SplineCoefsErr
## Not run: # FitzHugh-Nagumo Equations data(FhNdata) # Some data data(FhNest) # with some parameter estimates knots = seq(0,20,0.2) # Create a basis norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) lambda = 10000 # Penalty value DEfd = smooth.basis(FhNtimes,FhNdata,fdPar(bbasis,1,0.5)) # Smooth to estimate # coefficients first coefs = DEfd$fd$coefs colnames(coefs) = FhNvarnames profile.obj = LS.setup(pars=FhNpars,coefs=coefs,fn=make.fhn(), basisvals=bbasis,lambda=lambda,times=FhNtimes) lik = profile.obj$lik proc= profile.obj$proc res = inneropt(FhNdata,times=FhNtimes,FhNpars,coefs,lik,proc,in.meth='nlminb') plot(fd(res$coefs,bbasis)) ## End(Not run)
## Not run: # FitzHugh-Nagumo Equations data(FhNdata) # Some data data(FhNest) # with some parameter estimates knots = seq(0,20,0.2) # Create a basis norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) lambda = 10000 # Penalty value DEfd = smooth.basis(FhNtimes,FhNdata,fdPar(bbasis,1,0.5)) # Smooth to estimate # coefficients first coefs = DEfd$fd$coefs colnames(coefs) = FhNvarnames profile.obj = LS.setup(pars=FhNpars,coefs=coefs,fn=make.fhn(), basisvals=bbasis,lambda=lambda,times=FhNtimes) lik = profile.obj$lik proc= profile.obj$proc res = inneropt(FhNdata,times=FhNtimes,FhNpars,coefs,lik,proc,in.meth='nlminb') plot(fd(res$coefs,bbasis)) ## End(Not run)
Solves a differential equation going forward based on a proc
object.
IntegrateForward(y0,ts,pars,proc,more)
IntegrateForward(y0,ts,pars,proc,more)
y0 |
Initial conditions to start from. |
ts |
Vector of time points at which to report values of the differential equation solution. |
pars |
Initial values of parameters to be estimated processes. |
proc |
Object defining the state process. This can either be a function evaluating
the right hand side of the differential equation or a |
more |
If |
Returns the output from solving the differential equation using the lsoda
routines.
Specifically, it returns a list with elements
The output times.
The output states.
proc = make.SSEproc() proc$more = make.fhn() proc$more$names = c('V','R') y0 = c(-1,1) names(y0) = c('V','R') pars = c(0.2,0.2,3) names(pars) = c('a','b','c') ts = seq(0,20,0.5) value = IntegrateForward(y0,ts,pars,proc) matplot(value$times,value$states)
proc = make.SSEproc() proc$more = make.fhn() proc$more$names = c('V','R') y0 = c(-1,1) names(y0) = c('V','R') pars = c(0.2,0.2,3) names(pars) = c('a','b','c') ts = seq(0,20,0.5) value = IntegrateForward(y0,ts,pars,proc) matplot(value$times,value$states)
Returns a list of functions that calculate finite difference derivatives.
make.findif.ode() make.findif.loglik() make.findif.var()
make.findif.ode() make.findif.loglik() make.findif.var()
All these functions require the sepcification of more$eps
to give the size of the
finite differencing step. They also require more
to specify the original object (ODE right hand side functions,
definitions of lik
and proc
objects).
A list of functions that calculate the derivatives via finite differencing schemes.
make.findif.ode |
calculates finite differences of a transform. |
make.findif.loglik |
returns the finite differences to a calculated log likelihood; used
within |
make.findif.var |
finite difference approximations to variances; mostly used in
the |
# Sum of squared errors with finite differencing to get right-hand-side derivatives proc = make.SSEproc() proc$more = make.findif.ode() # Finite differencing for the log likelihood lik = make.findif.loglik() lik$more = make.SSElik() # Multivariate normal transitions with finite differencing for mean and variance functions lik = make.multinorm() lik$more = c(make.findif.ode,make.findif.var) # Finite differencing for transition density of a discrete time system proc = make.Dproc() proc$more = make.findif.loglik()
# Sum of squared errors with finite differencing to get right-hand-side derivatives proc = make.SSEproc() proc$more = make.findif.ode() # Finite differencing for the log likelihood lik = make.findif.loglik() lik$more = make.SSElik() # Multivariate normal transitions with finite differencing for mean and variance functions lik = make.multinorm() lik$more = c(make.findif.ode,make.findif.var) # Finite differencing for transition density of a discrete time system proc = make.Dproc() proc$more = make.findif.loglik()
Returns a list of functions that calculate the observation process distribution and its derivatives; designed to be used with the collocation inference functions.
make.SSElik() make.multinorm()
make.SSElik() make.multinorm()
These functions require more
to be a list with elements:
fn
The transform function of the states to observations, or to their derivatives.
dfdx
The derivative of fn
with respect to states.
dfdp
The derivative of fn
with respect to parameters.
d2fdx2
The second derivative of fn
with respect to states.
d2fdxdp
The cross derivative of fn
with respect to states and parameters.
make.Multinorm
further requires:
var.fn
The variance given in terms of states and parameters.
var.dfdx
The derivative of var.fn
with respect to states.
var.dfdp
The derivative of var.fn
with respect to parameters.
var.d2fdx2
The second derivative of var.fn
with respect to states.
var.d2fdxdp
The cross derivative of var.fn
with respect to states and parameters.
make.SSElik
further requres weights
giving weights to each observation.
A list of functions that calculate the log observation distribution and its derivatives.
make.SSElik |
calculates weighted squared error between predictions
(given by |
make.Multinorm |
calculates a multivariate normal distribution. |
# Straightforward sum of squares: lik = make.SSElik() lik$more = make.id() # Multivariate normal about an exponentiated state with constant variance lik = make.multinorm() lik$more = c(make.exp(),make.cvar())
# Straightforward sum of squares: lik = make.SSElik() lik$more = make.id() # Multivariate normal about an exponentiated state with constant variance lik = make.multinorm() lik$more = c(make.exp(),make.cvar())
Functions to modify liklihood, transform, lik
and proc
objects so that the operate with the state defined on a log scale.
make.logtrans() make.exptrans() make.logstate.lik() make.exp.Cproc() make.exp.Dproc()
make.logtrans() make.exptrans() make.logstate.lik() make.exp.Cproc() make.exp.Dproc()
All functions require more
to specify the original object (ODE right hand side functions,
definitions of lik
and proc
objects).
A list of functions that calculate log transforms and derivatives in various contexts.
make.logtrans |
modifies the right hand side of a differential equation and its derivatives for a loged state vector. |
make.exptrans |
modfies a map from states to observations to a map from logged states to observations along with its derivatives. |
make.logstate.lik |
modifies a |
make.exp.Cproc |
|
make.exp.Dproc |
|
LS.setup
, make.Cproc
, make.Dproc
# Model the log of an SEIR process proc = make.SSEproc() proc$more = make.logtrans() proc$more$more = make.SEIR() # Observe a linear combination of lik = make.logstate.lik() lik$more = make.SSElik() lik$more$more = make.genlin() # SEIR Model with multivariate transition densities proc = make.exp.Cproc() proc$more = make.multinorm() proc$more$more = c(make.SEIR(),make.cvar())
# Model the log of an SEIR process proc = make.SSEproc() proc$more = make.logtrans() proc$more$more = make.SEIR() # Observe a linear combination of lik = make.logstate.lik() lik$more = make.SSElik() lik$more$more = make.genlin() # SEIR Model with multivariate transition densities proc = make.exp.Cproc() proc$more = make.multinorm() proc$more$more = c(make.SEIR(),make.cvar())
Functions to define process distributions in the collocation inference package.
make.Dproc() make.Cproc() make.SSEproc()
make.Dproc() make.Cproc() make.SSEproc()
All functions require more
to specify this distribution. This should be a list containing
fn
The distribution specified.
dfdx
The derivative of fn
with respect to states.
dfdp
The derivative of fn
with respect to parameters.
d2fdx2
The second derivative of fn
with respect to states.
d2fdxdp
The cross derivative of fn
with respect to states and parameters.
For Cproc
and Dproc
this should specify the distribution; for SSEproc
it
should specify the right hand side of a differential equation.
A list of functions that the process distribution
make.Cproc |
creates functions to evaluate the distribution of the derivative of the state vector given the current state for continuous-time systems. |
make.Dproc |
creates functions to evaluate the distribution of the next time point of the state vector given the current state for discrete-state systems. |
make.SSEproc |
treats the distribution of the derivative as an independent gaussian and cacluates weighted sums of squared errors between derivatives and the prediction from the current state. |
# FitzHugh-Nagumo Equations proc = make.SSEproc() proc$more = make.fhn() # Henon Map proc = make.Dproc() proc$more = make.Henon # SEIR with multivariate normal transitions proc = make.Cproc() proc$more = make.multinorm() proc$more$more = c(make.SEIR(),make.var.SEIR())
# FitzHugh-Nagumo Equations proc = make.SSEproc() proc$more = make.fhn() # Henon Map proc = make.Dproc() proc$more = make.Henon # SEIR with multivariate normal transitions proc = make.Cproc() proc$more = make.multinorm() proc$more$more = c(make.SEIR(),make.var.SEIR())
Returns a list of functions that calculate the transform and its derivatives.
make.id() make.exp() make.genlin() make.fhn() make.Henon() make.SEIR() make.NS() chemo.fun(times,y,p,more=NULL)
make.id() make.exp() make.genlin() make.fhn() make.Henon() make.SEIR() make.NS() chemo.fun(times,y,p,more=NULL)
All the functions
created by make...
functions, require the arguments needed by chemo.fun
times |
Evaluation times |
y |
Values of the state at the evaluation times |
p |
Parameters to be used |
more |
A list of additional arguments, in this case |
make.genlin
requires the specification of further elements in the list. In particular
the element more
should be a list containing
mat
a matrix defining the linear transform before any parameters are added. This may be all zero, but it may also specify fixed elements, if desired.
sub
a k-by-3 matrix indicating which parameters should be entered into
which elements of mat
. Each row is a triple giving the row and colum of mat
to be
specified and the element of the parameter vector that should be substituted. sub
over-rides
any values in mat
.
force
if input functions are given, these are given as a list.
force.mat
specifying the influence of the elements of force
on the state
variables. Defined as in mat
.
force.sub
defined as in sub
, over-rides the elements of force.mat
with
parameter values.
make.diagnostics
estimates forcing-function diagnostics as in Hooker, 2009 for
goodness-of-fit assessment. It requires
psi
Values of a basis expansion for forcing functions at the quadrature points.
which
Which states are to be forced?
fn
, dfdx
, d2fdx2
Functions and derivatives as would be used to estimate parameters for the original equations.
Parameters to go into more$fn
.
make.SEIR
estimates parameters and a seasonal variation in the infection rate in an
SEIR model. It requires the specification of the seasonal change rate in more
by
a list with objects
beta.fun
A function to calculate beta, it should have arguments t
,
p
and betadef
and return a matrix giving the value of beta at times t
with parameters p
.
beta.dfdp
Should calculate the derivative of beta.fun
with respect to p
,
at times t
returning a matrix. The matrix should be of size length(t)
by
length(p)
where p
is the entire parameter vector.
betadef
Additional inputs (eg bases) to beta.fun
and beta.dfdp
.
make.NS
provides functions for the North Shore example. This is a possibly time-varying
forced linear system of one dimension. It requires more
to specify betabasis
to
describe the autoregressive coefficient, and alphabasis
to provide a contant in front of
the functional data object rainfd
.
chemo.fun
Is a five-state predator-prey-resources model used as an example. It stands
alone as a function and should be used with the findif.ode
functions.
A list of functions that calculate the transform and its derivatives, in a form compatible with the collocation inference functions.
make.id |
returns the identity transform. |
make.exp |
returns the exponential transform. |
make.genlin |
returns a linear combination transform – see details section below. |
make.fhn |
returns the FitzHugh-Nagumo equations. |
make.Henon |
reutrns the Henon map. |
make.SEIR |
returns SEIR equations for estimating the shape of a seasonal forcing component. |
make.diagnostics |
functions to perform forcing function diagnostics. |
# Observe the FitzHugh-Nagumo equations proc = make.SSEproc() proc$more = make.fhn() lik = make.SSElik() lik$more = make.id() # Observe an unknown scalar transform of each component of a Henon map, given # in the first two elements of the parameter vector: proc = make.Dproc() proc$more = make.multinorm() proc$more$more = c(make.Henon,make.cvar) lik = make.multinorm() lik$more = c(make.genlin,make.cvar) lik$more$more = list(mat = matrix(0,2,2),sub=matrix(c(1,1,1,2,2,2),2,3,byrow=TRUE)) # Model SEIR equations on the log scale and then exponentiate lik = make.SSElik() lik$more = make.exp() proc = make.SSEproc() proc$more = make.logtrans() proc$more$more = make.SEIR()
# Observe the FitzHugh-Nagumo equations proc = make.SSEproc() proc$more = make.fhn() lik = make.SSElik() lik$more = make.id() # Observe an unknown scalar transform of each component of a Henon map, given # in the first two elements of the parameter vector: proc = make.Dproc() proc$more = make.multinorm() proc$more$more = c(make.Henon,make.cvar) lik = make.multinorm() lik$more = c(make.genlin,make.cvar) lik$more$more = list(mat = matrix(0,2,2),sub=matrix(c(1,1,1,2,2,2),2,3,byrow=TRUE)) # Model SEIR equations on the log scale and then exponentiate lik = make.SSElik() lik$more = make.exp() proc = make.SSEproc() proc$more = make.logtrans() proc$more$more = make.SEIR()
Returns a list of functions that calculate a (possibly state and parameter dependent) variance.
make.cvar() make.var.SEIR()
make.cvar() make.var.SEIR()
make.cvar
requires the specification of further elements in the list. In particular
the element more
should be a list containing
A list of functions that calculate a variance function and its derivatives, in a form compatible with the collocation inference functions.
make.cvar |
returns a variance that is constant but may depend on parameters |
make.var.SEIR |
returns a state-dependent transition covariance matrix calculated for the SEIR equations. |
# Multivariate normal observation of the state vector. lik = make.multinorm() lik$more = c(make.id(),make.cvar())
# Multivariate normal observation of the state vector. lik = make.multinorm() lik$more = c(make.id(),make.cvar())
Groundwater Data from Vancouver's North Shore
NSgroundwater
NSgroundwater
A 315 by 1 matrix of data on groundwater level collected in vancouver.
A vector of 315 observation times corresponding to NSgroundwater.
Rainfall as a covariate to NSgroundwater; this quantity is lagged by 3 days.
Outer optimization; performs profiled estimation.
outeropt(data,times,pars,coefs,lik,proc, in.meth='nlminb',out.meth='nlminb', control.in=list(),control.out=list(),active=1:length(pars))
outeropt(data,times,pars,coefs,lik,proc, in.meth='nlminb',out.meth='nlminb', control.in=list(),control.out=list(),active=1:length(pars))
data |
Matrix of observed data values. |
times |
Vector observation times for the data. |
pars |
Initial values of parameters to be estimated processes. |
coefs |
Vector giving the current estimate of the coefficients in the spline. |
lik |
|
proc |
|
in.meth |
Inner optimization function currently one of 'nlminb', 'maxNR', 'optim' or 'SplineEst'. The last calls |
out.meth |
Outer optimization function to be used, one of 'optim' (defaults to BFGS routine in |
control.in |
Control object for inner optimization function. |
control.out |
Control object for outer optimization function. |
active |
Indices indicating which parameters of |
The outer optimization for parameters looks only at the objective defined by the lik
object. For every parameter value, coefs
are optimized by inneropt
and then the value of
lik
for these coefficients is computed.
A number of optimization routines can be used here, some experimentation is recommended. Libraries
for these optimization routines are not pre-loaded. Where these functions take options as explicit arguments
instead of a list, they should be listed in control.out
and will be called by their names.
The routine creates
temporary files 'curcoefs.tmp' and 'optcoefs.tmp' to update coefficients as pars
evolves. These overwrite
existing files of those names and are deleted before the function terminates.
A list containing
pars |
Optimized parameters |
coefs |
Optimized coefficients at |
res |
The result of the outer optimization. |
counter |
A set of parameters and objective values for each successful iteration. |
inneropt
, Profile.LS
, ProfileSSE
, ProfileErr
, LS.setup
, multinorm.setup
## Not run: data(FhNdata) data(FhNest) knots = seq(0,20,0.2) # Create a basis norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range,nbasis=nbasis,norder=norder,breaks=knots) lambda = 10000 # Penalty value DEfd = smooth.basis(FhNtimes,FhNdata,fdPar(bbasis,1,0.5)) # Smooth to estimate # coefficients first coefs = DEfd$fd$coefs colnames(coefs) = FhNvarnames profile.obj = LS.setup(pars=FhNpars,coefs=coefs,fn=make.fhn(),basisvals=bbasis, lambda=lambda,times=FhNtimes) lik = profile.obj$lik proc= profile.obj$proc res = outeropt(data=FhNdata,times=FhNtimes,pars=FhNpars,coefs=coefs,lik=lik,proc=proc, in.meth="nlminb",out.meth="nlminb",control.in=NULL,control.out=NULL) plot(res$coefs,main='outeropt') print(blah) ## End(Not run)
## Not run: data(FhNdata) data(FhNest) knots = seq(0,20,0.2) # Create a basis norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range,nbasis=nbasis,norder=norder,breaks=knots) lambda = 10000 # Penalty value DEfd = smooth.basis(FhNtimes,FhNdata,fdPar(bbasis,1,0.5)) # Smooth to estimate # coefficients first coefs = DEfd$fd$coefs colnames(coefs) = FhNvarnames profile.obj = LS.setup(pars=FhNpars,coefs=coefs,fn=make.fhn(),basisvals=bbasis, lambda=lambda,times=FhNtimes) lik = profile.obj$lik proc= profile.obj$proc res = outeropt(data=FhNdata,times=FhNtimes,pars=FhNpars,coefs=coefs,lik=lik,proc=proc, in.meth="nlminb",out.meth="nlminb",control.in=NULL,control.out=NULL) plot(res$coefs,main='outeropt') print(blah) ## End(Not run)
Objective function and derivatives to estimate parameters with a fixed smooth.
ParsMatchOpt(pars,coefs,proc,active=1:length(pars),meth='nlminb',control=list()) ParsMatchErr(pars,coefs,proc,active=1:length(pars),allpars,sgn=1) ParsMatchDP(pars,coefs,proc,active=1:length(pars),allpars,sgn=1) ParsMatchList(pars,coefs,proc,active=1:length(pars),allpars,sgn=1)
ParsMatchOpt(pars,coefs,proc,active=1:length(pars),meth='nlminb',control=list()) ParsMatchErr(pars,coefs,proc,active=1:length(pars),allpars,sgn=1) ParsMatchDP(pars,coefs,proc,active=1:length(pars),allpars,sgn=1) ParsMatchList(pars,coefs,proc,active=1:length(pars),allpars,sgn=1)
pars |
Initial values of parameters to be estimated processes. |
coefs |
Vector giving the current estimate of the coefficients in the spline. |
proc |
|
active |
Incides indicating which parameters of |
allpars |
Vector of all parameters, the assignment |
sgn |
Is the minimizing (1) or maximizing (0)? |
meth |
Optimization function currently one of 'nlminb', 'MaxNR', 'optim' or 'trust'. |
control |
Control object for optimization function. |
These routines fix the estimated states at the value given by coefs
and estimate pars
to maximize agreement between the fixed state and the objective
given by the proc
object.
A number of optimization routines have been implemented in FitMatchOpt
, some experimentation is advised.
ParsMatchOpt |
A list containing:
|
ParsMatchErr |
The value of the process likelihood at the current estimated states. |
ParsMatchDP |
The derivative fo |
ParsMatchList |
A list with entries |
FitMatchErr
, SplineCoefsErr
, inneropt
data(FhNdata) ############################### #### Basis Object ####### ############################### knots = seq(0,20,0.2) norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) # Initial values for coefficients will be obtained by smoothing DEfd = smooth.basis(FhNtimes,FhNdata,fdPar(bbasis,1,0.5)) # Smooth to estimate # coefficients first coefs = DEfd$fd$coefs colnames(coefs) = FhNvarnames ################################# ### Initial Parameter Guesses ### ################################# profile.obj = LS.setup(pars=FhNpars,coefs=coefs,fn=make.fhn(),basisvals=bbasis, lambda=1000,times=FhNtimes) lik = profile.obj$lik proc= profile.obj$proc pres = ParsMatchOpt(FhNpars,coefs,proc) npars = pres$pars
data(FhNdata) ############################### #### Basis Object ####### ############################### knots = seq(0,20,0.2) norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) # Initial values for coefficients will be obtained by smoothing DEfd = smooth.basis(FhNtimes,FhNdata,fdPar(bbasis,1,0.5)) # Smooth to estimate # coefficients first coefs = DEfd$fd$coefs colnames(coefs) = FhNvarnames ################################# ### Initial Parameter Guesses ### ################################# profile.obj = LS.setup(pars=FhNpars,coefs=coefs,fn=make.fhn(),basisvals=bbasis, lambda=1000,times=FhNtimes) lik = profile.obj$lik proc= profile.obj$proc pres = ParsMatchOpt(FhNpars,coefs,proc) npars = pres$pars
Newey-West estimate of covariance of parameter estimates from profiling.
Profile.covariance(pars,active=NULL,times,data,coefs,lik,proc, in.meth='nlminb',control.in=NULL,eps=1e-6,GN=FALSE)
Profile.covariance(pars,active=NULL,times,data,coefs,lik,proc, in.meth='nlminb',control.in=NULL,eps=1e-6,GN=FALSE)
pars |
Initial values of parameters to be estimated processes. |
active |
Incides indicating which parameters of |
times |
Vector observation times for the data. |
data |
Matrix of observed data values. |
coefs |
Vector giving the current estimate of the coefficients in the spline. |
lik |
|
proc |
|
in.meth |
Inner optimization function currently one of 'nlminb', 'MaxNR', 'optim' or 'house'. The last calls |
control.in |
Control object for inner optimization functions. |
eps |
Step-size for finite difference estimate of second derivatives. |
GN |
Indicator of whether a Gauss-Newton approximation for the Hessian should be employed. Only valid for least-squares methods. |
Currently assumes a lag-5 auto-correlation among observation vectors.
Returns a Newey-West estimate of the covariance matrix of the parameter estimates.
ProfileErr
, ProfileSSE
, Profile.LS
, Profile.multinorm
# See example in Profile.LS
# See example in Profile.LS
Profile estimation and objective functions for collocation estimation of parameters in continuous-time stochastic processes.
Profile.GausNewt(pars,times,data,coefs,lik,proc,in.meth="nlminb", control.in=NULL,active=1:length(pars), control=list(reltol=1e-6,maxit=50,maxtry=15,trace=1)) ProfileSSE(pars,allpars,times,data,coefs,lik,proc,in.meth='nlminb', control.in=NULL,active=1:length(pars),dcdp=NULL,oldpars=NULL, use.nls=TRUE,sgn=1) ProfileErr(pars,allpars,times,data,coefs,lik,proc,in.meth = "house", control.in=NULL,sgn=1,active=1:length(allpars)) ProfileDP(pars,allpars,times,data,coefs,lik,proc,in.meth = "house", control.in=NULL,sgn=1,sumlik=1,active=1:length(allpars)) ProfileList(pars,allpars,times,data,coefs,lik,proc,in.meth = "house", control.in=NULL,sgn=1,active=1:length(allpars))
Profile.GausNewt(pars,times,data,coefs,lik,proc,in.meth="nlminb", control.in=NULL,active=1:length(pars), control=list(reltol=1e-6,maxit=50,maxtry=15,trace=1)) ProfileSSE(pars,allpars,times,data,coefs,lik,proc,in.meth='nlminb', control.in=NULL,active=1:length(pars),dcdp=NULL,oldpars=NULL, use.nls=TRUE,sgn=1) ProfileErr(pars,allpars,times,data,coefs,lik,proc,in.meth = "house", control.in=NULL,sgn=1,active=1:length(allpars)) ProfileDP(pars,allpars,times,data,coefs,lik,proc,in.meth = "house", control.in=NULL,sgn=1,sumlik=1,active=1:length(allpars)) ProfileList(pars,allpars,times,data,coefs,lik,proc,in.meth = "house", control.in=NULL,sgn=1,active=1:length(allpars))
pars |
Initial values of parameters to be estimated processes. |
allpars |
Full parameter vector including |
times |
Vector observation times for the data. |
data |
Matrix of observed data values. |
coefs |
Vector giving the current estimate of the coefficients in the spline. |
lik |
|
proc |
|
in.meth |
Inner optimization function currently one of 'nlminb', 'MaxNR', 'optim' or 'house'. The last calls |
control.in |
Control object for inner optimization function. |
sgn |
Is the minimizing (1) or maximizing (0)? |
active |
Incides indicating which parameters of |
oldpars |
Starting parameter values for the Q-function in the EM algorithm. |
dcdp |
Estimate for the gradient of the optimized coefficients with respect to parameters; used internally. |
use.nls |
In ProfileSSE, is 'nls' being used in the outer-optimization? |
sumlik |
In ProfileDP and ProfileDP.AllPar; should the gradient be given for each observation, or summed over them? |
control |
A list giving control parameters for Newton-Raphson optimization. It should contain
|
Profile.GausNewt
provides a simple implementation of Gauss-Newton optimization and may
not result in optimized values that are as good as other optimizers in R
.
When Profile.GausNewt
is not being used, ProfileSEE
and ProfileERR
create the
following temporary files:
The number of halving-steps taken on the current optimization step.
The current estimates of the coefficients.
The optimal estimates of the coefficients in the iteration history.
These need to be removed manually when the optimization is finished. optcoefs.tmp
will contain
the optimal value of coefs
for plotting the estimated trajectories.
Profile.GausNewt |
Output of a simple Gaus-Newton iteration to optimizing the objective function when the observation likelihood takes the form of a sum of squared errors. Returns a list with the following elements:
|
ProfileSSE |
Output for the outer optimization when the observation likelihood is given by squared error. This is a list with the following elements
|
ProfileErr |
The outer optimization criterion in the general case: the value of the observation likelihood at the profiled
estimates of |
ProfileDP |
The derivative of |
ProfileList |
Returns the results of ProfileErr and ProfileDP as a list with elements |
outeropt
, Profile.LS
, Profile.multinorm
, LS.setup
, multinorm.setup
These functions are wrappers that create lik and proc functions and run generalized profiling.
Profile.LS(fn,data,times,pars,coefs=NULL,basisvals=NULL,lambda, fd.obj=NULL,more=NULL,weights=NULL,quadrature=NULL, likfn = make.id(), likmore = NULL, in.meth='nlminb',out.meth='nls', control.in,control.out,eps=1e-6,active=NULL,posproc=FALSE, poslik=FALSE,discrete=FALSE,names=NULL,sparse=FALSE) Profile.multinorm(fn,data,times,pars,coefs=NULL,basisvals=NULL,var=c(1,0.01), fd.obj=NULL,more=NULL,quadrature=NULL, in.meth='nlminb',out.meth='optim', control.in,control.out,eps=1e-6,active=NULL, posproc=FALSE,poslik=FALSE,discrete=FALSE,names=NULL,sparse=FALSE)
Profile.LS(fn,data,times,pars,coefs=NULL,basisvals=NULL,lambda, fd.obj=NULL,more=NULL,weights=NULL,quadrature=NULL, likfn = make.id(), likmore = NULL, in.meth='nlminb',out.meth='nls', control.in,control.out,eps=1e-6,active=NULL,posproc=FALSE, poslik=FALSE,discrete=FALSE,names=NULL,sparse=FALSE) Profile.multinorm(fn,data,times,pars,coefs=NULL,basisvals=NULL,var=c(1,0.01), fd.obj=NULL,more=NULL,quadrature=NULL, in.meth='nlminb',out.meth='optim', control.in,control.out,eps=1e-6,active=NULL, posproc=FALSE,poslik=FALSE,discrete=FALSE,names=NULL,sparse=FALSE)
fn |
A function giving the right hand side of a differential/difference equation. The function should have arguments
It should return a matrix of the same dimension of If
These functions take the same arguments as |
data |
Matrix of observed data values. |
times |
Vector observation times for the data. |
pars |
Initial values of parameters to be estimated processes. |
coefs |
Vector giving the current estimate of the coefficients in the spline. |
basisvals |
Values of the collocation basis to be used. This can either be a basis object from the
|
lambda |
( |
var |
( |
fd.obj |
(Optional) A functional data object; if this is non-null, |
more |
An object specifying additional arguments to |
weights |
( |
quadrature |
Quadrature points, should contain two elements (if not NULL)
|
in.meth |
Inner optimization function to be used, currently one of 'nlminb', 'MaxNR', 'optim' or 'house'.
The last calls |
out.meth |
Outer optimization function to be used, depending on the type of method
|
control.in |
Control object for inner optimization function. |
control.out |
Control object for outer optimization function. |
eps |
Finite differencing step size, if needed. |
active |
Incides indicating which parameters of |
posproc |
Should the state vector be constrained to be positive? If this is the case, the state is represented by
an exponentiated basis expansion in the |
poslik |
Should the state be exponentiated before being compared to the data? When the state is represented
on the log scale ( |
discrete |
Is this a discrete-time or a continuous-time system? If discrete, the derivative is instead taken to be the value at the next time point. |
names |
The names of the state variables if not given by the column names of |
sparse |
Should sparse matrices be used for basis values? This option can save memory when
|
likfn |
Defines a map from the trajectory to the observations. This should be in the same form as
|
likmore |
A list containing additional inputs to |
These functional all carry out the profiled optimization method of Ramsay et al 2007.
Profile.LS
uses a sum of squared errors criteria for both fit to data and the fit of the derivatives
to a differential equation. Profile.multinorm
uses multivariate normal approximations.
discrete
changes the system to a discrete-time difference equation with the right hand side function
giving the transition function.
Note that these all call outeropt
, which creates
temporary files 'curcoefs.tmp' and 'optcoefs.tmp' to update coefficients as pars
evolves. These overwrite
existing files of those names and are deleted before the function terminates.
A list with elements
pars |
Optimized parameters |
coefs |
Optimized coefficients at |
lik |
The |
proc |
The |
data |
The data used in doing the fitting. |
times |
The vector of times at which the observations were made |
outeropt
, ProfileErr
, ProfileSSE
, LS.setup
, multinorm.setup
############################### #### Data ####### ############################### data(FhNdata) ############################### #### Basis Object ####### ############################### knots = seq(0,20,0.2) norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) #### Start from pre-estimated values to speed up optimization data(FhNest) spars = FhNestPars coefs = FhNestCoefs lambda = 10000 res1 = Profile.LS(make.fhn(),data=FhNdata,times=FhNtimes,pars=spars,coefs=coefs, basisvals=bbasis,lambda=lambda,in.meth='nlminb',out.meth='nls') Covar = Profile.covariance(pars=res1$pars,times=FhNtimes,data=FhNdata, coefs=res1$coefs,lik=res1$lik,proc=res1$proc) ## Not run: ## Alternative, starting from perturbed coefficients -- takes too long for # automatic checks in CRAN # Initial values for coefficients will be obtained by smoothing DEfd = smooth.basis(FhNtimes,FhNdata,fdPar(bbasis,1,0.5)) # Smooth to estimate # coefficients first coefs = DEfd$fd$coefs colnames(coefs) = FhNvarnames ############################### #### Optimization ### ############################### spars = c(0.25,0.15,2.5) # Perturbed parameters names(spars)=FhNparnames lambda = 10000 res1 = Profile.LS(make.fhn(),data=FhNdata,times=FhNtimes,pars=spars,coefs=coefs, basisvals=bbasis,lambda=lambda,in.meth='nlminb',out.meth='nls') par(mfrow=c(2,1)) plotfit.fd(FhNdata,FhNtimes,fd(res1$coefs,bbasis)) ## End(Not run) ## Not run: #################################################### ### An Explicitly Multivariate Normal Formation ### #################################################### var = c(1,0.0001) res2 = Profile.multinorm(make.fhn(),FhNdata,FhNtimes,pars=res1$pars, res1$coefs,bbasis,var=var,out.meth='nlminb', in.meth='nlminb') ## End(Not run)
############################### #### Data ####### ############################### data(FhNdata) ############################### #### Basis Object ####### ############################### knots = seq(0,20,0.2) norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) #### Start from pre-estimated values to speed up optimization data(FhNest) spars = FhNestPars coefs = FhNestCoefs lambda = 10000 res1 = Profile.LS(make.fhn(),data=FhNdata,times=FhNtimes,pars=spars,coefs=coefs, basisvals=bbasis,lambda=lambda,in.meth='nlminb',out.meth='nls') Covar = Profile.covariance(pars=res1$pars,times=FhNtimes,data=FhNdata, coefs=res1$coefs,lik=res1$lik,proc=res1$proc) ## Not run: ## Alternative, starting from perturbed coefficients -- takes too long for # automatic checks in CRAN # Initial values for coefficients will be obtained by smoothing DEfd = smooth.basis(FhNtimes,FhNdata,fdPar(bbasis,1,0.5)) # Smooth to estimate # coefficients first coefs = DEfd$fd$coefs colnames(coefs) = FhNvarnames ############################### #### Optimization ### ############################### spars = c(0.25,0.15,2.5) # Perturbed parameters names(spars)=FhNparnames lambda = 10000 res1 = Profile.LS(make.fhn(),data=FhNdata,times=FhNtimes,pars=spars,coefs=coefs, basisvals=bbasis,lambda=lambda,in.meth='nlminb',out.meth='nls') par(mfrow=c(2,1)) plotfit.fd(FhNdata,FhNtimes,fd(res1$coefs,bbasis)) ## End(Not run) ## Not run: #################################################### ### An Explicitly Multivariate Normal Formation ### #################################################### var = c(1,0.0001) res2 = Profile.multinorm(make.fhn(),FhNdata,FhNtimes,pars=res1$pars, res1$coefs,bbasis,var=var,out.meth='nlminb', in.meth='nlminb') ## End(Not run)
Data generated for SEIR Examples
SEIRdata
SEIRdata
A 261 by 1 matrix of data generated from the SEIR Gillespie process run over 5 years.
A vector of 261 observation times corresponding to SEIRdata.
Named parameter vector used to generate SEIRdata
.
c('V','R')
: the state variable names for the SEIR system.
parameter names for the SEIR system.
Giles Hooker, Stephen P. Ellner, David Earn and Laura Roditi, 2010. "Parameterizing State-space Models for Infectious Disease Dynamics by Generalized Profiling: Measles in Ontario", Technical Report, Cornell University.
These functions set up lik and proc objects of squared error and multinormal processes.
LS.setup(pars,coefs=NULL,fn,basisvals=NULL,lambda,fd.obj=NULL, more=NULL,data=NULL,weights=NULL,times=NULL,quadrature=NULL, likfn = make.id(), likmore = NULL,eps=1e-6, posproc=FALSE,poslik=FALSE,discrete=FALSE,names=NULL,sparse=FALSE) multinorm.setup(pars,coefs=NULL,fn,basisvals=NULL,var=c(1,0.01),fd.obj=NULL, more=NULL,data=NULL,times=NULL,quadrature=NULL,eps=1e-6,posproc=FALSE, poslik=FALSE,discrete=FALSE,names=NULL,sparse=FALSE)
LS.setup(pars,coefs=NULL,fn,basisvals=NULL,lambda,fd.obj=NULL, more=NULL,data=NULL,weights=NULL,times=NULL,quadrature=NULL, likfn = make.id(), likmore = NULL,eps=1e-6, posproc=FALSE,poslik=FALSE,discrete=FALSE,names=NULL,sparse=FALSE) multinorm.setup(pars,coefs=NULL,fn,basisvals=NULL,var=c(1,0.01),fd.obj=NULL, more=NULL,data=NULL,times=NULL,quadrature=NULL,eps=1e-6,posproc=FALSE, poslik=FALSE,discrete=FALSE,names=NULL,sparse=FALSE)
pars |
Initial values of parameters to be estimated processes. |
coefs |
Vector giving the current estimate of the coefficients in the spline. |
fn |
A function giving the right hand side of a differential/difference equation. The function should have arguments
It should return a matrix of the same dimension of If
These functions take the same arguments as
|
basisvals |
Values of the collocation basis to be used. This can either be a basis object from the
For discrete systems, it may also be specified as a matrix, in which case If left as NULL, it is taken from |
lambda |
( |
var |
( |
fd.obj |
(Optional) A functional data object; if this is non-null, |
more |
An object specifying additional arguments to |
data |
The data to be used, this can be a matrix, or a three-dimensional array. If the latter, the middle dimension is taken to be replicates. The data are returned, if replicated they are returned in a concatenated form. |
weights |
( |
times |
Vector observation times for the data. If the data are replicated, times are returned in a concatenated form. |
quadrature |
Quadrature points, should contain two elements (if not NULL)
|
eps |
Finite differencing step size, if needed. |
posproc |
Should the state vector be constrained to be positive? If this is the case, the state is represented by
an exponentiated basis expansion in the |
poslik |
Should the state be exponentiated before being compared to the data? When the state is represented
on the log scale |
discrete |
Is this a discrete or continuous-time system? |
names |
The names of the state variables if not given by the column names of |
sparse |
Should sparse matrices be used for basis values? This option can save memory when
|
likfn |
Defines a map from the trajectory to the observations. This should be in the same form as
|
likmore |
A list containing additional inputs to |
These functions provide basic setup utilities for the collocation inference methods. They define
lik
and proc
objects for sum of squared errors and multivariate normal log likelihoods with
nonlinear transfer functions describing the evolution of the state vector.
Creates sum of squares functions
Creates multinormal log likelihoods for a continuous-time system.
A list with elements
coefs |
Starting values for |
lik |
The |
proc |
The |
data |
The data matrix, concatenated if from a 3d array. |
times |
The vector of observation times, concatenated if data is a 3d array. |
inneropt
, outeropt
, Profile.LS
, Profile.multinorm
, Smooth.LS
, Smooth.multinorm
# FitzHugh-Nagumo t = seq(0,20,0.05) # Observation times pars = c(0.2,0.2,3) # Parameter vector names(pars) = c('a','b','c') knots = seq(0,20,0.2) # Create a basis norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range,nbasis=nbasis,norder=norder,breaks=knots) lambda = 10000 # Penalty value coefs = matrix(0,nbasis,2) # Coefficient matrix profile.obj = LS.setup(pars=pars,coefs=coefs,fn=make.fhn(),basisvals=bbasis, lambda=lambda,times=t) # Using multinorm var = c(1,0.01) profile.obj = multinorm.setup(pars=pars,coefs=coefs,fn=make.fhn(), basisvals=bbasis,var=var,times=t) # Henon - discrete hpars = c(1.4,0.3) t = 1:200 coefs = matrix(0,200,2) lambda = 10000 profile.obj = LS.setup(pars=hpars,coefs=coefs,fn=make.Henon(),basisvals=NULL, lambda=lambda,times=t,discrete=TRUE)
# FitzHugh-Nagumo t = seq(0,20,0.05) # Observation times pars = c(0.2,0.2,3) # Parameter vector names(pars) = c('a','b','c') knots = seq(0,20,0.2) # Create a basis norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range,nbasis=nbasis,norder=norder,breaks=knots) lambda = 10000 # Penalty value coefs = matrix(0,nbasis,2) # Coefficient matrix profile.obj = LS.setup(pars=pars,coefs=coefs,fn=make.fhn(),basisvals=bbasis, lambda=lambda,times=t) # Using multinorm var = c(1,0.01) profile.obj = multinorm.setup(pars=pars,coefs=coefs,fn=make.fhn(), basisvals=bbasis,var=var,times=t) # Henon - discrete hpars = c(1.4,0.3) t = 1:200 coefs = matrix(0,200,2) lambda = 10000 profile.obj = LS.setup(pars=hpars,coefs=coefs,fn=make.Henon(),basisvals=NULL, lambda=lambda,times=t,discrete=TRUE)
Perform the inner optimization to estimate coefficients given parameters.
Smooth.LS(fn,data,times,pars,coefs=NULL,basisvals=NULL,lambda,fd.obj=NULL, more=NULL,weights=NULL,quadrature=NULL,likfn = make.id(), likmore = NULL,in.meth='nlminb',control.in,eps=1e-6, posproc=FALSE,poslik=FALSE,discrete=FALSE,names=NULL, sparse=FALSE) Smooth.multinorm(fn,data,times,pars,coefs=NULL,basisvals=NULL,var=c(1,0.01), fd.obj=NULL,more=NULL,quadrature=NULL,in.meth='nlminb', control.in,eps=1e-6,posproc=FALSE,poslik=FALSE,discrete=FALSE, names=NULL,sparse=FALSE)
Smooth.LS(fn,data,times,pars,coefs=NULL,basisvals=NULL,lambda,fd.obj=NULL, more=NULL,weights=NULL,quadrature=NULL,likfn = make.id(), likmore = NULL,in.meth='nlminb',control.in,eps=1e-6, posproc=FALSE,poslik=FALSE,discrete=FALSE,names=NULL, sparse=FALSE) Smooth.multinorm(fn,data,times,pars,coefs=NULL,basisvals=NULL,var=c(1,0.01), fd.obj=NULL,more=NULL,quadrature=NULL,in.meth='nlminb', control.in,eps=1e-6,posproc=FALSE,poslik=FALSE,discrete=FALSE, names=NULL,sparse=FALSE)
fn |
A function giving the right hand side of a differential/difference equation. The function should have arguments
It should return a matrix of the same dimension of If
These functions take the same arguments as |
data |
Matrix of observed data values. |
times |
Vector observation times for the data. |
pars |
Initial values of parameters to be estimated processes. |
coefs |
Vector giving the current estimate of the coefficients in the spline. |
basisvals |
Values of the collocation basis to be used. This can either be a basis object from the
|
lambda |
( |
var |
( |
fd.obj |
(Optional) A functional data object; if this is non-null, |
more |
An object specifying additional arguments to |
weights |
( |
quadrature |
Quadrature points, should contain two elements (if not NULL)
|
in.meth |
Inner optimization function to be used, currently one of 'nlminb', 'MaxNR', 'optim' or 'SplineEst'.
The last calls |
control.in |
Control object for inner optimization function. |
eps |
Finite differencing step size, if needed. |
posproc |
Should the state vector be constrained to be positive? If this is the case, the state is represented by
an exponentiated basis expansion in the |
poslik |
Should the state be exponentiated before being compared to the data? When the state is represented
on the log scale ( |
discrete |
Is this a discrete or continuous-time system? |
names |
The names of the state variables if not given by the column names of |
sparse |
Should sparse matrices be used for basis values? This option can save memory when
|
likfn |
Defines a map from the trajectory to the observations. This should be in the same form as
|
likmore |
A list containing additional inputs to |
These routines create lik
and proc
objects and call inneropt
.
A list with elements
coefs |
Optimized coefficients at |
lik |
The |
proc |
The |
res |
The result of the optimization method |
data |
The data used in doing the fitting. |
times |
The vector of times at which the observations were made |
inneropt
, LS.setup
, multinorm.setup
, SplineCoefsErr
############################### #### Data ####### ############################### data(FhNdata) ############################### #### Basis Object ####### ############################### knots = seq(0,20,0.2) norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) #### Start from pre-estimated values to speed up optimization data(FhNest) spars = FhNestPars coefs = FhNestCoefs lambda = 10000 res1 = Smooth.LS(make.fhn(),data=FhNdata,times=FhNtimes,pars=spars,coefs=coefs, basisvals=bbasis,lambda=lambda,in.meth='nlminb') ## Not run: # Henon system hpars = c(1.4,0.3) # Parameters t = 1:200 x = c(-1,1) # Create some dataa X = matrix(0,200+20,2) X[1,] = x for(i in 2:(200+20)){ X[i,] = make.Henon()$ode(i,X[i-1,],hpars,NULL) } X = X[20+1:200,] Y = X + 0.05*matrix(rnorm(200*2),200,2) basisvals = diag(rep(1,200)) # Basis is just identiy coefs = matrix(0,200,2) # For sum of squared errors lambda = 10000 res1 = Smooth.LS(make.Henon(),data=Y,times=t,pars=hpars,coefs,basisvals=basisvals, lambda=lambda,in.meth='nlminb',discrete=TRUE) ## End(Not run) ## Not run: # For multinormal transitions var = c(1,0.01) res2 = Smooth.multinorm(make.Henon(),data=Y,t,pars=hpars,coefs,basisvals=NULL, var=var,in.meth='nlminb',discrete=TRUE) ## End(Not run)
############################### #### Data ####### ############################### data(FhNdata) ############################### #### Basis Object ####### ############################### knots = seq(0,20,0.2) norder = 3 nbasis = length(knots) + norder - 2 range = c(0,20) bbasis = create.bspline.basis(range=range(FhNtimes),nbasis=nbasis, norder=norder,breaks=knots) #### Start from pre-estimated values to speed up optimization data(FhNest) spars = FhNestPars coefs = FhNestCoefs lambda = 10000 res1 = Smooth.LS(make.fhn(),data=FhNdata,times=FhNtimes,pars=spars,coefs=coefs, basisvals=bbasis,lambda=lambda,in.meth='nlminb') ## Not run: # Henon system hpars = c(1.4,0.3) # Parameters t = 1:200 x = c(-1,1) # Create some dataa X = matrix(0,200+20,2) X[1,] = x for(i in 2:(200+20)){ X[i,] = make.Henon()$ode(i,X[i-1,],hpars,NULL) } X = X[20+1:200,] Y = X + 0.05*matrix(rnorm(200*2),200,2) basisvals = diag(rep(1,200)) # Basis is just identiy coefs = matrix(0,200,2) # For sum of squared errors lambda = 10000 res1 = Smooth.LS(make.Henon(),data=Y,times=t,pars=hpars,coefs,basisvals=basisvals, lambda=lambda,in.meth='nlminb',discrete=TRUE) ## End(Not run) ## Not run: # For multinormal transitions var = c(1,0.01) res2 = Smooth.multinorm(make.Henon(),data=Y,t,pars=hpars,coefs,basisvals=NULL, var=var,in.meth='nlminb',discrete=TRUE) ## End(Not run)
Model-based smoothing; estimation, objective criterion and derivatives.
SplineEst.NewtRaph(coefs,times,data,lik,proc,pars, control=list(reltol=1e-12,maxit=1000,maxtry=10,trace=0)) SplineCoefsList(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsErr(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsDC(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsDP(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsDC2(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsDCDP(coefs,times,data,lik,proc,pars,sgn=1)
SplineEst.NewtRaph(coefs,times,data,lik,proc,pars, control=list(reltol=1e-12,maxit=1000,maxtry=10,trace=0)) SplineCoefsList(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsErr(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsDC(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsDP(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsDC2(coefs,times,data,lik,proc,pars,sgn=1) SplineCoefsDCDP(coefs,times,data,lik,proc,pars,sgn=1)
coefs |
Vector giving the current estimate of the coefficients in the spline. |
times |
Vector observation times for the data. |
data |
Matrix of observed data values. |
lik |
|
proc |
|
pars |
Parameters to be used for the processes. |
sgn |
Is the minimizing (1) or maximizing (0)? |
control |
A list giving control parameters for Newton-Raphson optimization. It should contain
|
SplineEst.NewtRaph
performs a simple Newton-Raphson estimate for the optimal value of the coefficients.
This estimate lacks the convergence checks of other estimation packages, but may yeild a fast solution when needed.
SplineEst.NewtRaph |
Returns a list that is the result of the optimization with elements
|
SplineCoefsList |
Collates the gradient calculations and returns a list with elements
|
SplineCoefsErr |
The complete data log likelihood for the smooth; the inner optimization objective. |
SplineCoefsDC |
The derivative of |
SplineCoefsDP |
The derivative of |
SplineCoefsDC2 |
The second derivative of |
SplineCoefsDCDP |
The second derivative of |
The output of gradients is in terms of an array with dimensions corresponding to derivatives. Derivatives with with respect to coefficients are given in dimensions before those that give derivatives with respect to parameters.