# Chapter 18 Intro to Modeling - Calibrate HBV

## 18.1 Introduction

The following activity is available as a template github repository at the following link: https://github.com/VT-Hydroinformatics/17-calibrate-model-for-loops

*Goals for this activity:*

- Learn how for loops work
- Use for loops to calibrate the HBV model using the Monte Carlo technique
- Assess the results of the calibration, discuss equifinality

We saw in the last activity that coming up with the parameters that create a model that fits our watershed discharge best is hard. For this reason, there are several ways people have devised for automating the process. We are going to explore one: a Monte Carlo calibration.

To use this approach, we will create a ton of random parameter sets, run the model for each, and then see which one fits best. The idea is that by creating a bunch of random sets of parameters, we will end up with at least one that works well. As you might guess, this can take a while. We will just do this for 100 or 1000 runs here in this activity, but we will also explore some output from a 100,000 run Monte Carlo that I ran and saved.

There is one new package this time, GGally, just for creating a parallel coordinate plot to look at the results of the 100k run Monte Carlo at the end.

```
library(tidyverse)
library(lubridate)
library(plotly)
library(GGally) #for making parallel coordinate plot
theme_set(theme_classic())
```

In order to do what we are going to do, we need “for” loops.

A for loop is a way to run a chunk of code several times while changing a value in that chunk of code each time.

So using an example from class: you could run some code that downloads data from a USGS gage, and each time through the loop it changes which gage you download.

In R, the syntax for this is

for(x in sequence){

x

}

Where x will change every time you go through the loop. The values x will have each time through the loop are those that are in “sequence” above. If you changed this to x in 1:3, x would have values of 1, 2, and 3 each time through. If you made it x in c(2,3,5) x would change to 2, then 3, and then 5 each time through the loop. You can make the sequence anything and x can have any name so “letter in c(a, b, c)” would have *letter* change to a, then b, then c each time through the loop.

The image below shows a short example of how a for loop works.

Let’s try some examples

```
<- 0
val
for(x in 1:10){
<- x
val[x]
}
val
```

`## [1] 1 2 3 4 5 6 7 8 9 10`

```
<- c("mon","tues","wednes")
days
for(x in 1:length(days)){
<- paste0(days[x], "day")
days[x]
}
days
```

`## [1] "monday" "tuesday" "wednesday"`

```
<- c("123","ABC","001")
gages
for(g in gages){
print(g)
}
```

```
## [1] "123"
## [1] "ABC"
## [1] "001"
```

## 18.2 Challenge: Write a for loop

Define a vector with values 5, 6, 7, 8, 9

Create a for loop that steps through this vector and adds 1 to each value.

Yes, you can do this by just typing the name of the vector + 1, but that’s not the point!

## 18.3 Prep data for HBV

Since we did this in the last chapter, we are just going to quickly run the code the preps our data to go into the model. Below we load the HBV movdel and make vectors of P, Qobs, and Temp.

```
#Create the HBV function with the HBV R file
source('HBV/HBV.R')
#set start and end dates for model to filter data
<- mdy("01-01-2009")
start <- mdy("12-31-2012")
end
#Precip
<- read_csv("HBV/Pwd2009-2012.csv") %>%
P1 select(DATE, WS_3) %>%
mutate(DATE = ymd(DATE)) %>%
filter(DATE >= start & DATE <= end)
```

```
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## DATE = col_character(),
## WS_1 = col_double(),
## WS_2 = col_double(),
## WS_3 = col_double(),
## WS_4 = col_double(),
## WS_5 = col_double(),
## WS_6 = col_double(),
## WS_7 = col_double(),
## WS_8 = col_double(),
## WS_9 = col_double()
## )
```

```
<- P1$WS_3
P
#Streamflow mm/d
<- read_csv("HBV/SWD2009-2012.csv") %>%
Qobs1 select(DATE, WS_3) %>%
mutate(DATE = ymd(DATE)) %>%
filter(DATE >= start & DATE <= end)
```

```
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## DATE = col_character(),
## WS_1 = col_double(),
## WS_2 = col_double(),
## WS_3 = col_double(),
## WS_4 = col_double(),
## WS_5 = col_double(),
## WS_6 = col_double(),
## WS_7 = col_double(),
## WS_8 = col_double(),
## WS_9 = col_double()
## )
```

