-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathd.R
executable file
·143 lines (107 loc) · 5.78 KB
/
d.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File d.R
# Part of the hydroGOF R package, https://github.com/hzambran/hydroGOF ;
# https://cran.r-project.org/package=hydroGOF
# http://www.rforge.net/hydroGOF/
# Copyright 2008-2024 Mauricio Zambrano-Bigiarini
# Distributed under GPL 2 or later
########################################
# 'IoA': Index of Agreement #
########################################
# December 18th, 2008; 06-Sep-09 #
# 28-Feb-2016 #
# Updates: 20-Jul-2022 ; 29-Jul-2022 #
########################################
# 1) Willmott, C.J. 1981. On the validation of models. Physical Geography, 2, 184-194
# 2) Willmott, C. J. (1984). "On the evaluation of model performance in physical geography." Spatial Statistics and Models, G. L. Gaile and C. J. Willmott, eds., 443-460.
# 3) Legates, D. R., and G. J. McCabe Jr. (1999), Evaluating the Use of "Goodness-of-Fit" Measures in Hydrologic and Hydroclimatic Model Validation, Water Resour. Res., 35(1), 233-241.
# Index of Agreement (Willmott et al., 1984) range from 0.0 to 1.0
# and the closer to 1 the better the performance of the model
# 'obs' : numeric 'data.frame', 'matrix' or 'vector' with observed values
# 'sim' : numeric 'data.frame', 'matrix' or 'vector' with simulated values
# 'Result': Index of Agreement between 'sim' and 'obs'
d <-function(sim, obs, ...) UseMethod("d")
d.default <- function(sim, obs, na.rm=TRUE,
fun=NULL, ...,
epsilon.type=c("none", "Pushpalatha2012", "otherFactor", "otherValue"),
epsilon.value=NA){
if ( is.na(match(class(sim), c("integer", "numeric", "ts", "zoo"))) |
is.na(match(class(obs), c("integer", "numeric", "ts", "zoo")))
) stop("Invalid argument type: 'sim' & 'obs' have to be of class: c('integer', 'numeric', 'ts', 'zoo')")
epsilon.type <- match.arg(epsilon.type)
# the next two lines are required for avoiding an strange behaviour
# of the difference function when sim and obs are time series.
if ( !is.na(match(class(sim), c("ts", "zoo"))) ) sim <- as.numeric(sim)
if ( !is.na(match(class(obs), c("ts", "zoo"))) ) obs <- as.numeric(obs)
# index of those elements that are present both in 'x' and 'y' (NON- NA values)
vi <- valindex(sim, obs)
if (length(vi) > 0) {
# Filtering 'obs' and 'sim', selecting only those pairs of elements
# that are present both in 'x' and 'y' (NON- NA values)
obs <- obs[vi]
sim <- sim[vi]
if (!is.null(fun)) {
fun1 <- match.fun(fun)
new <- preproc(sim=sim, obs=obs, fun=fun1, ...,
epsilon.type=epsilon.type, epsilon.value=epsilon.value)
sim <- new[["sim"]]
obs <- new[["obs"]]
} # IF end
# Mean of the observed values
Om <- mean(obs, na.rm=na.rm)
denominator <- sum( ( abs(sim - Om) + abs(obs - Om) )^2 )
if ( (denominator != 0) & (!is.na(denominator)) ) {
d <- 1 - ( sum( (obs - sim)^2 ) / denominator )
} else {
d <- NA
warning("'sum((abs(sim-Om)+abs(obs-Om))^2)=0', it is not possible to compute 'IoA'")
}
} else {
d <- NA
warning("There are no pairs of 'sim' and 'obs' without missing values !")
} # ELSE end
return(d)
} # 'd.default' end
d.matrix <- function(sim, obs, na.rm=TRUE,
fun=NULL, ...,
epsilon.type=c("none", "Pushpalatha2012", "otherFactor", "otherValue"),
epsilon.value=NA){
# Checking that 'sim' and 'obs' have the same dimensions
if ( all.equal(dim(sim), dim(obs)) != TRUE )
stop( paste("Invalid argument: dim(sim) != dim(obs) ( [",
paste(dim(sim), collapse=" "), "] != [",
paste(dim(obs), collapse=" "), "] )", sep="") )
d <- rep(NA, ncol(obs))
d <- sapply(1:ncol(obs), function(i,x,y) {
d[i] <- d.default( x[,i], y[,i], na.rm=na.rm, fun=fun, ...,
epsilon.type=epsilon.type, epsilon.value=epsilon.value)
}, x=sim, y=obs)
names(d) <- colnames(obs)
return(d)
} # 'd.matrix' end
d.data.frame <- function(sim, obs, na.rm=TRUE,
fun=NULL, ...,
epsilon.type=c("none", "Pushpalatha2012", "otherFactor", "otherValue"),
epsilon.value=NA){
sim <- as.matrix(sim)
obs <- as.matrix(obs)
d.matrix(sim, obs, na.rm=na.rm, fun=fun, ...,
epsilon.type=epsilon.type, epsilon.value=epsilon.value)
} # 'd.data.frame' end
################################################################################
# Author: Mauricio Zambrano-Bigiarini #
################################################################################
# Started: 22-Mar-2013 #
# Updates: 20-Jul-2022 ; 29-Jul-2022 #
################################################################################
d.zoo <- function(sim, obs, na.rm=TRUE,
fun=NULL, ...,
epsilon.type=c("none", "Pushpalatha2012", "otherFactor", "otherValue"),
epsilon.value=NA){
sim <- zoo::coredata(sim)
if (is.zoo(obs)) obs <- zoo::coredata(obs)
if (is.matrix(sim) | is.data.frame(sim)) {
d.matrix(sim, obs, na.rm=na.rm, fun=fun, ...,
epsilon.type=epsilon.type, epsilon.value=epsilon.value)
} else NextMethod(sim, obs, na.rm=na.rm, fun=fun, ...,
epsilon.type=epsilon.type, epsilon.value=epsilon.value)
} # 'd.zoo' end