Calculates common numerical model evaluation statistics, comparing modelled values (\(M_i\)) with observed values (\(O_i\)), with flexible conditioning.
Usage
modStats(
mydata,
mod = "mod",
obs = "obs",
statistic = c("n", "FAC2", "MB", "MGE", "NMB", "NMGE", "RMSE", "r", "COE", "IOA"),
type = NULL,
rank.name = NULL,
...
)Arguments
- mydata
A data frame.
- mod
Name of a variable in
mydatathat represents modelled values.- obs
Name of a variable in
mydatathat represents measured values.- statistic
The statistics to be calculated. See Details for a description of each. Matching is case-insensitive, e.g.,
"rmse"works as well as"RMSE". Statistics are always returned in the order listed in the default value.- type
typedetermines how the data are split, i.e., conditioned.NULL(the default) will produce statistics using the entire data.typecan be one of the built-in types as detailed incutData(), e.g.,"season","year","weekday"and so on. For example,type = "season"will produce four sets of statistics — one for each season.It is also possible to choose
typeas another variable in the data frame. If that variable is numeric, then the data will be split into four quantiles (if possible) and labelled accordingly. If type is an existing character or factor variable, then those categories/levels will be used directly. This offers great flexibility for understanding the variation of different variables and how they depend on one another.More than one type can be considered, e.g.,
type = c("season", "weekday")will produce statistics split by season and day of the week.- rank.name
Simple model ranking can be carried out if
rank.nameis supplied.rank.namewill generally refer to a column representing a model name, which is to be ranked. It is added totypeif it isn't already there, and models are ranked within each combination of any othertypes. The ranking is based on the COE performance, as that indicator is arguably the best single model performance indicator available, so"COE"is added tostatisticif needed.- ...
Passed to
cutData()for use withtype(e.g.,hemisphere), and tostats::cor.test()for therstatistic (method,alternative,exact,conf.levelandcontinuity).
Details
All statistics are based on the \(n\) complete pairs of mod and obs.
Where a statistic can't be calculated, e.g., if the observations are all the
same so that a denominator is zero, it is returned as NA.
n: the number of complete pairs of data.FAC2: the fraction of predictions within a factor of two of the observations, i.e., where \(0.5 \le M_i / O_i \le 2\).MB: the mean bias, $$MB = \frac{1}{n}\sum_{i=1}^{n}(M_i - O_i)$$MGE: the mean gross error, $$MGE = \frac{1}{n}\sum_{i=1}^{n}|M_i - O_i|$$NMB: the normalised mean bias, $$NMB = \frac{\sum_{i=1}^{n}(M_i - O_i)}{\sum_{i=1}^{n}O_i}$$NMGE: the normalised mean gross error, $$NMGE = \frac{\sum_{i=1}^{n}|M_i - O_i|}{\sum_{i=1}^{n}O_i}$$RMSE: the root mean squared error, $$RMSE = \sqrt{\frac{1}{n}\sum_{i=1}^{n}(M_i - O_i)^2}$$r: the Pearson correlation coefficient. Other methods can be used by passingmethod(e.g.,method = "spearman") through.... Also returned is the P value of the correlation coefficient,P, which may present as0for very low values. Needs at least 3 complete pairs.COE: the Coefficient of Efficiency based on Legates and McCabe (1999, 2012), $$COE = 1 - \frac{\sum_{i=1}^{n}|M_i - O_i|}{\sum_{i=1}^{n}|O_i - \bar{O}|}$$There have been many suggestions for measuring model performance over the years, but the COE is a simple formulation which is easy to interpret. A perfect model has a COE = 1. As noted by Legates and McCabe, although the COE has no lower bound, a value of COE = 0.0 has a fundamental meaning. It implies that the model is no more able to predict the observed values than does the observed mean. Therefore, since the model can explain no more of the variation in the observed values than can the observed mean, such a model can have no predictive advantage. For negative values of COE, the model is less effective than the observed mean in predicting the variation in the observations.
IOA: the Index of Agreement based on Willmott et al. (2011), which spans between -1 and +1 with values approaching +1 representing better model performance, $$IOA = \left\{ \begin{array}{ll} 1 - \frac{\sum_{i=1}^{n}|M_i - O_i|}{2\sum_{i=1}^{n}|O_i - \bar{O}|}, & when \sum_{i=1}^{n}|M_i - O_i| \le 2\sum_{i=1}^{n}|O_i - \bar{O}| \\ \frac{2\sum_{i=1}^{n}|O_i - \bar{O}|}{\sum_{i=1}^{n}|M_i - O_i|} - 1, & otherwise \end{array} \right.$$An IOA of 0.5, for example, indicates that the sum of the error-magnitudes is one half of the sum of the observed-deviation magnitudes. When IOA = 0.0, it signifies that the sum of the magnitudes of the errors and the sum of the observed-deviation magnitudes are equivalent. When IOA = -0.5, it indicates that the sum of the error-magnitudes is twice the sum of the perfect model-deviation and observed-deviation magnitudes. Values of IOA near -1.0 can mean that the model-estimated deviations about O are poor estimates of the observed deviations; but, they also can mean that there simply is little observed variability - so some caution is needed when the IOA approaches -1.
References
Legates DR, McCabe GJ. (1999). Evaluating the use of goodness-of-fit measures in hydrologic and hydroclimatic model validation. Water Resources Research 35(1): 233-241.
Legates DR, McCabe GJ. (2012). A refined index of model performance: a rejoinder, International Journal of Climatology.
Willmott, C.J., Robeson, S.M., Matsuura, K., 2011. A refined index of model performance. International Journal of Climatology.
See also
Other model evaluation functions:
TaylorDiagram(),
conditionalEval(),
conditionalQuantile()
Examples
## the example below is somewhat artificial --- assuming the observed
## values are given by NOx and the predicted values by NO2.
modStats(mydata, mod = "no2", obs = "nox")
#> # A tibble: 1 × 11
#> n FAC2 MB MGE NMB NMGE RMSE r P COE IOA
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 63095 0.170 -130. 130. -0.725 0.726 167. 0.787 0 -0.337 0.331
## evaluation stats by season
modStats(mydata, mod = "no2", obs = "nox", type = "season")
#> # A tibble: 4 × 12
#> season n FAC2 MB MGE NMB NMGE RMSE r P COE IOA
#> <ord> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 spring (… 17343 0.258 -108. 108. -0.685 0.685 140. 0.820 0 -0.250 0.375
#> 2 summer (… 14658 0.171 -116. 116. -0.705 0.705 145. 0.757 0 -0.420 0.290
#> 3 autumn (… 14775 0.0983 -154. 154. -0.753 0.753 192. 0.788 0 -0.437 0.281
#> 4 winter (… 16319 0.142 -143. 143. -0.749 0.751 185. 0.826 0 -0.321 0.340
## rank two "models" by COE, within each season
models <- rbind(
transform(mydata, model = "A", pred = nox * 1.1),
transform(mydata, model = "B", pred = nox * 0.5)
)
modStats(
models,
mod = "pred",
obs = "nox",
type = "season",
rank.name = "model"
)
#> # A tibble: 8 × 13
#> season model n FAC2 MB MGE NMB NMGE RMSE r P COE
#> <ord> <fct> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 spring… A 17344 1 15.7 15.7 0.100 0.100 19.0 1 0 0.817
#> 2 spring… B 17344 1 -78.5 78.5 -0.5 0.5 95.0 1 0 0.0874
#> 3 summer… A 14658 1 16.5 16.5 0.100 0.100 19.4 1 0 0.799
#> 4 summer… B 14658 1 -82.4 82.4 -0.5 0.5 96.8 1 0 -0.00641
#> 5 autumn… A 14789 1 20.5 20.5 0.100 0.100 24.4 1 0 0.809
#> 6 autumn… B 14789 1 -102. 102. -0.5 0.5 122. 1 0 0.0454
#> 7 winter… A 16319 1 19.1 19.1 0.100 0.100 23.4 1 0 0.824
#> 8 winter… B 16319 1 -95.5 95.5 -0.5 0.5 117. 1 0 0.120
#> # ℹ 1 more variable: IOA <dbl>