```
<- Qobs1$WS_3
Qobs
#Temp in deg C
<- read_csv("HBV/Tdm2009-2012.csv")%>%
Temp1 select(DATE, STA_1) %>%
mutate(DATE = ymd(DATE)) %>%
filter(DATE >= start & DATE <= end)
```

```
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## DATE = col_character(),
## STA_1 = col_double(),
## STA_6 = col_double(),
## STA_14 = col_double(),
## STA_INT = col_double(),
## STA_HQ = col_double(),
## STA_23 = col_double(),
## STA_17 = col_double(),
## STA_24 = col_double()
## )
```

```
<- Temp1$STA_1
Temp
#Snow in SWE, mm
<- read_csv("HBV/sno2009-2012.csv") %>%
snow select(DATE, STA2) %>%
mutate(DATE = ymd(DATE)) %>%
filter(DATE >= start & DATE <= end)
```

```
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## .default = col_double(),
## DATE = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
```

```
## Warning: 76 parsing failures.
## row col expected actual file
## 1 -- 22 columns 25 columns 'HBV/sno2009-2012.csv'
## 2 -- 22 columns 25 columns 'HBV/sno2009-2012.csv'
## 3 -- 22 columns 25 columns 'HBV/sno2009-2012.csv'
## 4 -- 22 columns 25 columns 'HBV/sno2009-2012.csv'
## 5 -- 22 columns 25 columns 'HBV/sno2009-2012.csv'
## ... ... .......... .......... ......................
## See problems(...) for more details.
```

`## Warning: 8 failed to parse.`

## 18.4 Calculate PET

Again, because we did this last time, we will just re-run this code to create our PET values.

```
<- 43 + 57/60 #43 degrees and 57 minutes
lat <- (lat/360) * 2 * pi #convert to radians
latrad
<- select(Temp1, DATE) %>%
PET1 mutate(DOY = yday(DATE)) %>% #DOY for dates
mutate(tempvar = (2 * pi / 365) * DOY) %>%
#declination of the sun above the celestial equator in
#radians on day JulDay of the year
mutate(delta_h = 0.4093 * sin(tempvar - 1.405)) %>%
#day length in h
mutate(daylen = (2 * acos(-tan(delta_h) * tan(latrad)) / 0.2618)) %>%
mutate(
PET = 29.8 * daylen * 0.611 * exp(17.3 * Temp /
+ 237.3)) / (Temp + 273.2)) #PET Hamon method
(Temp
<- PET1$PET PET
```

## 18.5 Monte Carlo step 1: generate random parameter sets

Below is a table of the parameters for the HBV model and their suggested min and max values for our application. What we basically want to do is mimic you sitting down at the computer and randomly changing all the parameters, running the model, and checking to see how well it did. Over, and over, and over.

You can do this by generating the set of parameters each time you run the model, but we are going to do it separately so we can see the Nashe-Sutcliff efficiency (NSE) for each set of parameters.

Object Name | Min | Max | Description |
---|---|---|---|

FC | 40 | 400 | Max soil moisture storage, field capacity |

beta | 1 | 6 | Shape coefficient governing fate of water input to soil moisture storage |

LP | .3 | 1 | Threshold for reduction of evaporation |

SFCF | 0.4 | 1.2 | Snowfall correction factor |

TT | -1.5 | 1.2 | Threshold temperature |

CFMAX | 1 | 8 | Degree-day factor |

k0 | 0.05 | 0.5 | Recession constant (upper storage, near surface) |

k1 | 0.01 | 0.3 | Recession constant (upper storage) |

k2 | 0.001 | 0.15 | Recession constant (lower storage) |

UZL | 0 | 70 | Threshold for shallow storage |

PERC | 0 | 4 | Percolation, max flow from upper to lower storage |

MAXBAS | 1 | 3 | base of the triangular routing function, days" |

To make our parameter sets, we first have to decide how many model runs we want to try. Let’s try 100 so we don’t burn down anyone’s computer.

Then we will use the runif() function to generate the same number of random values as the number of model runs we want to perform for each parameter, within the bounds of the min and max values above.

