### Building the network model
The model topology is very simple:
``` r
m <- new_networkModel() %>% set_topo("NH4 -> algae")
```
The initial conditions are taken from the `exp` table for $t=t_0$:
``` r
inits <- exp %>% filter(time.day == 0)
```
We set the initial conditions in the model:
``` r
m <- m %>% set_init(inits, comp = "species", size = "biomass", prop = "prop15N",
group_by = "aquariumID")
```
And we use all the rows from the `exp` table for which $t>t_0$ as the observations:
``` r
obs <- exp %>% filter(time.day > 0)
m <- m %>% set_obs(obs, time = "time.day")
m
```
```
## # A tibble: 2 × 5
## topology initial observations parameters group
##
Let's have a quick look at the posterior predictive check:
``` r
predictions <- predict(m, run)
plot(predictions, facet_row = "group")
```
The effect of the pulse is clearly visible in the proportion of marked tracer, both for ammonium and for algae (right panel). The pulse is also visible as an increase in the predicted ammonium compartment size (left panel)).
## Modelling drip events
Drip events are different from pulse events: while a pulse is a one-time, discrete addition of tracer, a drip is a continuous addition taking place over a time interval.
### Data preparation
The simulated data we use in this example can be loaded into your R session by running the code below:
``` r
exp <- tibble::tribble(
~time.day, ~species, ~biomass, ~prop15N, ~transect,
0, "NH4", 0.313, 0.0038, "transect_1",
4, "NH4", 0.2746, NA, "transect_1",
8, "NH4", 0.3629, 0.0295, "transect_1",
12, "NH4", NA, 0.0032, "transect_1",
16, "NH4", NA, 0.0036, "transect_1",
20, "NH4", 0.3414, 0.0038, "transect_1",
0, "epilithon", 89.2501, 0.0022, "transect_1",
8, "epilithon", 123.1212, 0.024, "transect_1",
12, "epilithon", NA, 0.02, "transect_1",
16, "epilithon", 90.5919, 0.0107, "transect_1",
20, "epilithon", 80.3261, 0.0062, "transect_1",
0, "NH4", 0.3525, 0.0035, "transect_2",
4, "NH4", 0.2958, 0.0362, "transect_2",
8, "NH4", NA, 0.03, "transect_2",
12, "NH4", 0.3392, 0.0044, "transect_2",
16, "NH4", 0.212, 0.0026, "transect_2",
20, "NH4", 0.3818, 0.0046, "transect_2",
0, "epilithon", 127.873, 0.0029, "transect_2",
4, "epilithon", NA, 0.0096, "transect_2",
8, "epilithon", 123.3216, NA, "transect_2",
12, "epilithon", 89.8053, 0.0144, "transect_2",
16, "epilithon", 74.9105, 0.0098, "transect_2",
20, "epilithon", 88.0108, 0.0067, "transect_2"
)
```
In this simulated experiment, we are studying a stream at two dfferent locations (transects 1 and 2) and we are measuring nitrogen flow between dissolved NH$_4^+$ and algae on the stream bed (`epilithon`). Since the water is flowing continuously in the stream, we will consider the ammonium as a steady-state compartment (see the [tutorial](tutorial-030-steady-state-comps.html) about steady state compartments).
The design of that experiment is that we start with two days of measurements during which no addition is performed, then a continuous drip of $^{15}$N-ammonium is performed between day 2 and day 10, and then the drip is stopped and we continue measurements until day 20. We will see below how to tell the model about the drip setup.
First let's have a look at the data:
``` r
library(ggplot2)
library(gridExtra)
p1 <- ggplot(exp, aes(x = time.day, y = biomass, col = species)) +
geom_point() + ggtitle("Biomass data") + ylab("Biomass (mg N)") +
facet_wrap(~ transect)
p2 <- ggplot(exp, aes(x = time.day, y = prop15N, col = species)) +
geom_point() + ggtitle("Heavy isotope proportions") + ylab("Proportion of 15N") +
facet_wrap(~ transect)
grid.arrange(p1, p2, nrow = 2)
```
### Building the model
We define the model topology:
``` r
m <- new_networkModel() %>% set_topo("NH4 -> epilithon")
```
The initial conditions are taken from the `exp` table for $t=t_0$:
``` r
inits <- exp %>% filter(time.day == 0)
```
We set the initial conditions in the model:
``` r
m <- m %>% set_init(inits, comp = "species", size = "biomass", prop = "prop15N",
group_by = "transect")
```
And we use all the rows from the `exp` table for which $t>t_0$ as the observations:
``` r
obs <- exp %>% filter(time.day > 0)
m <- m %>% set_obs(obs, time = "time.day")
```
```
## Using the same columns by default as the ones used with `set_init()`:
## comp = "species"
## size = "biomass"
## prop = "prop15N"
## group_by = "transect"
```
``` r
m
```
```
## # A tibble: 2 × 5
## topology initial observations parameters group
##
Let's have a quick look at the posterior predictive check:
``` r
predictions <- predict(m, run)
plot(predictions, facet_row = "group")
```
```
## Error in `purrr::map()`:
## ℹ In index: 1.
## Caused by error in `project()`:
## ! !any(is.na(params$value)) is not TRUE
```
Nice! The drip phase is clearly visible in the NH$_4^+$ enrichment profile.
This was the last tutorial focusing on specifying the experimental design of an addition experiment. The next tutorials will focus on **statistical aspects** of the model, such as incorporating the effects of covariates and choosing parameter priors.