Skip to content

Commit

Permalink
Loosened dimension constraints in t-1 cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
dochvam committed Oct 10, 2019
1 parent 8d51768 commit 4bdc509
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
20 changes: 11 additions & 9 deletions R/dCJS.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
#' probabilities, in that order, are scalar (s, meaning the probability applies
#' to every \code{x[t]}) or vector (v, meaning the probability is a vector
#' aligned with \code{x}). When \code{probCapture} and/or \code{probSurvive} is
#' a vector, they must be the same length as \code{x}. Note that in the case
#' where \code{len = 2}, \code{probSurvive} must be scalar, since as it would
#' have length \code{len - 1 = 1}.
#' a vector, they must be the same length as \code{x}.
#'
#' It is important to use the time indexing correctly for survival.
#' \code{probSurvive[t]} is the survival probabilty from time
#' \code{t} to time \code{t + 1}. Time indexing for detection is more
#' obvious: \code{probDetect[t]} is the detection probability at time
#' \code{t}.
#' \code{probSurvive[t]} is the survival probabilty from time \code{t} to time
#' \code{t + 1}. When a vector, \code{probSurvive} may have length greater than
#' \code{length(x) - 1}, but all values beyond that index are ignored.
#'
#' Time indexing for detection is more obvious: \code{probDetect[t]} is the
#' detection probability at time \code{t}.
#'
#' When called from R, the \code{len} argument to \code{dCJS_**} is not
#' necessary. It will default to the length of \code{x}. When used in
Expand Down Expand Up @@ -236,7 +236,8 @@ dCJS_vs <- nimbleFunction(
if (len != 0) {
if (len != length(x)) stop("Argument len must match length of data, or be 0.")
}
if (length(x) - 1 != length(probSurvive)) stop("Length of probSurvive does not match length of data.")
if (length(probSurvive) < length(x) - 1)
stop("Length of probSurvive must be at least length of data minus 1.")

## Note the calculations used here are actually in hidden Markov model form.
probAliveGivenHistory <- 1
Expand Down Expand Up @@ -286,7 +287,8 @@ dCJS_vv <- nimbleFunction(
if (len != 0) {
if (len != length(x)) stop("Argument len must match length of data, or be 0.")
}
if (length(x) - 1 != length(probSurvive)) stop("Length of probSurvive does not match length of data - 1.")
if (length(probSurvive) < length(x) - 1)
stop("Length of probSurvive must be at least length of data minus 1.")
if (length(x) != length(probCapture)) stop("Length of probCapture does not match length of data.")
## Note the calculations used here are actually in hidden Markov model form.
probAliveGivenHistory <- 1
Expand Down
7 changes: 4 additions & 3 deletions R/dDHMM.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
#'
#' \code{probTrans} has dimension S x S x (T - 1). \code{probTrans}[i, j, t] is
#' the probability that an individual in state \code{i} at time \code{t} takes on
#' state \code{j} at time \code{t+1}.
#' state \code{j} at time \code{t+1}. The length of the third dimension may be greater
#' than (T - 1) but all values indexed greater than T - 1 will be ignored.
#'
#' \code{initStates} has length S. \code{initStates[i]} is the
#' probability of being in state \code{i} at the first observation time.
Expand Down Expand Up @@ -205,7 +206,7 @@ dDHMMo <- nimbleFunction(
if (length(init) != dim(probTrans)[1]) stop("Length of init does not match dim(probTrans)[1] in dDHMMo.")
if (length(init) != dim(probTrans)[2]) stop("Length of init does not match dim(probTrans)[2] in dDHMMo.")
if (length(x) != len) stop("Length of x does not match len in dDHMM.")
if (len - 1 != dim(probTrans)[3]) stop("dim(probTrans)[3] does not match len - 1 in dDHMMo.")
if (len - 1 > dim(probTrans)[3]) stop("dim(probTrans)[3] does not match len - 1 in dDHMMo.")
if (len != dim(probObs)[3]) stop("dim(probObs)[3] does not match len in dDHMMo.")

pi <- init # State probabilities at time t=1
Expand Down Expand Up @@ -237,7 +238,7 @@ rDHMM <- nimbleFunction(
if (nStates != dim(probObs)[1]) stop("Length of init does not match nrow of probObs in dDHMM.")
if (nStates != dim(probTrans)[1]) stop("Length of init does not match dim(probTrans)[1] in dDHMM.")
if (nStates != dim(probTrans)[2]) stop("Length of init does not match dim(probTrans)[2] in dDHMM.")
if (len - 1 != dim(probTrans)[3]) stop("len - 1 does not match dim(probTrans)[3] in dDHMM.")
if (len - 1 > dim(probTrans)[3]) stop("len - 1 does not match dim(probTrans)[3] in dDHMM.")

returnType(double(1))
ans <- numeric(len)
Expand Down
33 changes: 20 additions & 13 deletions R/dDynOcc.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@
#' probabilities of persistence and colonization are a constant scalar (s)
#' or time-indexed vector (v). For example, \code{dDynOcc_svm} takes scalar
#' persistence probability \code{probPersist} with a vector of colonization
#' probabilities \code{probColonize[1:T]}.
#' probabilities \code{probColonize[1:(T-1)]}.
#'
#' When vectors, \code{probColonize} and \code{probPersist} may be of any
#' length greater than \code{length(x) - 1}. Only the first \code{length(x) - 1}
#' indices are used, each corresponding to the transition from time t to t+1
#' (e.g. \code{probColonize[2]} describes the transition probability from
#' t = 2 to t = 3). All extra values are ignored. This is to make it easier to
#' use one distribution for many sites, some requiring probabilities of length 1.
#'
#' The third letter in the suffix indicates whether the detection probability
#' is a constant (scalar), time-dependent (vector), or both time-dependent and
Expand Down Expand Up @@ -183,8 +190,8 @@ dDynOcc_vvm <- nimbleFunction(
start = double(1),
end = double(1),
log = double(0, default = 0)) {
if (length(probPersist) != dim(x)[1] - 1) stop("Length of probPersist vector does not match length of data.")
if (length(probColonize) != dim(x)[1] - 1) stop("Length of probColonize vector does not match length of data.")
if (length(probPersist) < dim(x)[1] - 1) stop("Length of probPersist vector must be at least length(x) - 1.")
if (length(probColonize) < dim(x)[1] - 1) stop("Length of probColonize vector must be at least length(x) - 1.")
if (dim(p)[1] != dim(x)[1]) stop("Dimension mismatch between x and p matrices.")
if (dim(p)[2] != dim(x)[2]) stop("Dimension mismatch between x and p matrices.")

Expand Down Expand Up @@ -240,7 +247,7 @@ dDynOcc_vsm <- nimbleFunction(
end = double(1),
log = double(0, default = 0)) {

if (length(probPersist) != dim(x)[1] - 1) stop("Length of probPersist vector does not match length of data.")
if (length(probPersist) < dim(x)[1] - 1) stop("Length of probPersist vector must be at least length(x) - 1.")
if (dim(p)[1] != dim(x)[1]) stop("Dimension mismatch between x and p matrices.")
if (dim(p)[2] != dim(x)[2]) stop("Dimension mismatch between x and p matrices.")

Expand Down Expand Up @@ -294,7 +301,7 @@ dDynOcc_svm <- nimbleFunction(
start = double(1),
end = double(1),
log = double(0, default = 0)) {
if (length(probColonize) != dim(x)[1] - 1) stop("Length of probColonize vector does not match length of data.")
if (length(probColonize) < dim(x)[1] - 1) stop("Length of probColonize vector must be at least length(x) - 1.")
if (dim(p)[1] != dim(x)[1]) stop("Dimension mismatch between x and p matrices.")
if (dim(p)[2] != dim(x)[2]) stop("Dimension mismatch between x and p matrices.")

Expand Down Expand Up @@ -516,8 +523,8 @@ dDynOcc_vvv <- nimbleFunction(
start = double(1),
end = double(1),
log = double(0, default = 0)) {
if (length(probPersist) != dim(x)[1] - 1) stop("Length of probPersist vector does not match length of data.")
if (length(probColonize) != dim(x)[1] - 1) stop("Length of probColonize vector does not match length of data.")
if (length(probPersist) < dim(x)[1] - 1) stop("Length of probPersist vector must be at least length(x) - 1.")
if (length(probColonize) < dim(x)[1] - 1) stop("Length of probColonize vector must be at least length(x) - 1.")
if (dim(p)[1] != dim(x)[1]) stop("Dimension mismatch between x matrix and p vector.")

## x is a year by rep matix
Expand Down Expand Up @@ -571,7 +578,7 @@ dDynOcc_vsv <- nimbleFunction(
end = double(1),
log = double(0, default = 0)) {

if (length(probPersist) != dim(x)[1] - 1) stop("Length of probPersist vector does not match length of data.")
if (length(probPersist) < dim(x)[1] - 1) stop("Length of probPersist vector must be at least length(x) - 1.")
if (dim(p)[1] != dim(x)[1]) stop("Dimension mismatch between x matrix and p vector.")

## x is a year by rep matix
Expand Down Expand Up @@ -624,7 +631,7 @@ dDynOcc_svv <- nimbleFunction(
start = double(1),
end = double(1),
log = double(0, default = 0)) {
if (length(probColonize) != dim(x)[1] - 1) stop("Length of probColonize vector does not match length of data.")
if (length(probColonize) < dim(x)[1] - 1) stop("Length of probColonize vector must be at least length(x) - 1.")
if (dim(p)[1] != dim(x)[1]) stop("Dimension mismatch between x matrix and p vector.")

## x is a year by rep matix
Expand Down Expand Up @@ -846,8 +853,8 @@ dDynOcc_vvs <- nimbleFunction(
start = double(1),
end = double(1),
log = double(0, default = 0)) {
if (length(probPersist) != dim(x)[1] - 1) stop("Length of probPersist vector does not match length of data.")
if (length(probColonize) != dim(x)[1] - 1) stop("Length of probColonize vector does not match length of data.")
if (length(probPersist) < dim(x)[1] - 1) stop("Length of probPersist vector must be at least length(x) - 1.")
if (length(probColonize) < dim(x)[1] - 1) stop("Length of probColonize vector must be at least length(x) - 1.")

## x is a year by rep matix
ProbOccNextTime <- init
Expand Down Expand Up @@ -900,7 +907,7 @@ dDynOcc_vss <- nimbleFunction(
end = double(1),
log = double(0, default = 0)) {

if (length(probPersist) != dim(x)[1] - 1) stop("Length of probPersist vector does not match length of data.")
if (length(probPersist) < dim(x)[1] - 1) stop("Length of probPersist vector must be at least length(x) - 1.")

## x is a year by rep matix
ProbOccNextTime <- init
Expand Down Expand Up @@ -952,7 +959,7 @@ dDynOcc_svs <- nimbleFunction(
start = double(1),
end = double(1),
log = double(0, default = 0)) {
if (length(probColonize) != dim(x)[1] - 1) stop("Length of probColonize vector does not match length of data.")
if (length(probColonize) < dim(x)[1] - 1) stop("Length of probColonize vector must be at least length(x) - 1.")

## x is a year by rep matix
ProbOccNextTime <- init
Expand Down

0 comments on commit 4bdc509

Please sign in to comment.