Skip to contents

Squared Pearson correlation is the square of correlation().

Usage

r2(obs, pred, na.rm = TRUE)

Arguments

obs

Numeric observation vector.

pred

Numeric prediction vector paired with obs.

na.rm

Logical; remove incomplete pairs?

Value

One numeric value.

Details

$$r^2= \frac{\left[\sum_{i=1}^{n}(obs_i-\bar{obs})(pred_i-\bar{pred})\right]^2} {\sum_{i=1}^{n}(obs_i-\bar{obs})^2\sum_{i=1}^{n}(pred_i-\bar{pred})^2}.$$

It ranges from zero to one and summarizes the strength, but not the sign, of linear association. It describes the dispersion of predictions and observations around their fitted linear relationship rather than their departure from the 1:1 line. Consequently, r2() is insensitive to additive bias and proportional scaling: a value of one can occur even when predictions are systematically biased or have a different scale from the observations. It should therefore not be interpreted as a general measure of predictive agreement or accuracy.

Do not confuse lowercase r2() with nse(), mec(), or uppercase R2(), which are model-efficiency statistics and are sensitive to departures from the line of equality. It returns NA with a warning when fewer than two valid pairs remain or either vector has zero variance.

References

Willmott, C. J. (1984). On the evaluation of model performance in physical geography. In G. L. Gaile and C. J. Willmott (Eds.), Spatial Statistics and Models (pp. 443-460). D. Reidel.

Legates, D. R. and McCabe, G. J. (1999). Evaluating the use of goodness-of-fit measures in hydrologic and hydroclimatic model validation. Water Resources Research, 35(1), 233-241. doi:10.1029/1998WR900018

Examples

r2(1:3, c(1, 3, 2))
#> [1] 0.25