## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)

## ----installation1, eval=FALSE------------------------------------------------
# install.packages("hydroGOF")

## ----installation2, eval=FALSE------------------------------------------------
# if (!require(devtools)) install.packages("devtools")
# library(devtools)
# install_github("hzambran/hydroGOF")

## ----LoadingPkg---------------------------------------------------------------
library(hydroGOF)

## ----Example1-----------------------------------------------------------------
obs <- 1:10
sim <- 1:10
NSE(sim, obs)

obs <- 1:10
sim <- 2:11
NSE(sim, obs)

## ----Example2-----------------------------------------------------------------
obs <- 1:10 + 0.1
sim <- 1:10 +0.1
NSE(sim, obs)

obs <- 1:10 + 0.1
sim <- 2:11 + 0.1
NSE(sim, obs)

## ----Example3-Loading---------------------------------------------------------
data(EgaEnEstellaQts)
obs <- EgaEnEstellaQts

## ----Example3-1---------------------------------------------------------------
sim <- obs 

## ----Example3-2, fig.width=8, fig.height=5------------------------------------
ggof(sim, obs)

## ----Example3-3---------------------------------------------------------------
NSE(sim=sim, obs=obs)

## ----Example4-1, fig.width=8, fig.height=5------------------------------------
sim[1:1826] <- obs[1:1826] + rnorm(1826, mean=10)
ggof(sim, obs)

NSE(sim=sim, obs=obs)

## ----Example4-2, fig.width=8, fig.height=5------------------------------------
mNSE(sim=sim, obs=obs)                 # Modified NSE
rNSE(sim=sim, obs=obs)                 # Relative NSE
wNSE(sim=sim, obs=obs)                 # Weighted NSE
wsNSE(sim=sim, obs=obs)                # Weighted Seasonal NSE

KGE(sim=sim, obs=obs)                  # Kling-Gupta efficiency (KGE), 2009
KGE(sim=sim, obs=obs, method="2012")   # Kling-Gupta efficiency (KGE), 2012
KGE(sim=sim, obs=obs, method="2021")   # Kling-Gupta efficiency (KGE), 2021
KGElf(sim=sim, obs=obs)                # KGE for low flows
KGEnp(sim=sim, obs=obs)                # Non-parametric KGE
sKGE(sim=sim, obs=obs)                 # Split KGE
KGEkm(sim=sim, obs=obs)                # Knowable Moments KGE
JDKGE(sim=sim, obs=obs)                # Joint Divergence KGE

d(sim=sim, obs=obs)                    # Index of Agreement
dr(sim=sim, obs=obs)                   # Refined Index of Agreement
md(sim=sim, obs=obs)                   # Modified Index of Agreement
rd(sim=sim, obs=obs)                   # Relative Index of Agreement

VE(sim=sim, obs=obs)                   # Volumetric Efficiency
cp(sim=sim, obs=obs)                   # Coefficient of Persistence
APFB(sim=sim, obs=obs)                 # Annual Peak Flow Bias
HFB(sim=sim, obs=obs)                  # High Flow Bias
LME(sim=sim, obs=obs)                  # Liu-Mean Efficiency
LCE(sim=sim, obs=obs)                  # Lee and Choi Efficiency
PMR(sim=sim, obs=obs)                  # Proxy for Model Robustness

pbias(sim=sim, obs=obs)                # Percent bias (PBIAS)
pbiasfdc(sim=sim, obs=obs)             # PBIAS in the slope of the midsegment of the FDC

me(sim=sim, obs=obs)                   # Mean Error
mae(sim=sim, obs=obs)                  # Mean Absolute Error
mse(sim=sim, obs=obs)                  # Mean Squared Error 
rmse(sim=sim, obs=obs)                 # Root Mean Square Error (RMSE)
ubRMSE(sim=sim, obs=obs)               # Unbiased RMSE
nrmse(sim=sim, obs=obs, norm="sd")     # Normalised Root Mean Square Error
nrmse(sim=sim, obs=obs, norm="maxmin") # Normalised Root Mean Square Error
nrmse(sim=sim, obs=obs, norm="mean")   # Normalised Root Mean Square Error
nrmse(sim=sim, obs=obs, norm="IQR")    # Normalised Root Mean Square Error

rPearson(sim=sim, obs=obs)             # Pearson correlation coefficient
rSpearman(sim=sim, obs=obs)            # Spearman rank correlation coefficient
R2(sim=sim, obs=obs)                   # Coefficient of determination (R2)
br2(sim=sim, obs=obs)                  # R2 multiplied by the slope of the regression line

