constrain in Rich Fitzjohn's package
diversitree.constrainParPaleo(f, ..., formulae = NULL, names = parnames(f),
extra = NULL)...x. Use this
only if parnames does not return a vector
for your function. Generally this should not argnames. This
can be used to set up dummy variables (example coming
later).optim, find.mle in diversitree or
mcmc. It will behave like any other function.
However, it has a modified class attribute so that
some methods will dispatch differently:
parnames, for example, will return the names
of the parameters of the constrained function and
parInit will return the initial values for
those same constrained set of parameters. All arguments in
addition to x will be passed through to the original
function f.
Additional useful information from the diversitree manual
(11/01/13):
For help in designing constrained models, the returned
function has an additional argument pars.only, when
this is TRUE the function will return a named vector
of arguments rather than evaluate the function (see
Examples).constrain from the package
diversitree. Users should refer to this parent
function for more detailed discussion of model constraint
usage and detailed examples.
The parent function was forked to add functionality
necessary for dealing with the high parameter count models
typical to some paleontological analyses, particularly the
inverse survivorship method. This necessitated that the new
function be entirely separate from its parent. Names of
functions involved (both exported and not) have been
altered to avoid overlap in the package namespaces. Going
forward, the paleotree package maintainer (Bapst) will try
to be vigilant with respect to changes in constrain
in the original package, diversitree.
Useful information from the diversitree manual (11/01/13):
"If f is a function that takes a vector x as
its first argument, this function returns a new function
that takes a shorter vector x with some elements
constrained in some way; parameters can be fixed to
particular values, constrained to be the same as other
parameters, or arbitrary expressions of free parameters."
In general, formulae should be of the structure:
LHS ~ RHS
...where the LHS is the 'Parameter We Want to Constrain'
and the RHS is whatever we are constraining the LHS to,
usually another parameter. LHS and RHS are the 'left-hand
side' and 'right-hand side' respectively (which I
personally find obscure).
Like the original constrain function this function
is based on, this function cannot remove constraints
previously placed on a model object and there may be cases
in which the constrained function may not make sense,
leading to an error. The original function will sometimes
issue nonsensical functions with an incorrect number/names
of parameters if the parameters to be constrained are given
in the wrong order in formulae.
modelMethods to set the parameter bounds as
equal. Finally, once parameters with the same bounds are
constrained, the output has updated bounds that reflect the
new set of parameters for the new constrained function.}constrain from the library
diversitree.#simulation example with make_durationFreqCont, with three random groups
set.seed(444)
taxa <- simFossilTaxa(p=0.1,q=0.1,nruns=1,mintaxa=20,maxtaxa=30,maxtime=1000,maxExtant=0)
rangesCont <- sampleRanges(taxa,r=0.5)
grp1 <- matrix(sample(1:3,nrow(taxa),replace=TRUE),,1) #groupings matrix
likFun <- make_durationFreqCont(rangesCont,groups=grp1)
# can constrain both extinction rates to be equal
constrainFun <- constrainParPaleo(likFun,q.2~q.1)
#see the change in parameter names and bounds
parnames(likFun)
parnames(constrainFun)
parbounds(likFun)
parbounds(constrainFun)
# some more ways to constrain stuff!
#constrain all extinction rates to be equal
constrainFun <- constrainParPaleo(likFun,q.all~q.1)
parnames(constrainFun)
#constrain all rates for everything to be a single parameter
constrainFun <- constrainParPaleo(likFun,r.all~q.all)
parnames(constrainFun)
#constrain all extinction rates to be equal & all sampling to be equal
constrainFun <- constrainParPaleo(likFun,q.all~q.1,r.all~r.1)
parnames(constrainFun)
#similarly, can use match.all to make all matching parameters equal each other
constrainFun <- constrainParPaleo(likFun,match.all~match.all)
parnames(constrainFun)
#Constrain rates in same group to be equal
constrainFun <- constrainParPaleo(likFun,r.match~q.match)
parnames(constrainFun)Run the code above in your browser using DataLab