-
Notifications
You must be signed in to change notification settings - Fork 314
Take over PR #3888: Add unit tests for benchmarking metrics and document metric_R2 #4041
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
Open
ayushman1210
wants to merge
10
commits into
PecanProject:develop
Choose a base branch
from
ayushman1210:pr_3888
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−18
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2559a3a
Add unit tests for benchmarking metrics and document metric_R2 fallback
tanmaydimriGSOC 08e9e7b
Update metric_R2.Rd to match roxygen documentation
tanmaydimriGSOC ce4d354
fix: address review comments on metric_R2 and test logic
ayushman1210 a2df664
Merge branch 'develop' into pr_3888
ayushman1210 2bc3d33
Merge branch 'develop' into pr_3888
ayushman1210 8e61801
Merge branch 'develop' into pr_3888
dlebauer 122831f
Update modules/benchmark/R/metric_R2.R
ayushman1210 4b9a3e3
Update modules/benchmark/R/metric_R2.R
ayushman1210 3b719d3
updated the pr as per the akash suggestion
ayushman1210 0c9cbee
Merge branch 'develop' into pr_3888
ayushman1210 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,20 @@ | ||
| ##' @name metric_R2 | ||
| ##' @title Coefficient of Determination (R2) | ||
| ##' @title Squared Pearson Correlation (R2) | ||
| ##' @export | ||
| ##' @param metric_dat dataframe | ||
| ##' @param metric_dat dataframe with columns \code{model} and \code{obvs} | ||
| ##' @param ... ignored | ||
| ##' | ||
| ##' @details | ||
| ##' Computes R-squared using the correlation-based formula: | ||
| ##' \eqn{R^2 = \left(\frac{\sum(obs - \bar{obs})(mod - \bar{mod})} | ||
| ##' {\sqrt{\sum(obs - \bar{obs})^2} \cdot \sqrt{\sum(mod - \bar{mod})^2}}\right)^2} | ||
| ##' | ||
| ##' Note: Because this is a correlation-based R2, it is invariant to bias and slope. | ||
| ##' A model that is perfectly correlated but biased (e.g., model = obs + 100) will still | ||
| ##' score 1. This is distinct from variance explained or Nash-Sutcliffe Efficiency (NSE). | ||
| ##' | ||
| ##' @author Betsy Cowdery | ||
|
|
||
| metric_R2 <- function(metric_dat, ...) { | ||
| PEcAn.logger::logger.info("Metric: Coefficient of Determination (R2)") | ||
| numer <- sum((metric_dat$obvs - mean(metric_dat$obvs)) * (metric_dat$model - mean(metric_dat$model))) | ||
| denom <- sqrt(sum((metric_dat$obvs - mean(metric_dat$obvs)) ^ 2)) * sqrt(sum((metric_dat$model - mean(metric_dat$model)) ^ 2)) | ||
|
|
||
| out <- (numer / denom) ^ 2 | ||
|
|
||
| if(is.na(out)){ | ||
| fit <- stats::lm(metric_dat$model ~ metric_dat$obvs) | ||
| out <- summary(fit)$r.squared | ||
| } | ||
|
|
||
| return(out) | ||
|
|
||
| PEcAn.logger::logger.info("Metric: Squared Pearson Correlation (R2)") | ||
| stats::cor(metric_dat$model, metric_dat$obvs, use = "pairwise.complete.obs")^2 | ||
| } # metric_R2 | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| test_that("metric_RMSE returns 0 for perfect predictions", { | ||
| dat <- data.frame(model = c(1, 2, 3), obvs = c(1, 2, 3)) | ||
| expect_equal(metric_RMSE(dat), 0) | ||
| }) | ||
|
|
||
| test_that("metric_RMSE handles NA values", { | ||
| dat <- data.frame(model = c(1, NA, 3), obvs = c(1, 2, 3)) | ||
| expect_equal(metric_RMSE(dat), 0) | ||
| }) | ||
|
|
||
| test_that("metric_RMSE returns numeric", { | ||
| dat <- data.frame(model = c(2, 4), obvs = c(1, 3)) | ||
| expect_equal(metric_RMSE(dat), 1) | ||
| }) | ||
|
|
||
| test_that("metric_MAE returns 0 for perfect predictions", { | ||
| dat <- data.frame(model = c(1, 2, 3), obvs = c(1, 2, 3)) | ||
| expect_equal(metric_MAE(dat), 0) | ||
| }) | ||
|
|
||
| test_that("metric_MAE returns correct value", { | ||
| dat <- data.frame(model = c(3, 3), obvs = c(1, 1)) | ||
| expect_equal(metric_MAE(dat), 2) | ||
| }) | ||
|
|
||
| test_that("metric_cor returns 1 for perfect linear relationship", { | ||
| dat <- data.frame(model = c(1, 2, 3), obvs = c(1, 2, 3)) | ||
| expect_equal(metric_cor(dat), 1) | ||
| }) | ||
|
|
||
| test_that("metric_R2 returns 1 for perfect predictions", { | ||
| dat <- data.frame(model = c(1, 2, 3), obvs = c(1, 2, 3)) | ||
| expect_equal(metric_R2(dat), 1) | ||
| }) | ||
|
|
||
| test_that("metric_R2 returns NA for constant model output", { | ||
| dat <- data.frame(model = c(2, 2, 2), obvs = c(1, 2, 3)) | ||
| expect_warning( | ||
| result <- metric_R2(dat), | ||
| "the standard deviation is zero" | ||
| ) | ||
| expect_equal(result, NA_real_) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.