## ----Example5-1---------------------------------------------------------------
NSE(sim=sim, obs=obs, fun=log)

## ----Example5-2---------------------------------------------------------------
lsim <- log(sim)
lobs <- log(obs)
NSE(sim=lsim, obs=lobs)

## ----Example5-3, fig.width=8, fig.height=5------------------------------------
mNSE(sim=sim, obs=obs)                 # Modified NSE
rNSE(sim=sim, obs=obs)                 # Relative NSE
wNSE(sim=sim, obs=obs)                 # Weighted NSE
wsNSE(sim=sim, obs=obs)                # Weighted Seasonal NSE

KGE(sim=sim, obs=obs)                  # Kling-Gupta efficiency (KGE), 2009
KGE(sim=sim, obs=obs, method="2012")   # Kling-Gupta efficiency (KGE), 2012
KGE(sim=sim, obs=obs, method="2021")   # Kling-Gupta efficiency (KGE), 2021
KGElf(sim=sim, obs=obs)                # KGE for low flows
KGEnp(sim=sim, obs=obs)                # Non-parametric KGE
sKGE(sim=sim, obs=obs)                 # Split KGE
KGEkm(sim=sim, obs=obs)                # Knowable Moments KGE
JDKGE(sim=sim, obs=obs)                # Joint Divergence KGE

d(sim=sim, obs=obs)                    # Index of Agreement
dr(sim=sim, obs=obs)                   # Refined Index of Agreement
md(sim=sim, obs=obs)                   # Modified Index of Agreement
rd(sim=sim, obs=obs)                   # Relative Index of Agreement

VE(sim=sim, obs=obs)                   # Volumetric Efficiency
cp(sim=sim, obs=obs)                   # Coefficient of Persistence
APFB(sim=sim, obs=obs)                 # Annual Peak Flow Bias
HFB(sim=sim, obs=obs)                  # High Flow Bias
LME(sim=sim, obs=obs)                  # Liu-Mean Efficiency
LCE(sim=sim, obs=obs)                  # Lee and Choi Efficiency
PMR(sim=sim, obs=obs)                  # Proxy for Model Robustness

pbias(sim=sim, obs=obs)                # Percent bias (PBIAS)
pbiasfdc(sim=sim, obs=obs)             # PBIAS in the slope of the midsegment of the FDC

me(sim=sim, obs=obs)                   # Mean Error
mae(sim=sim, obs=obs)                  # Mean Absolute Error
mse(sim=sim, obs=obs)                  # Mean Squared Error 
rmse(sim=sim, obs=obs)                 # Root Mean Square Error (RMSE)
ubRMSE(sim=sim, obs=obs)               # Unbiased RMSE
nrmse(sim=sim, obs=obs, norm="sd")     # Normalised Root Mean Square Error
nrmse(sim=sim, obs=obs, norm="maxmin") # Normalised Root Mean Square Error
nrmse(sim=sim, obs=obs, norm="mean")   # Normalised Root Mean Square Error
nrmse(sim=sim, obs=obs, norm="IQR")    # Normalised Root Mean Square Error

rPearson(sim=sim, obs=obs)             # Pearson correlation coefficient
rSpearman(sim=sim, obs=obs)            # Spearman rank correlation coefficient
R2(sim=sim, obs=obs)                   # Coefficient of determination (R2)
br2(sim=sim, obs=obs)                  # R2 multiplied by the slope of the regression line

## ----Example6-1---------------------------------------------------------------
NSE(sim=sim, obs=obs, fun=log, epsilon.type="Pushpalatha2012")

## ----Example6-2---------------------------------------------------------------
eps  <- mean(obs, na.rm=TRUE)/100
lsim <- log(sim+eps)
lobs <- log(obs+eps)
NSE(sim=lsim, obs=lobs)

## ----Example6-3---------------------------------------------------------------
gof(sim=sim, obs=obs, fun=log, epsilon.type="Pushpalatha2012", do.spearman=TRUE, do.pbfdc=TRUE, do.pmr=TRUE)

## ----Example7-1---------------------------------------------------------------
eps <- 0.01
NSE(sim=sim, obs=obs, fun=log, epsilon.type="otherValue", epsilon.value=eps)

## ----Example7-2---------------------------------------------------------------
lsim <- log(sim+eps)
lobs <- log(obs+eps)
NSE(sim=lsim, obs=lobs)

## ----Example7-3---------------------------------------------------------------
gof(sim=sim, obs=obs, fun=log, epsilon.type="otherValue", epsilon.value=eps, do.spearman=TRUE, do.pbfdc=TRUE, do.pmr=TRUE)

