/
BlockStructure.R
216 lines (194 loc) · 9.21 KB
/
BlockStructure.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
##############################################################################################
# Code accompanying the manuscript
# "Modularity and stability in ecological communities"
# by Jacopo Grilli & Stefano Allesina
##############################################################################################
# v. 0.1 --- June 2015
# For any question/comment/bug, please contact
# Stefano Allesina sallesina@uchicago.edu
##############################################################################################
# LICENSE:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##############################################################################################
# USE:
# This file shows how to use of the programs provided in the files "BuildMatrices.R"
# and "Predictions.R".
#
# There are three parts:
# Part 1: How to build the matrices
# Part 2: How to predict the eigenvalues
# Part 3: Examples taken from Figure 8 of the manuscript.
##############################################################################################
# code for building matrices
source("BuildMatrices.R")
# code for predicting eigenvalues and plotting
source("Predictions.R")
##############################################################################################
# 1. HOW TO BUILD THE MATRICES
##############################################################################################
# The file "BuildMatrices.R" contains the function BuildMatrices, which builds all the matrices
mats <- BuildMatrices(S = 1000, # size of the system
connectance = 0.1, # overall connectance (C in the manuscript)
a = 0.4, # proportion of species in the smaller subsystem (\alpha in the manuscript)
mu = 0.5, # mean of the coefficients in W
sigma = 1, # standard deviation coefficients in W
rho = -0.5, # correlation between pairs in W
Q = 0.48, # modularity
Distr = "Normal", # either "Normal" (as in the manuscript), or "FourCorner" (introduced in Allesina et al., Nature Communications 2015)
Eyeball = FALSE) # if TRUE, build a food web using the modified cascade model, as explained in the manuscript
# see the structure of the return object
str(mats)
# mats$parameters contains all the parameters
print(mats$parameters)
# mats$membership the subsystem membership (\gamma in the manuscript)
print(mats$membership[1:10])
# mats$matrices is a list of the three matrices M, A, and B
print(mats$matrices$M[1:10, 1:10])
##############################################################################################
# 2. PREDICTING THE EIGENVALUES
##############################################################################################
# In the case of random ecosystems (i.e., Eyeball == FALSE), the manuscript introduces
# analytical predictions for the rightmost eigenvalue (Re(\lambda_{M, 1})) when:
# 1. the subsystems have equal size (i.e., a = 1/2)
# 2. the network is perfectly modular (i.e., Cb = 0)
# 3. the network is perfectly bipartite (i.e., Cw = 0)
# The function Predictions in the file "Predictions.R" calculates the eigenvalues of M
# and (if it's one of the cases treated in the manuscript) the corresponding analytical prediction
# By default, the function also plots the predictions
pr <- Predictions(mats, plot = TRUE)
# The return object contains the observed as well as the predicted real part of the leading eigenvalue:
print(paste("Re(lambda_{M, 1}) Observed:", pr$ReL1.observed, "Predicted:", pr$ReL1.predicted))
# The object also stores the rightmost eigenvalue on the bulk
print(pr$Re.bulk)
# and rightmost outlier
print(pr$Re.outlier)
# The object also stores the eigenvalues of M
plot(pr$eigenvalues.M)
# For the corresponding unstructured case, repeat the same procedure, but setting Q = 0
##############################################################################################
# 3. EXAMPLES
##############################################################################################
# Interesting cases (those in Figure 8 in the supplementary information)
set.seed(10)
# Figure 8, Equal Size a)
EqualSize.a <- BuildMatrices(S = 1000,
connectance = 0.1,
a = 0.5,
mu = 0.5,
sigma = 1,
rho = -0.5,
Q = -0.2,
Distr = "Normal",
Eyeball = FALSE)
pr <- Predictions(EqualSize.a)
# We can change the distribution to a discrete one (See Allesina et al. Nature Communications, 2015)
# and the results are qualitatively the same
EqualSize.a1 <- BuildMatrices(S = 1000,
connectance = 0.1,
a = 0.5,
mu = 0.5,
sigma = 1,
rho = -0.5,
Q = -0.2,
Distr = "FourCorner",
Eyeball = FALSE)
pr <- Predictions(EqualSize.a1)
# Figure 8, Equal Size b)
EqualSize.b <- BuildMatrices(S = 1000,
connectance = 0.2,
a = 0.5,
mu = -0.5,
sigma = 1,
rho = 0.5,
Q = 0,
Distr = "Normal",
Eyeball = FALSE)
pr <- Predictions(EqualSize.b)
# Figure 8, Equal Size c)
EqualSize.c <- BuildMatrices(S = 1000,
connectance = 0.4,
a = 0.5,
mu = 0.5,
sigma = 1,
rho = 0,
Q = 0.2,
Distr = "Normal",
Eyeball = FALSE)
pr <- Predictions(EqualSize.c)
# Now perfectly modular structures
# Figure 8, Perfectly Modular a)
Modular.a <- BuildMatrices(S = 1000,
connectance = 0.1,
a = 0.4,
mu = 1.0,
sigma = 1,
rho = -0.5,
Q = 0.48,
Distr = "Normal",
Eyeball = FALSE)
pr <- Predictions(Modular.a)
# Figure 8, Perfectly Modular b)
Modular.b <- BuildMatrices(S = 1000,
connectance = 0.3,
a = 0.3,
mu = -1.0,
sigma = 1,
rho = -0.25,
Q = 0.42,
Distr = "Normal",
Eyeball = FALSE)
pr <- Predictions(Modular.b)
# Figure 8, Perfectly Modular c)
Modular.c <- BuildMatrices(S = 1000,
connectance = 0.4,
a = 0.1,
mu = 0.5,
sigma = 1,
rho = 0.35,
Q = 0.18,
Distr = "Normal",
Eyeball = FALSE)
pr <- Predictions(Modular.c)
# Finally, perfectly bipartite structures
# Figure 8, Perfectly Bipartite a)
Bipartite.a <- BuildMatrices(S = 1000,
connectance = 0.1,
a = 0.1,
mu = -0.5,
sigma = 1,
rho = -0.5,
Q = -0.82,
Distr = "Normal",
Eyeball = FALSE)
pr <- Predictions(Bipartite.a)
# Figure 8, Perfectly Bipartite b)
Bipartite.b <- BuildMatrices(S = 1000,
connectance = 0.25,
a = 0.25,
mu = -0.5,
sigma = 1,
rho = 0.5,
Q = -0.625,
Distr = "Normal",
Eyeball = FALSE)
pr <- Predictions(Bipartite.b)
# Figure 8, Perfectly Bipartite c)
Bipartite.c <- BuildMatrices(S = 1000,
connectance = 0.2,
a = 0.3,
mu = 0.25,
sigma = 1,
rho = -0.25,
Q = -0.58,
Distr = "Normal",
Eyeball = FALSE)
pr <- Predictions(Bipartite.c)