-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f43af9e
commit 4f4a24b
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Matrix operators for population models | ||
# place in R 'Resources/R/' folder | ||
|
||
## maximum lambda function | ||
max.lambda <- function(x) Re((eigen(x)$values)[1]) ## where 'x' is a Leslie matrix | ||
|
||
## Maximum r function | ||
max.r <- function(x) log(Re((eigen(x)$values)[1])) ## where 'x' is a Leslie matrix | ||
|
||
## Stable stage distribution | ||
stable.stage.dist <- function(x) ((x %*% (Re((eigen(x)$vectors)[,1])))/(sum((x %*% (Re((eigen(x)$vectors)[,1]))))))[,1] | ||
|
||
## Generation length function | ||
R.val <- function(X,age.max) ## reproductive value (R0) where X = Leslie matrix; age.max = maximum age of females | ||
{ | ||
## define the transition matrix | ||
T <- X[1:age.max,1:age.max] | ||
T[1,1:(age.max)] <- 0 | ||
|
||
## define the fertility matrix | ||
F <- X[1:age.max,1:age.max] | ||
diag(F[2:age.max,1:(age.max-1)]) <- 0 | ||
|
||
## define the identity matrix | ||
I <- matrix(data<-0,nrow<-age.max,ncol<-age.max) | ||
diag(I) <- 1 | ||
|
||
## define the fundamental matrix | ||
library(MASS) | ||
N.fund <- ginv(I-T) | ||
|
||
## define the reproductive matrix | ||
R <- F %*% N.fund | ||
|
||
## define R0 (number of female offspring produced per female during lifetime) | ||
R0 <- Re((eigen(R)$values)[1]) | ||
|
||
## output | ||
print("number of female offspring produced per female during its lifetime") | ||
print("_________________________________________________________________") | ||
print(R0) | ||
|
||
} | ||
|
||
## Mean generation time function | ||
G.val <- function (X,age.max) ## where X is a Leslie Matrix | ||
{ | ||
G <- (log(R.val(X,age.max)))/(log(Re((eigen(X)$values)[1]))) | ||
print("mean generation time") | ||
print("____________________") | ||
print(G) | ||
} |