-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloggerhead_fun.R
69 lines (53 loc) · 2.38 KB
/
loggerhead_fun.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
## Methods to accompany the loggerhead turtle workbook
createProjectionMatrix <- function (surv, fec, d = NULL) {
## Create a stage-based population projection matrix
## Args:
## surv: Stage-based survivorship estimates.
## fec: Stage-based fecundity estimates.
## d: Length of stages. If NULL the matrix reduces to an age-based projection matrix (Leslie matrix).
## Returns: Projection matrix A in which diagonal elements indicate the
## probability of remaining in a given stage; sub-diagonal elements indicate
## the probability of surviving and growing into the next stage; and the
## first row indicates the contribution to newborns from each stage.
dimA <- length(surv)
## expect fec to have same length
if (length(fec) != dimA)
stop("expecting equal length survival and fecundity estimates")
if (is.null(d)) {
d <- rep(1, dimA)
} else if (length(d) != dimA) {
stop("expecting equal length survival and stage length vectors")
}
if(any(surv==1)) {
warning("replacing survivorship values of 1 with 0.999 to avoid NaNs")
surv[surv==1] = 0.999
}
# Calculate P (survival transition) and G (growth transition) values
P <- ((1 - surv^(d-1))/(1 - surv^d))*surv # See Crouse 1987, Eq. (1)
G <- (surv^d*(1 - surv))/(1 - surv^d) # See Crouse 1987, Eq. (2)
# Create the projection matrix and set the diagonal to P.
A <- diag(P)
# Set the sub-diagonal to G (discarding the last value which is spurious because
# the last stage is absorbing).
diag(A[-1,-dimA]) <- G[1:(dimA-1)]
# Set the first row to the fecundity values, eliding the first entry which we assume
# corresponds to a non-reproductive stage.
A[1,2:dimA] <- fec[2:dimA]
return(A)
}
getMPM <- function (surv, fecund, stage.length = NULL) {
## Create a stage-based matrix population model
## Args:
## surv: Stage-based survivorship estimates.
## fecund: Stage-based fecundity estimates.
## stage.length: Length of stages.
## Returns: List containing the following MPM components:
## A: projection matrix
## eig: object of class eigen containing eigenvectors and eigenvalues for
## the matrix
## lambda: largest eigenvalue (coerced to real number)
A <- createProjectionMatrix(surv, fecund, stage.length)
eig <- eigen(A, symmetric = FALSE)
lambda <- Re(eig$values[1])
return(list(A = A, eig = eig, lambda = lambda))
}