Remember, runif() creates a set of random numbers in a uniform distribution with a specified length with a specified minimum and maximum value. A uniform distribution is one where each number has the same probability of occurrence as the next, so it’s basically a set of random numbers between your min and max values.

We will also create a blank vector of the same length which we will populate with the NSE values for each model run, and then slap them all together with cbind() (column bind).

```
#number of runs
<- 100
N
# PARAMETERS RANGE and generate set
<- runif(N, min = 40 , max = 400) #Max soil moisture storage, field capacity
FC <- runif(N, min = 1 , max = 6) #Shape coefficient governing fate of water input to soil moisture storage
beta <- runif(N, min = 0.3 , max = 1) #Threshold for reduction of evap
LP <- runif(N, min = 0.4 , max = 1.2) #Snowfall correction factor
SFCF <- runif(N, min = -1.5 , max = 1.2) #Threshold temperature
TT <- runif(N, min = 1 , max = 8) #Degree-day factor
CFMAX <- runif(N, min = 0.05 , max = 0.5) #Recession constant (upper storage, near surface)
k0 <- runif(N, min = 0.01 , max = 0.3) #Recession constant (upper storage)
k1 <- runif(N, min = 0.001, max = 0.15) #Recession constant (lower storage)
k2 <- runif(N, min = 0 , max = 70) #Threshold for shallow storage
UZL <- runif(N, min = 0 , max = 4) #Percolation, max flow from upper to lower storage
PERC <- rep(1, N) #base of the triangular routing function, days
MAXBAS#MAXBAS is just 1's because routing will be set to zero, so the parameter isn't used
<- rep(NA, N) #create NSE column, to be filled in for loop
NSE
<- 0
routing
<- cbind(FC, beta, LP, SFCF,
pars
TT, CFMAX, k0, k1, k2, UZL, PERC, MAXBAS, NSE)
```

## 18.6 Run the model for each parameter set

Alright, let’s get at this. We now have our random sets of parameters. So we will run the model for each and see how it does.

Here’s how we will do that:

First: Define the period for which we want to calculate NSE. Remember we are going to drop the first 40% of the model run to let the model “warm up.” Since we are starting with nothing in the storages we need to let the boxes “fill up” and settle in.

Then we run a FOR LOOP!

Our for loop will step through a sequence of numbers from 1 to the total number of parameter sets we have. So in this case: 100. We will use i as our variable. So i will be 1, 2, 3…. all the way to 100.

In the code portion of the loop, it will grab the parameters from row i of the parameter set we created. And pass that to the HBV function with the Precip, Temp, and PET data. Remember routing is just set to zero.

After we run the model (still in the for loop) we will stick our observed values onto the results and then trip them to the period we want to evaluate. (drop the first 40%)

Finally, we calculate NSE for that individual model run, and then add it into the NSE column we made before. So each run will just tack on an NSE value to the end of the paramter set. We don’t need to save the model results, because we can just rerun the model with the same parameters and it’ll give us the same output.

The figure below illustrates how the for loop will work:

```
#trim the first 40% (warm up) of Qobs off for NSE calculation
<- floor(length(Qobs) * 0.4)
EvalStart <- length(Qobs)
EvalEnd
for (i in 1:N){
#call model with i parameter set generated above
<- HBV(pars[i,1:12], P, Temp, PET, routing)
results
#add the Qobs to results
<- cbind(results, Qobs)
results
#trim the first 40% of the record so it isn't included in the NSE calculation
<- results[EvalStart:EvalEnd,]
results
#Calculate NSE and add to parameter set
13] <- 1 - ((sum((results$Qobs - results$q) ^ 2)) / sum((results$Qobs - mean(results$Qobs)) ^ 2))
pars[i,
}
```

## 18.7 Find the best parameter set

Alright, now we have a NSE value that tells us how well each parameter set did at estimating runoff. So we need to find the best one!

To do this, we will convert the parameter sets to a tibble, use filter to grab the row with the maximum NSE, then use slice to pull just that row out of the tibble. Finally, we will run as.numeric on the data to be sure everything is numbers.

Now we have our best parameter set, let’s see how it did!

We will do what we did in the previous lesson: run the model with the parameter set, slap on the observed data, and filter it to the time period we want to look at. (after 08-07-2010 in this case, that’s the 40% cutoff in this case)

