Title: | Mark-Recapture Analysis |
---|---|
Description: | Accomplishes mark-recapture analysis with covariates. Models available include the Cormack-Jolly-Seber open population (Cormack (1972) <doi:10.2307/2556151>; Jolly (1965) <doi:10.2307/2333826>; Seber (1965) <doi:10.2307/2333827>) and Huggin's (1989) <doi:10.2307/2336377> closed population. Link functions include logit, sine, and hazard. Model selection, model averaging, plot, and simulation routines included. Open population size by the Horvitz-Thompson (1959) <doi:10.2307/2280784> estimator. |
Authors: | Trent McDonald [cre, aut], Eric Regehr [ctb], Bryan Manly [ctb], Jeff Bromaghin [ctb] |
Maintainer: | Trent McDonald <[email protected]> |
License: | GNU General Public License |
Version: | 2.16.11 |
Built: | 2024-11-25 06:25:11 UTC |
Source: | https://github.com/tmcd82070/mra |
Description - This package contains analysis functions, and associated routines, to conduct analyses of mark-recapture (capture-recapture) data using individual, time, and individual-time varying covariates. In general, these routines relate vectors of capture histories to vectors of covariates using a regression approach (Amstrup et al. 2005, Ch 9). All capture, survival, transition, etc. parameters are functions of individual and time specific covariates, and the estimated parameters are coefficients in logistic-linear equations.
Relationship to MARK - For the most part, these routines perform a subset of the analyses available in program MARK or via the MARK front-end package, RMark. The most significant difference between this package and MARK is parameterization. The parameterization used here does not utilize triangular "parameter information matrices" (PIMs) as MARK (and RMark) does. Because of this, the "design" matrix utilized by this package is not parallel to the "design" matrix of program MARK. For those new to mark-recapture analysis, this parameterization difference will be inconsequential. The approach taken here provides equivalent modeling flexibility, yet is easier to grasp and visualize, in our opinion. For those already familiar with the PIMs used by program MARK, it is helpful to view the "PIMs" of this package as rectangular matrices of the real parameters. I.e., the "PIMs" of this package are rectangular matrices where cell (i,j) contains the real parameter (capture or survival) for individual i at capture occasion j.
Analyses available here that are not included in program MARK include:
Estimation of population size from open population CJS models via the Horvitz-Thompson estimator.
Residuals, goodness of fit tests, and associated plots for assessing model fit in open CJS models.
Future Research - The author of MRA welcome interest in and routines that perform the following analyzes:
Continuous time models. Especially those that allow inclusion of covariates.
Band recovery models.
Baysian models.
Joint live-dead recovery models.
MCMC methods or routines that can be applied to exiting models.
Plotting methods for exiting models.
Model selection methods for existing models.
Simulation methods and routines.
Package: | mra |
Type: | Package |
License: | GNU General Public License |
Trent McDonald
Maintainer: Trent McDonald <[email protected]>
Amstrup, S.C., T.L. McDonald, and B.F.J. Manly. 2005. Handbook of Capture-Recapture Analysis, Princeton: Princeton University Press.
Example capture-recapture data from a study of European dippers.
data(dipper.data)
data(dipper.data)
A data frame containing 294 capture histories and the sex designation of birds captured. Capture indicators are either 0 = not captured, 1 = captured, or 2 = captured but died and not released back into the population. Columns in the data frame are:
h1
a numeric vector indicating capture at occasion 1
h2
a numeric vector indicating capture at occasion 2
h3
a numeric vector indicating capture at occasion 3
h4
a numeric vector indicating capture at occasion 4
h5
a numeric vector indicating capture at occasion 5
h6
a numeric vector indicating capture at occasion 6
h7
a numeric vector indicating capture at occasion 7
males
a numeric vector indicating males. 1 = males, 0 = females
females
a numeric vector indicating females. 0 = males, 1 = females
This is a popular capture-recapture example data set. It has been analyzed by Lebreton et al. (1992) Amstrup et al. (2005) and others.
dipper.males
is a vector indicating male birds. I.e., dipper.males <- dipper.data\$males
dipper.histories
is a matrix of just the capture history columns h1 - h7
, extracted
from dipper.data
and made into a matrix. This matrix can be fed directly into
one of the estimation routines, such as F.cjs.estim
.
To access: After loading the MRA library (with library(mra)
) you must
execute data(dipper.data)
, data(dipper.data)
, or data(dipper.males)
to get access to these data frames. They are not attached when the library is loaded.
Amstrup, S. C., McDonald, T. L., and Manly, B. F. J. 2005. Handbook of Capture-Recapture Analysis. Princeton University Press. [Chapter 9 has several examples that use this data.]
data(dipper.data)
data(dipper.data)
Returns a 3D model matrix for capture-recapture modeling in the form of a (giant) 2D matrix.
F.3d.model.matrix(formula, d1, d2)
F.3d.model.matrix(formula, d1, d2)
formula |
A formula object specifying covariates in a capture-recapture
model. Must not have a response, i.e., ~, followed by the names
of 2-D arrays or 1-D vectors contained inside calls to |
d1 |
Magnitude of dimension 1 of the returned matrix. This is
always number of rows in the returned matrix. Usually, |
d2 |
Magnitude of dimension 2 of the returned matrix. This is number of columns in the capture history matrix. |
This routine is intended to be called internally by the routines of MRA. General users should never have to call this routine.
This routine uses a call to eval
with a model frame, and calls the
R internal model.matrix
to
resolve the matrices in the formula. All matrices specified in the models
should be in the current scope and accessible to both eval
and model.matrix
.
See help(F.cjs.estim)
for examples of ways to specify models.
A (giant) 2-d matrix containing covariate values suitable for passing to
the Fortran code that does the estimation for MRA. This matrix has all the
2-d matrices of the model cbind
-ed together. It's dimension is NAN x
NS*(number of coefficients). A convenient way to view the matrix is to assign
a 3-d dimension. I.e., if x
is the result of a call to this function
and there are NX coefficients in the model,
then dim(x) <- c(NAN,NS,NX)
makes a 3-d matrix with NAN rows, NS columns,
and NX pages. View the covariates for a single animal with x[3,,]
or similar
statement.
Names of variables in the model are returned as attribute "variables". Whether the model has an intercept is returned as attribute "intercept".
Trent McDonald, WEST-INC, [email protected]
F.cr.model.matrix
, tvar
, ivar
,
model.matrix
, eval
# Synthetic example with 10 animals and 5 occasions nan <- 10 ns <- 5 sex <- as.factor(as.numeric(runif( nan ) > 0.5)) attr(sex,"ns") <- ns x <- matrix( runif( nan*ns ) , nrow=nan, ncol=ns ) F.3d.model.matrix( ~ ivar(sex) + x, nan, ns )
# Synthetic example with 10 animals and 5 occasions nan <- 10 ns <- 5 sex <- as.factor(as.numeric(runif( nan ) > 0.5)) attr(sex,"ns") <- ns x <- matrix( runif( nan*ns ) , nrow=nan, ncol=ns ) F.3d.model.matrix( ~ ivar(sex) + x, nan, ns )
Return an x and y 3-D array for estimation of a traditional time-variant Cormack-Jolly-Seber capture-recapture model.
F.cjs.covars(nan, ns)
F.cjs.covars(nan, ns)
nan |
Number of individuals/animals. |
ns |
Number of trap/mark occasions. |
Pages from \$x
are designed to useful
for fitting classical CJS models with time-variant, but individual-invariant effects.
To fit a CJS model using this function, the commands would be
something like:
tmp<-F.cjs.covars(nan,ns);F.cjs.estim(capture=~tmp\$x[,,2]+tmp\$x[,,3]+ ..., survival=
~tmp\$x[,,1]+tmp\$x[,,2]+ ..., histories=my.histories)
A list containing a single component, \$x
, that can be used to
estimate a classical CJS model when included in a subsequent call to
F.cjs.estim
. The returned component, \$x
,
is a 3-D array containing 0's everywhere, except for 1's in certain columns.
\$x
has dimension nan
X ns
X ns
.
Element [i,j,k] of \$x
is 1 if j == k, and 0 otherwise. I.e., the k-th "page" of
the 3-D array has 1's in the k-th column, 0's elsewhere.
Trent McDonald, WEST Inc., [email protected]
## Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) dipper.cjs <- F.cjs.estim( capture=~xy$x[,,2]+xy$x[,,3]+xy$x[,,4]+xy$x[,,5]+xy$x[,,6], survival=~xy$x[,,1]+xy$x[,,2]+xy$x[,,3]+xy$x[,,4]+xy$x[,,5], dipper.histories ) print(dipper.cjs)
## Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) dipper.cjs <- F.cjs.estim( capture=~xy$x[,,2]+xy$x[,,3]+xy$x[,,4]+xy$x[,,5]+xy$x[,,6], survival=~xy$x[,,1]+xy$x[,,2]+xy$x[,,3]+xy$x[,,4]+xy$x[,,5], dipper.histories ) print(dipper.cjs)
Estimates Cormack-Jolly-Seber (CJS) capture-recapture models with individual, time, and individual-time varying covariates using the "regression" parametrization of Amstrup et al (2005, Ch 9). For live recaptures only. Losses on capture allowed. Uses a logistic link function to relate probability of capture and survival to external covariates.
F.cjs.estim(capture, survival, histories, cap.init, sur.init, group, nhat.v.meth = 1, c.hat = -1, df = NA, intervals=rep(1,ncol(histories)-1), conf=0.95, link="logit", control=mra.control())
F.cjs.estim(capture, survival, histories, cap.init, sur.init, group, nhat.v.meth = 1, c.hat = -1, df = NA, intervals=rep(1,ncol(histories)-1), conf=0.95, link="logit", control=mra.control())
capture |
Formula specifying the capture probability model. Must be a formula object with
no response. I.e., "~" followed by the names of 2-D arrays of covariates to fit in the capture model.
For example: 'capture = ~ age + sex', where age and sex are matrices of size NAN X NS
containing the age and sex covariate values. NAN = number of animals = number of rows in
|
survival |
Formula specifying the survival probability model. Must be a formula object with
no response. I.e., "~" followed by the names of 2-D arrays of covariates to fit in the survival model.
For example: 'survival = ~ year + ageclass' where year and ageclass are matrices of size NAN X NS
containing year and ageclass covariate values. Number of matrices specified in the survival
model is assumed to be NY.
Time varying and individual varying vectors are fitted using |
histories |
A NAN X NS = (number of animals) X (number of capture occasions) matrix containing capture histories. Capture histories are comprised of 0's, 1',s and 2's. 0 in cell (i,j) means animal i was not captured on occasion j, 1 in cell (i,j) means animal i was captured on occasion j and released live back into the population, 2 in cell (i,j) means animal i was captured on occasion j and was not released back into the population (e.g., it died). Animals with '2' as the last non-zero entry of their history are considered 'censored'. Their lack of capture information is removed from the likelihood after the occasion with the 2. Rows of all zeros (i.e., no captures) are allowed in the history matrix, but do not affect coefficient or population size estimates. A warning is thrown if rows of all zeros exist. Capture and survival probabilities are computed for animals with all zero histories. In this way, it is possible to have the routine compute capture or survival estimates for combinations of covariates that do not exist in the data by associating the covariate combinations with histories that have all zero entries. |
cap.init |
(optional) Vector of initial values for coefficients in the capture model. One element
per covariate in |
sur.init |
(optional) Vector or initial values for coefficients in the survival model. One element
per covariate in |
group |
(optional) A vector of length NAN giving the (non-changing) group membership of every
captured animal (e.g., sex). Group is used only for computing TEST 2 and TEST 3.
TEST 2 and TEST 3 are computed separately for each group. E.g., if group=sex,
TEST 2 and TEST 3 are computed for each sex. TEST 2 and TEST3 are used only
to estimate C-hat. See |
nhat.v.meth |
Integer specifying method for computing variance estimates
of population size estimates. |
c.hat |
External (override) estimate of variance inflation factor ( |
df |
External (override) model degrees of freedom to use during estimation.
If |
intervals |
Time intervals. This is a vector of length |
conf |
Confidence level for the confidence intervals placed around estimates of population size. Default 95% confidence. |
link |
The link function to be used. The link function converts linear predictors in the range (-infinity, infinity) to probabilities in the range (0,1). Valid values for the link function are "logit" (default), "sine", and "hazard". (see Examples for a plot of the link functions)
|
control |
A list containing named control parameters for the minimization and estimation process.
Control parameters include number of iterations, covariance estimation method, etc.
Although the default values work in the vast majority of cases, changes to these
variables can effect speed and performance for ill-behaved models. See
|
This is the work-horse routine for estimating CJS models. It compiles all the covariate matrices, then calls a Fortran routine to maximize the CJS likelihood and perform goodness-of-fit tests. Horvitz-Thompson-type population size estimates are also computed by default.
If control=mra.control(trace=1)
, a log file, named mra.log
, is written to the current directory. This file contains
additional details, such as individual Test 2 and Test 3 components, in a semi-friendly
format. This file is overwritten each run. See help(mra.control)
for more details.
Model Specification: Both the capture
and survival
model can be
specified as any combination of 2-d matrices (time and individual varying covariates),
1-d time varying vectors, 1-d individual
varying vectors, 1-d time varying factors, and 1-d individual varying factors.
Specification of time or individual varying effects uses the
tvar
(for 'time varying') and ivar
(for 'individual varying') functions.
These functions expand covariate vectors along the appropriate dimension to
be 2-d matrices suitable for fitting in the model. ivar
expands
an individual varying vector to all occasions. tvar
expands a time
varying covariate to all individuals. To do the expansion, both tvar
and ivar
need to know the size of the 'other' dimension. Thus, tvar(x,100)
specifies a 2-d matrix with size 100
by length(x)
.
ivar(x,100)
specifies a 2-d matrix with size length(x)
by 100
.
For convenience, the 'other' dimension of time or individual varying covariates
can be specified as an attribute of the vector. Assuming x
is a NS vector and the 'nan' attribute of x
has been set as
attr(x,"nan") <- NAN
, tvar(x,NAN)
and
tvar(x)
are equivalent. Same, but vise-versa, for individual varying
covariates (i.e.,
assign the number of occasions using attr(x,"ns")<-NS
). This saves
some typing in model specification.
Factors are allowed in ivar
and tvar
. When a factor is specified,
the contr.treatment
coding is used. By default, an intercept
is assumed and the first level of all
factors are dropped from the model (i.e., first levels are the reference levels,
the default R action). However, there are applications where more than one level
will need to be dropped, and the user has control over this via the drop.levels
argument to ivar
and tvar
. For example,
tvar(x,drop.levels=c(1,2))
drops the first 2 levels of factor x.
tvar(x,drop.levels=length(levels(x)))
does the SAS thing and drops
the last level of factor x
. If drop.levels
is outside the range
[1,length(levels(x))
] (e.g., negative or 0), no levels of the factor
are dropped. If no intercept is fitted in the model, this results in the
so-called cell means coding for factors.
Example model specifications: Assume 'age' is a NAN x NS 2-d matrix of ages, 'effort' is a size NS 1-d vector of efforts, and 'sex' is a size NAN 1-d factor of sex designations ('M' and 'F').
capture= ~ 1 : constant effect over all individuals and time (intercept only model)
capture= ~ age : Intercept plus age
capture= ~ age + tvar(effort,NAN) : Intercept plus age plus effort
capture= ~ age + tvar(effort,NAN) + ivar(sex,NS) : Intercept plus age plus effort plus sex. Females (1st level) are the reference.
capture= ~ -1 + ivar(sex,NS,0) : sex as a factor, cell means coding
capture= ~ tvar(as.factor(1:ncol(histories)),nrow(histories),c(1,2)) : time varying effects
Values in 2-d Matrix Covariates: Even though covariate matrices are required to be NAN x NS (same size as capture histories), there are not that many parameters. The first capture probability cannot be estimated in CJS models, and the NS-th survival parameter does not exist. When a covariate matrix appears in the capture model, only values in columns 2:ncol(histories) are used. When a covariate matrix appears in the survival model, only values in columns 1:(ncol(histories)-1) are used. See examples for demonstration.
An object (list) of class c("cjs","cr") with many components. Use print.cr
to print
it nicely. Use names(fit)
, where the call was fit <- F.cr.estim(...)
,
to see names of all returned components. To see values of individual components,
issue commands like fit$s.hat, fit$se.s.hat, fit$n.hat, etc.
Components of the returned object are as follows:
histories |
The input capture history matrix. |
aux |
Auxiliary information about the fit, mostly stored input values. This is a list containing the following components:
|
loglik |
Maximized CJS likelihood value for the model |
deviance |
Model deviance = -2* |
aic |
AIC for the model = |
qaic |
QAIC (quasi-AIC) = ( |
aicc |
AIC with small sample correction = AIC + (2* |
qaicc |
QAIC with small sample correction = QAIC + (2* |
vif |
Variance inflation factor used = estimate of c.hat = |
chisq.vif |
Composite Chi-square statistic from Test 2 and Test 3 used to compute |
vif.df |
Degrees of freedom for composite chi-square statistic from Test 2 and Test 3, based on pooling rules. |
parameters |
Vector of all coefficient estimates, NX capture probability coefficients first, then NY survival coefficients. This vector is length NX+NY regardless of estimated DF. |
se.param |
Standard error estimates for all coefficients. Length NX+NY. |
capcoef |
Vector of coefficients in the capture model. Length NX. |
se.capcoef |
Vector of standard errors for coefficients in capture model. Length NX. |
surcoef |
Vector of coefficients in the survival model. Length NY. |
se.surcoef |
Vector of standard errors for coefficients in survival model. Length NY. |
covariance |
Variance-covariance matrix for the estimated model coefficients. Size (NX+NY) X (NX+NY). |
p.hat |
Matrix of estimated capture probabilities computed from the model. One for each animal each occasion. Cell (i,j) is estimated capture probability for animal i during capture occasion j. Size NAN X NS. First column corresponding to first capture probability is NA because cannot estimate P1 in a CJS model. |
se.p.hat |
Matrix of standard errors for estimated capture probabilities. One for each animal each occasion. Size NAN X NS. First column is NA. |
s.hat |
Matrix of estimated survival probabilities computed from the model. One for each animal each occasion. Size NAN X NS. Cell (i,j) is estimated probability animal i survives from occasion j to j+1. There are only NS-1 intervals between occasions. Last column corresponding to survival between occasion NS and NS+1 is NA. |
se.s.hat |
Matrix of standard errors for estimated survival probabilities. Size NAN X NS. Last column is NA. |
df |
The number of parameters assumed in the model. This value was used in the penalty term of AIC, AICc, QAIC, and QAICc.
This value is either the number of independent
parameters estimated from the rank of the variance-covariance matrix (by default),
or NX+NY, or a specified value, depending on the input value of DF parameter. See |
df.estimated |
The number of parameters estimated from the rank of the variance-covariance matrix. This
is stored so that |
control |
A list containing the input maximization and estimation control parameters. |
message |
A vector of strings interpreting various codes about the estimation.
The messages interpret, in this order, the codes for (1) maximization algorithm used,
(2) exit code from the maximization algorithm (interprets |
exit.code |
Exit code from the maximization routine. Interpretation for
|
cov.code |
A code indicating the method used to compute the covariance matrix. |
fn.evals |
The number of times the likelihood was evaluated prior to exit from the minimization routine.
If |
ex.time |
Execution time for the maximization routine, in minutes. This is returned for
2 reasons. First, this is useful for benchmarking. Second, in conjunction with
|
n.hat |
Vector of Horvitz-Thompson estimates of population size. The Horvitz-Thompson estimator of size is,
Length of |
se.n.hat |
Estimated standard errors for |
n.hat.lower |
Lower limit of |
n.hat.upper |
Upper limit of |
n.hat.conf |
Confidence level of intervals on |
nhat.v.meth |
Code for method used to compute variance of |
num.caught |
Vector of observed number of animals captured each occasion. Length NS. |
fitted |
Matrix of fitted values for the capture histories. Size NAN X NS.
Cell (i,j)
is expected value of capture indicator in cell (i,j) of |
residuals |
Matrix of Pearson residuals defined as,
,
where |
resid.type |
String describing the type of residuals computed. Currently, only Pearson residuals are returned. |
MARK Users: Due to differences in the way MRA and MARK parameterize the sine link, coefficient estimates will differ between the two packages when this link is used to fit the same model in both packages. The fit (measured by deviance, AIC, etc.) will agree between the two packages. Capture and survival probability estimates will also agree between the two packages.
MARK does not contain a hazard rate link function.
Trent McDonald, WEST-INC, [email protected]
Taylor, M. K., J. Laake, H. D. Cluff, M. Ramsay, and F. Messier. 2002. Managing the risk from hunting for the Viscount Melville Sound polar bear population. Ursus 13:185-202.
Manly, B. F. J., L. L. McDonald, and T. L. McDonald. 1999. The robustness of mark-recapture methods: a case study for the northern spotted owl. Journal of Agricultural, Biological, and Environmental Statistics 4:78-101.
Huggins, R. M. 1989. On the statistical analysis of capture experiments. Biometrika 76:133-140.
Amstrup, S. C., T. L. McDonald, and B. F. J. Manly (editors). 2005. Handbook of Capture-Recapture Analysis. Princeton University Press.
Peterson. 1986. Statistics and Probability Letters. p.227.
McDonald, T. L., and S. C. Amstrup. 2001. Estimation of population size using open capture-recapture models. Journal of Agricultural, Biological, and Environmental Statistics 6:206-220.
tvar
, ivar
, print.cjs
, residuals.cjs
, plot.cjs
,
F.cjs.covars
, F.cjs.gof
, mra.control
, F.update.df
## Fit CJS model to dipper data, time-varying capture and survivals. ## Method 1 : using factors data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Method 2 : same thing using 2-d matrices xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) # The following extracts 2-D matrices of 0s and 1s for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x3+x4+x5+x6+x7, ~x2+x3+x4+x5, dipper.histories ) ## Values in the 1st column of capture covariates do not matter x3.a <- x3 x3.a[,1] <- 999 dipper.cjs2 <- F.cjs.estim( ~x3.a+x4+x5+x6+x7, ~x2+x3+x4+x5, dipper.histories ) # compare dipper.cjs2 to dipper.cjs ## Values in the last column of survival covariates do not matter x3.a <- x3 x3.a[,ncol(dipper.histories)] <- 999 dipper.cjs2 <- F.cjs.estim( ~x3+x4+x5+x6+x7, ~x2+x3.a+x4+x5, dipper.histories ) # compare dipper.cjs2 to dipper.cjs ## A plot to compare the link functions sine.link <- function(eta){ ifelse( eta < -4, 0, ifelse( eta > 4, 1, .5*(1+sin(eta*pi/8)))) } eta <- seq(-5,5, length=40) p1 <- 1 / (1 + exp(-eta)) p2 <- sine.link(eta) p3 <- 1.0 - exp( -exp( eta )) plot(eta, p1, type="l" ) lines(eta, p2, col="red" ) lines(eta, p3, col="blue" ) legend( "topleft", legend=c("logit", "sine", "hazard"), col=c("black", "red", "blue"), lty=1)
## Fit CJS model to dipper data, time-varying capture and survivals. ## Method 1 : using factors data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Method 2 : same thing using 2-d matrices xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) # The following extracts 2-D matrices of 0s and 1s for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x3+x4+x5+x6+x7, ~x2+x3+x4+x5, dipper.histories ) ## Values in the 1st column of capture covariates do not matter x3.a <- x3 x3.a[,1] <- 999 dipper.cjs2 <- F.cjs.estim( ~x3.a+x4+x5+x6+x7, ~x2+x3+x4+x5, dipper.histories ) # compare dipper.cjs2 to dipper.cjs ## Values in the last column of survival covariates do not matter x3.a <- x3 x3.a[,ncol(dipper.histories)] <- 999 dipper.cjs2 <- F.cjs.estim( ~x3+x4+x5+x6+x7, ~x2+x3.a+x4+x5, dipper.histories ) # compare dipper.cjs2 to dipper.cjs ## A plot to compare the link functions sine.link <- function(eta){ ifelse( eta < -4, 0, ifelse( eta > 4, 1, .5*(1+sin(eta*pi/8)))) } eta <- seq(-5,5, length=40) p1 <- 1 / (1 + exp(-eta)) p2 <- sine.link(eta) p3 <- 1.0 - exp( -exp( eta )) plot(eta, p1, type="l" ) lines(eta, p2, col="red" ) lines(eta, p3, col="blue" ) legend( "topleft", legend=c("logit", "sine", "hazard"), col=c("black", "red", "blue"), lty=1)
Goodness of fit measures for a CJS open-population capture recapture model.
F.cjs.gof( cjsobj, resid.type="pearson", rule.of.thumb = 2, HL.breaks = "deciles" )
F.cjs.gof( cjsobj, resid.type="pearson", rule.of.thumb = 2, HL.breaks = "deciles" )
cjsobj |
A CJS capture-recapture fitted object from a previous call to |
resid.type |
Type of residual to return. |
rule.of.thumb |
Rule of thumb to include a cell in one of the chi-square statistics. For example,
if |
HL.breaks |
vector of bin break points to use in the Hosmer-Lemeshow statistic. This must
be a partition of the interval [0,1], with 0 as lowest break and 1 as max.
E.g., if HL.breaks = c(.25,.75), the bins used are [0,.25),[.25,.75),[.75,1].
The default, "deciles", calculates breakpoints such that 10
values are in each. I.e., approximately |
The "overall" Chi-square test computes the sum of [(h(ij) - Psi(ij))*(h(ij) - Psi(ij))] / Psi(ij) over all "live" cells in the capture-recapture problem. "Live" cells are those following initial captures, prior to and including the occasion when an animal was censoring (died on capture and removed). If an animal was not censored, the "live" cells for it extend from occasion following initial capture to the end of the study. In the above, h(ij) is the 0-1 capture indicator for animal i at occasion j. Psi(ij) is the expected value of h(ij), and is computed as the produce of survival estimates from initial capture to occasion j, times probability of capture at occasion j. Assuming animal i was initially captured at the a-th occasion, Psi(ij) is computed as phi(ia) * phi(i(a+1)) * ... * phi(i(j-1)) * p(ij), where phi(ij) is the modeled estimate of survival for animal i from occasion j to occasion j+1, and p(ij) is the probability of capturing animal i during occasion j.
The other derived GOF tests computed here use h(ij) and its expected value Psi(ij). Test 4 sums observed and expected over individuals. Test 5 sums observed and expected over occasions. The other 3 tests were borrowed from logistic regression by viewing h(ij) as a binary response, and Psi(ij) as its expected value.
A CJS object equivalent to the input crobj, with additional components for GOF testing. Additional components are a variety of goodness of fit statistics. Goodness of tests included are: (1) "Overall" = Chi-square test of overall goodness of fit based on all "live" cells in the capture histories, (2) "Osius and Rojek" = Osius and Rojeck correction to the overall chi-square test, (3) "Test 4" = Chi-square of observed and expected captures by occasion, (4) "Test 5" = Chi-square of observed and expected captures by individual, summed over animals, (5) "Hosmer-Lemeshow" = Hosmer-Lemeshow Chi-square GOF over all occasions and animals, and (6) "ROC" = area under the curve overall classification accuracy of expected values for capture histories. Tests (2), (5), and (6) are based on methods in chapter 5 of Hosmer and Lemeshow (2000).
Specifically, the output object has class c("cjsgof", "cjs", "cr"), contains all the components of the original CJS object, plus the following components:
gof.chi |
Chi-square statistic for overall goodness of fit based on all "live" cells in the capture-recapture histories. |
gof.df |
Degrees of freedom for overall goodness of fit test. |
gof.pvalue |
P-value for overall goodness of fit. |
or.table |
Chi-square table for the Osius and Rojek correction to the overall GOF test (See p. 153 of Hosmer and Lemeshow (2000)). |
or.chi |
Chi-square statistic for the Osius and Rojek test. |
or.df |
Degrees of freedom for the Osius and Rojek test. |
or.correction |
Correction to the Osius and Rojek test. This is computed as number of unique expected values minus the sum of 1 over the individual cell counts. |
or.rss |
Root sum-of-squares for the Osius and Rojek test, obtained from weighted regression. |
or.z |
Osius and Rojek Z statistic. This is computed as (or.chi - or.df) / sqrt( or.correction + or.rss ) |
or.pvalue |
2-tailed Osius and Rojek p-value computed from standard normal distribution and the Osius and Rojek Z statistic. |
t4.table |
Chi-square table for Test 4, which sums observed and expected captures over individuals. This table has one cell for each occasion. |
t4.chi |
Chi-square statistic for Test 4, computed from |
t4.df |
Degrees of freedom for Test 4. Equal to number of cells meeting |
t4.pvalue |
P-value for Test 4 computed from Chi-squared distribution. |
t5.table |
Chi-square table for Test 5, which sums observed and expected captures over occasions. This table has one cell for each individual. |
t5.chi |
Chi-square statistic for Test 5, compute from |
t5.df |
Degrees of freedom for Test 5. Equal to number of cells meeting |
t5.pvalue |
P-value for Test 5 computed from Chi-squared distribution. |
HL.table |
Chi-square table for the Hosmer-Lemeshow test. |
HL.chi |
Chi-square statistic for the Hosmer-Lemeshow test. |
HL.df |
Degrees of freedom for the Hosmer-Lemeshow test. |
HL.pvalue |
P-value for the Hosmer-Lemeshow test. |
roc |
Area under the curve statistic for the ability of the "live" cell expected values to classify captures. |
Future plans include adding the following: (1) Osius-Rojek = Overall z statistic for GOF over all occasions and animals; and (2) Stukel = Overall z test for appropriateness of the logistic link.
Future plans also include a plot method whereby all tests, especially the ROC, could be assessed graphically.
Print the GOF results in a nice format using print.cjs
.
Trent McDonald, WEST Inc., [email protected]
Hosmer, D. W. and S. Lemeshow. 2000. Applied Logistic Regression, 2nd edition. New York: John Wiley and Sons.
data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) dipper.cjs.gof <- F.cjs.gof( dipper.cjs ) print(dipper.cjs.gof)
data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) dipper.cjs.gof <- F.cjs.gof( dipper.cjs ) print(dipper.cjs.gof)
This function generates capture history matrices that follow open Cormack-Jolly-Seber (CJS) models. A super-population approach is taken wherein individuals with unique capture and survival probabilities are randomly 'born' into the realized population and captured. Any CJS model, including those with heterogeneous survival or capture probabilities, can be simulated. Closed populations can also be simulated.
F.cjs.simulate(super.p, super.s, fit, N1 = 1000, births.per.indiv = "constant.popln", R = 100)
F.cjs.simulate(super.p, super.s, fit, N1 = 1000, births.per.indiv = "constant.popln", R = 100)
super.p |
A matrix or vector of true capture probabilities in the super-population of individuals.
|
super.s |
A matrix or vector of true survival probabilities in the super-population of individuals.
Number of survival probabilities in |
fit |
A previously estimated CJS object. Instead of specifying |
N1 |
A scalar specifying the initial population size. I.e., |
births.per.indiv |
Either a vector of births per individual in the realized population, or
the string "constant.popln" (the default). If
|
R |
A scalar specifying the number of replications for the simulation. A total of |
Some examples: A two-group heterogeneous population contains one group of individuals with one common set of capture probabilities, and another group of individuals with another set of common capture probabilities. A population with one group of individuals having capture probability equal to 0.25, and another group with capture probability equal to 0.75 can be simulated using
F.cjs.simulate( rbind( rep(0.25,10),rep(0.75,10) ), rep(s,9) ).
,
where s
is some survival probability between 0 and 1. If s
= 1, a
closed (no births or deaths) two-group heterogeneous model is simulated. In this example, the
realized population is sampled for 10 occasions.
Non-equal sized heterogeneous groups can be simulated using
F.cjs.simulate( rbind( matrix(0.25,1,10),matrix(0.75,9,10) ), rep(1,9) ).
Using this call, approximately 10% of individuals in the realized population will have capture probabilities
equal to 0.25, while 90% will have capture probabilities equal to 0.75. Additional
groups can be included by including more rows with distinct probabilities in super.p
.
A population with heterogeneous capture probabilities proportional to a vector w
can be simulated using
F.cjs.simulate( matrix( w/sum(x), length(w), 10), rep(s,9) )
.
A stochastic population that varies around a specified size of N1
= 1000
can be simulated with a statement like
F.cjs.simulate( rep(0.25,10), rep(s,9), N1=1000, births.per.indiv=rep((1-s)/s,9) ).
In this simulation, N(j)*(1-s) individuals die between each occasion, but are replaced because the N(j)*s surviving individuals each have (1-s)/s offspring.
Because of the super-population approach taken here, it is not possible to specify which individuals have which survival or capture probabilities, nor to guarantee that a certain number of individuals in the realized population have capture probabilities equal to any particular value.
A list of length R
. Each component of this list is a list of length 2.
Each of these R
sublists contains the following components:
hists |
The simulated capture histories for a particular iteration. This is a matrix with a random number of rows (due to the stochastic nature of captures) and NS columns. |
popln.n |
A vector of length NS containing the true population sizes at each sampling occasion. |
Trent McDonald, WEST Inc. ([email protected])
## Not run: ## Don't run specified because these examples can take > 10 seconds. ## Simulate constant model, and analyze ns <- 10 N <- 100 sim.list <- F.cjs.simulate( rep(0.3,ns), rep(0.9,ns-1), N1=N, R=100 ) f.analyze <- function(x){ fit <- F.cjs.estim( ~1, ~1, x$hists, control=mra.control(maxfn=200, cov.meth=2) ) if( fit$exit.code == 1 ){ return( fit$n.hat ) } else { return( rep(NA,ncol(x$hists)) ) } } results <- t(sapply(sim.list, f.analyze)) plot( 1:10, colMeans(results, na.rm=TRUE), xlab="Occasion", ylab="Mean population estimate", col="red", type="b") abline( h=N ) ## Plot RMSE by occasion std <- apply(results, 2, sd, na.rm=TRUE) bias <- apply(results - N, 2, mean, na.rm=TRUE) plot( std, bias, type="n" ) text( std, bias, 2:10 ) abline(h=0) title(main="RMSE by Sample Occasion") ## Show bias when heterogeniety is present sim.list <- F.cjs.simulate( matrix(c(0.3,.7,.7,.7),4,ns), rep(0.9,ns-1), N1=N, R=100 ) results <- t(sapply(sim.list, f.analyze)) mean.N <- colMeans(results, na.rm=TRUE) plot( 1:length(mean.N), mean.N, ylim=range(c(mean.N,N),na.rm=TRUE), xlab="Occasion", ylab="Mean population estimate", col="red", type="b") abline( h=N ) abline( h=mean(mean.N), col="red", lty=2) title(main="Heterogeniety causes negative bias") ## Simulate CJS model, first estimate one data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Now generate histories from it. sim.list <- F.cjs.simulate( fit=dipper.cjs, N1=100, birth.rate=rep(1,6), R=100 ) ## Now analyze generated histories using lapply or sapply. Can fit any model. ## Here we fit the correct model. f.analyze <- function(x){ # write a counter to console, this is not necessary i <- get("i", env=.GlobalEnv) + 1 cat(paste("Iteration", i, "\n")) assign("i",i,env=.GlobalEnv) ct <- as.factor( 1:ncol(x$hists) ) fit <- F.cjs.estim( ~tvar(ct,nan=nrow(x$hists),drop=c(1,2)), ~tvar(ct,nan=nrow(x$hists),drop=c(1,6,7)), x$hists, control=mra.control(maxfn=200, cov.meth=2) ) if( fit$exit.code == 1 ){ return( fit$n.hat ) } else { return( rep(NA,ncol(x$hists)) ) } } i <- 0 results <- t(sapply(sim.list, f.analyze)) mean.N <- colMeans(results, na.rm=TRUE) plot( 1:length(mean.N), mean.N, ylim=range(c(mean.N,N),na.rm=TRUE), xlab="Occasion", ylab="Mean population estimate", col="red", type="b") abline( h=N ) title(main="Time varying CJS model") ## End(Not run)
## Not run: ## Don't run specified because these examples can take > 10 seconds. ## Simulate constant model, and analyze ns <- 10 N <- 100 sim.list <- F.cjs.simulate( rep(0.3,ns), rep(0.9,ns-1), N1=N, R=100 ) f.analyze <- function(x){ fit <- F.cjs.estim( ~1, ~1, x$hists, control=mra.control(maxfn=200, cov.meth=2) ) if( fit$exit.code == 1 ){ return( fit$n.hat ) } else { return( rep(NA,ncol(x$hists)) ) } } results <- t(sapply(sim.list, f.analyze)) plot( 1:10, colMeans(results, na.rm=TRUE), xlab="Occasion", ylab="Mean population estimate", col="red", type="b") abline( h=N ) ## Plot RMSE by occasion std <- apply(results, 2, sd, na.rm=TRUE) bias <- apply(results - N, 2, mean, na.rm=TRUE) plot( std, bias, type="n" ) text( std, bias, 2:10 ) abline(h=0) title(main="RMSE by Sample Occasion") ## Show bias when heterogeniety is present sim.list <- F.cjs.simulate( matrix(c(0.3,.7,.7,.7),4,ns), rep(0.9,ns-1), N1=N, R=100 ) results <- t(sapply(sim.list, f.analyze)) mean.N <- colMeans(results, na.rm=TRUE) plot( 1:length(mean.N), mean.N, ylim=range(c(mean.N,N),na.rm=TRUE), xlab="Occasion", ylab="Mean population estimate", col="red", type="b") abline( h=N ) abline( h=mean(mean.N), col="red", lty=2) title(main="Heterogeniety causes negative bias") ## Simulate CJS model, first estimate one data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Now generate histories from it. sim.list <- F.cjs.simulate( fit=dipper.cjs, N1=100, birth.rate=rep(1,6), R=100 ) ## Now analyze generated histories using lapply or sapply. Can fit any model. ## Here we fit the correct model. f.analyze <- function(x){ # write a counter to console, this is not necessary i <- get("i", env=.GlobalEnv) + 1 cat(paste("Iteration", i, "\n")) assign("i",i,env=.GlobalEnv) ct <- as.factor( 1:ncol(x$hists) ) fit <- F.cjs.estim( ~tvar(ct,nan=nrow(x$hists),drop=c(1,2)), ~tvar(ct,nan=nrow(x$hists),drop=c(1,6,7)), x$hists, control=mra.control(maxfn=200, cov.meth=2) ) if( fit$exit.code == 1 ){ return( fit$n.hat ) } else { return( rep(NA,ncol(x$hists)) ) } } i <- 0 results <- t(sapply(sim.list, f.analyze)) mean.N <- colMeans(results, na.rm=TRUE) plot( 1:length(mean.N), mean.N, ylim=range(c(mean.N,N),na.rm=TRUE), xlab="Occasion", ylab="Mean population estimate", col="red", type="b") abline( h=N ) title(main="Time varying CJS model") ## End(Not run)
Computes model averaged estimates of survival, capture probability, or population size estimates from a set of previously fitted MRA objects.
F.cr.model.avg( fits=ls(pattern="^fit"), what="survival", fit.stat="qaicc" )
F.cr.model.avg( fits=ls(pattern="^fit"), what="survival", fit.stat="qaicc" )
fits |
A character vector of MRA fitted object names. Each will be retrieved from the global environment
(i.e., .GlobalEnv) using |
what |
A text string naming the parameter to average. Choices are "survival" (the default), "capture", and "n.hat". Only the first character is inspected (e.g., "c" is equivalent to "capture"). |
fit.stat |
A string (scalar) naming the model fit statistic to use when computing model weights. Possible values are: "qaicc" (the default), "qaic", "aicc", and "aic". |
Each model is checked for convergence prior to including in the model averaging process.
The test for whether a model converged is
(fit$exit.code == 1) & (fit$cov.code == 0) & (fit$df > 0)
, where fit
is
the fitted object. If the model did not converge,
it is excluded from model averaging.
Conditional and unconditional variance estimates are computed following Burnham and Anderson 2002 (pages 150 and 162 and surrounding).
If what
= "n.hat", the returned object is suitable for printing using print.nhat
and plotting using plot.cjs
.
If what
= "survival" or "capture", the returned object is unclassed and the user is responsible for printing and plotting.
If what
= "survival" or "capture", the return is a list object containing the following components:
fit.table |
A data frame, sorted by |
s.hat or p.hat |
A matrix of size |
se.s.hat or se.p.hat |
A matrix of size |
se.s.hat.conditional or se.p.hat.conditional |
A matrix of size |
mod.selection.proportion |
A matrix of size |
If what
= "n.hat", the return is a list of class "n.hat" containing the following components:
fit.table |
A data frame, sorted by |
n.hat |
A vector of length |
se.n.hat |
A vector of length |
se.n.hat.conditional |
A vector of length |
mod.selection.proportion |
A vector of length |
n.hat.lower |
A vector of length |
n.hat.upper |
A vector of length |
nhat.v.meth |
Scalar indicating the type of variance estimate used. Values are: |
Original routine by Eric Regehr, US Fish and Wildlife. Modified for MRA by Trent McDonald, WEST-INC, [email protected]
Burnham, K. and D. Anderson (2002) "Model Selection: A practical guide". Cambridge University Press.
F.cjs.estim
, F.huggins.estim
, F.fit.table
,
plot.cjs
## Fit several CJS model to dipper data. Model average survival ## Time varying survival and capture (true CJS model) data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.01 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Linear trend in survival cT <- 1:ncol(dipper.histories) dipper.02 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(cT, nan=nrow(dipper.histories)), dipper.histories ) ## No trend in survival dipper.03 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~1, dipper.histories ) ## Model average mod.avg.surv <- F.cr.model.avg( ls(pat="^dipper.[0-9]"), what="s", fit.stat="aicc" ) mod.avg.n <- F.cr.model.avg( ls(pat="^dipper.[0-9]"), what="n", fit.stat="aicc" ) ## Plot plot(mod.avg.n)
## Fit several CJS model to dipper data. Model average survival ## Time varying survival and capture (true CJS model) data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.01 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Linear trend in survival cT <- 1:ncol(dipper.histories) dipper.02 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(cT, nan=nrow(dipper.histories)), dipper.histories ) ## No trend in survival dipper.03 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~1, dipper.histories ) ## Model average mod.avg.surv <- F.cr.model.avg( ls(pat="^dipper.[0-9]"), what="s", fit.stat="aicc" ) mod.avg.n <- F.cr.model.avg( ls(pat="^dipper.[0-9]"), what="n", fit.stat="aicc" ) ## Plot plot(mod.avg.n)
Returns two model matrices for capture-recapture modeling. Both are in the form of (giant) 2D matrices.
F.cr.model.matrix(capture, survival, nan, ns)
F.cr.model.matrix(capture, survival, nan, ns)
capture |
Formula for the capture model. Must be a formula object with no response, then ~, followed by the names of 2-D arrays of covariates to fit in the capture model. For example: capture = ~ age + sex, where age and sex are matrices. |
survival |
Formula for the survival model. Must be a formula object with no response, then ~, followed by the names of 2-D arrays of covariates to fit in the survival model. For example: capture = ~ age + sex, where age and sex are matrices. |
nan |
Number of individuals in the model. This is necessary for the
|
ns |
Number of sampling occasions. Normally, |
This routine is intended to be called internally by model fitting routines of MRA. General users should never have to call this routine.
This routine uses a call to eval
with a model frame, and calls the
R internal model.matrix
to
resolve the matrices in the formula. All matrices specified in the models
should be in the current scope and accessible to both eval
and model.matrix
.
This routine calls F.3d.model.matrix
twice. F.3d.model.matrix
does all the work.
A list containing the following components:
capX |
A NAN by IX+(NX*NS) matrix containing covariate values for the capture
model. Matrices specified in the model are column appended together.
NAN = |
surX |
A NAN by IY+(NY*NS) matrix containing covariate values for the survival
model. Matrices specified in the model are column appended together.
NAN = |
n.cap.covars |
Number of matrices specified in the capture model (NX above). |
n.sur.covars |
Number of matrices specified in the survival model (NY above). |
cap.intercept |
TRUE or FALSE depending on whether an intercept was included in the capture model |
sur.intercept |
TRUE or FALSE depending on whether an intercept was included in the survival model |
cap.vars |
Vector of names for the NX covariates in the capture model. |
sur.vars |
Vector of names for the NY covariates in the survival model. |
Trent McDonald, WEST-INC, [email protected]
F.cjs.estim
, model.matrix
, eval
# Synthetic example with 10 animals and 5 occasions nan <- 10 ns <- 5 sex <- matrix( as.numeric(runif( nan ) > 0.5), nrow=nan, ncol=ns ) x <- matrix( runif( nan*ns ) , nrow=nan, ncol=ns ) F.cr.model.matrix( capture= ~ sex + x, survival= ~ -1 + x, nan, ns )
# Synthetic example with 10 animals and 5 occasions nan <- 10 ns <- 5 sex <- matrix( as.numeric(runif( nan ) > 0.5), nrow=nan, ncol=ns ) x <- matrix( runif( nan*ns ) , nrow=nan, ncol=ns ) F.cr.model.matrix( capture= ~ sex + x, survival= ~ -1 + x, nan, ns )
A utility function to compile a table of fit statistics from a list of MRA fitted objects contained in the .GlobalEnv (i.e., 'working') environment. The table produced by this routine contains model name, fit statistics (AICc or QAICc), and is ranked by (sorted by) one of these fit statistics.
F.fit.table( fits=ls(pattern="^fit"), rank.by= "qaicc", plausible.p=0.01 )
F.fit.table( fits=ls(pattern="^fit"), rank.by= "qaicc", plausible.p=0.01 )
fits |
A character vector of MRA fitted object names to include in the
summary table. These names do not need to have a common root name. The default value
will use any object whose name starts with "fit" in the working directory (.GlobalEnv).
An an example, if fitted objects are named
"fit1.01", "fit1.02", and "fit1.03", |
rank.by |
A string (scalar) naming the model fit statistic to include in the summary table. The resulting table is sorted by this statistic. Possible values are: "qaicc" (the default), and "aicc". |
plausible.p |
A scalar specifying the cut-point in |
A rudimentary check for convergence is done on each fitted model. If this routine believes
a model did not converge, the model is included in the table, but the model's fit
statistics are set to Inf
. The test for whether a model converged is
(fit$exit.code == 1) & (fit$cov.code == 0) & (fit$df > 0)
, where fit
is
the fitted object.
Fitted objects are pulled from the .GlobalEnv
environment. Usually, this is the
current working directory. Currently, there is no way to pull fits from another environment, but
a savvy R programmer could modify the where
argument of the get
command embedded in
this routine.
A data frame, sorted by rank.by
, with the following columns
model.num |
Model number assigned by this routine, equal to the position of the model in the input list of fits. |
model.name |
Name of the fitted object. |
converged |
Logical values indicating whether this routine thinks the model converged or not. Value is TRUE if the this routine thinks the model converged, FALSE otherwise. |
n.est.parameters |
Number of estimable parameters in the model. This is MRA's guess at the number of estimable parameters in the model, not length of the coefficient vector. |
n.coefficients |
Number of coefficients in the model. This is length of the coefficient
vector without regard to number of estimable parameters. If |
loglike |
value of the log likelihood evaluated at the maximum likelihood parameters. |
aicc |
AIC of the model including the small sample correction =
AIC + (2* |
delta.aicc |
Difference between AICc for the model and the minimum AICc in the table. |
aicc.wgt |
AICc model weights. These weights equal exp(-.5*(delta.aicc)), scaled to sum to 1.0, |
qaicc |
QAIC of the model including the small sample correction =
QAIC + (2* |
delta.qaicc |
Difference between QAICc for the model and the minimum QAICc in the table. |
qaicc.wgt |
QAICc model weights. These weights equal exp(-.5*(delta.qaicc)), scaled to sum to 1.0, |
plausible |
Indicates ‘plausible’ models as defined by Bromaghin et al. (2013). The value
in this column is TRUE if the model has |
Trent McDonald, WEST-INC, [email protected]
Bromaghin, J.F., McDonald, T. L., and Amstrup, S. C., (2013) "Plausible Combinations: An improved methods to evaluate the covariate structure of Cormack-Jolly-Seber mark-recapture models", Open Journal of Ecology, v.3, p. 11-22. (included in vignettes)
F.cjs.estim
, F.huggins.estim
, vignette("Bromaghin_etal_2013_OJE")
## Fit several CJS model to dipper data. Summarize fits. ## Time varying survival and capture (true CJS model) data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipr.01 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Linear trend in survival cT <- 1:ncol(dipper.histories) dipr.02 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(cT, nan=nrow(dipper.histories)), dipper.histories ) ## No trend in survival dipr.03 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~1, dipper.histories ) ## Summary table F.fit.table( ls(pat="^dipr") )
## Fit several CJS model to dipper data. Summarize fits. ## Time varying survival and capture (true CJS model) data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipr.01 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Linear trend in survival cT <- 1:ncol(dipper.histories) dipr.02 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(cT, nan=nrow(dipper.histories)), dipper.histories ) ## No trend in survival dipr.03 <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~1, dipper.histories ) ## Summary table F.fit.table( ls(pat="^dipr") )
Estimates Huggin's closed population capture-recapture models with individual, time, and individual-time varying covariates using the "regression" parameterization of Amstrup et al (2006, Ch 9). For live recaptures only. A logistic link function is used to relate probability of capture to external covariates.
F.huggins.estim(capture, recapture=NULL, histories, remove=FALSE, cap.init, recap.init, nhat.v.meth=1, df=NA, link="logit", control=mra.control())
F.huggins.estim(capture, recapture=NULL, histories, remove=FALSE, cap.init, recap.init, nhat.v.meth=1, df=NA, link="logit", control=mra.control())
capture |
Formula specifying covariates to included in the initial
capture probability model. Must be a formula object without
a response. Specify ~, followed by the names of 2-D arrays of covariates to relate to
initial capture probability.
For example: 'capture = ~ age + sex', where age and sex are matrices of size NAN X NS
containing the age and sex covariate values. NAN = number of individuals = number of rows in
|
recapture |
Formula specifying covariates to included in the
recapture probability model, or NULL. Should be specified the same way as the
|
histories |
A NAN X NS = (number of individuals) X (number of capture occasions) matrix containing capture histories. Capture histories are comprised of 0's and 1's only. 0 in cell (i,j) of this matrix means individual i was not captured on occasion j, 1 in cell (i,j) means individual i was captured on occasion j and released live back into the population. Because the population being sampled is assumed closed, deaths on capture (known removals) are not allowed. If deaths on capture occurred and an estimate of N at the beginning of the study is sought, remove the entire history, estimate N using this routine from the remaining histories, and add back the number of deleted histories. |
remove |
A logical scalar, or vector of logical values, specifying which
|
cap.init |
(optional) Vector of initial values for coefficients in the initial
capture model. One element
per covariate in |
recap.init |
(optional) Vector of initial values for coefficients in the
recapture model. One element
per covariate in |
nhat.v.meth |
Integer specifying method for computing variance estimate
for the population size estimate. Currently, only |
df |
External (override) model degrees of freedom to use during estimation.
If |
link |
The link function to be used. The link function converts linear predictors in the
range (-infinity, infinity) to probabilities in the range (0,1). Valid values for the link
function are "logit" (default), "sine", and "hazard". (see Examples in help for
|
control |
A list containing named control parameters for the minimization and estimation process.
Control parameters include number of iterations, covariance estimation method, etc.
Although the default values work in the vast majority of cases, changes to these
variables can effect speed and performance for ill-behaved models. See
|
This routine compiles all the covariate matrices, then calls a Fortran routine to maximize the Huggins closed population likelihood. So-called heterogeneous models that utilize mixture distributions for probability of capture cannot be fitted via this routine.
If remove=FALSE
(default) the models for initial capture and
subsequent recapture are,
and
where the x's and z's are covariate values specified in capture
and recapture
, respectively, and the 's and
's are estimated coefficients. (For brevity,
'a' has been substituted for NX, 'b' for NY.) In other words, by default
all effects in the capture model also appear in the recapture model
with the same estimated coefficients. This is done so that
capture and recapture probabilities can be constrained to equal one another.
If
capture=~1
and recapture=NULL
, capture and recapture
probabilities are constant and equal to one another.
If capture=~x1
and recapture=NULL
, capture and recapture
probabilities are equal, and both are the exact same function of
covariate x1
. A simple additive
behavioral (trap happy or trap shy) effect is fitted by specifying an
intercept-only model for recaptures, i.e.,
capture=~x1+x2+...+xp
and recapture=~1
.
When a Huggin's model object is printed using the default
print method (print.hug
), a "C" (for "capture") appears next to coefficients
in the recapture model that are also in the initial capture model. These
coefficients are fixed in the recapture model. A "B" (for "behavioral")
appears next to free coefficients in the recapture model that do not
appear in the initial capture model.
If remove
is something other than FALSE, it is extended to have length
NX, and if element i equals TRUE, the i-th
effect in the capture model is removed from the recapture model. If
remove=c(FALSE, TRUE, FALSE)
, capture=~x1+x2
, and
recapture=~x1+x3
, the models for initial capture and subsequent
recapture are,
and
Note that x1
appears in the recapture equation, but with a
different estimated coefficient. If remove=TRUE
, all capture effects
are removed from the recapture model and the models are completely separate.
The ability to remove terms from the recapture model adds modeling flexibility.
For example, if initial captures were hypothesized to depend on the variable
sex
, but recaptures were hypothesized to be constant (no sex
effect),
the arguments to fit this model would be capture=~sex
, recapture=~1
,
and remove=TRUE
. A pure time-varying model with different time
effects in the initial and subsequent capture models can be fitted using
capture=~tvar(1:ns,nan)
, recapture=~tvar(1:ns,nan)
,
and remove=TRUE
. In this case, the same model, but parameterized differently,
can be fitted with remove=FALSE
.
See Details of help(F.cjs.estim)
for ways that 2-d matrices, 1-d
vectors, and 1-d factors can be specified in the capture and recapture
models.
If argument trace
in a call to mra.control
is set to something
other than 0, a log file named mra.log
is written to the current directory.
See mra.control
for actions associated with values of trace
.
CAREFUL: mra.log
is overwritten each run.
Values in 2-d Matrix Covariates: Even though covariate matrices are required to be NAN x NS (same size as capture histories), there are not that many recapture parameters. Recapture parameters for the first occasion are not defined. For all covariates in the recapture model, only values in columns 2:ncol(histories) are used. See examples for demonstration.
An object (list) of class c("hug","cr") with many components.
Use print.hug
to print
it nicely. Use names(fit)
, where the call was fit <- F.huggins.estim(...)
,
to see names of all returned components. To see values of individual components,
issue commands like fit\$n.hat, fit\$se.n.hat, etc.
Components of the returned object are as follows:
histories |
The input capture history matrix. Size NAN x NS |
aux |
Auxiliary information, mostly stored input values. This is a list containing: \$call, \$nan=number of individuals, \$ns=number of samples, \$nx=number of coefficients in the initial capture model, \$ny=number of coefficients in recapture model, \$cov.name=names of the covariates in both models (initial capture covariates first, then recapture covariates), \$ic.name=name of capture history matrix, \$mra.version=version number of this package, \$R.version=R version used, \$run.date=date the model was estimated. |
loglik |
Value of the Huggins log likelihood at it's maximum. |
deviance |
Model deviance = -2* |
aic |
AIC for the model = |
aicc |
AIC with small sample correction = AIC + (2* |
capcoef |
Vector of estimated coefficients in the initial capture model. Length NX. |
se.capcoef |
Vector of estimated standard errors for coefficients in initial capture model. Length NX. |
recapcoef |
Vector of estimated coefficients in the recapture model. Length NY. |
se.surcoef |
Vector of standard errors for coefficients in recapture model. Length NY. |
covariance |
Variance-covariance matrix for the estimated model coefficients. Size (NX+NY) X (NX+NY). |
p.hat |
Matrix of estimated initial capture probabilities computed from the model. Size of this matrix is NAN x NS. Cell (i,j) is estimated probability of first capture for individual i during capture occasion j. |
se.p.hat |
Matrix of standard errors for estimated initial capture probabilities. Size NAN x NS. |
c.hat |
Matrix of estimated recapture probabilities computed from the model. Size NAN x NS. Cell (i,j) is estimated probability of capturing individual i during occasion j given that it was initially captured prior to j. |
se.c.hat |
Matrix of standard errors for estimated recapture probabilities. Size NAN X NS. |
df |
Number of estimable parameters in the model. |
message |
A string indicating whether the maximization routine converged. |
exit.code |
Exit code from the maximization routine.
Interpretation for |
cov.code |
A code indicating the method used to compute the covariance matrix. |
cov.meth |
String indicating method used to compute covariance matrix.
Interprets |
n.hat |
The Huggins estimate of population size. This estimate is
sum( 1/ pstar(i) ), where pstar(i) is probability of observing individual i,
which equals 1 - p.hat[i,1]*p.hat[i,2]* ... *p.hat[i,NS], where p.hat is the
returned value of |
se.n.hat |
Estimated standard error of |
n.hat.lower |
Lower limit of log based confidence interval for
|
n.hat.upper |
Upper limit of log based confidence interval for
|
n.hat.conf |
Confidence level for the interval on |
nhat.v.meth |
Code for method used to compute variance of |
num.caught |
Number of individuals ever captured = number of
rows in the |
n.effective |
Effective sample size = number of observed individuals times number of occasions = NAN * NS |
Trent McDonald, WEST-INC, [email protected]
Huggins, R. M. 1989. On the statistical analysis of capture experiments. Biometrika 76:133-140.
Amstrup, S. C., T. L. McDonald, and B. F. J. Manly (editors). 2005. Handbook of Capture-Recapture Analysis. Princeton University Press.
# Fake the data for these examples set.seed(3425) ch.mat <- matrix( round(runif(30*5)), nrow=30, ncol=5) ch.mat <- ch.mat[ apply(ch.mat,1,sum) > 0, ] # no zero rows allowed ct <- as.factor(1:ncol(ch.mat)) attr(ct,"nan") <- nrow(ch.mat) # used to fit time varying factor sex <- round(runif(nrow(ch.mat))) # fake sex attr(sex,"ns") <- ncol(ch.mat) # Models parallel to the 8 Otis et al. models. # see Amstrup et al. (2005, p. 77) # Constant model (model M(0)). hug.0 <- F.huggins.estim( ~1, NULL, ch.mat ) # Time varying model (model M(t)) hug.t <- F.huggins.estim( ~tvar(ct), NULL, ch.mat) # Additive Behavioral model (model M(b)) hug.b <- F.huggins.estim( ~1, ~1, ch.mat ) # Time and Behavioral model (model M(tb)) hug.tb <- F.huggins.estim( ~tvar(ct), ~1, ch.mat ) # Individual effects (model M(h)) hug.h <- F.huggins.estim( ~ivar(sex), NULL, ch.mat ) # Individual and Behavioral effects (model M(bh)) hug.bh <- F.huggins.estim( ~ivar(sex), ~1, ch.mat ) # Individual and time effects (model M(th)) hug.th <- F.huggins.estim( ~ivar(sex)+tvar(ct), NULL, ch.mat ) # Individual, time, and behavoral effects (model M(tbh)) hug.tbh <- F.huggins.estim( ~ivar(sex)+tvar(ct), ~1, ch.mat ) # Time varying initial captures, recaptures are constant and depend on sex. hug.custom1 <- F.huggins.estim( ~tvar(ct), ~ivar(sex), ch.mat, remove=TRUE ) # Compare hug.custom1 to the following: Time varying initial captures with # time varying recaptures that depend on sex. hug.custom2 <- F.huggins.estim( ~tvar(ct), ~ivar(sex), ch.mat, remove=FALSE ) # Values in first column of recapture covariates do not matter. # Below, mod.1 and mod.2 are identical. mod.1 <- F.huggins.estim( ~tvar(ct), ~tvar( c( 0,1,2,3,4), nrow(ch.mat)), ch.mat, remove=TRUE) mod.2 <- F.huggins.estim( ~tvar(ct), ~tvar( c(-9,1,2,3,4), nrow(ch.mat)), ch.mat, remove=TRUE)
# Fake the data for these examples set.seed(3425) ch.mat <- matrix( round(runif(30*5)), nrow=30, ncol=5) ch.mat <- ch.mat[ apply(ch.mat,1,sum) > 0, ] # no zero rows allowed ct <- as.factor(1:ncol(ch.mat)) attr(ct,"nan") <- nrow(ch.mat) # used to fit time varying factor sex <- round(runif(nrow(ch.mat))) # fake sex attr(sex,"ns") <- ncol(ch.mat) # Models parallel to the 8 Otis et al. models. # see Amstrup et al. (2005, p. 77) # Constant model (model M(0)). hug.0 <- F.huggins.estim( ~1, NULL, ch.mat ) # Time varying model (model M(t)) hug.t <- F.huggins.estim( ~tvar(ct), NULL, ch.mat) # Additive Behavioral model (model M(b)) hug.b <- F.huggins.estim( ~1, ~1, ch.mat ) # Time and Behavioral model (model M(tb)) hug.tb <- F.huggins.estim( ~tvar(ct), ~1, ch.mat ) # Individual effects (model M(h)) hug.h <- F.huggins.estim( ~ivar(sex), NULL, ch.mat ) # Individual and Behavioral effects (model M(bh)) hug.bh <- F.huggins.estim( ~ivar(sex), ~1, ch.mat ) # Individual and time effects (model M(th)) hug.th <- F.huggins.estim( ~ivar(sex)+tvar(ct), NULL, ch.mat ) # Individual, time, and behavoral effects (model M(tbh)) hug.tbh <- F.huggins.estim( ~ivar(sex)+tvar(ct), ~1, ch.mat ) # Time varying initial captures, recaptures are constant and depend on sex. hug.custom1 <- F.huggins.estim( ~tvar(ct), ~ivar(sex), ch.mat, remove=TRUE ) # Compare hug.custom1 to the following: Time varying initial captures with # time varying recaptures that depend on sex. hug.custom2 <- F.huggins.estim( ~tvar(ct), ~ivar(sex), ch.mat, remove=FALSE ) # Values in first column of recapture covariates do not matter. # Below, mod.1 and mod.2 are identical. mod.1 <- F.huggins.estim( ~tvar(ct), ~tvar( c( 0,1,2,3,4), nrow(ch.mat)), ch.mat, remove=TRUE) mod.2 <- F.huggins.estim( ~tvar(ct), ~tvar( c(-9,1,2,3,4), nrow(ch.mat)), ch.mat, remove=TRUE)
Calculate the log likelihood of a fully saturated time varying CJS
model. Use to convert the relative deviance output by F.cjs.estim
to
actual deviance.
F.sat.lik(ch)
F.sat.lik(ch)
ch |
A capture history matrix consisting of 0's, 1's, and 2's. |
The number reported as deviance
by F.cjs.estim
is relative
deviance, calculated as -2*log(likelihood). IF THERE ARE NO INDIVIDUAL-VARYING
COVARIATES in the model, it is possible to compute the theoretical log-likelihood
for a set of data assuming perfect prediction. This is the saturated log-likelihood.
The actual deviance of a model is the deviance of the model relative to this
theoretical maximum, computed as -2*((saturated log-likelihood) -
2*(model log-likelihood)).
In the parameterization of F.cjs.estim
, all covariates are potentially individual and
time varying, and in this case the saturated log-likelihood is unknown. Consequently,
the saturated likelihood is not often needed in MRA. This routine was included
as a utility function because the saturated likelihood is handy in some cases, including
parametric bootstrapping to estimate C-hat.
Assuming cjs.fit
is an estimated CJS model with time varying
covariates only fit to histories in cjs.hists
, compute deviance as
-F.sat.lik(cjs.hists) - 2*cjs.fit\$loglik
=
cjs.fit\$deviance - F.sat.lik(cjs.hists)
A scalar equal to the value of the saturated CJS log-likelihood. The saturated log-likelihood is the theoretical best predictive model possible, and actual deviance is calculated relative to this. See Examples.
CAUTION: This routine works for time varying models only. If
individual-varying or individual-and-time-varying covariates are fitted
in the model,
the routine cannot sense it and will run but yield an incorrect answer.
Use relative deviance reported by F.cjs.estim
in this case.
Also, this routine will not run if animals have been removed (censored). I.e., the
capture history matrix cannot have any 2's in it. Use relative deviance reported
by F.cjs.estim
when animals have been removed.
Eric V. Regehr (USGS, [email protected]) and Trent McDonald (WEST Inc., [email protected])
Look up "saturated model" in the program MARK help file for the equations implemented by this function.
data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) deviance <- -F.sat.lik( dipper.histories ) - 2*dipper.cjs$loglik
data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) deviance <- -F.sat.lik( dipper.histories ) - 2*dipper.cjs$loglik
Conducts automated stepwise model selection of CJS models. Selection includes forward steps followed by backward looks. Steps consider covariates in both the survival and probability of capture models.
F.step.cjs(cap.covars, surv.covars, fit.crit = "qaicc", signif.drop = 0, signif.increase = 0, plot = TRUE, ...)
F.step.cjs(cap.covars, surv.covars, fit.crit = "qaicc", signif.drop = 0, signif.increase = 0, plot = TRUE, ...)
cap.covars |
A character vector containing the names of single covariates or combinations of covariates to consider in the capture equation. See Details. |
surv.covars |
A character vector containing the names of single covariates or combinations of covariates to consider in the survival equation. See Details. |
fit.crit |
A character scalar specifying the model fit criterion to use during each step. This criterion
will be used to rank variables
during the forward steps and backward looks. Possible values are "qaicc" (the default),
"aic", "qaic", and "aicc". During forward (addition) steps, the variable(s) that
decreased |
signif.drop |
A scalar specifying the decrease in |
signif.increase |
A scalar specifying the increase in |
plot |
A scalar specifying whether the minimum |
... |
Additional arguments to |
Elements of both the cap.covars
and surv.covars
arguments can specify the names of single covariates or sets of
covariates to consider as a group to be included or excluded
together. For example, if cap.covars = c("sex",
"ageclass1 + ageclass2 + ageclass3" )
, the routine will
include and exclude sex
as a single covariate with 1
estimated coefficient and ageclass1 + ageclass2 +
ageclass3
as a set of 3 covariates (with 3 estimated
coefficients) to be included and excluded as a set. This is
useful if factor covariates are pre-coded as indicator variables. In the
example above, specifying ageclass1 + ageclass2 +
ageclass3
would make sense if age of an individual was
classified into 1 of 4 classes, and ageclassX
was a
matrix with elements equal to 1 if the individual was in age
class X during a capture occasion, and 0 otherwise.
Specifying a term like a +
b + ab
would ensure that main effect matrices (a
and b
) are included whenever the interaction matrix
(ab
) is included during model selection. However, this way
of including interactions will only be useful if the main effects
are not considered separately. That is, specifying
cap.covars = c("a", "b", "a + b + ab")
will not work
because the routine does not know that "a" and "b" are
components of "a + b + ab". Nonsense models like
"a + b + ab + a + b" could result. Thus, this routine is
likely only useful for terms that do not include interactions.
A useful way to proceed in this case may be to use stepwise
to select a model of main effects, then consider interactions.
Time varying and individual varying variables can be specified
using the tvar
and ivar
functions in the elements
of cap.covars
and surv.covars
. For example,
specifying "tvar(year,nan=nrow(ch))"
as an element of
surv.covars
, where year = 1:ncol(ch)
and
ch
is the capture history matrix, will fit a linear
year effect (1 coefficient) when this element is added during
stepwise selection. Likewise, factors are preserved during
selection. If year
in the above example had been a
factor (i.e., year = as.factor(1:ncol(ch))
), separate
effects for each year (ncol(ch) - 1
coefficients) would
have been fitted when this effect came up for consideration
during stepwise selection.
The variable to add or eliminate is selected after all variables
in both the capture and survival models are considered. That is, the
variable that decreases (or increases) fit.crit
the most over both
models is added.
Selection does not iteratively fix one model and select variables
in the other. For example, on one step, the variable that decreases fit.crit
the most may be a survival covariate, while on the next step
the variable that decreases fit.crit
the most may be a capture covariate.
The final CJS model resulting from application of stepwise model selection. This object
is a valid MRA CJS model and has class 'cjs'. See help for F.cjs.estim
for a
description of the components of this object.
Trent McDonald, WEST-INC, [email protected]
# Aquire data and intermediate variables data(dipper.histories) data(dipper.males) ch <- dipper.histories males <- dipper.males ns <- ncol(ch) nan <- nrow(ch) # Construct covariates small.t <- as.factor( paste("T",1:ns, sep="")) post.flood <- as.numeric( 1:ns >= 4 ) year <- 1:ns - (ns / 2) males.postflood <- outer( c(males), post.flood ) # individual and time varying print(dim(males.postflood)) # Attach attributes to covariates. For convienence only. attr(small.t, "nan") <- nan attr(small.t, "drop") <- c(1,2) attr(year, "nan") <- nan attr(post.flood, "nan") <- nan attr(males, "ns") <- ns # Define pool of variables to be considered in each model cap.vars <- c("tvar(small.t)","tvar(year)") surv.vars <- c("tvar(small.t)","tvar(year)", "tvar(post.flood)", "ivar(males)", "males.postflood") # Do stepwise selection final.fit <- F.step.cjs( cap.vars, surv.vars, histories=ch, control=mra.control(maxfn=500, cov.meth=2) )
# Aquire data and intermediate variables data(dipper.histories) data(dipper.males) ch <- dipper.histories males <- dipper.males ns <- ncol(ch) nan <- nrow(ch) # Construct covariates small.t <- as.factor( paste("T",1:ns, sep="")) post.flood <- as.numeric( 1:ns >= 4 ) year <- 1:ns - (ns / 2) males.postflood <- outer( c(males), post.flood ) # individual and time varying print(dim(males.postflood)) # Attach attributes to covariates. For convienence only. attr(small.t, "nan") <- nan attr(small.t, "drop") <- c(1,2) attr(year, "nan") <- nan attr(post.flood, "nan") <- nan attr(males, "ns") <- ns # Define pool of variables to be considered in each model cap.vars <- c("tvar(small.t)","tvar(year)") surv.vars <- c("tvar(small.t)","tvar(year)", "tvar(post.flood)", "ivar(males)", "males.postflood") # Do stepwise selection final.fit <- F.step.cjs( cap.vars, surv.vars, histories=ch, control=mra.control(maxfn=500, cov.meth=2) )
Updates the degrees of freedom in a fitted object to either the value estimated from the rank of the variance-covariance matrix, the number of coefficients, or a user-specified value.
F.update.df(fit, df=NA)
F.update.df(fit, df=NA)
fit |
An MRA fitted CJS model. Class must be c("cjs", "cr"). These are produced by |
df |
The new value for degrees of freedom.
If |
An object (list) of class c("cjs","cr") with degrees of freedom, AIC, QAIC, AICc, and QAICc updated.
Trent McDonald, WEST-INC, [email protected]
## Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Update the degrees of freedom dipper.cjs <- F.update.df( dipper.cjs, -1 )
## Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories ) ## Update the degrees of freedom dipper.cjs <- F.update.df( dipper.cjs, -1 )
Expands a vector of individual-varying values into a 2-d matrix of the appropriate size for use in MRA model formulas.
ivar(x, ns=attr(x,"ns"), drop.levels=attr(x,"drop.levels"))
ivar(x, ns=attr(x,"ns"), drop.levels=attr(x,"drop.levels"))
x |
The vector of individual varying values to expand. This can be
a factor (see |
ns |
Number of sampling occasions. Default is to use the 'ns' attribute
of |
drop.levels |
A vector of integers specifying which levels of a factor
to drop. Only applicable if |
A 2-d matrix of size length(x)
x ns
suitable for passing to the
Fortran DLL of MRA for estimation. Values within rows are constant, values
across rows vary according to x
. If x
is a factor, this matrix
contains 0-1 indicator functions necessary to fit the factor.
If x
is a factor, attributes of the returned matrix are
"levels" = levels of the factor and "contr" = contrasts used in the coding (always
contr.treatment
). For other contrast coding of factors, make your own
2-d matrix with a call to the appropriate function (like contr.poly
).
Trent McDonald, WEST-INC, [email protected]
nan <- 30 ns <- 5 age <- as.factor(sample( c("J","S1","S2","Adult"), size=nan, replace=TRUE )) attr(age,"ns") <- ns # Note that levels get reordered (by R default, alphabetically) attr(age,"drop.levels") <- (1:length(levels(age)))[ levels(age) == "J" ] age.mat <- ivar(age) # level J is the reference age.mat <- ivar(age, drop=4) # level S2 is the reference # Look at 3-D matrix produced when called with a factor. dim(age.mat) <- c(nan,ns,length(levels(age))-1) print(age.mat) # each page is the 2-d matrix used in the fit. print(age.mat[1,,]) age.mat <- ivar(age, drop=c(3,4)) # level S1 and S2 are combined and are the reference # compare above to ivar( c(1,1,2,2,3,3), 5 )
nan <- 30 ns <- 5 age <- as.factor(sample( c("J","S1","S2","Adult"), size=nan, replace=TRUE )) attr(age,"ns") <- ns # Note that levels get reordered (by R default, alphabetically) attr(age,"drop.levels") <- (1:length(levels(age)))[ levels(age) == "J" ] age.mat <- ivar(age) # level J is the reference age.mat <- ivar(age, drop=4) # level S2 is the reference # Look at 3-D matrix produced when called with a factor. dim(age.mat) <- c(nan,ns,length(levels(age))-1) print(age.mat) # each page is the 2-d matrix used in the fit. print(age.mat[1,,]) age.mat <- ivar(age, drop=c(3,4)) # level S1 and S2 are combined and are the reference # compare above to ivar( c(1,1,2,2,3,3), 5 )
Add a line to an existing CJS capture-recapture plot showing either N or survival estimates.
## S3 method for class 'cjs' lines( x, what="n", animals=-1, occasions=-1, ... )
## S3 method for class 'cjs' lines( x, what="n", animals=-1, occasions=-1, ... )
x |
CJS object from |
what |
Indicator for what to plot. what = "n" plots estimates of size (i.e,. \$n.hat). what = "s" plots estimates of survival. |
animals |
Index of animals to plot. This is the row number for
animals to include. E.g., if |
occasions |
Sampling occasions to plot. This must match the occasions argument to
the last |
... |
Additional arguments to |
This is a utility function for plotting. Lines are added to the current plot. A current plot must be displayed.
Nothing. A value of 1 is invisibly returned.
Trent McDonald, WEST Inc., [email protected]
data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } # Fit constant capture probability, period (i.e., flood) effects on survival dipper.cjs <- F.cjs.estim( ~1, ~x2+x3, dipper.histories ) plot(dipper.cjs, type="s", animals=1) lines(dipper.cjs, what="s", animals=c(4, 10))
data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } # Fit constant capture probability, period (i.e., flood) effects on survival dipper.cjs <- F.cjs.estim( ~1, ~x2+x3, dipper.histories ) plot(dipper.cjs, type="s", animals=1) lines(dipper.cjs, what="s", animals=c(4, 10))
Auxiliary function providing a user interface for
mra
fitting. Typically only used when calling one of the
*.estim
routines.
mra.control(algorithm=1, maxfn=1000, cov.meth=1, trace=0, tol=1e-07 )
mra.control(algorithm=1, maxfn=1000, cov.meth=1, trace=0, tol=1e-07 )
algorithm |
Integer specifying the maximization algorithm to use. If |
maxfn |
Maximum number of likelihood evaluations allowed during the fitting process.
This determines when the minimization routine stops and concludes that the problem
will not converge. The routine stops after the likelihood has been evaluated
|
cov.meth |
Integer specifying the covariance estimation method. |
trace |
Integer controlling output of intermediate results. If When using
|
tol |
Vector or scalar of tolerance(s) for coefficients in the model. Minimization stops and concludes it has converged when |delta.b(i)| < tol(i) for all i, where delta.b(i) is change in parameter i on an iteration. |
A list with the input arguments as components.
Trent McDonald, WEST-INC, [email protected]
data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories, control=mra.control(trace=1, maxfn=200) )
data(dipper.histories) ct <- as.factor( paste("T",1:ncol(dipper.histories), sep="")) attr(ct,"nan")<-nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories, control=mra.control(trace=1, maxfn=200) )
Plot the population size or survival estimates for a Cormack-Jolly-Seber model estimated by F.cjs.estim
## S3 method for class 'cjs' plot(x, type="n", ci = TRUE, smooth = TRUE, occasions = -1, animals = -1, smubass = 5, ...)
## S3 method for class 'cjs' plot(x, type="n", ci = TRUE, smooth = TRUE, occasions = -1, animals = -1, smubass = 5, ...)
x |
An object of class 'cjs'. Objects of this class are estimated open population Cormack-Jolly-Seber models produced by F.cjs.estim. |
type |
Type of estimates to plot. type = "n" (the default) produces a plot of population size estimates versus capture occasion. type = "s" produces a plot of survival estimates versus capture occasion. |
ci |
Plot confidence intervals? If ci=TRUE, confidence intervals around population size or survival estimates are plotted (depending on 'type='), otherwise, only confidence intervals are not plotted. |
smooth |
Smooth estimates of population size? If type="n", smooth=TRUE will produce a smoothed (supsmu) line through plotted size estimates. Ignored for type="s". |
occasions |
Vector of occasion numbers to use in plot. If any(occasions <= 0), all occasions are plotted. Otherwise, plot the occasions specified. For example, if occasions = c(1,3,5), only estimates from the 1st, 3rd, and 5th capture occasion are plotted. If type = "n", occasion = 1 (1st occasion) cannot be plotted because it can't be estimated. If type = "s", occasion = ncol(y) (last occasion) cannot be plotted because no survival interval exist beyond the end of the study. |
animals |
Vector of individuals to plot. This parameter is used only when plotting survival estimates. For example, animals = c(1,2,10) plots the survival estimates of the 1st, 2nd, and 10th animals (rows 1, 2, and 10 of the survival estimate matrix). |
smubass |
Bass parameter for super-smoothed line, if called for by smooth=TRUE. Must be between 0 and 10. Larger numbers produce smoother lines. |
... |
Additional arguments to |
Confidence intervals on survival estimates cannot be plotted with this routine. To plot confidence
intervals surrounding survival estimates, call this routine with type="s", then compute
confidence intervals for survival estimates, and use lines
to add lines to the plot.
For type="s", the survival estimate matrix that was plotted is is invisibly returned. For type = "n", the smooth fit is invisibly returned if called for by smooth = TRUE, otherwise NA is invisibly returned if smooth = FALSE.
Trent McDonald, WEST-INC, [email protected]
F.cjs.estim
, matplot
, lines
, plot
data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) plot(dipper.cjs) print(dipper.cjs$s.hat) plot(dipper.cjs, type="s", animals=1)
data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) plot(dipper.cjs) print(dipper.cjs$s.hat) plot(dipper.cjs, type="s", animals=1)
Plot the population size estimates for a Cormack-Jolly-Seber model estimated by F.cjs.estim
## S3 method for class 'nhat' plot(x, ci = TRUE, smooth = TRUE, occasions = -1, smubass = 5, ...)
## S3 method for class 'nhat' plot(x, ci = TRUE, smooth = TRUE, occasions = -1, smubass = 5, ...)
x |
An object of class 'nhat', which inherits from 'cjs'. Objects of this class are estimated open population Cormack-Jolly-Seber models produced by F.cjs.estim. |
ci |
Plot confidence intervals? If ci=TRUE, confidence intervals around population size or survival estimates are plotted (depending on 'type='), otherwise, only confidence intervals are not plotted. |
smooth |
Smooth estimates of population size? If type="n", smooth=TRUE will produce a smoothed (supsmu) line through plotted size estimates. Ignored for type="s". |
occasions |
Vector of occasion numbers to use in plot. If any(occasions <= 0), all occasions are plotted. Otherwise, plot the occasions specified. For example, if occasions = c(1,3,5), only estimates from the 1st, 3rd, and 5th capture occasion are plotted. If type = "n", occasion = 1 (1st occasion) cannot be plotted because it can't be estimated. If type = "s", occasion = ncol(y) (last occasion) cannot be plotted because no survival interval exist beyond the end of the study. |
smubass |
Bass parameter for super-smoothed line, if called for by smooth=TRUE. Must be between 0 and 10. Larger numbers produce smoother lines. |
... |
Additional arguments to |
The smooth fit is invisibly returned if called for by smooth = TRUE, otherwise NA is invisibly returned.
Trent McDonald, WEST-INC, [email protected]
F.cjs.estim
, matplot
, lines
, plot
, plot.cjs
data(dipper.histories) dipper.cjs <- F.cjs.estim( ~1, ~1, dipper.histories ) plot(dipper.cjs,type="n") # See examples for F.cr.model.avg for a way to plot model averaged population size estimates.
data(dipper.histories) dipper.cjs <- F.cjs.estim( ~1, ~1, dipper.histories ) plot(dipper.cjs,type="n") # See examples for F.cr.model.avg for a way to plot model averaged population size estimates.
Predictor method for CJS capture-recapture objects. Return expected values for all active cells in the model.
## S3 method for class 'cjs' predict(object, ...)
## S3 method for class 'cjs' predict(object, ...)
object |
CJS capture-recapture model as output from F.cjs.estim |
... |
Additional arguments to other functions. Not used, but must be here
for compatibility with the generic |
The only components of cjsobj
needed are $histories
, $p.hat
, $s.hat
A nan X ns matrix of fitted values (=cell expected value), where
nan=number of animals and ns=number of samples. Fitted values in the
non-active cells are set to NA. Non-active
cells are those prior to and including the initial capture, and after
the occasion on which an animal is known to have died. Computation of
expected values is described in the details
section of the help file
for F.cjs.gof
.
Trent McDonald, WEST Inc., [email protected]
# Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) dipper.expected <- predict(dipper.cjs)
# Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) dipper.expected <- predict(dipper.cjs)
Print method for Cormack-Jolly-Seber (CJS) models estimated by F.cjs.estim().
## S3 method for class 'cjs' print(x, alpha=c(0.05,0.01), ...)
## S3 method for class 'cjs' print(x, alpha=c(0.05,0.01), ...)
x |
An object of class "cjs" produced by F.cjs.estim() |
alpha |
A vector with length either 2 or 3 containing alpha levels used to put "*" or "**" beside the GOF results. One * is printed if significance is between alpha[1] and alpha[2] (i.e., if alpha[2] < p < alpha[1]). Two ** are printed if significance is less than alpha[2] (p < alpha[2]). |
... |
Arguments to other functions called by this one. Currently no other
functions are called, so this is not used, but must be here
for compatibility with the generic |
Nothing is returned. This function is used exclusively for its side effects. It prints an object of class "cjs" in a nice human-readable format. If goodness-of-fit tests are present, they are printed. If population size estimates are present, they are printed.
Trent McDonald, Ph.D., WEST-INC, [email protected]
# Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) print(dipper.cjs)
# Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) print(dipper.cjs)
Print method for Huggin's closed population model objects estimated by F.huggins.estim().
## S3 method for class 'hug' print(x, ...)
## S3 method for class 'hug' print(x, ...)
x |
An object of class "hug" produced by F.huggins.estim() |
... |
Arguments to other functions called by this one. Currently no other
functions are called, so this is not used, but must be here
for compatibility with the generic |
If there are no free covariates in the recapture model (i.e., recapture=NULL
),
only the combined capture and recapture model is printed. If the recapture
model has coefficients, coefficients in both are printed in separate columns.
When a recapture model has free coefficients, a "C" (for "capture") appears next to coefficients in the recapture model that also appear in the capture model. These coefficients are fixed in the recapture model because they are not free. These values were estimated from initial capture information. A "B" (for "behavioral") appears next to free coefficients in the recapture model that do not appear in the initial capture model.
Nothing is returned. This function is used exclusively for its side effects. It prints an object of class "hug" in a nice human-readable format.
Trent McDonald, Ph.D., WEST-INC, [email protected]
data(dipper.histories) dipper.fit <- F.huggins.estim( ~1, histories= dipper.histories ) print(dipper.fit)
data(dipper.histories) dipper.fit <- F.huggins.estim( ~1, histories= dipper.histories ) print(dipper.fit)
Print the estimates of N from a CJS object in a nice format.
## S3 method for class 'nhat' print(x, width=10, ...)
## S3 method for class 'nhat' print(x, width=10, ...)
x |
An object of class "nhat", which inherits from class "cr". This class of object is
output from |
width |
The minimum width of columns in the table printed by this routine. |
... |
Arguments to other functions called by this one. Currently no other
functions are called, so this is not used, but must be here
for compatibility with the generic |
Horvitz-Thompson estimates of N, along with
standard errors, are printed. print.cjs
also prints N estimates, if present,
but as a brief, one-row summary. This routine prints a more complete table. Numerical
values of the supsmu
smooth of N estimates (bass
= 0.5) associated with
plots produced by plot.cjs
are printed.
Nothing. Run for side effects
Trent McDonald, WEST-INC, [email protected]
plot.cjs
, print.cjs
, F.cjs.estim
# Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) # Method 1: explicit matricies xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) print(dipper.cjs) # Method 2: indicator vectors x <- factor(1:ncol(dipper.histories), labels=paste("t",1:ncol(dipper.histories),sep="")) nd <- nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(x,nan=nd,drop=c(1,7)), ~tvar(x,nan=nd,drop=c(6,7)), dipper.histories) print(dipper.cjs)
# Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) # Method 1: explicit matricies xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) print(dipper.cjs) # Method 2: indicator vectors x <- factor(1:ncol(dipper.histories), labels=paste("t",1:ncol(dipper.histories),sep="")) nd <- nrow(dipper.histories) dipper.cjs <- F.cjs.estim( ~tvar(x,nan=nd,drop=c(1,7)), ~tvar(x,nan=nd,drop=c(6,7)), dipper.histories) print(dipper.cjs)
Residual extraction routine for a CJS object. Returns Pearson or deviance residuals of a CJS capture-recapture model.
## S3 method for class 'cjs' residuals(object, type="pearson", ...)
## S3 method for class 'cjs' residuals(object, type="pearson", ...)
object |
a CJS (Cormack-Jolly-Seber capture-recapture) object, which is usually the result of calling F.cjs.estim |
type |
string indicating type of residual to return. Either "pearson" for Pearson residuals (i.e., (o - e)/sqrt(e*(1-e))) or "deviance" for deviance residuals (i.e., 2*sign(o-e)*sqrt(o*log(o/e) + (1-o)*log((1-o)/(1-e))) ) |
... |
Additional arguments to other functions. Not used, but must be here
for compatibility with the generic |
In almost all cases, a CJS model fitted by F.cjs.estim already has a $residuals
component. This
routine either extracts this component, or computes residuals of the component if not found.
Observed component (o(ij)) in formulas above is the capture indicator for animal i during occasion j. If animal i was seen during occasion j, o(ij) = 1. Otherwise, o(ij) = 0.
Expected component (e(ij)) in formula above is the expected value of the capture indicator for animal i during occasion j. In other words, o(ij) is a binomial random variable with expected value e(ij). Under the assumptions of a CJS model, e(ij) is computed as phi(i(1)) * phi(i(2)) * ... * phi(i(j-1)) * p(ij), where p(ij) is the estimated capture probability of animal i during occasion j, and phi(i(1)) is estimated survival during the first interval following initial capture of the animal, phi(i(2)) is survival during the second interval after initial capture, and phi(i(j-1)) is survival during the interval just prior to occasion j.
A NAN X NS matrix of residuals, where NAN = number of animals and NS = number of capture occasions. Residuals in the non-active cells are set to NA. Non-active cells are those prior to and including the initial capture, and after the occasion on which an animal is known to have died.
If type = "pearson", the residual for active cell (i,j) is (o(ij) - e(ij)) / sqrt(e(ij) * (1 - e(ij))).
If type = "deviance", the residual for active cell (i,j) is 2 * sign(o(ij) - e(ij)) * sqrt(o(ij)*log(o(ij) / e(ij)) + (1 - o(ij)) * log((1 - o(ij)) / (1 - e(ij)))).
Observed (o(ij)) and expected (e(ij)) are defined in Details.
Trent McDonald
# Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) residuals(dipper.cjs)
# Fit CJS model to dipper data, time-varying capture and survivals. data(dipper.histories) xy <- F.cjs.covars( nrow(dipper.histories), ncol(dipper.histories) ) for(j in 1:ncol(dipper.histories)){ assign(paste("x",j,sep=""), xy$x[,,j]) } dipper.cjs <- F.cjs.estim( ~x2+x3+x4+x5+x6, ~x1+x2+x3+x4+x5, dipper.histories ) residuals(dipper.cjs)
Expands a vector of time-varying values into a 2-d matrix for use in MRA model formulas.
tvar(x, nan=attr(x,"nan"), drop.levels=attr(x,"drop.levels"))
tvar(x, nan=attr(x,"nan"), drop.levels=attr(x,"drop.levels"))
x |
The vector of time varying values to expand. This can be
a factor (see |
nan |
Number of individuals. Default is to use the 'nan' attribute
of |
drop.levels |
A vector of integers specifying which levels of a factor
to drop. Only applicable if |
A 2-d matrix of size nan
x length(x)
suitable for passing to the
Fortran DLL of MRA for estimation. Values within columns are constant, values
across columns vary according to x
. If x
is a factor, this matrix
contains 0-1 indicator functions necessary to fit the factor.
If x
is a factor, attributes of the returned matrix are
"levels" = levels of the factor and "contr" = contrasts used in the coding (always
contr.treatment
). For other contrast coding of factors, make your own
2-d matrix with a call to the appropriate function (like contr.poly
).
Trent McDonald, WEST-INC, [email protected]
nan <- 30 ns <- 5 time.occ <- as.factor(paste("T",1:ns, sep="")) attr(time.occ,"nan") <- nan attr(time.occ,"drop.levels") <- ns time.mat <- tvar(time.occ) # Last occasion is the reference, the SAS and MARK default. time.mat <- tvar(as.factor(1:ns),nan,ns) #equivalent to above. # Look at 3-d matrix produced when called with factors dim(time.mat) <- c(nan,ns,length(levels(time.occ))-1) print(time.mat) # each page is the 2-d matrix used in the fit. print(time.mat[1,,]) # compare above to tvar( 1:ns, nan )
nan <- 30 ns <- 5 time.occ <- as.factor(paste("T",1:ns, sep="")) attr(time.occ,"nan") <- nan attr(time.occ,"drop.levels") <- ns time.mat <- tvar(time.occ) # Last occasion is the reference, the SAS and MARK default. time.mat <- tvar(as.factor(1:ns),nan,ns) #equivalent to above. # Look at 3-d matrix produced when called with factors dim(time.mat) <- c(nan,ns,length(levels(time.occ))-1) print(time.mat) # each page is the 2-d matrix used in the fit. print(time.mat[1,,]) # compare above to tvar( 1:ns, nan )