Skip to content

Commit e426247

Browse files
exercises
1 parent e5917ed commit e426247

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

exploration-exercises.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
Exploration Exercises
2+
================
3+
4+
## `ggplot` Recipes
5+
6+
Simulations from `macpan2` come in a standard format with columns
7+
described
8+
[here](https://canmod.github.io/macpan2/articles/quickstart.html#generating-simulations),
9+
and full documentation
10+
[here](https://canmod.github.io/macpan2/reference/mp_trajectory.html#value).
11+
The key point is that there is always a `time`, `matrix`, and `value`
12+
column. Let’s focus on those. We can use this SIR example.
13+
14+
``` r
15+
library(macpan2); library(ggplot2)
16+
```
17+
18+
##
19+
## Attaching package: 'ggplot2'
20+
21+
## The following object is masked _by_ '.GlobalEnv':
22+
##
23+
## theme_bw
24+
25+
``` r
26+
sir = mp_tmb_library("starter_models", "sir", package = "macpan2")
27+
sim = mp_simulator(sir, time_steps = 25, outputs = "infection")
28+
dat = mp_trajectory(sim)
29+
print(dat)
30+
```
31+
32+
## matrix time row col value
33+
## 1 infection 1 0 0 0.1980000
34+
## 2 infection 2 0 0 0.2169692
35+
## 3 infection 3 0 0 0.2376233
36+
## 4 infection 4 0 0 0.2600847
37+
## 5 infection 5 0 0 0.2844793
38+
## 6 infection 6 0 0 0.3109346
39+
## 7 infection 7 0 0 0.3395786
40+
## 8 infection 8 0 0 0.3705380
41+
## 9 infection 9 0 0 0.4039349
42+
## 10 infection 10 0 0 0.4398849
43+
## 11 infection 11 0 0 0.4784928
44+
## 12 infection 12 0 0 0.5198490
45+
## 13 infection 13 0 0 0.5640248
46+
## 14 infection 14 0 0 0.6110669
47+
## 15 infection 15 0 0 0.6609922
48+
## 16 infection 16 0 0 0.7137807
49+
## 17 infection 17 0 0 0.7693697
50+
## 18 infection 18 0 0 0.8276464
51+
## 19 infection 19 0 0 0.8884416
52+
## 20 infection 20 0 0 0.9515225
53+
## 21 infection 21 0 0 1.0165878
54+
## 22 infection 22 0 0 1.0832626
55+
## 23 infection 23 0 0 1.1510954
56+
## 24 infection 24 0 0 1.2195575
57+
## 25 infection 25 0 0 1.2880447
58+
59+
If we are simulating a single variable, the `matrix` column will be
60+
constant and so we will use recipes like this.
61+
62+
``` r
63+
(dat
64+
|> ggplot()
65+
+ geom_line(aes(time, value))
66+
)
67+
```
68+
69+
![](figures/ggplot-1.png)<!-- -->
70+
71+
If we have multiple variables we can use the `matrix` column in our
72+
visualizations to keep track of different variables.
73+
74+
``` r
75+
sim = mp_simulator(sir, time_steps = 25, outputs = mp_flow_vars(sir))
76+
dat = mp_trajectory(sim)
77+
(dat
78+
|> ggplot()
79+
+ geom_line(aes(time, value))
80+
+ facet_wrap(~matrix)
81+
)
82+
```
83+
84+
![](figures/multimat-1.png)<!-- -->
85+
86+
``` r
87+
(dat
88+
|> ggplot()
89+
+ geom_line(aes(time, value, colour = matrix))
90+
)
91+
```
92+
93+
![](figures/multimat-2.png)<!-- -->
94+
95+
## Functional Forms of Flow Rates
96+
97+
We have so far focused on specifying flows with either constant
98+
per-capita rates or mass-action rates. There are a variety of
99+
[mathematical
100+
functions](https://canmod.github.io/macpan2/reference/engine_functions)
101+
that you can use to build more complex functional forms for these rates.
102+
103+
| <img src="images/exercise.svg" width="120" /> |
104+
|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
105+
| Building a simple model with seasonal cycles in transmission. What are some other forms of complexity in the functional forms of rates? Try to implement them. Keep in mind that you can use tilde-based expressions to define intermediate variables. |
106+
107+
## Unbalanced Flows
108+
109+
So far we have used the
110+
[mp_per_capita_flow](https://canmod.github.io/macpan2/reference/mp_per_capita_flow)
111+
function that specifies perfectly balanced flows – the total number out
112+
of the `from` box is equal to the total number into the `to` box. But
113+
processes like birth/death and importation do not behave this way – for
114+
example, birth adds individuals to the `to` box without removing
115+
individuals from the `from` box. Viral shedding into wastewater is
116+
another process that is not a balanced flow. The above linked help page
117+
describes two other kinds of flows that are not balanced.
118+
119+
| <img src="images/exercise.svg" width="120" /> |
120+
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
121+
| Modify one of your models to include birth and death. Allow for the possibility of more birth than death. If your model uses a constant total population size, `N`, you will need to add expressions that update `N` dynamically. |
122+
123+
| <img src="images/exercise.svg" width="120" /> |
124+
|:---------------------------------------------------|
125+
| Add a wastewater compartment to an existing model. |
126+
127+
## Initializing Variables
128+
129+
So far we have initialized variables by adding numerical values to the
130+
`default` list.
131+
132+
## Relationships Parameters and Simulations

0 commit comments

Comments
 (0)