# [R] Age-Length key with kimura algorith

Alberto Murta amurta at ipimar.pt
Wed Aug 29 12:33:42 CEST 2007

```Hi Jorge

In fact I have been working with that method. I made a simple R function to
apply it, and I'm sending you the code below. If you have any questions about
it just send me a message.
Cheers

Alberto

On Tuesday 28 August 2007 8:44 pm, Jorge Cornejo Donoso wrote:
> Hi, I’m looking for information to implement the kimura method (kimura &
> Chikuni. 1987. Mixtures of empirical distributions: an interative
> application of the age length key) for calculation of fisheries age-length
> key.
>
> If someone have a manuals, methodology or examples about it I will be
> really gratefull if you could share it.
>
>
>
>
>
>
> Jorge Cornejo Donoso
>
> Biólogo Marino Mención Oceanografía y Calidad Ambiental
>
> Centro Trapananda, Universidad Austral de Chile
>
> Portales 73, Coyhaique, Chile
>
> Fono: +56(67)244520 fax: +56(67)239377
>
>
>
>
> 	[[alternative HTML version deleted]]

--
Alberto G. Murta
IPIMAR - National Institute of Fisheries and Marine Research
Avenida de Brasilia s/n; 1449-006 - Lisboa; Portugal
Tel: +351 213027000; Fax: +351 21 3015948; email: amurta at ipimar.pt
-------------- next part --------------
## Function to apply the method by Kimura and Chikuni (1987):
## Kimura, D.K. and Chikuni, S. (1987) Mixtures of empirical distributions:
##  an iterative application of the age-length key. Biometrics. 43, 23-35.
## 'freq.mat' is a matrix with the number of fish in each length (row) and age (column) class.
## This matrix can be obtained by simple random sampling or length-stratified random sampling.
## 'length.vec1' and 'length.vec2' are vectors with the number of fish in each length-class in population 1
##  and population 2, respectively.

iterativeALK <- function(freq.mat, length.vec1, length.vec2, stop.value=0.001){
if(length(length.vec1) != length(length.vec2) || length(length.vec2) != nrow(freq.mat) ||
c(length.vec1, length.vec2, apply(freq.mat, 1, sum)) <= 0){
stop("The number of length-classes must be the same in all data sets and all length-classes
must have been sampled.")
}
nij1.temp <- length.vec1 * freq.mat/apply(freq.mat, 1, sum)
denom <- apply(nij1.temp, 2, sum)
denom[denom==0] <- 1
ialk.temp <- sweep(nij1.temp, 2, denom,"/")
pj2.temp <- rep(1/length(age.class), length(age.class))
criterion <- 10
iterations <- 0
while(criterion > stop.value){
iterations <- iterations + 1
pj2.temp.old <- pj2.temp
denom <- apply(sweep(ialk.temp, 2, pj2.temp, "*"),1, sum)
denom[denom==0] <- 1
alk.temp <- sweep(ialk.temp, 2, pj2.temp, "*")/denom
nij2.temp <- length.vec2 * alk.temp
pj2.temp <- apply(nij2.temp, 2, sum)/sum(nij2.temp)
criterion <- sum(abs(pj2.temp - pj2.temp.old))
}
output <- list("Number of fish in each length and age-class in population 2" = nij2.temp,
"Number of iterations to convergence" = iterations)
return(output)
}
```