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

fixed issue with reports over several strata #175

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 1.5.0-9004
* Fixed a bug with missing error detection when trying to report estimates over several strata (#173)
* Fixed a bug which would cause duplicate samplingUnitIds in the SelectionTable when running ComputePsuSamplingParameters with the option "adHocStoxBiotic" (#172)
* Fixed a bug which cause the menu for ExtendAnalyticalSamplingFrameCoverage to not show options for StratificationVariables
* Fixed a bug which caused AssignPSUSamplingParameters to report the wrong records as missing, when data is not provided for all sampled records
Expand Down
33 changes: 29 additions & 4 deletions R/StoxAnalyticalReportFunction.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#' Raise an error if reporting is attempted for estimates with several strata.
#' @noRd
checkAggregation <- function(AnalyticalPopulationEstimateData){
if (length(unique(AnalyticalPopulationEstimateData$StratificationVariables$Stratum))>1){
stop("Report function does not support several strata. Consider aggregating with 'AggregateAnalyticalEstimate'. Specify any desired grouping variables as PSU-domains.")
}
}

#' Redefines age variable as lowest age in age group
#' Adds plusgroup and puts NA group to 0.
#' Assumes zero covariance for unkown covariances.
Expand Down Expand Up @@ -126,7 +134,10 @@ ReportAnalyticalCatchAtAge <- function(AnalyticalPopulationEstimateData, PlusGro
AgeDomainVar = "IndividualAge"

checkMandatory(AnalyticalPopulationEstimateData, "AnalyticalPopulationEstimateData")
stopifnot(is.AnalyticalPopulationEstimateData(AnalyticalPopulationEstimateData))
if (!is.AnalyticalPopulationEstimateData(AnalyticalPopulationEstimateData)){
stop("Malformed 'AnalyticalPopulationEstimateData'")
}
checkAggregation(AnalyticalPopulationEstimateData)

if (!(AgeDomainVar %in% names(AnalyticalPopulationEstimateData$DomainVariables))){
stop(paste("Catch-at-age reporting, requires the StoxBiotic variable 'IndividualAge' to be among the domain variables of 'AnalyticalPopulationEstimateData'."))
Expand Down Expand Up @@ -200,7 +211,11 @@ ReportAnalyticalCatchAtAge <- function(AnalyticalPopulationEstimateData, PlusGro
ReportAnalyticalCatchAtLength <- function(AnalyticalPopulationEstimateData, LengthGroupVariable=character(), IntervalWidth=numeric(), Decimals=integer(), Unit=RstoxData::getUnitOptions("cardinality", conversionRange=c(1,1e12))){

checkMandatory(AnalyticalPopulationEstimateData, "AnalyticalPopulationEstimateData")
stopifnot(is.AnalyticalPopulationEstimateData(AnalyticalPopulationEstimateData))
if (!is.AnalyticalPopulationEstimateData(AnalyticalPopulationEstimateData)){
stop("Malformed 'AnalyticalPopulationEstimateData'")
}
checkAggregation(AnalyticalPopulationEstimateData)

checkMandatory(LengthGroupVariable, "LengthGroupVariable")
if (!(LengthGroupVariable %in% names(AnalyticalPopulationEstimateData$DomainVariables))){
stop(paste("Catch-at-length reporting, requires the StoxBiotic variable identified by 'LengthGroupVariable'", LengthGroupVariable," to be among the domain variables of 'AnalyticalPopulationEstimateData'."))
Expand Down Expand Up @@ -322,7 +337,12 @@ ReportAnalyticalWeightAtAge <- function(AnalyticalPopulationEstimateData, PlusGr
AgeDomainVar = "IndividualAge"
WeightVar = "IndividualRoundWeight"
checkMandatory(AnalyticalPopulationEstimateData, "AnalyticalPopulationEstimateData")
stopifnot(is.AnalyticalPopulationEstimateData(AnalyticalPopulationEstimateData))

if (!is.AnalyticalPopulationEstimateData(AnalyticalPopulationEstimateData)){
stop("Malformed 'AnalyticalPopulationEstimateData'")
}
checkAggregation(AnalyticalPopulationEstimateData)

if (!(AgeDomainVar %in% names(AnalyticalPopulationEstimateData$DomainVariables))){
stop(paste("Weight-at-age reporting, requires the StoxBiotic variable '",AgeDomainVar,"' to be among the domain variables of 'AnalyticalPopulationEstimateData'."))
}
Expand Down Expand Up @@ -379,7 +399,12 @@ ReportAnalyticalLengthAtAge <- function(AnalyticalPopulationEstimateData, PlusGr
AgeDomainVar = "IndividualAge"
LengthVar = "IndividualTotalLength"
checkMandatory(AnalyticalPopulationEstimateData, "AnalyticalPopulationEstimateData")
stopifnot(is.AnalyticalPopulationEstimateData(AnalyticalPopulationEstimateData))

if (!is.AnalyticalPopulationEstimateData(AnalyticalPopulationEstimateData)){
stop("Malformed 'AnalyticalPopulationEstimateData'")
}
checkAggregation(AnalyticalPopulationEstimateData)

if (!(AgeDomainVar %in% names(AnalyticalPopulationEstimateData$DomainVariables))){
stop(paste("Length-at-age reporting, requires the StoxBiotic variable '",AgeDomainVar,"' to be among the domain variables of 'AnalyticalPopulationEstimateData'."))
}
Expand Down
24 changes: 24 additions & 0 deletions inst/tinytest/test-StoxAnalyticalReportFunction.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,27 @@ cal <- RstoxFDA:::ReportAnalyticalCatchAtLength(popEst, LengthGroupVariable = "L
expect_true(abs(sum(cal$NbyLength$CatchAtLength) - sum(caaReport$NbyAge$CatchAtAge)) / sum(cal$NbyLength$CatchAtLength) < 1e-2)
expect_true("Gear" %in% names(cal$NbyLength))
expect_true("Gear" %in% cal$GroupingVariables$GroupingVariables)



#
# check reports for estimates with several strata
#
stationDesign <- RstoxFDA::CatchLotterySamplingExample
stationDesign$StratificationVariables <- rbind(stationDesign$StratificationVariables, data.table::data.table(Stratum="S2", CountryVessel="OUT"))
stationDesign$SampleTable <- rbind(stationDesign$SampleTable, data.table::data.table(Stratum="S2", N=100, n=2, SelectionMethod="Poisson", FrameDescription="OUT"))
stationDesign$SampleTable$n[1] <- stationDesign$SampleTable$n[1]-2
stationDesign$SelectionTable$Stratum[stationDesign$SelectionTable$SamplingUnitId %in% c("38401","38433")] <- "S2"

ex <- RstoxFDA::CatchLotteryExample
ex$SpeciesCategory$SpeciesCategory <- "061104"
stationDesign <- RstoxFDA::AssignPSUSamplingParameters(stationDesign, ex, "serialnumber", "Haul", "MissingAtRandom")
srs <- RstoxFDA:::ComputeIndividualSamplingParameters(ex, "SRS", c("IndividualAge"))
#add stratificaion variable to domain definition
psuEst <- RstoxFDA:::AnalyticalPSUEstimate(ex, srs, c("IndividualRoundWeight"), c("IndividualAge", "SpeciesCategory"))
popEst <- RstoxFDA:::AnalyticalPopulationEstimate(stationDesign, psuEst)

expect_error(RstoxFDA:::ReportAnalyticalCatchAtAge(popEst, Decimals = 1, IntervalWidth = .9, Unit = "individuals"), "Report function does not support several strata. Consider aggregating with 'AggregateAnalyticalEstimate'. Specify any desired grouping variables as PSU-domains.")
agg <- RstoxFDA::AggregateAnalyticalEstimate(popEst, AggregateStratumName = "agg")
aggRep <- RstoxFDA:::ReportAnalyticalCatchAtAge(agg, Decimals = 1, IntervalWidth = .9, Unit = "individuals")
expect_true("SpeciesCategory" %in% names(aggRep$NbyAge))
Loading