This function is operationally similar to the function mark in RMark
in that is is a shell that calls several other functions to perform the
following steps: 1) process.data
to setup data and parameters
and package them into a list (processed data),2)
make.design.data
to create the design data for each parameter
in the specified model, 3) create.dm
to create the design
matrices for each parameter based on the formula provided for each
parameter, 4) call to the specific function for model fitting. As with mark
the calling
arguments for crm
are a compilation of the calling arguments for each
of the functions it calls (with some arguments renamed to avoid conflicts).
expects to find a value for ddl
. Likewise, if the data have not been
processed, then ddl
should be NULL. This dual calling structure
allows either a single call approach for each model or alternatively the preferred method
where the data area processed and the design data (ddl
) created once and
then a whole series of models can be analyzed without repeating those steps.
There are some optional arguments that can be used to set initial values and
control other aspects of the optimization. The optimization is done with
the R package/function optimx
and the arguments method
and
hessian
are described with the help for that function. In addition,
any arguments not matching those in the fitting functions (eg cjs_admb
) are passed to
optimx
allowing any of the other parameters to be set. If you set
debug=TRUE
, then at each function evaluation (cjs.lnl
the current values of the parameters and -2*log-likelihood value are output.
In the current implementation, a logit link is used to constrain the
parameters in the unit interval (0,1) except for probability of entry which
uses an mlogit and N which uses a log link. For the probitCJS model, a probit link is
used for the parameters. These could be generalized to
use other link functions. Following the notation of MARK, the parameters in
the link space are referred to as beta
and those in the actual
parameter space of Phi
and p
as reals.
Initial values can be set in 2 ways. 1) Define a list of named vectors with
the initial beta parameter values (eg logit link) in initial
.
The names of the vectors should be the parameter names in the model. Any unspecified
values are set to 0. 2) Specify a previously run model for initial. The code will match the names of the
current design matrix to the names in beta
and use the appropriate
initial values. Any non-specified values are set to 0. If no value is specified for initial,
all beta are started at a value of 0, except for the CJS model which attempts to use a glm approach to
setting starting values. If the glm fails then they are set to 0.
If you have a study with unequal time intervals between capture occasions,
then these can be specified with the argument time.intervals
.
The argument accumulate
defaults to TRUE
. When it is
TRUE
it will accumulate common capture histories that also have
common design and common fixed values (see below) for the parameters. This
will speed up the analysis because in the calculation of the likelihood
(cjs.lnl
it loops over the unique values. In general the
default will be the best unless you have many capture histories and are
using many individual covariate(s) in the formula that would make each entry
unique. In that case there will be no effect of accumulation but the code
will still try to accumulate. In that particular case by setting
accumulate=FALSE
you can skip the code run for accumulation.
Most of the arguments controlling the fitted model are contained in lists in
the arguments model.parameters
and design.parameters
which are
similar to their counterparts in mark inb RMark
. Each is a named list
with the names being the parameters in the model (e.g., Phi and p in "cjs"
and "Phi","p","pent","N" in "js"). Each named element is also a list
containing various values defining the design data and model for the
parameter. The elements of model.parameters
can include
formula
which is an R formula to create the design matrix for the
parameter and fixed
is a matrix of fixed values as described below.
The elements of design.parameters
can include time.varying
,
fields
, time.bins
,age.bins
, and cohort.bins
. See
create.dmdf
for a description of the first 2 and
create.dm
for a description of the last 3.
Real parameters can be set to fixed values using fixed=x
where x is a
matrix with 3 columns and any number of rows. The first column specifies
the particular animal (capture history) as the row number in the dataframe
x
. The second specifies the capture occasion number for the real
parameter to be fixed. For Phi
and pent
these are 1 to
nocc
-1 and for p
they are 2 to nocc
for "cjs" and 1 to
nocc
for "js". This difference is due to the parameter labeling by
the beginning of the interval for Phi (e.g., survival from occasion 1 to 2)
and by the occasion for p
. For "cjs" p
is not estimated for
occasion 1. The third element in the row is the real value in the closed
unit interval [0,1] for the fixed parameter. This approach is completely
general allowing you to fix a particular real parameter for a specific
animal and occasion but it is a bit kludgy. Alternatively, you can set fixed
values by specifying values for a field called fix in the design data for a parameter.
If the value of fix is NA the parameter is estimated and if it is not NA then the real
parameter is fixed at that value. If you also specify fixed as decribed above, they will over-ride any
values you have also set with fix in the design data. To set all of the real values for a
particular occasion you can use the following example with the dipper data
as a template:
model.parameters=list(Phi=list(formula=~1,
fixed=cbind(1:nrow(dipper),rep(2,nrow(dipper)),rep(1,nrow(dipper)))))
The above sets Phi
to 1 for the interval between occasions 2 and 3
for all animals.
Alternatively, you could do as follows:
data(dipper)
dp=process.data(dipper)
ddl=make.design.data(dp)
ddl$Phi$fix=ifelse(ddl$Phi$time==2,1,NA)
At present there is no modification of the parameter count
to address fixing of real parameters except that if by fixing reals, a beta is not needed in the design it will be dropped.
For example, if you were to use ~time for Phi with survival fixed to 1 for time 2, then then beta for that time would not
be included.
To use ADMB (use.admb=TRUE), you need to install: 1) the R package R2admb, 2) ADMB, and 3) a C++ compiler (I recommend gcc compiler).
The following are instructions for installation with Windows. For other operating systems see (http://www.admb-project.org/) and
(https://www.admb-project.org/tools/gcc/).
Windows Instructions:
1) In R use install.packages function or choose Packages/Install Packages from menu and select R2admb.
2) Install ADMB 11: https://www.admb-project.org/downloads/. Put the software in C:/admb to
avoid problems with spaces in directory name and for the function below to work.
3) Install gcc compiler from: https://www.admb-project.org/tools/gcc/. Put in c:/MinGW
I use the following function in R to setup R2admb to access ADMB rather than adding to my path so gcc versions
with Rtools don't conflict.
prepare_admb=function()
{
Sys.setenv(PATH = paste("c:/admb/bin;c:admb/utilities;c:/MinGW/bin;",
Sys.getenv("PATH"), sep = ";"))
Sys.setenv(ADMB_HOME = "c:/admb")
invisible()
}
To use different locations you'll need to change the values used above
Before running crm with use.admb=T, execute the function prepare_admb(). You could put this function or the code it
contains in your .First or .Rprofile so it runs each time you start R.