function3

text/r-latex functions3.r — 3.1 KB

File contents

###########################
#
# functions.r - a collection of functions to assist with fitting to a length distribution
#
###########################
# 
###########################
#
# predle - a function to predict a length distribution from a set of parameters
#
###########################
predle<-function(pi,mu,sigma,lmin,lmax){
  if(length(sigma)!=length(mu))sigma<-c(sigma,rep(sigma[nsigma],length(mu)-nsigma))
  nl<-lmax-lmin+1
  if(length(pi)!=length(mu)){
    pi<-c(pi,1-sum(pi))
    pi<-abs(pi)                    # Make sure all are positive
    pi<-pi/sum(pi)                 # Readjust, just in case
  }
  fit<-rep(0,nl)                   # Compute the fitted values
  for(lgrp in lmin:lmax){
    fit[lgrp]<-sum(pi*(pnorm((lgrp+0.5-mu)/sigma)-pnorm((lgrp-0.5-mu)/sigma)))
  }
  return(fit)
}
###########################
#
# sseprop - a function to evaluate the fit of different vectors of proportions
#
###########################
sseprop<-function(pvec,mu,sigma,dat,lmin,lmax){         # The pvector is input - the mu and sigma are fixed
  pi<-c(pvec,1-sum(pvec))        # na-1 values - the last is computed
  pi<-abs(pi)                    # Make sure all are positive
  pi<-pi/sum(pi)                 # Readjust, just in case
  fit<-predle(pi,mu,sigma,lmin=lmin,lmax=lmax)       # Compute the fitted values
  sse<-1e6*sum((dat-fit)^2)
  return(sse)
}
###########################
#
# ssemu - a function to evaluate the fit of different mu vectors.
#
###########################
ssemu<-function(muvec,pi,sigma,dat,lmin,lmax){
  fit<-predle(pi,muvec,sigma,lmin=lmin,lmax=lmax)       # Compute the fitted values
  sse<-1e6*sum((dat-fit)^2)
  return(sse)
}
###########################
#
# ssesigma - a function to evaluate the fit of different sigma vectors.
#
###########################
ssesigma<-function(sigmavec,pi,mu,dat,lmin,lmax){
  sigma<-c(sigmavec,rep(sigmavec[nsigma],length(mu)-nsigma))
  fit<-predle(pi,mu,sigmavec,lmin=lmin,lmax=lmax)       # Compute the fitted values
  sse<-1e6*sum((dat-fit)^2)
  return(sse)
}

###########################
#
# ssevonB - a function to evaluate the SSE when estimating the parameters of a von Bertalanffy
#
###########################
ssevonB<-function(beta,agrps,sigma,dat,lmin,lmax){
  Linf<-exp(beta[1])
  k<-beta[2]
  muvec<-Linf*(1-exp(-k*(agrps)))
  fit<-predle(pi,muvec,sigma,lmin=lmin,lmax=lmax)       # Compute the fitted values
  sse<-sum((dat-fit)^2)
  return(sse)
}

###########################
#
# ssefull - a placeholder for a function to use when estimating all parameters in a length distribution
#           *** Not tested
###########################
ssefull<-function(b,dat,lmin,lmax){
  pvec<-b[1:(na-1)]
  mu<-b[na:(2*na-1)]
  sigma<-b[(2*na):(2*na+nsigma)]
  pvec<-ifelse(pvec<0,-pvec,pvec)
  pi<-c(pvec,1-sum(pvec))
  pi<-abs(pi)                             # In case something stupid happened in the minimizer
  pi<-pi/sum(pi )                         # Proportions - routinely making sure they add to 1
  fit<-predle(pi,mu,sigma,lmin=lmin,lmax=lmax)       # Compute the fitted values
  sse<-sum((dat-fit)^2)
  cat("SSE=",sse,"\n")
  return(sse)
}