Skip to content

Commit

Permalink
Fixed #64
Browse files Browse the repository at this point in the history
  • Loading branch information
droglenc committed Jan 15, 2021
1 parent 8368a70 commit 6deab7f
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 301 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Expand Up @@ -8,6 +8,7 @@
* Moved a bunch of plotting examples in the documentation to `tests\plottests\` to speed up testing. The `tests\plottests\` was added to `.Rbuildignore` .
* `hist.formula()`: Modified. Fixed bug with y-axes when `freq=FALSE` is used (fixes [#62](https://github.com/droglenc/FSA/issues/62); thanks to @carlostorrescubila).
* `fitPlot()`: Modified. Fixed bugs with handling models that used character rather than factor variables.
* `psdAdd()`: Modified. Fixed bug relate to species that were `NA` (fixes [#64](https://github.com/droglenc/FSA/issues/64); thanks to Dan Shoup). Added more tests and fixed some typos in the documentation.
* `psdPlot()`: Modified. Fixed bug with box around the plot when `add.psd=FALSE`. Added 5% expansion to top of y-axis so that bars did not run into the box.
* `residPlot()`: Modified. Fixed bugs with handling models that used character rather than factor variables.

Expand Down
70 changes: 42 additions & 28 deletions R/psdAdd.R
Expand Up @@ -12,7 +12,7 @@
#' @param verbose A logical that indicates whether detailed messages about species without Gabelhouse lengths or with no recorded values should be printed or not.
#' @param \dots Not used.
#'
#' @details This computes a vector that contains the Gabelhouse lengths specific to each species for all individuals in an entire data frame. The vector can be appended to an existing data.frame to create a variable that contains the Gabelhouse lengths for each individual. The Gabelhouse length value will be \code{NA} for each individual for which a Gabelhouse length definitions do not exist in \code{\link{PSDlit}}. Species names in the data.frame must be the same as those used in \code{\link{PSDlit}}. See the examples for one method for changing species names to something that this function will recognize.
#' @details This computes a vector that contains the Gabelhouse lengths specific to each species for all individuals in an entire data frame. The vector can be appended to an existing data.frame to create a variable that contains the Gabelhouse lengths for each individual. The Gabelhouse length value will be \code{NA} for each individual for which Gabelhouse length definitions do not exist in \code{\link{PSDlit}}. Species names in the data.frame must be the same as those used in \code{\link{PSDlit}}. See the examples for one method for changing species names to something that this function will recognize.
#'
#' Individuals shorter than \dQuote{stock} length will be listed as \code{substock} if \code{use.names=TRUE} or \code{0} if \code{use.names=FALSE}.
#'
Expand Down Expand Up @@ -101,40 +101,54 @@ psdAdd.default <- function(len,species,units=c("mm","cm","in"),use.names=TRUE,
use.names <- FALSE
}
## Prepare the PSD literature values data frame
# get is used to eliminate problem with rcmd check
PSDlit <- get(utils::data("PSDlit", envir = environment()), envir = environment())

## Create data.frame with length, species, rownumbers, and PSD values (blank)
data <- data.frame(len,species,rownums=seq_along(len),PSD=rep(NA,length(len)))
## initiate a blank new data frame with same columns as old data frame
ndata <- data[-c(seq_len(nrow(data))),]
## get list of species
# get() is used to eliminate problem with rcmd check
PSDlit <- get(utils::data("PSDlit",envir=environment()),envir=environment())
## Find species that have known Gabelhouse lengths
# get list of species in data
specs <- levels(factor(species))

## cycle through each species where PSD values are known
for (i in seq_along(specs)) {
## isolate the current species
tmpdf <- data[data[,2]==specs[i],]
## compute PSD
if (specs[i] %in% levels(PSDlit$species)) {
## put in additional lengths if they are provided
if (specs[i] %in% addSpec) tmpAddLens <- addLens[which(addSpec==specs[i])]
else tmpAddLens <- NULL
# get the Gabelhouse length categories
glhse <- psdVal(specs[i],units=units,addLens=tmpAddLens)
# computes the Gabelhouse length categories and adds to the data frame
if (all(is.na(tmpdf[,1]))) {
if (verbose) message("All values in 'len' were missing for ",specs[i])
tmpdf$PSD <- tmpdf[,1]
} else tmpdf$PSD <- lencat(tmpdf[,1],breaks=glhse,use.names=use.names,as.fact=FALSE)
} else if (verbose) message("No known Gabelhouse (PSD) lengths for ",specs[i])
GLHSspecs <- specs[specs %in% levels(PSDlit$species)]
## Create data.frames with species that are NA and w/o Gabelhouse lengths and
## one with Gabelhouse lengths. The loop below will then start with a
## the non-Gabelhouse species and sequentially add the Gabelhouse fish
# Create data.frame with length, species, rownumbers, and PSD values (blank)
# - rownumbers is needed to get back the original order
# - PSD will eventually have the Gabelhouse length categories
data <- data.frame(len,species,rownums=seq_along(len),PSD=rep(NA,length(len)))
# data.frame where species is NA and doesn't have Gabelhousee length
ndata <- data[is.na(data$species) | !data$species %in% GLHSspecs,]
if (verbose & nrow(ndata)>0)
message("No known Gabelhouse (PSD) lengths for: ",unique(ndata$species))
# data.frame where species have Gabelhouse lengths ... make sure no NAs
data <- data[data$species %in% GLHSspecs,]
data <- data[!is.na(data$species),]

## Cycle through each species where PSD values are known, add PSD categories
## and append to data.frame that contained species w/o Gabelhouse lengths
for (i in seq_along(GLHSspecs)) {
# isolate a data.frame with the current species
tmpdf <- data[data$species==GLHSspecs[i],]
# add Gabelhouse lengths ... put in additional lengths if they are provided
if (GLHSspecs[i] %in% addSpec)
tmpAddLens <- addLens[which(addSpec==GLHSspecs[i])]
else tmpAddLens <- NULL
# get the Gabelhouse length categories
glhse <- psdVal(GLHSspecs[i],units=units,addLens=tmpAddLens)
# computes the Gabelhouse length categories and adds to the data frame
if (all(is.na(tmpdf$len))) {
if (verbose) message("All values in 'len' were missing for ",GLHSspecs[i])
tmpdf$PSD <- tmpdf$len
} else tmpdf$PSD <- lencat(tmpdf$len,breaks=glhse,
use.names=use.names,as.fact=FALSE)
# bind current species to the new data frame being created
ndata <- rbind(ndata,tmpdf)
}
## reorder the data.frame to match original rows
ndata <- ndata[order(ndata$rownums),]
## factor the PSD variable if using category names
if (use.names) ndata$PSD <- factor(ndata$PSD,levels=c("substock","stock","quality","preferred","memorable","trophy"))
if (use.names)
ndata$PSD <- factor(ndata$PSD,
levels=c("substock","stock","quality",
"preferred","memorable","trophy"))
## return just the vector of PSD values
ndata$PSD
}
Expand Down
2 changes: 2 additions & 0 deletions docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Expand Up @@ -2,7 +2,7 @@ pandoc: 2.11.3.1
pkgdown: 1.6.1
pkgdown_sha: ~
articles: {}
last_built: 2021-01-11T18:42Z
last_built: 2021-01-15T12:26Z
urls:
reference: http://derekogle.com/FSA/reference
article: http://derekogle.com/FSA/articles
Expand Down
26 changes: 13 additions & 13 deletions docs/reference/bootCase.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6deab7f

Please sign in to comment.