-
Notifications
You must be signed in to change notification settings - Fork 25
Issue #474: Make default scoring rules functions rather than stored data sets #536
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
07b9426
Create functions for default scoring rules
nikosbosse d952fe5
Update documentation for new functions
nikosbosse 518353b
Switch to using functions instead of package data for default rules
nikosbosse 83e37f9
Delete package data with default scoring rules
nikosbosse 1bd191d
Update existing tests
nikosbosse 9e810b9
Make sure that input to `select_rules` is a list
nikosbosse 00c1e99
Add tests for default scoring rules
nikosbosse ea2d09f
Fix linting issues
nikosbosse d51a2fd
Update NEWS.md file
nikosbosse e78dfea
replace `\()` by `function()` so that code works with R versions <4
nikosbosse 0d96e56
Change "possibilities" to "rules" as function argument, require named…
nikosbosse 2253c6b
fix error checking for named input
nikosbosse 28213bd
fix failing test
nikosbosse 97cca59
Export `select_rules()` to users
nikosbosse 9240ba7
Change function default from `select = "all"` to `select = NULL`
nikosbosse 12c69b5
Update argument to `select = NULL`
nikosbosse b29223a
Update function documentation and pkgdown keywords
nikosbosse c71f1ba
Update function documentation
nikosbosse cf59af6
Update tests to reflect the change to `select = NULL` in `select_rule…
nikosbosse 890ab29
Update failing test
nikosbosse 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| #' @title Select Scoring Rules From A List of Possible Scoring Rules | ||
| #' @description Helper function to return only the scoring rules selected by | ||
| #' the user from a list of possible scoring rules. | ||
| #' @param rules A list of scoring rules. | ||
| #' @param select A character vector of scoring rules to select from the list. | ||
| #' If `select` is `NULL` (the default), all possible scoring rules are returned. | ||
| #' @param exclude A character vector of scoring rules to exclude from the list. | ||
| #' If `select` is not `NULL`, this argument is ignored. | ||
| #' @return A list of scoring rules. | ||
| #' @keywords metric | ||
| #' @importFrom checkmate assert_subset assert_list | ||
| #' @export | ||
| #' @examples | ||
| #' select_rules( | ||
| #' rules = rules_binary(), | ||
| #' select = "brier_score" | ||
| #' ) | ||
| #' select_rules( | ||
| #' rules = rules_binary(), | ||
| #' exclude = "log_score" | ||
| #' ) | ||
| select_rules <- function(rules, select = NULL, exclude = NULL) { | ||
| assert_character(x = c(select, exclude), null.ok = TRUE) | ||
| assert_list(rules, names = "named") | ||
| allowed <- names(rules) | ||
|
|
||
| if (is.null(select) && is.null(exclude)) { | ||
| return(rules) | ||
| } else if (is.null(select)) { | ||
| assert_subset(exclude, allowed) | ||
| select <- allowed[!allowed %in% exclude] | ||
| return(rules[select]) | ||
| } else { | ||
| assert_subset(select, allowed) | ||
| return(rules[select]) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| #' @title Scoring Rules for Binary Forecasts | ||
| #' @description Helper function that returns a named list of default | ||
| #' scoring rules suitable for binary forecasts. | ||
| #' | ||
| #' The default scoring rules are: | ||
| #' - "brier_score" = [brier_score()] | ||
| #' - "log_score" = [logs_binary()] | ||
| #' @inherit select_rules params return | ||
| #' @export | ||
| #' @keywords metric | ||
| #' @examples | ||
| #' rules_binary() | ||
| #' rules_binary(select = "brier_score") | ||
| #' rules_binary(exclude = "log_score") | ||
| rules_binary <- function(select = NULL, exclude = NULL) { | ||
| all <- list( | ||
| brier_score = brier_score, | ||
| log_score = logs_binary | ||
| ) | ||
| selected <- select_rules(all, select, exclude) | ||
| return(selected) | ||
| } | ||
|
|
||
seabbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #' @title Scoring Rules for Point Forecasts | ||
| #' @description Helper function that returns a named list of default | ||
| #' scoring rules suitable for point forecasts. | ||
| #' | ||
| #' The default scoring rules are: | ||
| #' - "ae_point" = [ae()][Metrics::ae()] | ||
| #' - "se_point" = [se()][Metrics::se()] | ||
| #' - "ape" = [ape()][Metrics::ape()] | ||
| #' @inherit select_rules params return | ||
| #' @export | ||
| #' @keywords metric | ||
| #' @examples | ||
| #' rules_point() | ||
| #' rules_point(select = "ape") | ||
| rules_point <- function(select = NULL, exclude = NULL) { | ||
| all <- list( | ||
| ae_point = Metrics::ae, | ||
| se_point = Metrics::se, | ||
| ape = Metrics::ape | ||
| ) | ||
| selected <- select_rules(all, select, exclude) | ||
| return(selected) | ||
| } | ||
|
|
||
|
|
||
| #' @title Scoring Rules for Sample-Based Forecasts | ||
| #' @description Helper function that returns a named list of default | ||
| #' scoring rules suitable for forecasts in a sample-based format | ||
| #' | ||
| #' The default scoring rules are: | ||
| #' - "mad" = [mad_sample()] | ||
| #' - "bias" = [bias_sample()] | ||
| #' - "dss" = [dss_sample()] | ||
| #' - "crps" = [crps_sample()] | ||
| #' - "log_score" = [logs_sample()] | ||
| #' - "mad" = [mad_sample()] | ||
| #' - "ae_median" = [ae_median_sample()] | ||
| #' - "se_mean" = [se_mean_sample()] | ||
| #' @inherit select_rules params return | ||
| #' @export | ||
| #' @keywords metric | ||
| #' @examples | ||
| #' rules_sample() | ||
| #' rules_sample(select = "mad") | ||
| rules_sample <- function(select = NULL, exclude = NULL) { | ||
| all <- list( | ||
| bias = bias_sample, | ||
| dss = dss_sample, | ||
| crps = crps_sample, | ||
| log_score = logs_sample, | ||
| mad = mad_sample, | ||
| ae_median = ae_median_sample, | ||
| se_mean = se_mean_sample | ||
| ) | ||
| selected <- select_rules(all, select, exclude) | ||
| return(selected) | ||
| } | ||
|
|
||
|
|
||
| #' @title Scoring Rules for Quantile-Based Forecasts | ||
| #' @description Helper function that returns a named list of default | ||
| #' scoring rules suitable for forecasts in a quantile-based format | ||
| #' | ||
| #' The default scoring rules are: | ||
| #' - "wis" = [wis] | ||
| #' - "overprediction" = [overprediction()] | ||
| #' - "underprediction" = [underprediction()] | ||
| #' - "dispersion" = [dispersion()] | ||
| #' - "bias" = [bias_quantile()] | ||
| #' - "coverage_50" = [interval_coverage_quantile()] | ||
| #' - "coverage_90" = function(...) \{ | ||
| #' run_safely(..., range = 90, fun = [interval_coverage_quantile]) | ||
| #' \} | ||
| #' - "coverage_deviation" = [interval_coverage_dev_quantile()], | ||
| #' - "ae_median" = [ae_median_quantile()] | ||
| #' | ||
| #' Note: The `coverage_90` scoring rule is created as a wrapper around | ||
| #' [interval_coverage_quantile()], making use of the function [run_safely()]. | ||
| #' This construct allows the function to deal with arbitrary arguments in `...`, | ||
| #' while making sure that only those that [interval_coverage_quantile()] can | ||
| #' accept get passed on to it. `range = 90` is set in the function definition, | ||
| #' as passing an argument `range = 90` to [score()] would mean it would also | ||
| #' get passed to `coverage_50`. | ||
| #' @inherit select_rules params return | ||
| #' @export | ||
| #' @keywords metric | ||
| #' @examples | ||
| #' rules_quantile() | ||
| #' rules_quantile(select = "wis") | ||
| rules_quantile <- function(select = NULL, exclude = NULL) { | ||
| all <- list( | ||
| wis = wis, | ||
| overprediction = overprediction, | ||
| underprediction = underprediction, | ||
| dispersion = dispersion, | ||
| bias = bias_quantile, | ||
| coverage_50 = interval_coverage_quantile, | ||
| coverage_90 = function(...) { | ||
| run_safely(..., range = 90, fun = interval_coverage_quantile) | ||
| }, | ||
| coverage_deviation = interval_coverage_dev_quantile, | ||
| ae_median = ae_median_quantile | ||
| ) | ||
| selected <- select_rules(all, select, exclude) | ||
| return(selected) | ||
| } | ||
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.