-
Notifications
You must be signed in to change notification settings - Fork 1
/
GLM-GLMM-PGLMM.Rmd
353 lines (227 loc) · 9.94 KB
/
GLM-GLMM-PGLMM.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
349
350
351
352
---
title: |
| Supplemental Material for
| 'Niche estimation above and below the species level'
author:
- name: Adam B. Smith
affilnum: 1
email: adam.smith@mobot.org
- name: William Godsoe
affilnum: 2
- name: Francisco Rodríguez-Sánchez
affilnum: 3
- name: Hsiao-Hsuan Wang
affilnum: 4
- name: Dan Warren
affilnum: '5,6'
affiliation:
- affilnum: 1
affil: Center for Conservation and Sustainable Development, Missouri Botanical Garden, 4344 Shaw Boulevard, Saint Louis, MO 63116 USA
- affilnum: 2
affil: BioProtection Research Centre, Burns Building Lincoln University, Ellesmere Junction Road/Springs Road Lincoln University, Lincoln, New Zealand
- affilnum: 3
affil: Department of Integrative Ecology, Estación Biológica de Doñana, Consejo Superior de Investigaciones Científicas, Avda. Américo Vespucio 26, 41092 Sevilla, Spain
- affilnum: 4
affil: Department of Wildlife and Fisheries Sciences, Texas A&M University, 534 John Kimbrough Blvd., Building 1537, 2258 TAMU, College Station, Texas 77843, USA
- affilnum: 5
affil: Senckenberg Biodiversity and Climate Research Center (SBiK-F), Frankfurt am Main, Germany
- affilnum: 6
affil: Biodiversity and Biocomplexity Unit, Okinawa Institute of Science and Technology, Japan
output:
pdf_document:
fig_caption: yes
fig_width: 5
fig_height: 5
keep_tex: yes
number_sections: no
template: manuscript.latex
includes:
in_header: header.tex
documentclass: article
classoption: a4paper
capsize: normalsize
fontsize: 11pt
geometry: margin=1in
linenumbers: no
spacing: doublespacing
footerdate: no
abstract: no
---
## R code for comparing splitting, lumping, partial pooling, and phylogenetically-informed ecological niche models on a simulated dataset (*Box 3*)
This source code is archived in Zenodo (Rodríguez-Sánchez 2018) and can also be found at https://github.com/Pakillo/phyloENM-TREE.
### Simulate taxa niches and phylogeny
Here we simulate (following Pearse *et al.* 2016) a group of phylogenetically related taxa with different sentivities to temperature, so that closely related taxa will have more similar niches.
```{r}
nspp <- 6
nsite <- 30
# simulate a phylogenetic tree
library(ape)
set.seed(6)
phy <- rtree(n = nspp)
phy <- compute.brlen(phy, method = "Grafen", power = 0.5)
#plot(phy)
# standardize the phylogenetic covariance matrix to have determinant 1
Vphy <- vcv(phy)
Vphy.std <- Vphy/(det(Vphy)^(1/nspp))
# Perform a Cholesky decomposition of Vphy
iD <- t(chol(Vphy.std))
# Generate environmental site variable
env <- seq(0, 20, length.out = nsite)
## Set up species-specific regression coefficients as random effects
set.seed(79)
intercept <- iD %*% runif(nspp, -1, 2)
slope <- iD %*% runif(nspp, -0.4, 0.1)
intercept <- intercept[gtools::mixedorder(rownames(intercept)), ]
slope <- slope[gtools::mixedorder(rownames(slope)), ]
#intercept
#slope
## Calculate suitabilities for each taxa and site
suitab <- rep(intercept, each = nsite)
suitab <- suitab + rep(slope, each = nsite) * rep(env, nspp)
suitab.error <- suitab + rnorm(nspp * nsite, mean = 0, sd = 1) #add some random 'error'
suitab.invlogit <- arm::invlogit(suitab)
suitab.error.invlogit <- arm::invlogit(suitab.error)
set.seed(114)
pres <- rbinom(length(suitab.error), size = 1, prob = suitab.error.invlogit) # pres-abs
dat <- data.frame(taxa = paste("t", sort(rep(1:nspp, nsite)), sep = ""),
site = rep(1:nsite, nspp),
env = rep(env, nspp),
suitab.invlogit = suitab.invlogit,
presabs = pres)
```
### Plot simulated niches
```{r message = FALSE}
library(ggplot2)
library(cowplot)
library(viridis)
simul.niches <- ggplot(dat, aes(env, suitab.invlogit, colour = taxa)) +
ylim(0, 1) +
labs(x = "Temperature", y = "Suitability", title = "True niches") +
geom_line(size = 2) +
scale_color_viridis(discrete = TRUE) +
theme(plot.title = element_text(size = 18)) +
theme(legend.position = "none")
simul.niches
```
### Plot phylogeny
```{r message=FALSE}
library(ggtree)
phylog <- ggplot(phy) +
geom_tree(size = 2, colour = "grey70") +
geom_tree(size = 2, aes(color = label)) +
scale_color_viridis(discrete = "TRUE") +
theme(legend.position = "none",
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank()) +
labs(title = "Phylogeny") +
theme(plot.title = element_text(size = 18)) +
theme(plot.margin = unit(c(0.2,1,1,1), "cm"))
phylog
```
### Splitting
Here we fit independent binomial generalized linear models (GLM) to each taxon.
```{r}
split <- ggplot(dat, aes(env, presabs, colour = taxa)) +
ylim(0, 1) +
labs(x = "Temperature", y = "Suitability", title = "Splitting (GLM)") +
geom_smooth(method = "glm", method.args = list(family = "binomial"), se = FALSE, size = 2) +
scale_color_viridis(discrete = TRUE) +
theme(plot.title = element_text(size = 18)) +
theme(legend.position = "none")
split
```
### Lumping
Here we fit a single binomial generalized linear model (GLM) to all taxa together.
```{r}
lump <- ggplot(dat, aes(env, presabs)) +
ylim(0, 1) +
labs(x = "Temperature", y = "Suitability", title = "Lumping (GLM)") +
geom_smooth(method = "glm", method.args = list(family = "binomial"), se = FALSE, size = 3,
colour = "black") +
# for comparison with real niches:
geom_line(aes(env, suitab.invlogit, colour = taxa), size = 2, alpha = 0.2) +
scale_color_viridis(discrete = TRUE, guide = guide_legend(override.aes = list(alpha = 1))) +
theme(plot.title = element_text(size = 18)) +
theme(legend.position = "none")
lump
```
### Pooling (GLMM)
Here we fit a mixed effects model (GLMM) with varying intercepts and slopes, so that there is partial pooling among taxa.
```{r message=FALSE}
library(lme4)
mixed <- glmer(presabs ~ env + (1 + env | taxa), data = dat, family = binomial)
# summary(mixed)
# coef(mixed)
dat$mixedpred <- fitted(mixed, newdata = dat, type = "response")
mixedfig <- ggplot(dat, aes(env, mixedpred, colour = taxa)) +
ylim(0, 1) +
labs(x = "Temperature", y = "Suitability", title = "Pooling (GLMM)") +
geom_line(size = 2) +
scale_color_viridis(discrete = TRUE) +
theme(plot.title = element_text(size = 18)) +
theme(legend.position = "none")
mixedfig
```
### Phylogenetic pooling (PGLMM)
Here we fit a phylogenetic generalized linear model (PGLMM) with varying intercepts and slopes, so that closely related taxa tend to have more similar niches.
```{r cache=TRUE, message=FALSE}
inv.phylo <- MCMCglmm::inverseA(phy, nodes = "TIPS", scale = TRUE)
A <- solve(inv.phylo$Ainv)
rownames(A) <- rownames(inv.phylo$Ainv)
library(brms)
pglmm <- brm(
presabs ~ env + (1 + env | taxa),
data = dat,
family = bernoulli(),
cov_ranef = list(taxa = A),
prior = c(
prior(normal(0, 3), "b"),
prior(normal(0, 3), "Intercept"),
prior(student_t(3, 0, 3), "sd")), # sd of group random effect
chains = 3, cores = 3, iter = 2000,
control = list(adapt_delta = 0.96)
)
# summary(pglmm)
# plot(pglmm)
# coef(pglmm)
# pp_check(pglmm)
dat$pglmm <- fitted(pglmm, newdata = dat, scale = "response")[,1]
```
```{r}
pglmmfig <- ggplot(dat, aes(env, pglmm, colour = taxa)) +
ylim(0, 1) +
labs(x = "Temperature", y = "Suitability", title = "Phylogenetic Pooling (PGLMM)") +
geom_line(size = 2) +
scale_color_viridis(discrete = TRUE) +
theme(plot.title = element_text(size = 18)) +
theme(legend.position = "none")
pglmmfig
```
### Combined figure
```{r out.height='9in', out.width='7in'}
figure <- plot_grid(simul.niches, phylog,
split, lump,
mixedfig, pglmmfig,
nrow = 3, ncol = 2)
#figure
save_plot(paste0("FigureBox3", ".pdf"), figure, base_height = 11, base_width = 9)
```
### References and software used
Bates, Douglas, Martin Mächler, Ben Bolker, and Steve Walker. 2015. “Fitting Linear Mixed-Effects Models Using lme4.” Journal of Statistical Software 67 (1): 1–48. doi:10.18637/jss.v067.i01.
Bürkner, Paul-Christian. 2017. “brms: An R Package for Bayesian Multilevel Models Using Stan.” Journal of Statistical Software 80 (1): 1–28. doi:10.18637/jss.v080.i01.
Garnier, Simon. 2018. Viridis: Default Color Maps from ’Matplotlib’. https://CRAN.R-project.org/package=viridis.
Hadfield, Jarrod D. 2010. “MCMC Methods for Multi-Response Generalized Linear Mixed Models: The MCMCglmm R Package.” Journal of Statistical Software 33 (2): 1–22. http://www.jstatsoft.org/v33/i02/.
Paradis, E., J. Claude, and K. Strimmer. 2004. “APE: Analyses of Phylogenetics and Evolution in R Language.” Bioinformatics 20: 289–90.
Pearse WD, MW Cadotte, J Cavender-Bares, AR Ives, C Tucker, S Walker & MR Helmus. 2016. A gentle introduction to Phylogenetic Generalised Linear Mixed Models. https://cran.r-project.org/web/packages/pez/vignettes/pez-pglmm-overview.pdf
R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Rodríguez-Sánchez F. 2018. R code for comparing splitting, lumping, partial pooling, and phylogenetically-informed ecological niche models (version 1.1). Zenodo. http://doi.org/10.5281/zenodo.1470855
Wickham, Hadley. 2009. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. http://ggplot2.org.
Wilke, Claus O. 2017. Cowplot: Streamlined Plot Theme and Plot Annotations for ’Ggplot2’. https://CRAN.R-project.org/package=cowplot.
Xie, Yihui. 2018. Knitr: A General-Purpose Package for Dynamic Report Generation in R. https://yihui.name/knitr/.
Yu, Guangchuang, David Smith, Huachen Zhu, Yi Guan, and Tommy Tsan-Yuk Lam. 2017. “Ggtree: An R Package for Visualization and Annotation of Phylogenetic Trees with Their Covariates and Other Associated Data.” Methods in Ecology and Evolution. doi:10.1111/2041-210X.12628.
### Details on computation environment
```{r echo=FALSE}
sessioninfo::session_info()
```