Skip to contents

It estimates the R2 for a continuous predicted-observed dataset.

Usage

R2(data = NULL, obs, pred, tidy = FALSE, na.rm = TRUE)

Arguments

data

(Optional) argument to call an existing data frame containing the data.

obs

Vector with observed values (numeric).

pred

Vector with predicted values (numeric).

tidy

Logical operator (TRUE/FALSE) to decide the type of return. TRUE returns a data.frame, FALSE returns a list; Default : FALSE.

na.rm

Logic argument to remove rows with missing values (NA). Default is na.rm = TRUE.

Value

an object of class numeric within a list (if tidy = FALSE) or within a data frame (if tidy = TRUE).

Details

The R2 is one of the most widely used metrics evaluating models performance. R2 is appropriate to estimate the strength of linear association between two variables. It is positively bounded to 1, but it may produce negative values. The closer to 1 the better linear association between predictions and observations. However, R2 presents a major flaw for prediction performance evaluation: it is not sensitive to lack of accuracy (additive or proportional bias). Thus, R2 only measures precision, but it does not account for accuracy of the predictions. For the formula and more details, see online-documentation

References

Yang et al. (2014). An evaluation of the statistical methods for testing the performance of crop models with observed data. Agric. Syst. 127, 81-89. doi:10.1016/j.agsy.2014.01.008

Examples

# \donttest{
set.seed(1)
X <- rnorm(n = 100, mean = 0, sd = 10)
Y <- X + rnorm(n=100, mean = 0, sd = 3)
R2(obs = X, pred = Y)
#> $R2
#> [1] 0.9070934
#> 
# }