ssefcn.r

text/plain — 1.2 KB

File contents

# ssefcn.r
# statistical stock assessment
# "few" true ages, a=1,...,A and a plus group
#  Data:  indices for each age group
#         and catch (yield)
#         w: vector of length A+1 - weight at age 
#  Parameters:
#   Recr, Fmort,
#   selpat: vector of length A+1 - selection pattern
#   Ninit : vector of length A+1 - initial pop size
#   M,q
# 
#  Dimensions:  A=#true ages
#               numyears= # years
ssefcn<-function(parameters,printit=F){
  Fmort<-exp(parameters[1:numyears])
  Recr<-exp(parameters[(numyears+1):(2*numyears)])
  q    <-exp(parameters[(2*numyears+1):(2*numyears+A+1)])
  #Rtemp<-Recr[1]
  #Ninit<-Rtemp*exp(-(0:A)*M)  # First start-of-year stock size - equil.

  prediction<-predict(Fmort,Recr)
  Nmat<-prediction$Nmat
  Yhat<-prediction$Yhat
  Ihat<-t(t(Nmat)*q)
  Ihat<-Ihat[,1:A]
  qoldest<-q[length(q)]
  Uhat<-qoldest*c(Nmat%*%w)
  #Uhat<-q[A+1]*w[A+1]*Nmat[,A+1]
  
  Ihat<-cbind(Ihat[1:numyears,],Uhat[1:numyears])
  SSEI<-sum((log(I)-log(Ihat))^2)
  SSEY<-sum((log(Y)-log(Yhat))^2)
  if(printit==1){
    print(round(c(SSEI=SSEI,SSEY=SSEY),2))
  }
  if(printit==2){
    print(round(c(SSEI=SSEI,SSEY=SSEY),2))
    print(round(Nmat))
    print(round(Ihat,2))
    #print(round(Uhat,2))
  }
  SSE<-SSEI+SSEY
  return(SSE)
}