Skip to content

Commit 4e94b4c

Browse files
committed
feat: add RMSE
1 parent ff466b2 commit 4e94b4c

18 files changed

Lines changed: 323 additions & 17 deletions

File tree

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ S3method(nse, data.frame)
66
S3method(kge, data.frame)
77
S3method(kge2012, data.frame)
88
S3method(mse, data.frame)
9+
S3method(rmse, data.frame)
910
S3method(pbias, data.frame)
1011
S3method(press, data.frame)
1112
S3method(sfe, data.frame)
1213
export(nse)
1314
export(kge)
1415
export(kge2012)
1516
export(mse)
17+
export(rmse)
1618
export(pbias)
1719
export(press)
1820
export(sfe)
1921
export(nse_vec)
2022
export(kge_vec)
2123
export(kge2012_vec)
2224
export(mse_vec)
25+
export(rmse_vec)
2326
export(pbias_vec)
2427
export(press_vec)
2528
export(sfe_vec)

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# tidyhydro (development version)
22

3+
## New features
4+
- Added RMSE
5+
6+
## Miscellaneous
7+
8+
- Added structure. Functions are now groupped into two categories: regression and GOF
9+
310
# tidyhydro 0.1.1
411

512
## New features
@@ -13,6 +20,7 @@
1320
- Removed unicode characters α, β
1421

1522
## Miscellaneous
23+
1624
- Created website with vignettes (https://atsyplenkov.github.io/tidyhydro)
1725

1826
# tidyhydro 0.1.0

R/RcppExports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ kge_cpp <- function(obs, sim, na_rm = TRUE, version = "2012") {
55
.Call(`_tidyhydro_kge_cpp`, obs, sim, na_rm, version)
66
}
77

8-
mse_cpp <- function(truth, estimate, na_rm = TRUE) {
9-
.Call(`_tidyhydro_mse_cpp`, truth, estimate, na_rm)
8+
mse_cpp <- function(truth, estimate, na_rm = TRUE, sqrt = TRUE) {
9+
.Call(`_tidyhydro_mse_cpp`, truth, estimate, na_rm, sqrt)
1010
}
1111

1212
nse_cpp <- function(truth, estimate, performance = FALSE, na_rm = TRUE) {

R/mse.R

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,88 @@ mse_vec <- function(
9696
) {
9797
yardstick::check_numeric_metric(truth, estimate, case_weights = NULL)
9898

99-
mse_cpp(truth, estimate, na_rm = na_rm)
99+
mse_cpp(truth, estimate, na_rm = na_rm, sqrt = FALSE)
100+
}
101+
102+
#' Root Mean Squared Error (RMSE)
103+
#' @keywords gof
104+
#'
105+
#' @details
106+
#' The RMSE is estimated as follows:
107+
#' \deqn{
108+
#' RMSE = \sqrt{\frac{1}{n} \sum_{i=1}^{n}{(sim_i - obs_i)^2}}
109+
#' }
110+
#' where:
111+
#' \itemize{
112+
#' \item \eqn{sim} defines model simulations at time step \eqn{i}
113+
#' \item \eqn{obs} defines model observations at time step \eqn{i}
114+
#' }
115+
#'
116+
#' @family numeric metrics
117+
#' @family accuracy metrics
118+
#' @templateVar fn rmse
119+
#' @template return
120+
#'
121+
#' @param data A `data.frame` containing the columns specified by the `truth`
122+
#' and `estimate` arguments.
123+
#'
124+
#' @param truth The column identifier for the true results
125+
#' (that is `numeric`). This should be an unquoted column name although
126+
#' this argument is passed by expression and supports
127+
#' [quasiquotation][rlang::quasiquotation] (you can unquote column
128+
#' names). For `_vec()` functions, a `numeric` vector.
129+
#'
130+
#' @param estimate The column identifier for the predicted
131+
#' results (that is also `numeric`). As with `truth` this can be
132+
#' specified different ways but the primary method is to use an
133+
#' unquoted variable name. For `_vec()` functions, a `numeric` vector.
134+
#'
135+
#' @param na_rm A `logical` value indicating whether `NA`
136+
#' values should be stripped before the computation proceeds.
137+
#'
138+
#' @param ... Not currently used.
139+
#'
140+
#' @template examples-numeric
141+
#'
142+
#' @export
143+
#'
144+
rmse <- function(data, ...) {
145+
UseMethod("rmse")
146+
}
147+
148+
rmse <- yardstick::new_numeric_metric(
149+
rmse,
150+
direction = "minimize"
151+
)
152+
153+
#' @rdname rmse
154+
#' @export
155+
rmse.data.frame <- function(
156+
data,
157+
truth,
158+
estimate,
159+
na_rm = TRUE,
160+
...
161+
) {
162+
yardstick::numeric_metric_summarizer(
163+
name = "rmse",
164+
fn = rmse_vec,
165+
data = data,
166+
truth = !!rlang::enquo(truth),
167+
estimate = !!rlang::enquo(estimate),
168+
na_rm = na_rm
169+
)
170+
}
171+
172+
#' @rdname rmse
173+
#' @export
174+
rmse_vec <- function(
175+
truth,
176+
estimate,
177+
na_rm = TRUE,
178+
...
179+
) {
180+
yardstick::check_numeric_metric(truth, estimate, case_weights = NULL)
181+
182+
mse_cpp(truth, estimate, na_rm = na_rm, sqrt = TRUE)
100183
}

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)