```
#find best parameters
<- as_tibble(pars)
pars
<- pars %>% filter(NSE == max(NSE)) %>%
bestparams slice(1) %>%
as.numeric()
#run with best parameters
<- HBV(bestparams, P, Temp, PET, routing)
modeloutput
#add observations for plotting
<- bind_cols(modeloutput, Qobs1)
modeloutput
#trim out warm up period for plotting
<- filter(modeloutput, DATE >= mdy("08-07-2010")) OutputTrim
```

Below we will look at the model run using plotly. First, we calculate the NSE again, just to be sure, and then plot the same way we did in the last lesson.

How did the model do? Did the 100 run Monte Carlo do better or worse than your manual attempts from last lesson?

```
#Calculate NSE
<- 1 - ((sum((OutputTrim$q - OutputTrim$WS_3) ^ 2)) /
NSE sum((OutputTrim$WS_3 - mean(OutputTrim$WS_3)) ^ 2))
#Create plot with NSE in title
%>% plot_ly(x = ~DATE) %>%
OutputTrim add_trace(y = ~q, name = 'Modeled', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~WS_3, name = 'Measured', type = 'scatter', mode = 'lines') %>%
layout(title=paste("NSE: ", round(NSE,2)))
```

## 18.8 Investigating a much bigger Monte Carlo

The next couple of code chunks make plots from a Monte Carlo I ran with 100,000 parameter sets.

We will look at the two plots and discuss.

How much difference was there in NSE in the top 100 runs of the 100,000 run set? How much would you expect the parameters to vary between these runs? What do you think it means if they did or did not vary much?

```
#the [,-1] drops the first column, which is just row numbers
<- read_csv("HBV/parsFrom100kRun.csv")[,-1] pars100
```

`## Warning: Missing column names filled in: 'X1' [1]`

```
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## X1 = col_double(),
## FC = col_double(),
## beta = col_double(),
## LP = col_double(),
## SFCF = col_double(),
## TT = col_double(),
## CFMAX = col_double(),
## k0 = col_double(),
## k1 = col_double(),
## k2 = col_double(),
## UZL = col_double(),
## PERC = col_double(),
## MAXBAS = col_double(),
## NSE = col_double(),
## ranks = col_double()
## )
```

```
ggplot(pars100, aes(NSE))+
geom_histogram()
```

`## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.`

The chunk below creates a parallel coordinate plot. Each parameter is shown, but normalized, where 1 is it’s maximum value from our random sets, and 0 is the minimum value.

Each connected line is a set of parameters. The green line is the set that had the highest NSE.

Look at this plot for a bit and discuss.

Are there any parameters that were always in the same range for these runs? What does that tell you about them? Are there any parameters that look like they spanned almost the whole range for these runs? What doe sthat tell you about them? What does this plot tell you about the model’s representation of the real world?

```
#filter the 100,000 run to the top ten
<- filter(pars100, ranks <= 10)
rankedpars
#set the min and max values for each parameter for the plot
<- c(40, 1, 0.3 , 0.4, -1.5, 1, 0.05, 0.01, 0.001, 0, 0, 1, 0, 0)
mins <- c(400, 6, 1, 1.2, 1.2, 8, 0.5, 0.3, 0.15, 70 , 4, 3, 0, 0)
maxs
#add maxes and mins to the parameter sets
<- rbind(rankedpars, mins, maxs)
rankedpars
#Make NSE a character so the legend will work properly
<- rankedpars %>% mutate(NSE = as.character(round(NSE, 2)))
rankedpars
#Make the NSE equal "Min/Max" for appropriate legend labels
$NSE[rankedpars$NSE == "0"] <- "Min/Max"
rankedpars
#Make the ranks a factor datatype
<- rankedpars %>% mutate(ranks = factor(ranks))
rankedpars
#Create parallel coordinate plot
%>% ggparcoord(columns = 1:11,
rankedpars groupColumn = 13,
showPoints = TRUE,
scale = "uniminmax") +
theme_minimal() +
ggtitle("Top 10 parameter sets from 100,000 run Monte Carlo")+
ylab("normalized parameter values")+
xlab("parameters")
```