Skip to content

Commit 6eef2b7

Browse files
committed
add cv
1 parent 6c7bc26 commit 6eef2b7

12 files changed

Lines changed: 175 additions & 0 deletions

File tree

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ S3method(mse, data.frame)
99
S3method(rmse, data.frame)
1010
S3method(pbias, data.frame)
1111
S3method(press, data.frame)
12+
S3method(cv, data.frame)
1213
S3method(sfe, data.frame)
1314
export(nse)
1415
export(kge)
@@ -18,6 +19,7 @@ export(rmse)
1819
export(pbias)
1920
export(press)
2021
export(sfe)
22+
export(cv)
2123
export(nse_vec)
2224
export(kge_vec)
2325
export(kge2012_vec)
@@ -26,3 +28,4 @@ export(rmse_vec)
2628
export(pbias_vec)
2729
export(press_vec)
2830
export(sfe_vec)
31+
export(cv_vec)

R/variability.R

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#' Coefficient of Variation (Cv)
2+
#' @keywords summary
3+
#'
4+
#' @family numeric metrics
5+
#' @family accuracy metrics
6+
#' @templateVar fn cv
7+
#' @template return
8+
#'
9+
#' @param data A `data.frame` containing the columns specified by the `truth`
10+
#' and `estimate` arguments.
11+
#'
12+
#' @param truth The column identifier for the true results
13+
#' (that is `numeric`). This should be an unquoted column name although
14+
#' this argument is passed by expression and supports
15+
#' [quasiquotation][rlang::quasiquotation] (you can unquote column
16+
#' names). For `_vec()` functions, a `numeric` vector.
17+
#'
18+
#' @param na_rm A `logical` value indicating whether `NA`
19+
#' values should be stripped before the computation proceeds.
20+
#'
21+
#' @param ... Not currently used.
22+
#'
23+
#' @template examples-description
24+
#'
25+
#' @export
26+
#'
27+
28+
cv <- function(data, ...) {
29+
UseMethod("cv")
30+
}
31+
32+
cv <- yardstick::new_numeric_metric(
33+
cv,
34+
direction = "minimize"
35+
)
36+
37+
#' @rdname cv
38+
#' @export
39+
cv.data.frame <- function(
40+
data,
41+
truth,
42+
na_rm = TRUE,
43+
...
44+
) {
45+
yardstick::numeric_metric_summarizer(
46+
name = "cv",
47+
fn = cv_vec,
48+
data = data,
49+
truth = !!rlang::enquo(truth),
50+
estimate = !!rlang::enquo(truth),
51+
na_rm = na_rm
52+
)
53+
}
54+
55+
#' @rdname cv
56+
#' @export
57+
cv_vec <- function(
58+
truth,
59+
na_rm = TRUE,
60+
...
61+
) {
62+
yardstick::check_numeric_metric(truth, truth, case_weights = NULL)
63+
64+
if (na_rm) {
65+
truth <- truth[!is.na(truth)]
66+
}
67+
68+
x0 <- mean(truth)
69+
k <- truth / x0
70+
71+
sqrt(sum((k - 1)^2) / (length(truth) - 1))
72+
}

man-roxygen/examples-description.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#' @examples
2+
#' library(tidyhydro)
3+
#' data(avacha)
4+
#'
5+
#' # Supply truth and predictions as bare column names
6+
#' <%=fn %>(avacha, obs)
7+
#'
8+
#' # Or as numeric vectors
9+
#' <%=fn %>_vec(avacha$obs)

man/cv.Rd

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/kge.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/kge2012.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/mse.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/nse.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/pbias.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/press.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)