FSA icon indicating copy to clipboard operation
FSA copied to clipboard

Change depletion() to use data=()

Open droglenc opened this issue 1 year ago • 0 comments

The current version of depletion() uses vectors of catch and effort as the first two arguments. While this may be useful if catch and effort were entered into separate stand-alone vectors it is cumbersome if catch and effort data are in a data.frame (see examples with SMBassLS). Alternatively, it may be more useful to have a formula version that uses data=. For backward compatibility, the current use of vectors should be maintained (I think this is the case with catchCurve(), so check that code for an example).

This stems from a question from Keane Flynn about wanting to do something like group_by() |> depletion() to efficiently perform the analysis for multiple groups of data. A current hacky work-around for that situation is below (probably could have used split() and lapply() here as well). Adding the formula notation with data= should make this easier.

## Dummy example data (grp="A" is SMBassLS example ... just FYI)
tmpdf <- data.frame(ct=c(131,69,99,78,56,76,49,42,63,47,
                         117,75,87,67,58,67,42),
                    ft=c(7,7,7,7,7,7,7,7,7,7,
                         5,7,5,5,4,6,5),
                    grp=as.factor(c("A","A","A","A","A","A","A","A","A","A",
                                    "B","B","B","B","B","B","B")))
str(tmpdf)

## Initialize a results data.frame
res <- data.frame("grp"=factor(),
                  "No"=double(),"No.se"=double(),
                  "q"=double(),"q.se"=double())

## Use a loop to process groups individually
for (i in levels(tmpdf$grp)) {
  tmp <- dplyr::filter(tmpdf,grp==i)
  tmpres <- FSA::depletion(tmp$ct,tmp$ft,method="Leslie")
  res <- dplyr::add_row(res,"grp"=i,
                        No=tmpres$est["No","Estimate"],
                        No.se=tmpres$est["No","Std. Err."],
                        q=tmpres$est["q","Estimate"],
                        q.se=tmpres$est["q","Std. Err."])
}

## See the result
res

droglenc avatar Oct 22 '24 13:10 droglenc