GLUE analysis

Introduction
This is an example of R code to perform a Generalised Likelihood Uncertainty Estimation (GLUE) on a hydrological model. In the example we use topmodel (implimented as topmodel) on a catchment in the Ecuadorian Andes (Huagrahuma). Other models can be used as long as they are implemented as an R function.

Libraries needed
Library Hmisc (CRAN) is needed for the wgt.quantile function. See ?wgt.quantile for details. We use topmodel which is part of the topmodel-package on this site. The data are included in the package.

> library(Hmisc) > library(topmodel) > data(Huagrahuma)

Procedure
Sample a parameter set from a prior parameter distribution. This example uses the uniform distribution, which can be sampled between 0 and 1 with Runif. We can then scale linearly to the appropriate range using scaling factors a and b.

parameter1 <- a1 + runif(1)*b1 parameter2 <- a2 + runif(1)*b2 ...

param.set <- c(parameter1,parameter2,...)

Run the model for the calibration period with the generated parameter set to obtain the simulated discharge

Qsim <- topmodel(param.set,rain,ET0,topidxclasses,...)

Calculate the likelihood of this parameter set using the simulated and observed discharge. The choice of the likelihood function is up to the user, but the Nash - Sutcliffe efficiency is given here as an example:

eff <- 1 - sum((Qobs - Qsim)^2) / sum((Qobs-mean(Qobs))^2)

NOTE: calculation of the Nash-Sutcliffe efficiency is also implimented in NSeff.

Decide whether the parameter set is behavioural or not and retain the parameter set if behavioural

NOTE: this decision is again subjective. For a more scientifically sound determination of the behavioural limit, see Beven (2006). Here we will use an efficiency of 0.6 as a threshold. The efficiency, parameter set and simulated discharge of a behavioural run are stored in resp. the objects total.eff, total.param.set and total.qsim

if(eff > 0.6) { total.eff <- eff total.param.set <- param.set }

The above procedure should be repeated until enough behavioural runs are obtained (e.g. using a while-loop)

Behavioural runs can be added as rows to the total matrices:

total.eff <- c(total.eff,eff) total.param.set <- cbind(total.param.set,param.set)

In the next section it is assumed that each column of total.param.set contains a behavioural parameter set and that the corresponding efficiency is found at the same location in the total.eff vector

Rerun the model for the prediction period, using each of the behavioural parameter sets. The simulated discharges are stored in the columns of a matrix called predicted.qsim

predicted.qsim <- model(total.param.set[,1], rain, ...)

for(i in 2:dim(param.set)[2]) { qsim <- model(total.param.set[,i], rain, ...) predicted.qsim <- cbind(predicted.obs,qobs) }

Normalise the efficiencies so that they sum up to 1:

eff <- eff - 0.6 eff <- eff/sum(eff)

Define a quantile for the prediction bounds. Here we take the 0.05 and 0.95 quantiles resulting in 90% prediction limits.

lower <- 0.05 upper <- 0.95

Create the objects in which we will store the prediction limits:

Ulimit <- 0 Llimit <- 0

Now we calculate the quantiles for each timestep (this can also be done with apply function)

for(i in 1:dim(predicted.qsim)[1]) { Llimit[i] <- wtd.quantile(predicted.qsim[i,],weights = eff, probs = L, normwt=T) Ulimit[i] <- wtd.quantile(predicted.qsim[i,],weights = eff, probs = U, normwt=T) }

If everything went well, the final prediciton limits are in Llimit and Ulimit.

Final notes

 * If topmodel is used, some loops can be avoided because topmodel can work on entire parameter set matrices. It can also return the Nash-Sutcliffe efficiency directly (see the topmodel page)
 * The procedure can be very memory intensive because all simulated discharges for all parameter sets are stored in memory (the matrix predicted.qsim). If the model can give output per timestep, the above procedure can be repeated for each timestep separately to reduce memory usage