Skip to content

Commit 6fa04c5

Browse files
committed
feat: geometric mean
1 parent d4c13df commit 6fa04c5

8 files changed

Lines changed: 152 additions & 981 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Imports:
1212
Rcpp (>= 1.0.12),
1313
rlang (>= 1.1.0),
1414
yardstick (>= 1.3.1),
15-
cli
15+
checkmate (>= 2.3.1)
1616
LinkingTo: Rcpp
1717
Roxygen: list(markdown = TRUE)
1818
RoxygenNote: 7.3.2
@@ -29,10 +29,9 @@ LazyData: true
2929
Config/Needs/website: bench, ggplot2, quarto, lubridate, dplyr
3030
Collate:
3131
'RcppExports.R'
32-
'import-standalone-types-check.R'
3332
'aaa-new.R'
33+
'central-tendency.R'
3434
'data.R'
35-
'import-standalone-obj-type.R'
3635
'kge.R'
3736
'mse.R'
3837
'nse.R'

NAMESPACE

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
useDynLib(tidyhydro, .registration = TRUE)
22
importFrom(Rcpp, evalCpp)
3-
import(rlang)
43
# exportPattern("^[[:alpha:]]+")
54

6-
S3method(nse, data.frame)
7-
S3method(kge, data.frame)
8-
S3method(kge2012, data.frame)
9-
S3method(mse, data.frame)
10-
S3method(rmse, data.frame)
11-
S3method(pbias, data.frame)
12-
S3method(press, data.frame)
13-
S3method(cv, data.frame)
14-
S3method(sfe, data.frame)
155
S3method(print, measure)
166
S3method(format, measure)
7+
8+
# general functions
179
export(nse)
1810
export(kge)
1911
export(kge2012)
@@ -23,6 +15,21 @@ export(pbias)
2315
export(press)
2416
export(sfe)
2517
export(cv)
18+
export(gm)
19+
20+
# data.frame methods
21+
S3method(nse, data.frame)
22+
S3method(kge, data.frame)
23+
S3method(kge2012, data.frame)
24+
S3method(mse, data.frame)
25+
S3method(rmse, data.frame)
26+
S3method(pbias, data.frame)
27+
S3method(press, data.frame)
28+
S3method(sfe, data.frame)
29+
S3method(cv, data.frame)
30+
S3method(gm, data.frame)
31+
32+
# vector functions
2633
export(nse_vec)
2734
export(kge_vec)
2835
export(kge2012_vec)
@@ -32,3 +39,4 @@ export(pbias_vec)
3239
export(press_vec)
3340
export(sfe_vec)
3441
export(cv_vec)
42+
export(gm_vec)

R/aaa-new.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' measure functions in `tidyhydro`: measures of central tendency, variability
99
#' and symmetry. They add a measure-specific class to `fn` and
1010
#' mimic a behaviour of [metric_set][yardstick::metric_set]. These features
11-
#' are used by [measure_set].
11+
#' are used by measure_set.
1212
#'
1313
#' See [Custom performance
1414
#' metrics](https://www.tidymodels.org/learn/develop/metrics/) for more
@@ -37,9 +37,8 @@ new_sym_measure <- function(fn) {
3737
new_measure(fn, class = "sym_measure")
3838
}
3939

40-
#' @include import-standalone-types-check.R
41-
new_measure <- function(fn, class = NULL, call = caller_env()) {
42-
check_function(fn, call = call)
40+
new_measure <- function(fn, class = NULL) {
41+
checkmate::assert_function(fn, args = "data")
4342

4443
class <- c(class, "measure", "function")
4544

@@ -70,7 +69,5 @@ format.measure <- function(x, ...) {
7069
"measure"
7170
)
7271

73-
cli::cli_format_method(
74-
cli::cli_text(c("A {measure_type}"))
75-
)
72+
cat(paste("A", measure_type))
7673
}

R/central-tendency.R

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#' Geometric Mean (GM)
2+
#' @keywords summary_stats
3+
#'
4+
#' @family descriptive statistics
5+
#' @templateVar fn gm
6+
#' @template return
7+
#'
8+
#' @param data A `data.frame` containing the columns specified by the `truth`
9+
#' and `estimate` arguments.
10+
#'
11+
#' @param truth The column identifier for the true results
12+
#' (that is `numeric`). This should be an unquoted column name although
13+
#' this argument is passed by expression and supports
14+
#' [quasiquotation][rlang::quasiquotation] (you can unquote column
15+
#' names). For `_vec()` functions, a `numeric` vector.
16+
#'
17+
#' @param na_rm A `logical` value indicating whether `NA`
18+
#' values should be stripped before the computation proceeds.
19+
#'
20+
#' @param ... Not currently used.
21+
#'
22+
#' @template examples-description
23+
#'
24+
#' @export
25+
#'
26+
27+
# TODO:
28+
# Add tests
29+
30+
gm <- function(data, ...) {
31+
UseMethod("gm")
32+
}
33+
34+
gm <- new_tendency_measure(gm)
35+
36+
#' @rdname gm
37+
#' @export
38+
gm.data.frame <- function(
39+
data,
40+
truth,
41+
na_rm = TRUE,
42+
...
43+
) {
44+
yardstick::numeric_metric_summarizer(
45+
name = "gm",
46+
fn = gm_vec,
47+
data = data,
48+
truth = !!rlang::enquo(truth),
49+
estimate = !!rlang::enquo(truth),
50+
na_rm = na_rm
51+
)
52+
}
53+
54+
#' @rdname gm
55+
#' @export
56+
gm_vec <- function(
57+
truth,
58+
na_rm = TRUE,
59+
...
60+
) {
61+
checkmate::assert_numeric(
62+
truth,
63+
lower = 1e-323
64+
)
65+
exp(mean(log(truth), na.rm = na_rm))
66+
}

0 commit comments

Comments
 (0)