forked from eco-evo-thr-2022/final-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-spp_comp.R
50 lines (35 loc) · 1.01 KB
/
multi-spp_comp.R
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
S <- 50
m <- matrix(runif(S^2, 0, 0.01), nrow = S)
diag(m) <- runif(S, 0.01, 0.2)
n <- rep(10, S)
d0 <- ((m %*% n) * n)[, 1]
la <- 1 * max(d0)
ntstep <- 12
nmat <- matrix(0, nrow = ntstep, ncol = S)
nmat[1, ] <- n
for(i in 2:ntstep) {
# birth or death?
# death probabilities
d <- ((m %*% n) * n)[, 1]
# birth probabilities
b <- la * n
# `event = 0` is death and `event = 1` is birth
event <- sample(0:1, size = 1, prob = c(sum(d), sum(b)))
if(event == 0) { # someone dies
# who dies?
thisOneDead <- sample(1:S, size = 1, prob = d)
# update populations
n[thisOneDead] <- n[thisOneDead] - 1
} else { # someone is born
thisOneBorn <- sample(1:S, size = 1, prob = b)
# update populations
n[thisOneBorn] <- n[thisOneBorn] + 1
}
nmat[i, ] <- n
}
# timeseries
matplot(nmat, type = 'l', lty = 1, col = viridis::viridis(S))
# sad
x <- nmat[ntstep, ]
x <- sort(x[x > 0], TRUE)
plot(x, log = 'y')