fishR-Core-Team / FSA

FSA (Fisheries Stock Assessment) package provides R functions to conduct typical introductory fisheries analyses.
https://fishr-core-team.github.io/FSA/
GNU General Public License v2.0
66 stars 22 forks source link

alkIndivAge() fails ungracefully if NA in length variable of length sample #88

Closed droglenc closed 2 years ago

droglenc commented 2 years ago

The problem

alkIndivAge() fails if any of the lengths in the length sample were not recorded. It makes sense that it cannot assign an age using the ALK if there is no length. However, the error is largely uninterpretable:

Error in `[<-.data.frame`(`*tmp*`, data$TMPLCAT == i, ca, value = c(4, : 
missing values are not allowed in subscripted assignments of data frames

Thus, we should perform a pre-check for this and if NAs exist return a useful error message that indicates the problem and suggests that the user filter out those individuals.


Demonstration of The Problem

The following code from the documentation for alkIndivAge() runs without error:

WR1 <- WR79
WR1$LCat <- lencat(WR1$len,w=5)
WR1.age <- subset(WR1, !is.na(age))
WR1.len <- subset(WR1, is.na(age))
raw <- xtabs(~LCat+age,data=WR1.age)
( WR1.key <- prop.table(raw, margin=1) )
alkIndivAge(WR1.key,age~len,data=WR1.len)

However, if the length sample is modified to include a fish without a measured length ...

WR1.len.problem <- rbind(WR1.len,c(ID=999,len=NA,age=NA,LCat=NA))

Then the last line above modified to use the modified length sample data frame will produce the error shown above.

alkIndivAge(WR1.key,age~len,data=WR1.len.problem)

Possible Fix

The following lines inserted above line 124 in alkIndivAge.R (above where the random seed may be set) may fix this issue.

If (any(is.na(data[,cl]))) STOP("Length variable contains 'NA's;\n  Please remove these fish from the length sample before using 'alkIndivAge()'.")
droglenc commented 2 years ago

Will be fixed in the next version (after v0.9.3)