-
Notifications
You must be signed in to change notification settings - Fork 1
/
5_Dynamic simulation models.Rmd
182 lines (125 loc) · 4.38 KB
/
5_Dynamic simulation models.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
---
title: 'Dynamic simulation models'
author: "Vikram Dayal and M Rahul"
output: pdf_document
---
Nonlinear dynamics can be complex, as we can see in Figure \ref{fig:sim1} of a simulation with the equation
$X_{t + 1} = X_{t} + (rX_{t}(1 - (X_{t}/K)))$, $r = 2.6, K = 100, X_{0} = 40$.
```{r, echo = F, message = F, warning=FALSE}
library(tidyverse)
num <- 40
X <- numeric(num)
K <- 100
r <- 2.6
X[1] <- 40
for (i in 2:num) {
X[i] <- X[i - 1] + ( r*X[i - 1]* (
1 - ( X[i - 1] /K)) )
}
Xdat <- data.frame(X, time = 1:num)
```
```{r, echo = F, fig.width = 4, fig.height = 3, fig.cap = "\\label{fig:sim1}simulated logistic difference equation"}
gg1 <- ggplot(Xdat, aes(x = time,
y = X)) +
geom_line()
gg1
```
A key issue in environmental economics concerns biological invasions. Biologically invasive species include *pests and pathogens*, plants and animals, which invade and take over ecological systems, harming the production of ecosystem services^[see Perrings et al. 2002 and Dayal 2007]
Epidemiologists, ecologists, economists, and ecological economists build on `workhorse' models of infectious disease epidemiology^[see Stevens 2010.].
We now see how we can simulate a difference equation in R. Consider a simple difference equation:
$X_{t + 1} = X_{t} + 2$.
Let $X_{1}$ = 3.
Then,
$X_{2} = X_{1} + 2 = 5$
$X_{3} = X_{2} + 2 = 7$
So we can numerically simulate a difference equation by iteration. This means we can use a for loop.
```{r}
X <- numeric(3)
X[1] <- 3
for(i in 2:3){
X[i] <- X[i - 1] + 2
}
X
```
We consider a simple SIR model in R. Here we will focus on qualitative insight. An SIR model consists of:
**Susceptible** Individuals who are not yet infected, but may be tomorrow (S).
**Infected** Individuals who are infected (I).
**Resistant** Individuals who are resistant (R).
Note that total individuals, N is given by $N = S + I + R$. People move through the compartments: $S \rightarrow I \rightarrow R$. The dynamics are easier to think of if we consider two periods: $newI_{2} = \beta I_{1} S_{1}$, $R_{2} = \gamma I_{1}$.
We now set up for our simulation in R.
```{r}
num <- 100
N <- 1e6
S <- numeric(num)
S[1] <- N
I <- numeric(num)
I[1] <- 10
nI <- numeric(num)
Beta <- 4.5e-07
Measures <- 1
R <- numeric(num)
Gamma <- 1/5
```
We can use a 'for loop'
```{r}
for(i in 2:num) {
nI[i] <- Beta*Measures * S[i -1] * I[i -1]
S[i] <- S[i - 1] - nI[i]
R[i] <- Gamma * I[i - 1]
I[i] <- I[i - 1] + nI[i] - R[i]
}
```
We collect the values.
```{r, message = F, warning = F}
Time <- 1:num
SIR <- data.frame(S, I, nI, R, Time)
library(tidyverse)
```
We plot the results.
```{r, fig.width = 3, fig.height=2, fig.cap = "Proportion of infected"}
ggplot(SIR, aes(x = Time, y = I/N)) +
geom_line()
```
We create a function that allows us to vary $\beta$.
```{r}
SIRfunc <- function(Measures = 1) {
num <- 100
N <- 1e6
S <- numeric(num)
S[1] <- N
I <- numeric(num)
I[1] <- 10
nI <- numeric(num)
Beta <- 4.5e-07
#Measures <- 1
R <- numeric(num)
Gamma <- 1/5
for(i in 2:num) {
nI[i] <- Beta*Measures * S[i -1] * I[i -1]
S[i] <- S[i - 1] - nI[i]
R[i] <- Gamma * I[i - 1]
I[i] <- I[i - 1] + nI[i] - R[i]
}
Time <- 1:num
SIR <- data.frame(S, I, nI, R, Time)
library(tidyverse)
ggplot(SIR, aes(x = Time,
y = I/N)) +
geom_line()
}
```
We use the function.
```{r, fig.width = 3, fig.height = 2, fig.cap = "Proportion of infected, with beta = 0.8"}
SIRfunc(0.8)
```
The basic model can be elaborated in different ways.
* Make it more detailed, add E, so SEIR. Two patches, mobility between patches.
* Provide a tool (Shiny) so non-specialists can play with different settings of parameters and see resulting scenarios.
* Use a calibration approach, take parameters from literature etc.
* Explore mathematical properties: stability, see how dynamics change with parameters
* Use for detailed predictions
* Fit to data, likelihood or Bayesian approaches.
# References
Dayal V. 2007. Social diversity and ecological complexity: How an invasive tree could affect diverse agents in the land of the tiger. Environment and Development Economics 12(04):553-571
Perrings C, Williamson M, Barbier E B, Delfino D, Dalmazzone S, Shogren J, Simmons P, Watkinson A. Biological invasion risks and the public good: an economic perspective. 2002. Conservation ecology, Vol. 6, No. 1, 1, 06.200
Stevens M H. 2010. A primer of ecology with R. Springer