forked from PecanProject/pecan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetric_Coverage.R
More file actions
34 lines (29 loc) · 1.25 KB
/
Copy pathmetric_Coverage.R
File metadata and controls
34 lines (29 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#' @name metric_Coverage
#' @title Prediction Interval Coverage
#' @export
#' @param dat dataframe with columns `model_q05` and `model_q95` (or `model_q025` and `model_q975`)
#' @param ... ignored
#' @return A numeric value representing the fraction of observations that fall within the prediction interval.
#' @details
#' Measures the fraction of observations that fall within the model's
#' stated prediction interval (defaults to 90% interval via `model_q05`/`model_q95`, or 95% via `model_q025`/`model_q975`).
metric_Coverage <- function(dat, ...) {
q_low <- NULL
q_high <- NULL
if (all(c("model_q05", "model_q95") %in% names(dat))) {
q_low <- dat$model_q05
q_high <- dat$model_q95
} else if (all(c("model_q025", "model_q975") %in% names(dat))) {
q_low <- dat$model_q025
q_high <- dat$model_q975
} else {
PEcAn.logger::logger.severe("Metric Coverage requires quantile columns ('model_q05'/'model_q95' or 'model_q025'/'model_q975') in the dataset.")
}
PEcAn.logger::logger.info("Metric: Prediction Interval Coverage")
valid <- !is.na(dat$obvs) & !is.na(q_low) & !is.na(q_high)
if (!any(valid)) {
return(NA_real_)
}
covered <- dat$obvs[valid] >= q_low[valid] & dat$obvs[valid] <= q_high[valid]
return(mean(covered))
}