Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change depletion() to use data=() #111

Open
droglenc opened this issue Oct 22, 2024 · 0 comments
Open

Change depletion() to use data=() #111

droglenc opened this issue Oct 22, 2024 · 0 comments
Assignees

Comments

@droglenc
Copy link
Contributor

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 droglenc self-assigned this Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant