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

Issue #500: Reduce messages in bias_quantile() #501

Merged
merged 2 commits into from
Nov 28, 2023
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
12 changes: 7 additions & 5 deletions R/metrics-quantile.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
reformatted[, eval(cols) := do.call(
interval_score,
list(observed = observed,
lower = lower,

Check warning on line 123 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=123,col=9,[indentation_linter] Indentation should be 6 spaces but is 9 spaces.
upper = upper,
interval_range = range,
weigh = weigh,
Expand All @@ -128,7 +128,7 @@
)
)]

if (!count_median_twice) {

Check warning on line 131 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=131,col=7,[if_not_else_linter] In a simple if/else statement, prefer `if (A) x else y` to the less-readable `if (!A) y else x`.
reformatted[, weight := ifelse(range == 0, 0.5, 1)]
} else {
reformatted[, weight := 1]
Expand All @@ -137,7 +137,7 @@
# summarise results by forecast_id
reformatted <- reformatted[
, lapply(.SD, weighted.mean, na.rm = na.rm, w = weight),
by = c("forecast_id"),

Check warning on line 140 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=140,col=10,[unnecessary_concatenation_linter] Unneeded concatenation of a constant. Remove the "c" call.
.SDcols = colnames(reformatted) %like% paste(cols, collapse = "|")
]

Expand Down Expand Up @@ -224,14 +224,14 @@
necessary_quantiles <- c((100 - range) / 2, 100 - (100 - range) / 2) / 100
if (!all(necessary_quantiles %in% quantile)) {
warning(
"To compute the coverage for a range of ", range, "%, the quantiles ",

Check warning on line 227 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=227,col=6,[indentation_linter] Hanging indent should be 12 spaces but is 6 spaces.
necessary_quantiles, " are required. Returning `NA`.")
return(NA)
}
r <- range
reformatted <- quantile_to_interval(observed, predicted, quantile)
reformatted <- reformatted[range %in% r]
reformatted[, coverage := ifelse(

Check warning on line 234 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=234,col=29,[redundant_ifelse_linter] Just use the logical condition (or its negation) directly instead of calling ifelse(x, TRUE, FALSE)
observed >= lower & observed <= upper, TRUE, FALSE
)]
return(reformatted$coverage)
Expand Down Expand Up @@ -291,7 +291,7 @@
#' )
#' quantile <- c(0.1, 0.25, 0.5, 0.75, 0.9)
#' interval_coverage_deviation_quantile(observed, predicted, quantile)
interval_coverage_deviation_quantile <- function(observed, predicted, quantile) {

Check warning on line 294 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=294,col=1,[object_length_linter] Variable and function names should not be longer than 30 characters.
assert_input_quantile(observed, predicted, quantile)

# transform available quantiles into central interval ranges
Expand All @@ -299,25 +299,25 @@

# check if all necessary quantiles are available
necessary_quantiles <- unique(c(
(100 - available_ranges) / 2,

Check warning on line 302 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=302,col=4,[indentation_linter] Hanging indent should be 34 spaces but is 4 spaces.
100 - (100 - available_ranges) / 2) / 100
)
if (!all(necessary_quantiles %in% quantile)) {
missing <- necessary_quantiles[!necessary_quantiles %in% quantile]
warning(
"To compute coverage deviation, all quantiles must form central ",

Check warning on line 308 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=308,col=6,[indentation_linter] Hanging indent should be 12 spaces but is 6 spaces.
"symmetric prediction intervals. Missing quantiles: ",
toString(missing), ". Returning `NA`.")
return(NA)
}

reformatted <- quantile_to_interval(observed, predicted, quantile)[range != 0]
reformatted[, coverage := ifelse(

Check warning on line 315 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=315,col=29,[redundant_ifelse_linter] Just use the logical condition (or its negation) directly instead of calling ifelse(x, TRUE, FALSE)
observed >= lower & observed <= upper, TRUE, FALSE
)]
reformatted[, coverage_deviation := coverage - range / 100]
out <- reformatted[, .(coverage_deviation = mean(coverage_deviation)),
by = c("forecast_id")]

Check warning on line 320 in R/metrics-quantile.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/metrics-quantile.R,line=320,col=27,[unnecessary_concatenation_linter] Unneeded concatenation of a constant. Remove the "c" call.
return(out$coverage_deviation)
}

Expand Down Expand Up @@ -390,8 +390,14 @@
if (is.null(dim(predicted))) {
dim(predicted) <- c(n, N)
}
if (!(0.5 %in% quantile)) {
message(
"Median not available, computing bias as mean of the two innermost ",
"quantiles in order to compute bias."
)
}
bias <- sapply(1:n, function(i) {
bias_quantile_single_vector(observed[i], predicted[i,], quantile, na.rm)
bias_quantile_single_vector(observed[i], predicted[i, ], quantile, na.rm)
})
return(bias)
}
Expand Down Expand Up @@ -437,10 +443,6 @@
median_prediction <- predicted[quantile == 0.5]
} else {
# if median is not available, compute as mean of two innermost quantile
message(
"Median not available, computing as mean of two innermost quantile",
" in order to compute bias."
)
median_prediction <-
0.5 * predicted[quantile == max(quantile[quantile < 0.5])] +
0.5 * predicted[quantile == min(quantile[quantile > 0.5])]
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-metrics-quantile.R
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,10 @@ test_that("bias_quantile(): quantiles must be unique", {
quantiles <- c(0.3, 0.5, 0.8, 0.9)
expect_silent(bias_quantile(observed = 3, predicted, quantiles))
})

test_that("bias_quantile only produces one message", {
expect_message(
bias_quantile(observed, predicted[, -3], quantile[-3]),
"Median not available, computing bias as mean of the two innermost quantiles in order to compute bias."
)
})
Loading