-
Notifications
You must be signed in to change notification settings - Fork 2
Outsource uncertainty #96
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
Open
shawndove
wants to merge
92
commits into
main
Choose a base branch
from
outsource_uncertainty
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 29 commits
Commits
Show all changes
92 commits
Select commit
Hold shift + click to select a range
a62e3ff
store raw cube data
shawndove b6df31f
calculate bootstrap CIs using dubicube and add them to calculated ind…
shawndove 895ef5a
add dubicube as dependency
shawndove c073d18
add documentation and indicator level CIs
shawndove 42f4e7f
export add_ci
shawndove 81b8146
remove ci calculations (offloaded to add_ci)
shawndove 3384d31
documentation update
shawndove 981154d
first commit
shawndove cdbfec6
version update
shawndove 9dfffdf
version update
shawndove be41572
add dubicube to remotes
shawndove 97dd488
fix dubicube github url
shawndove 9caef30
update CI calculation in vignette
shawndove a996cc8
remove leftover confidence interval code and adjust documentation
shawndove 03528ce
remove CI code artifacts
shawndove d3dcdd2
fix spelling mistake
shawndove dcbc575
documentation fix
shawndove 209696a
add missing null assignment
shawndove 1f6584c
fix documentation, add missing null assignments, and fix incorrectly …
shawndove 84395c3
fix roxygen2 code style issue
shawndove 2bbb77e
fix documentation typo
shawndove 283eb35
add separate grouping logic for species occurrences and species range
shawndove a168427
prevent warning during testing of single point geometry
shawndove ca4efd3
add missing parameter
shawndove 303505d
fix broken tests
shawndove 78faaad
remove invalid paramter from documentation
shawndove d1fde1e
increase hill diversity bootstrap defaults to 1000
shawndove b0fafc1
documentation update
shawndove b45e4d2
fix broken tests
shawndove 308f58e
feat(uncertainty): Implement add_ci with dubicube integration and add…
shawndove 4ed87e6
conductor(plan): Mark Phase 2 as COMPLETED and Phase 3 as IN PROGRESS
shawndove 846b9ec
fix(uncertainty): Fix full_join error in add_ci and include bootstrap…
shawndove 423c9c7
docs(vignette): Add dubicube exploration details and links
shawndove 556aca0
chore(metadata): Update documentation and project metadata for versio…
shawndove 3d289d2
fix(docs): Standardize roxygen2 tags and ignore files to fix building…
shawndove 9ea866a
conductor(plan): Update commit hashes for integration and documentation
shawndove 8e14023
fix(ci): Resolve R-CMD-check failures by fixing ignore file encoding …
shawndove aab7c33
test(utils): Fix expected type in breaks_pretty_int for NA input
shawndove 8566502
fix type mismatch
shawndove 68f1bd9
refactor(ci): Correctly categorize indicators for whole-cube vs group…
shawndove 488d144
add space
wlangera 88f7e3b
identify bootstrap method
wlangera 80d0704
prepare indicator bootstrapping with dubicube
wlangera 5bc0a3d
add transformation functions
wlangera 6dd8053
add function to namespace
wlangera 20ad4b4
add unit tests
wlangera 508bb68
fix bug
wlangera 1b92517
parse no_bias argument according to dubicube v0.11.0
wlangera 8a05b54
update documentation
wlangera 112f445
Merge pull request #103 from b-cubed-eu/update-dubicube-ward
shawndove e5974cc
reorder ci_type options, and update documentation
shawndove 49ddb1e
update news items to reflect merged changes
shawndove b0355f9
update after merged changes
shawndove d06f60f
update to reflect merged changes
shawndove 989bfb1
Merge branch 'main' into outsource_uncertainty
shawndove faba649
fix malformed function call
shawndove 6f8da1e
fix missing package issue for r-cmd-check
shawndove 712d6b8
fix missing colon
shawndove c203d1d
add another missing package that fell off CRAN to remotes
shawndove 1055299
fix missing comma
shawndove b234969
another attempt to fix missing dependency
shawndove 1885b01
another attempt to fix missing dependency
shawndove a9f63cd
another attempt to fix missing dependency
shawndove 5986ae4
another attempt to solve missing dependency
shawndove 71ed724
fix malformed function call
shawndove 35bcd4b
attempt r-cmd-check fix through workflow update
shawndove c11fc19
call dubicube as an extra package
shawndove 0ef0692
try again
shawndove 4011d19
fix accidental rename of bootstrap_results
shawndove 2c1c4e6
add ellipsis back to parameters, as it is used to transfer paramters …
shawndove 0eb019b
documentation update
shawndove 51bb93f
fix year to handle more type mismatches
shawndove 256a846
use all_of with select to fix warning
shawndove fec5999
add missing seed parameter
shawndove 2373f98
fix broken tests
shawndove 5a81648
only add seed to bootstrap_params if not NULL
shawndove 4591343
fix select line
shawndove 89ff894
documentation update
shawndove 001511b
fix parameter naming mismatch with dubicube
shawndove b2c893f
suppress 'extreme order statistics' warning due to low replicates
shawndove 2571e6f
fix missing seed parameter
shawndove 632d67c
set default seed and adjust logit function to avoid failure when even…
shawndove ba95cc7
reduce bootstraps to make faster
shawndove 1bb3537
fix: create composite key for multiple grouping variables
shawndove 2f930d2
fix: add validation for missing grouping columns
shawndove d1e6a50
fix: preserve raw data without indicator classes for bootstrapping
shawndove 53d7f1b
fix: update test mock data to include required grouping columns
shawndove d43ed2f
fix: update test expectations for new composite key behavior
shawndove cdca6e2
fix: apply indicator class to data for S3 method dispatch during boot…
shawndove 7417331
fix: ensure indicator class is first for S3 dispatch and add debug ou…
shawndove 0be520b
test: add S3 dispatch verification test and diagnostic script
shawndove 99929d6
chore: remove temporary diagnostic script
shawndove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| #' Add Confidence Intervals to an Indicator Object | ||
| #' | ||
| #' @description | ||
| #' This function calculates bootstrap confidence intervals for an existing | ||
| #' `indicator_ts` object. It supports both cube-level bootstrapping (resampling | ||
| #' occurrence records) and indicator-level bootstrapping (resampling | ||
| #' calculated values), allowing for advanced transformations during the | ||
| #' CI calculation process. | ||
| #' | ||
| #' @param indicator An object of class `indicator_ts` to which confidence | ||
| #' intervals should be added. | ||
| #' @param num_bootstrap (Optional) Number of bootstrap replicates to perform. | ||
| #' (Default: 1000) | ||
| #' @param bootstrap_level (Optional) Level at which to perform bootstrapping: | ||
| #' * `cube` (default): Bootstrapping is done by resampling the | ||
| #' occurrence records in the cube. This is mathematically more robust as it | ||
| #' captures the underlying sampling uncertainty. | ||
| #' * `indicator`: Bootstrapping is done by resampling indicator | ||
| #' values. This is faster for large cubes but less robust. | ||
| #' | ||
| #' @param ci_type (Optional) Type of bootstrap confidence intervals to | ||
| #' calculate. (Default: `"norm"`). Supported options are: | ||
| #' * `norm`: Normal approximation intervals. | ||
| #' * `basic`: Basic bootstrap intervals. | ||
| #' * `perc`: Percentile intervals. | ||
| #' * `bca`: Bias-corrected and accelerated intervals. | ||
| #' * `none`: No confidence intervals calculated. | ||
| #' | ||
| #' @param trans (Optional) A function for transforming the indicator values | ||
| #' before calculating confidence intervals (e.g., `log`). | ||
| #' (Default: identity function) | ||
| #' @param inv_trans (Optional) The inverse of the transformation function | ||
| #' `trans` (e.g., `exp`). Used to back-transform the intervals | ||
| #' to the original scale. (Default: identity function) | ||
| #' @param confidence_level (Optional) The confidence level for the calculated | ||
| #' intervals (e.g., 0.95 for 95% CIs). (Default: 0.95) | ||
| #' @param overwrite (Optional) Logical. If the indicator already contains | ||
| #' confidence intervals (`ll` and `ul` columns), should they | ||
| #' be replaced? (Default: TRUE) | ||
| #' @param ... Additional arguments passed to the underlying functions. | ||
wlangera marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
shawndove marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #' | ||
| #' @details | ||
| #' The function acts as a bridge to the `dubicube` package for statistical | ||
shawndove marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #' heavy lifting. For certain indicators (e.g., Hill numbers), confidence | ||
wlangera marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #' intervals cannot be added post-hoc as they are calculated internally by | ||
| #' the `iNext` package during the initial calculation. In such cases, | ||
| #' a warning is issued and the original object is returned. | ||
| #' | ||
| #' @return An updated object of class `indicator_ts` containing the | ||
| #' original data with the following additional columns: | ||
| #' * `ll`: Lower limit of the confidence interval. | ||
| #' * `ul`: Upper limit of the confidence interval. | ||
| #' * `est_boot`: The bootstrap estimate of the indicator value. | ||
| #' * `se_boot`: The bootstrap standard error. | ||
| #' \item `bias_boot`: The bootstrap estimate of bias. | ||
| #' \item `int_type`: The type of interval calculated (e.g., 'perc'). | ||
| #' \item `conf`: The confidence level used. | ||
| #' | ||
| #' @seealso [dubicube::bootstrap_cube()], [dubicube::calculate_bootstrap_ci()] | ||
| #' | ||
| #' @export | ||
| add_ci <- function(indicator, | ||
| num_bootstrap = 1000, | ||
| bootstrap_level = c("cube", | ||
| "indicator"), | ||
| ci_type = c("norm", | ||
| "basic", | ||
| "perc", | ||
| "bca", | ||
| "none"), | ||
| trans = function(t) t, | ||
| inv_trans = function(t) t, | ||
| confidence_level = 0.95, | ||
| overwrite = TRUE, | ||
| ...) { | ||
|
|
||
| # Check for correct object class | ||
| if (!inherits(indicator, "indicator_ts")) { | ||
| stop("indicator must be an indicator_ts object.") | ||
| } | ||
|
|
||
| ll <- ul <- year <- est_original <- NULL | ||
|
|
||
| # List of indicators for which bootstrapped confidence intervals should not | ||
| # be calculated | ||
| noci_list <- c("obs_richness", | ||
| "cum_richness", | ||
| "occ_turnover", | ||
| "tax_distinct", | ||
| "hill0", | ||
| "hill1", | ||
| "hill2") | ||
|
|
||
| # Match ci_type argument | ||
| ci_type <- match.arg(ci_type) | ||
| bootstrap_level <- match.arg(bootstrap_level) | ||
|
|
||
| # If indicator is in noci_list, return indicator without calculating CIs | ||
| if (indicator$div_type %in% noci_list) { | ||
| if (indicator$div_type %in% c("hill0", "hill1", "hill2")) { | ||
| warning( | ||
| paste0( | ||
| "Confidence intervals cannot calculated for ", | ||
| indicator$div_type, | ||
| " as they are handled by the iNext package when calculating | ||
| your indicator. Returning indicator without adding CIs." | ||
| ) | ||
| ) | ||
| } else | ||
| warning( | ||
| paste0( | ||
| "Cannot calculate sensible confidence intervals for ", | ||
| indicator$div_type, ". Returning indicator without CIs." | ||
| ) | ||
| ) | ||
| return(indicator) | ||
| } | ||
|
|
||
| # Extract data from indicator object | ||
| x <- indicator$data | ||
| raw_data <- indicator$raw_data | ||
|
|
||
| if(any(c("ll", "ul") %in% names(x)) & !overwrite) { | ||
| warning( | ||
| paste0( | ||
| "Indicator already contains confidence intervals. Returning indicator | ||
| without adding CIs. Use 'replace = TRUE' argument to recalculate CIs." | ||
| ) | ||
| ) | ||
| return(indicator) | ||
| } | ||
|
|
||
| # Remove existing confidence intervals if overwrite = TRUE | ||
| if (overwrite & | ||
| all(c("ll", "ul") %in% names(x))) { | ||
| x <- x %>% | ||
| dplyr::select(-ll, -ul) | ||
| } | ||
|
|
||
| # Calculate confidence intervals | ||
| if (bootstrap_level == "indicator") { | ||
|
|
||
| # Send data to calc_ci for indicator level bootstrapping | ||
| indicator <- calc_ci(raw_data, | ||
| indicator = indicator, | ||
| num_bootstrap = num_bootstrap, | ||
| ci_type = ci_type, | ||
| ...) | ||
|
|
||
| } else if (bootstrap_level == "cube") { | ||
|
|
||
| # Identify indicators that require species-level grouping | ||
| species_level_indicators <- c("spec_occ", "spec_range") | ||
|
|
||
| if (indicator$div_type %in% species_level_indicators) { | ||
| group_cols <- c("year", "taxonKey") | ||
| } else { | ||
| group_cols <- "year" | ||
| } | ||
|
|
||
| # Bootstrap cube data | ||
| bootstrap_results <- dubicube::bootstrap_cube( | ||
| data_cube = raw_data, | ||
| fun = calc_ts, | ||
| grouping_var = group_cols, | ||
| samples = num_bootstrap, | ||
| seed = 123, | ||
| progress = TRUE, | ||
| processed_cube = FALSE, | ||
| #method = "whole_cube", | ||
wlangera marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| # Calculate confidence intervals from bootstrap results | ||
| ci_df <- dubicube::calculate_bootstrap_ci( | ||
| bootstrap_samples_df = bootstrap_results, | ||
| grouping_var = group_cols, | ||
| type = ci_type, | ||
| h = trans, | ||
| hinv = inv_trans, | ||
| conf = confidence_level, | ||
| data_cube = raw_data, | ||
| fun = calc_ts | ||
| ) %>% | ||
| dplyr::select(-est_original) | ||
|
|
||
| # Join confidence intervals to indicator object | ||
| if (nrow(ci_df) > 0) { | ||
| # Convert negative values to zero as rarity cannot be less than zero | ||
| ci_df$ll <- ifelse(ci_df$ll > 0, ci_df$ll, 0) | ||
| # Join confidence intervals to indicator values by year | ||
| x <- x %>% | ||
| dplyr::full_join(ci_df, | ||
| by = group_cols) | ||
| indicator$data <- x | ||
| return(indicator) | ||
| } else { | ||
| warning( | ||
| paste0( | ||
| "Unable to calculate confidence intervals. There may be ", | ||
| "insufficient data." | ||
| ) | ||
| ) | ||
| } | ||
| } else { | ||
| stop("Invalid bootstrap_level. Choose 'cube' or 'indicator'.") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.