## ----Example8-1---------------------------------------------------------------
fact <- 1/50
NSE(sim=sim, obs=obs, fun=log, epsilon.type="otherFactor", epsilon.value=fact)

## ----Example8-2---------------------------------------------------------------
fact <- 1/50
eps  <- fact*mean(obs, na.rm=TRUE)
lsim <- log(sim+eps)
lobs <- log(obs+eps)
NSE(sim=lsim, obs=lobs)

## ----Example8-3---------------------------------------------------------------
gof(sim=sim, obs=obs, fun=log, epsilon.type="otherFactor", epsilon.value=fact, do.spearman=TRUE, do.pbfdc=TRUE, do.pmr=TRUE)

## ----Example9-1---------------------------------------------------------------
fun1 <- function(x) {sqrt(x+1)}
NSE(sim=sim, obs=obs, fun=fun1)

## ----Example9-2---------------------------------------------------------------
sim1 <- sqrt(sim+1)
obs1 <- sqrt(obs+1)
NSE(sim=sim1, obs=obs1)

## ----Example9-3---------------------------------------------------------------
gof(sim=sim, obs=obs, fun=fun1, do.spearman=TRUE, do.pbfdc=TRUE, do.pmr=TRUE)

## ----LoadingEgaEnEstellaQts---------------------------------------------------
require(zoo)
data(EgaEnEstellaQts)
obs <- EgaEnEstellaQts

## ----SettingSim---------------------------------------------------------------
sim <- obs 

## ----ggof-default1, fig.width=8, fig.height=5---------------------------------
ggof(sim, obs)

## ----ggof-userdefined1, fig.width=8, fig.height=5-----------------------------
ggof(sim, obs, 
     gofs=c( "PBIAS", "dr", "R2",  "NSE", "KGE",  "LCE", "JDKGE", "APFB", "HFB"))

## ----ComputingGOFs------------------------------------------------------------
gof(sim=sim, obs=obs, do.spearman=TRUE, do.pbfdc=TRUE, do.pmr=TRUE)

## ----AddingNoiseToSim---------------------------------------------------------
sim[1:1826] <- obs[1:1826] + rnorm(1826, mean=10)

## ----ggof-default2, fig=TRUE, pdf=TRUE, eps=FALSE, fig.width=10, fig.height=7----
ggof(sim=sim, obs=obs, ftype="dm", FUN=mean)

## ----ggof-userdefined2, fig.width=8, fig.height=5-----------------------------
ggof(sim=sim, obs=obs, ftype="dm", FUN=mean,
     gofs=c( "PBIAS", "dr", "R2",  "NSE", "KGE",  "LCE", "JDKGE", "APFB", "HFB"))

## ----ggof2, fig=TRUE, pdf=TRUE, eps=FALSE, fig.width=10, fig.height=7---------
ggof(sim=sim, obs=obs, ftype="dm", FUN=mean, cal.ini="1963-01-01")

## ----ComputingGOF-------------------------------------------------------------
sim <- window(sim, start="1963-01-01")
obs <- window(obs, start="1963-01-01")

gof(sim, obs)

## ----ubands1, fig.width=8, fig.height=5---------------------------------------
lband <- obs - 5
uband <- obs + 5

## ----ubands2, fig.width=8, fig.height=5---------------------------------------
plotbands(obs, lband, uband)

## ----ubands3------------------------------------------------------------------
sim <- obs + rnorm(length(obs), mean=3)

## ----ubands4, fig.width=8, fig.height=5---------------------------------------
plotbands(obs, lband, uband, sim)

## ----P-Factor, fig.width=8, fig.height=5--------------------------------------
( pfactor(x=obs, lband=lband, uband=uband) )

## ----R-Factor, fig.width=8, fig.height=5--------------------------------------
( rfactor(x=obs, lband=lband, uband=uband) )

## ----Computing_r1-------------------------------------------------------------
r <- sim-obs

## ----Computing_r2-------------------------------------------------------------
library(hydroTSM)
smry(r) 

## ----hydroplot2, fig=TRUE, pdf=TRUE, fig.width=10, fig.height=12--------------
# daily, monthly and annual plots, boxplots and histograms
hydroplot(r, FUN=mean)

## ----hydroplo3, fig=TRUE, eval=TRUE, pdf=TRUE, eps=FALSE, fig.width=8, fig.height=8----
# daily, monthly and annual plots, boxplots and histograms
hydroplot(r, FUN=mean, pfreq="seasonal")

## ----echo=FALSE---------------------------------------------------------------
sessionInfo()$platform
sessionInfo()$R.version$version.string 
paste("hydroGOF", sessionInfo()$otherPkgs$hydroGOF$Version)

