-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathubRMSE.R
executable file
·152 lines (110 loc) · 5.91 KB
/
ubRMSE.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
144
145
146
147
148
149
150
151
152
# File ubRMSE.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 2024-2024 Mauricio Zambrano-Bigiarini
# Distributed under GPL 2 or later
################################################################################
# 'ubRMSE': Unbiased Root Mean Square Error #
################################################################################
# Author : Mauricio Zambrano-Bigiarini #
################################################################################
# Started: 18-Jan-2024 #
# Updates: #
################################################################################
# The optimal value of ubRMSE is 0
# ubRMSE vaies int he followign range:
#
# | sigma_sim - sigma_obs | <= ubRMSE <= sqrt( sigma_sim^2 + sigma_obs^2)
# where:
#
# -) sigma_sim: standard deviation of simulated values
# -) sigma_obs: standard deviation of observed values
# Ref:
# Entekhabi, D., Reichle, R. H., Koster, R. D., & Crow, W. T. (2010).
# Performance metrics for soil moisture retrievals and application requirements.
# Journal of Hydrometeorology, 11(3), 832-840. doi: 10.1175/2010JHM1223.1
# 'obs' : numeric 'data.frame', 'matrix' or 'vector' with observed values
# 'sim' : numeric 'data.frame', 'matrix' or 'vector' with simulated values
# 'Result': Root Mean Square Error between 'sim' and 'obs', in the same units of 'sim' and 'obs'
ubRMSE <-function(sim, obs, ...) UseMethod("ubRMSE")
ubRMSE.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')")
if ( length(obs) != length(sim) )
stop("Invalid argument: 'sim' & 'obs' doesn't have the same length !")
epsilon.type <- match.arg(epsilon.type)
# index of those elements that are present both in 'sim' and 'obs' (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
RMSE <- sqrt( mean( (sim - obs)^2, na.rm = na.rm) )
BIAS <- mean(sim - obs, na.rm = na.rm)
ubRMSE <- sqrt(RMSE^2 - BIAS^2)
} else {
ubRMSE <- NA
warning("There are no pairs of 'sim' and 'obs' without missing values !")
} # ELSE end
return(ubRMSE)
} # 'ubRMSE.default' end
ubRMSE.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="") )
ubRMSE <- rep(NA, ncol(obs))
ubRMSE <- sapply(1:ncol(obs), function(i,x,y) {
ubRMSE[i] <- rmse.default( x[,i], y[,i], na.rm=na.rm, fun=fun, ...,
epsilon.type=epsilon.type, epsilon.value=epsilon.value)
}, x=sim, y=obs )
names(ubRMSE) <- colnames(obs)
return(ubRMSE)
} # 'ubRMSE.matrix' end
ubRMSE.data.frame <- function(sim, obs, na.rm=TRUE,
fun=NULL, ...,
epsilon.type=c("none", "Pushpalatha2012", "otherFactor", "otherValue"),
epsilon.value=NA) {
# Checking 'epsilon.type'
epsilon.type <- match.arg(epsilon.type)
sim <- as.matrix(sim)
obs <- as.matrix(obs)
ubRMSE.matrix(sim, obs, na.rm=na.rm, fun=fun, ...,
epsilon.type=epsilon.type, epsilon.value=epsilon.value)
return(ubRMSE)
} # 'ubRMSE.data.frame' end
################################################################################
# Author: Mauricio Zambrano-Bigiarini #
################################################################################
# Started: 22-Mar-2013 #
# Updates: 18-Jan-2024 #
################################################################################
ubRMSE.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)) {
ubRMSE.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)
} # 'ubRMSE.zoo' end