cohortN

text/r-latex cohortN.r — 1.2 KB

File contents

# cohortN:  Cohort analysis based on assumed Noldest and Nlast
#           cnum:    Y*A matrix of catch-in-numbers
#           Nlast:   length A vector of numbers in the last year
#           Noldest: length y+1 vector of numbers of age A+1
cohortN<-function(cnum,M,Nlast,Noldest){
  A<-ncol(cnum)
  Y<-nrow(cnum)
  Nmat<-c(Nlast,Noldest[Y+1])             # Set up the last line of the full N-matrix
  N1<-Nlast                               # Ages 1 through oldest
  for(y in Y:1){                          # Loop over years in the catch matrix
    Cline<-cnum[y,]                       # A line in the catch matrix
    Ntmp<-c(N1[2:A],Noldest[y+1])         # Ages 2 through oldest plus one
    N0<-(Ntmp*exp(M/2)+Cline)*exp(M/2)  # Start of year, ages 1:A
    N0<-c(N0,Noldest[y])                  # Stock numbers, ages 1:(A+1) at start of year
    Nmat<-rbind(N0,Nmat)
    N1<-N0                                # Prepare to repeat the loop
  }
  # Clean up the age- and year-names
  Ages<-as.numeric(dimnames(cnum)[[2]])
  Ages<-c(Ages,max(Ages)+1)
  Years<-as.numeric(dimnames(cnum)[[1]])
  Years<-c(Years,max(Years)+1)
  dimnames(Nmat)<-list(Years=Years,Ages=Ages)
  return(Nmat)
}