-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.Rmd
349 lines (288 loc) · 16.2 KB
/
README.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
---
title: "traitmatch"
author: "I. Bartomeus"
date: "1 Aug 2015"
output: html_document
---
# traitmatch: Package to predict trait matching from species interactions.
This document reproduces the analysis done in Bartomeus et al. 2015 (Functional Ecology) to show how it works.
To install the package run (only once):
```{r}
install.packages("devtools")
install.packages("GenSA")
install.packages("SDMTools")
require(devtools)
install_github("traitmatch", "ibartomeus")
require(traitmatch)
```
We first use data on predator and prey body size from Barnes et al. 2008. We load the data and logtransform it. The original data is multiplied by 10 to avoid problems with the integral of the normal distribution
```{r}
# Load data for Pred/Prey
#fish <- read.table("data/Barnes2008.txt", header = TRUE)
fish <- Barnes2008
# Each row represents an interaction
head(fish)
# Define the vectors for the predator and prey size
MPred = log10(fish$standardised_predator_length*10)
MPrey = log10(fish$si_prey_length*10)
```
Then we fit the `integrated_model` that integrates neutral and niche constraints. We use pairwise Prey-Predatory interactions (Tlevel vectors) and we asume that the distribution of preys is well characterized by this network (we use mean and standard deviation directly from the distribution of prey interactions). Here we constrain the parameters with a priori information. For example, the slope of the relationship has to be positive (the bigger the predator, the bigger the prey). Tuning the a priori assumptions is important to get good estimates of the parameters. With unconstrained estimates (MLE), some model optimization will struggle to find maximum likelihood estimates and yield erroneous parameter estimates, such as a vertical slope. It is therefore critical to check that the results make sense before accepting them as the MLE. This process is slow, especially for large datasets like this one. You can use the max.time (in seconds) to cut the process to e.g. 900 seconds (15 minutes). The default is 30 minutes. Note that the estimates can be bad if too little time is allowed for the simulated annealing (SA) to converge.
```{r}
?fit_it
mt <- 60 #Define max.time to 60 sec to run things fast. Set to minimum 900 for a decent estimation of parameters.
pars_pre <- fit_it(integrated_model,
Tlevel1 = MPrey,
Tlevel2 = MPred,
mean_Tlevel1 = mean(MPrey),
sd_Tlevel1 = sd(MPrey),
pars = c(a0 = 0, a1 = 0, b0 = 0, b1 = 0),
par_lo = c(a0 = -10, a1 = 0, b0 = -10, b1 = -10),
par_hi = c(a0 = 10, a1 = 10, b0 = 10, b1 = 10),
max.time = mt)
pars_pre
```
We can plot the predicted model:
```{r}
# With this model, the first estimate is the intercept and second number the slope.
?plot_pred
plot_pred(pars = pars_pre, Tlevel1 = MPrey,
Tlevel2 = MPred, xlab = "log (Predator body size)",
ylab = "log (prey body size)", pch = ".")
```
You may want to compare that with a pure niche model (not taking into account the abundances). We can fit the niche models as follow:
```{r}
pars_pre_niche <- fit_it(niche_model,
Tlevel1 = MPrey,
Tlevel2 = MPred,
mean_Tlevel1 = mean(MPrey),
sd_Tlevel1 = sd(MPrey),
pars = c(a0 = 0, a1 = 0, b0 = 0, b1 = 0),
par_lo = c(a0 = -10, a1 = 0, b0 = -10, b1 = -10),
par_hi = c(a0 = 10, a1 = 10, b0 = 10, b1 = 10),
max.time = mt)
plot_pred(pars = pars_pre_niche, Tlevel1 = MPrey,
Tlevel2 = MPred, xlab = "log (Predator body size)", ylab = "log (prey body size)",
pch = ".")
```
We can see in this case that the estimates are similar, but that the slope (alpha1) is flatter and the range a bit wider. We can compare the accuracy of the integrated, niche and neutral models by comparing their likelihood.
```{r}
# Compute likelihoods
lh_model <- -integrated_model(pars_pre, MPrey, MPred, mean(MPrey),sd(MPrey))
lh_niche <- -niche_model(pars_pre_niche, MPrey, MPred, mean(MPrey), sd(MPrey))
lh_neutral <- -neutral_model(pars = NULL, MPrey, MPred, mean(MPrey), sd(MPrey))
# Visualization
barplot(c(lh_model, lh_niche, lh_neutral), names.arg = c("integrated", "niche", "neutral"))
l1 <- c("Predation", lh_model, lh_niche, lh_neutral)
```
In this case, the integrated model is slightly better than the niche model, and the neutral model the worst.
Now we do the same for grasshopper hervibore data.
```{r}
# Load data for grasshoppers
#grass <- read.table("data/Deraison2014.txt", h = TRUE)
grass <- Deraison2014
head(grass)
# As is experimental data, we subset only the "presences" for illustrative purposes.
grass <- subset(grass, Herbivory > 0)
# We first fit a binary model, interaction/no interaction.
pars_grass_bin <- fit_it(integrated_model,
Tlevel1 = grass$P.Leaf.dry.matter.content,
Tlevel2 = grass$G.Incisive.strength,
mean_Tlevel1 = mean(grass$P.Leaf.dry.matter.content),
sd_Tlevel1 = sd(grass$P.Leaf.dry.matter.content),
max.time = mt)
# Note that in this case we use the default pars, which are quite unconstrained, but work well in this case.
pars_grass_bin
plot_pred(pars = pars_grass_bin, Tlevel1 = jitter(grass$P.Leaf.dry.matter.content),
Tlevel2 = jitter(grass$G.Incisive.strength), xlab = "Incisive strength",
ylab = "Leaf dry matter content")
# Compare with the likelihood of the three models
pars_grass_bin_niche = fit_it(niche_model,
Tlevel1 = grass$P.Leaf.dry.matter.content,
Tlevel2 = grass$G.Incisive.strength,
mean_Tlevel1 = mean(grass$P.Leaf.dry.matter.content),
sd_Tlevel1 = sd(grass$P.Leaf.dry.matter.content),
max.time = mt)
# Plot_pred(pars = pars_grass_bin_niche, Tlevel1 = jitter(grass$P.Leaf.dry.matter.content),
# Tlevel2 = jitter(grass$G.Incisive.strength), xlab = "Incisive strength", ylab = "Leaf dry matter content")
# Likelihoods
lh_model <- -integrated_model(pars_grass_bin, grass$P.Leaf.dry.matter.content,
grass$G.Incisive.strength,
mean(grass$P.Leaf.dry.matter.content),
sd(grass$P.Leaf.dry.matter.content))
lh_niche <- -niche_model(pars_grass_bin_niche, grass$P.Leaf.dry.matter.content,
grass$G.Incisive.strength,
mean(grass$P.Leaf.dry.matter.content),
sd(grass$P.Leaf.dry.matter.content))
lh_neutral <- -neutral_model(pars = NULL, grass$P.Leaf.dry.matter.content, grass$G.Incisive.strength,
mean(grass$P.Leaf.dry.matter.content),
sd(grass$P.Leaf.dry.matter.content))
barplot(c(lh_model, lh_niche, lh_neutral), names.arg = c("integrated", "niche", "neutral"))
l2 <- c("Hervibory_bin", lh_model, lh_niche, lh_neutral)
# Here the integrated is as good as the neutral model.
# Now we test the same data taking into account the frequency of interactions.
# Prepare the data
head(grass)
Incisive.strength <- c()
for(i in 1:nrow(grass)){
temp <- rep(grass$G.Incisive.strength[i], round(grass$Herbivory[i]))
Incisive.strength <- c(Incisive.strength,temp)
}
Dry.matter <- c()
for(i in 1:nrow(grass)){
temp <- rep(grass$P.Leaf.dry.matter.content[i], round(grass$Herbivory[i]))
Dry.matter <- c(Dry.matter,temp)
}
# Fit models
pars_grass_freq <- fit_it(integrated_model,
Tlevel1 = Dry.matter,
Tlevel2 = Incisive.strength,
mean_Tlevel1 = mean(grass$P.Leaf.dry.matter.content),
sd_Tlevel1 = sd(grass$P.Leaf.dry.matter.content),
max.time = mt)
# Note the distribution (mean and standard deviation) is unweigthed because the experiment had equal abundances of plant species. We can use this knowledge to atribute each plant equal weight.
pars_grass_freq
plot_pred(pars = pars_grass_freq, Tlevel1 = jitter(Dry.matter,100),
Tlevel2 = jitter(Incisive.strength), xlab = "Incisive strength", ylab = "Leaf dry matter content")
# Compare models
pars_grass_freq_niche <- fit_it(niche_model,
Tlevel1 = Dry.matter,
Tlevel2 = Incisive.strength,
mean_Tlevel1 = mean(grass$P.Leaf.dry.matter.content),
sd_Tlevel1 = sd(grass$P.Leaf.dry.matter.content),
max.time = mt)
# plot_pred(pars = pars_grass_freq_niche, Tlevel1 = jitter(Dry.matter,100),
# Tlevel2 = jitter(Incisive.strength), xlab = "Incisive strength", ylab = "Leaf dry matter content")
# Likelihood
lh_model <- -integrated_model(pars_grass_freq, Dry.matter, Incisive.strength,
mean(grass$P.Leaf.dry.matter.content),
sd(grass$P.Leaf.dry.matter.content))
lh_niche <- -niche_model(pars_grass_freq_niche, Dry.matter, Incisive.strength,
mean(grass$P.Leaf.dry.matter.content),
sd(grass$P.Leaf.dry.matter.content))
lh_neutral <- -neutral_model(pars = NULL, Dry.matter, Incisive.strength,
mean(grass$P.Leaf.dry.matter.content),
sd(grass$P.Leaf.dry.matter.content))
# Visualization
barplot(c(lh_model, lh_niche, lh_neutral), names.arg = c("integrated", "niche", "neutral"))
l3 <- c("Hervibory_freq", lh_model, lh_niche, lh_neutral)
```
And here is the code for the plants and pollinators:
```{r}
# Read data
#pols <- read.table("data/Bartomeus2008.txt", h = TRUE)
pols <- Bartomeus2008
head(pols)
# Transform body size to tongue lenght first based on Cariveau et al. Submitted.
require(devtools)
install_github("BeeIT", "ibartomeus") #just once
library(BeeIT)
head(pols)
pols$tongue <- ITtongue(pols$IT_mm,pols$family , mouthpart = "tongue")
#plot(pols$tongue, pols$IT_mm) #see the relationship if interested
#subset the plant data for later use.
plants <- unique(pols[,c("plant", "nectar_holder_depth_mm", "cover")])
# Fit models
pars_pols <- fit_it(integrated_model,
Tlevel1 = pols$nectar_holder_depth_mm,
Tlevel2 = log(pols$tongue),
mean_Tlevel1 = weighted_mean(plants$nectar_holder_depth_mm, plants$cover),
sd_Tlevel1 = weighted_sd(plants$nectar_holder_depth_mm, plants$cover),
max.time = mt)
# Note that here we have independent data on plant abundance (% cover). Hence we can use this data directly to fit the model.
# If interested, you can see that the inference from the network, is quite similar to the real cover
#plot(dnorm(x = seq(-7,10,1), weighted_mean(plants$nectar_holder_depth_mm, plants$cover),
# weighted_sd(plants$nectar_holder_depth_mm, plants$cover)), col = "red", type = "l")
#par(new = TRUE)
#plot(dnorm(x = seq(-7,10,1), mean(pols$nectar_holder_depth_mm),
# sd(pols$nectar_holder_depth_mm)), col = "blue", type = "l")
plot_pred(pars = pars_pols, Tlevel1 = jitter(pols$nectar_holder_depth_mm, 10),
Tlevel2 = log(pols$tongue), xlab = "log(Pollinator tongue size)", ylab = "Nectar depth")
# Model comparision
pars_pol_niche = fit_it(niche_model,
Tlevel1 = pols$nectar_holder_depth_mm,
Tlevel2 = log(pols$tongue),
mean_Tlevel1 = weighted_mean(plants$nectar_holder_depth_mm, plants$cover),
sd_Tlevel1 = weighted_sd(plants$nectar_holder_depth_mm, plants$cover),
max.time = mt)
plot_pred(pars = pars_pol_niche, Tlevel1 = jitter(pols$nectar_holder_depth_mm, 10),
Tlevel2 = log(pols$tongue), xlab = "log(Pollinator tongue size)", ylab = "Nectar depth")
# Likelihood
lh_model <- -integrated_model(pars_pols, pols$nectar_holder_depth_mm, log(pols$tongue),
weighted_mean(plants$nectar_holder_depth_mm, plants$cover),
weighted_sd(plants$nectar_holder_depth_mm, plants$cover))
lh_niche <- -niche_model(pars_pol_niche, pols$nectar_holder_depth_mm, log(pols$tongue),
weighted_mean(plants$nectar_holder_depth_mm, plants$cover),
sd(pols$nectar_holder_depth_mm))
lh_neutral <- -neutral_model(pars = NULL, pols$nectar_holder_depth_mm, log(pols$tongue),
weighted_mean(plants$nectar_holder_depth_mm, plants$cover),
weighted_sd(plants$nectar_holder_depth_mm, plants$cover))
barplot(c(lh_model, lh_niche, lh_neutral), names.arg = c("integrated", "niche", "neutral"))
l5 <- c("Pollintion", lh_model, lh_niche, lh_neutral)
```
Hosts and parasitoids. An example that doesn't work as well.
```{r}
# Read and format data
#host <- read.table("data/Tylianakis2008.txt", h = TRUE)
host <- Tylianakis2008
head(host)
host_body_length <- c()
for(i in 1:nrow(host)){
temp <- rep(host$host_body_length[i], host$freq[i])
host_body_length <- c(host_body_length, temp)
}
parasite_body_length <- c()
for(i in 1:nrow(host)){
temp <- rep(host$parasite_body_length[i], host$freq[i])
parasite_body_length <- c(parasite_body_length,temp)
}
# Fit models
pars_host <- fit_it(integrated_model,
Tlevel1 = host_body_length,
Tlevel2 = parasite_body_length,
mean_Tlevel1 = weighted_mean(host$host_body_length, host$freq),
sd_Tlevel1 = weighted_sd(host$host_body_length, host$freq),
par_lo = c(a0 = 0, a1 = -10, b0 = -10, b1 = -10),
par_hi = c(a0 = 10, a1 = 0, b0 = 10, b1 = 10),
max.time = mt)
# With unconstrained parameters, the model behaves weirdly, so we have to constrain the values to sensible limits. You can do that by looking at x and y axes ranges and think which slope values are possible.
pars_host
plot_pred(pars = pars_host, Tlevel1 = jitter(host_body_length),
Tlevel2 = jitter(parasite_body_length), xlab = "Parasite body size", ylab = "Host body size")
# As you can see everything is predicted.
# Model comparison
pars_host_niche <- fit_it(niche_model,
Tlevel1 = host_body_length,
Tlevel2 = parasite_body_length,
mean_Tlevel1 = weighted_mean(host$host_body_length, host$freq),
sd_Tlevel1 = weighted_sd(host$host_body_length, host$freq),
par_lo = c(a0 = 0, a1 = -10, b0 = -10, b1 = -10),
par_hi = c(a0 = 10, a1 = 0, b0 = 10, b1 = 10),
max.time = mt)
plot_pred(pars = pars_host_niche, Tlevel1 = host_body_length,
Tlevel2 = parasite_body_length, xlab = "Parasite body size", ylab = "Host body size")
# Likelihoods
lh_model <- -integrated_model(pars_host, host_body_length, parasite_body_length,
weighted_mean(host$host_body_length, host$freq),
weighted_sd(host$host_body_length, host$freq))
lh_niche <- -niche_model(pars_host_niche, host_body_length, parasite_body_length,
weighted_mean(host$host_body_length, host$freq),
weighted_sd(host$host_body_length, host$freq))
lh_neutral <- -neutral_model(pars = NULL, host_body_length, parasite_body_length,
weighted_mean(host$host_body_length, host$freq),
weighted_sd(host$host_body_length, host$freq))
barplot(c(lh_model, lh_niche, lh_neutral), names.arg = c("integrated", "niche", "neutral"))
l4 <- c("Parasitsim", lh_model, lh_niche, lh_neutral)
```
Thats's it. You can also gather a summary table of parameters and likelihoods for all the models you have run:
```{r}
# Table of likelihoods
d <- rbind(l1,l2,l3,l4,l5)
d <- as.data.frame(d)
colnames(d) <- c("system", "integrated", "niche", "neutral")
d
dd <- rbind(pars_pre, pars_pre_niche, pars_grass_bin, pars_grass_bin_niche, pars_grass_freq,
pars_grass_freq_niche, pars_host, pars_host_niche, pars_pols, pars_pol_niche)
dd <- as.data.frame(dd)
colnames(dd) <- c("a0", "a1", "b0", "b1")
dd
```