-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathanalyse_modobs.R
87 lines (75 loc) · 3.23 KB
/
analyse_modobs.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
analyse_modobs <- function( mod, obs,
plot.fil=NA,
plot.xlab="observed",
plot.ylab="modelled",
xlim=NA,
ylim=NA,
plot.title=NA,
plot.col=NA,
do.plot=TRUE,
plot.linmod=TRUE,
corner="bottomright",
lab.xpos=0.75,
lab.ypos=0.75,
... ){
library(Metrics)
library(hydroGOF)
library(LSD)
## get statistics
idxs <- which(!is.na(mod) & !is.na(obs))
numb <- sum(!is.na(obs))
rmse <- Metrics::rmse( obs[idxs], mod[idxs] )
prmse <- 100 * rmse / mean( obs, na.rm = TRUE )
linmod <- lm( mod ~ obs )
# rsq <- summary( linmod )$r.squared
rsq <- summary( linmod )$adj.r.squared
nse <- hydroGOF::NSE( mod, obs, na.rm=TRUE )
# pbias <- sum( mod[idxs] - obs[idxs] ) / sum( obs[idxs] )
pbias <- mean( (mod[idxs] - obs[idxs]) / obs[idxs] )
ptoohi <- sum( mod[idxs] > obs[idxs] ) / length( idxs )
# pbias <- hydroGOF::p( mod, obs, na.rm=TRUE )
## plot
if (do.plot){
if (!is.na(plot.fil)){
pdf( plot.fil, width=6, height=6 )
}
par( las=1, mar=c(4.5,4.5,3,2) )
if (is.na(xlim)) xlim <- c( min(range(mod, na.rm=TRUE)[1], range(obs, na.rm=TRUE)[1]), max(range(mod, na.rm=TRUE)[2], range(obs, na.rm=TRUE)[2]) )
if (is.na(ylim)) ylim <- xlim
heatscatter(
obs,
mod,
main="",
xlim=xlim,
ylim=ylim,
xlab=plot.xlab,
ylab=plot.ylab,
...
)
abline( c(0,0), c(1,1), col="red" )
if (plot.linmod) abline( linmod, col="red", lty=2 )
# mtext( paste( "RMSE =", format( rmse, digits = 3 ) ), side=3, line=0, cex=1.0, adj=0.0 )
# mtext( bquote( R^2 == .(format( rsq, digits = 3) ) ), side=3, line=1, cex=1.0, adj=0.0 )
# mtext( paste( "NSE =", format( nse, digits = 3 ) ), side=3, line=2, cex=1.0, adj=0.0 )
# mtext( paste( "N =", format( sum(!is.na(obs)), digits = 1 ) ), side=3, line=0, cex=1.0, adj=1.0 )
if (corner=="bottomright"){
x0 <- lab.xpos*xlim[2]
y0 <- seq(1, 5, 1) * (ylim[2]-ylim[1]) * 0.06
} else if (corner=="topleft"){
x0 <- 0.05*xlim[2]
y0 <- lab.ypos*(ylim[2]-ylim[1])+ylim[1]
}
text( x0, y0[1], paste( "bias =", format( pbias, digits = 3 ), "%" ), adj=0.0, cex=0.8 )
text( x0, y0[2], paste( "RMSE =", format( rmse, digits = 2 ), " (", format( prmse, digits = 2 ), "%)", sep="" ), adj=0.0, cex=0.8 )
text( x0, y0[3], bquote( italic(R)^2 == .(format( rsq, digits = 2) ) ), adj=0.0, cex=0.8 )
text( x0, y0[4], paste( "NSE =", format( nse, digits = 2 ) ), adj=0.0, cex=0.8 )
text( x0, y0[5], paste( "N =", format( numb, digits = 1 ) ), adj=0.0, cex=0.8 )
title( plot.title, cex.main=0.9, font=1 )
if (!is.na(plot.fil)){
dev.off()
}
}
## return statistics
out <- list( rmse=rmse, linmod=linmod, rsq=rsq, nse=nse, prmse=prmse, pbias=pbias, ptoohi=ptoohi, N=numb )
return( out )
}