forked from saramortara/niche_neutral
-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.r
345 lines (262 loc) · 14.1 KB
/
code.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
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
######################################################################################
# Partitioning niche and neutral dynamics on community assembly #####################
# Mortara et al ######################################################################
#####################################################################################
## Code for applying the niche-neutral GLMM framework
## Data and analysis from the manuscript
#############################
# PART 1: loading packages #
############################
# required packages
library(bbmle)
library(lme4)
#library(optimx)
library(xtable)
library(piecewiseSEM)
library(dplyr)
source("r2_table.R")
#############################
# PART 2: loading data ######
############################
fern.data <- read.csv("fern_data_Mortaraetal.csv", header=TRUE)
head(fern.data)
fern.data$site <- scale(rep(1:30, length(unique(fern.data$species))))
#####################################################################
# PART 3: building the model to represent our hypothesis ############
# Step by step building models corresponding to general hypothesis #
####################################################################
# Ecological Strategy defined by all the three traits: laminar thickness, life form and indumentum interacting with altitude, drift among species sharing the same ES, local and regional limited dispersal
m.full <- glmer(abundance ~ thickness*alt_std + thickness*I(alt_std^2)
#+ indumentum*alt_std + indumentum*I(alt_std^2)
+ life_form*alt_std + life_form*I(alt_std^2)
+ (1|species) + (1|species:mountain) + (1|species:site) + (1|site),
data=fern.data, family="poisson",
control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=5e6)))
head(fern.data)
unique(fern.data$life_form)
fern.data$ep <- ifelse(fern.data$life_form!='ep', 'non.ep', 'ep')
head(fern.data)
unique(fern.data$ep)
m.full.lf <- glmer(abundance ~ #thickness*alt_std + thickness*I(alt_std^2)
#+ indumentum*alt_std + indumentum*I(alt_std^2)
ep*alt_std + ep*I(alt_std^2)
+ (1|species) + (1|species:mountain) + (1|species:site) + (1|site),
data=fern.data, family="poisson",
control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=5e6)))
m.full2 <- glmer(abundance ~ alt_std + I(alt_std^2)
+ (1|species) + (1|species:mountain) + (1|species:site) + (1+alt_std|species),
data=fern.data, family="poisson",
control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=5e6)))
m.neutral <- glmer(abundance ~ (1|species) + (1|species:mountain) + (1|species:site) + (1 |site),
data=fern.data, family="poisson",
control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=1e6)))
m.niche <- glmer(abundance ~ thickness*alt_std + thickness*I(alt_std^2)
#+ indumentum*alt_std + indumentum*I(alt_std^2)
+ life_form*alt_std + life_form*I(alt_std^2)
+ (1|species) + (1|site),
data=fern.data, family="poisson",
control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=5e6)))
m.env <- glmer(abundance ~ alt_std + I(alt_std^2)
+ (1|species) + (1|site) + (1+alt_std|species),
data=fern.data, family="poisson",
control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=5e6)))
m.null <- glmer(abundance ~ 1 +
(1|species) + (1|mountain) + (1|site),
data=fern.data, family="poisson",
control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=5e6)))
m.list <- list(m.full, m.neutral, m.niche, m.null, m.full2, m.env, m.full.lf)
bic.tab <- sapply(m.list, BIC)
mod.names <- c("niche & neutral", "neutral", "niche", "null", "env & neutral", "env", "lifeform & neutral")
names(bic.tab) <- mod.names
sort(bic.tab)
r2.table(m.neutral)
r2.tab <- sapply(m.list, r2.table)
r2.tab <- bind_rows(r2.tab)
row.names(r2.tab) <- mod.names
r2.tab
#####################################################################
# PART 4: Calculating predicted values from best model #############
####################################################################
# First, we create a data frame with all combination of sites, species and traits
comb.table <- data.frame(expand.grid(mountain=levels(fern.data$mountain),
alt_std=unique(fern.data$alt_std),
site=unique(fern.data$site),
species=unique(fern.data$species)),
life_form=fern.data$life_form,
thickness=fern.data$thickness,
indumentum=fern.data$indumentum)
comb.table <- na.omit(comb.table)
# Second, we use the function predict to create a data frame of predicted values for all possible combinations based on the best model m5.4.3
pred.values <- predict(m.full, re.form=NULL, newdata=comb.table,
type='response')
# Third we calculate mean predicted values and standard error for each altitude
## Predicted mean values
pred.table <- aggregate(pred.values, list(altitude=comb.table$alt_std, thickness=comb.table$thickness,
indumentum=comb.table$indumentum,
life_form=comb.table$life_form), mean)
names(pred.table)[5] <- "mean"
## Predicted stardard error
pred.table$se <- aggregate(pred.values, by=list(altitude=comb.table$alt_std, thickness=comb.table$thickness,
indumentum=comb.table$indumentum,
life_form=comb.table$life_form),
function(x)sd(x)/sqrt(length(x)))$x
head(pred.table)
# Finally, we calculate the upper and lower confidence interval based on t distribution
## Confidence Interval (mean +- standard error * t(pdf)
t.prev <- pt(pred.table$mean, df=(nrow(pred.table)-1))
pred.table$lower <- (pred.table$mean - pred.table$se)*t.prev
pred.table$upper <- (pred.table$mean + pred.table$se)*t.prev
# Second we create a data frame with observed mean values and its standard error
obs <- aggregate(fern.data$abundance, by=list(altitude=fern.data$alt_std, thickness=fern.data$thickness,
indumentum=fern.data$indumentum,
life_form=fern.data$life_form), mean)
## Observed standard error
obs$se <- aggregate(fern.data$abundance, by=list(altitude=fern.data$alt_std, thickness=fern.data$thickness,
indumentum=fern.data$indumentum,
life_form=fern.data$life_form),
function(x)sd(x)/sqrt(length(x)))$x
head(obs)
names(obs) <- c("Altitude", "thickness", "indumentum", "life_form", "Abundance", "std")
#############################################
######### Creating figures ##################
############################################
###############################################
######### DAQUI PRA FRENTE AINDA NAO FUNFA ####
###############################################
# all trait combinations
esp.hab <- expand.grid(c('membranacea', 'coriacea'), c('ausente','presente'), c('ter', 'hemi', 'ep'))
esp.hab
#########################################
#### GRAFICO MODELO #####################
#########################################
cor1 <-rgb(140, 1, 28, maxColorValue=255) #rgb(44, 152, 32, maxColorValue=255) # terrestre
cor3 <- rgb(4, 70, 120, maxColorValue=255) #rgb(239, 144, 33, maxColorValue=255) # hemi
cor2 <- rgb(199, 172, 29, maxColorValue=255) # ep
head(obs)
ep.cor.si <- subset(obs, thickness=="coriacea" & indumentum=="ausente" &life_form=="ep")
ep.cor.ci <- subset(obs, thickness=="coriacea" & indumentum=="presente" &life_form=="ep")
ep.mem.si <- subset(obs, thickness=="membranacea" & indumentum=="ausente" &life_form=="ep")
ep.mem.ci <- subset(obs, thickness=="membranacea" & indumentum=="presente" &life_form=="ep")
head(obs)
par(mfrow=c(1,2))
plot(Abundance ~ Altitude, data=ep.cor.si, log='x', ylim=c(0,20), col=cor1, las=1)
segments(x0=ep.cor.si[,1],
y0= ep.cor.si[,5] + ep.cor.si[,6],
y1= ep.cor.si[,5] - ep.cor.si[,6], col=cor1)
points(Abundance ~ Altitude, data=ep.cor.ci, pch=19,col=cor1)
segments(x0=ep.cor.ci[,1],
y0= ep.cor.ci[,5] + ep.cor.ci[,6],
y1= ep.cor.ci[,5] - ep.cor.ci[,6], col=cor1)
plot(Abundance ~ Altitude, data=ep.mem.si, log='x', ylim=c(0,20), col=cor1, las=1)
segments(x0=ep.mem.si[,1],
y0= ep.mem.si[,5] + ep.mem.si[,6],
y1= ep.mem.si[,5] - ep.mem.si[,6], col=cor1)
points(Abundance ~ Altitude, data=ep.mem.ci, pch=19, col=cor1)
segments(x0=ep.mem.ci[,1],
y0= ep.mem.ci[,5] + ep.mem.ci[,6],
y1= ep.mem.ci[,5] - ep.mem.ci[,6], col=cor1)
head(ep.cor.ci)
loadfonts(device = "postscript")
pdf("graf_modelo.pdf")
par(mai=c(0.5, 0.5, 0.2, 0.25), oma=c(1, 1, 1, 0.1))
layout(matrix(c(0, 0, 0, 0,
0, 1, 2, 0,
0, 3, 4, 0,
0, 5, 6, 0),4,4, byrow=TRUE),
widths=c(0.1, 1, 1, 0.1), heights=0.1)
for(i in 1:8){
plot(obs[obs$thickness==esp.hab[i,1] & obs$indumentum==esp.hab[i,2] & obs$life_form==esp.hab[i,3], c(1,5)],
pch=rep(c(21, 19), 3)[i], bty="l", xlab="", ylab="", cex=1.7, yaxt="n", xaxt="n", log='y')
#pt.bg='white' )#,
col=rep(c(cor1, cor2, cor3), each=2)[i],
ylim=rbind(c(0.1,40), c(0.1,40), c(0.1,120), c(0.1,120), c(0.1,23), c(0.1,23))[i,])
# controlando eixos
if(i %in% c(5,6)){
axis(1, at=unique(com.obs$Altitude), labels=unique(com.obs$Altitude), cex.axis=1.3)}
else{axis(1, at=unique(com.obs$Altitude), labels=FALSE)}
if(i %in% seq(1,6,2)){
axis(2, cex.axis=1.3, las=1)}
else{axis(2, labels=FALSE)}
# erro padrao obs
segments(x0=com.obs[com.obs$esp==esp.hab[i,1] & com.obs$hab==esp.hab[i,2], 1],
y0=com.obs[com.obs$esp==esp.hab[i,1] & com.obs$hab==esp.hab[i,2], 4] +
com.obs[com.obs$esp==esp.hab[i,1] & com.obs$hab==esp.hab[i,2], 5],
y1=com.obs[com.obs$esp==esp.hab[i,1] & com.obs$hab==esp.hab[i,2], 4] -
com.obs[com.obs$esp==esp.hab[i,1] & com.obs$hab==esp.hab[i,2], 5],
col=rep(c(cor1, cor2, cor3), each=2)[i])
## Previsto medio
lines(com.prev[com.prev$esp==esp.hab[i,1] & com.prev$hab==esp.hab[i,2], c(1,4)],
col=rep(c(cor1, cor2, cor3), each=2)[i])
## Intervalo de mais ou mesno 2 x se
lines(com.prev[com.prev$esp==esp.hab[i,1] & com.prev$hab==esp.hab[i,2], c(1,6)], lty=2,
col=rep(c(cor1, cor2, cor3), each=2)[i])
lines(com.prev[com.prev$esp==esp.hab[i,1] & com.prev$hab==esp.hab[i,2], c(1,7)], lty=2,
col=rep(c(cor1, cor2, cor3), each=2)[i])
mtext(paste(paste("(", letters[1:6][i], sep=""), ")", sep=""), side=3, adj=0.05, padj=-0.5, cex=1) #font=2
}
mtext("Mean species abundances (log)", side=2, outer=TRUE, padj=1, cex=1.2)
mtext("Altitude (m)", side=1, outer=TRUE, padj=-0.5, cex=1.2)
mtext("Membranaceous", side=3, adj=0.25, padj=1, outer=TRUE, font=2)
mtext("Coriaceous", side=3, outer=TRUE, adj=0.8, padj=1, font=2)
mtext("Terrestrial", side=4, outer=TRUE, padj=-1.7, adj=0.87, font=2)
mtext("Hemiepiphyte", side=4, outer=TRUE, padj=-1.7, font=2)
mtext("Epiphyte", side=4, outer=TRUE, padj=-1.7, adj=0.135, font=2)
dev.off()
embed_fonts("graf_modelo.eps", outfile = "graf_modelo.eps",
options = "-dEPSCrop")
#########################################
#### GRAFICO SADS #####################
#########################################
head(com.rank2)
head(atri)
atri.cor <- atri[,c(1, 26, 27)]
head(atri.cor)
atri.cor
atri.cor$comb <- NA
atri.cor$comb2 <- NA
head(atri.cor)
atri.cor$comb[atri.cor$habitoB=="ter" & atri.cor$espessuraB=="membranacea"] <- cor1
atri.cor$comb[atri.cor$habitoB=="ep" & atri.cor$espessuraB=="membranacea"] <- cor3
atri.cor$comb[atri.cor$habitoB=="hemi" & atri.cor$espessuraB=="membranacea"] <- cor2
atri.cor$comb[atri.cor$habitoB=="ter" & atri.cor$espessuraB=="coriacea"] <- cor1
atri.cor$comb[atri.cor$habitoB=="ep" & atri.cor$espessuraB=="coriacea"] <- cor3
atri.cor$comb[atri.cor$habitoB=="hemi" & atri.cor$espessuraB=="coriacea"] <- cor2
atri.cor$comb2[atri.cor$habitoB=="ter" & atri.cor$espessuraB=="membranacea"] <- 1
atri.cor$comb2[atri.cor$habitoB=="ep" & atri.cor$espessuraB=="membranacea"] <- 1
atri.cor$comb2[atri.cor$habitoB=="hemi" & atri.cor$espessuraB=="membranacea"] <- 1
atri.cor$comb2[atri.cor$habitoB=="ter" & atri.cor$espessuraB=="coriacea"] <- 19
atri.cor$comb2[atri.cor$habitoB=="ep" & atri.cor$espessuraB=="coriacea"] <- 19
atri.cor$comb2[atri.cor$habitoB=="hemi" & atri.cor$espessuraB=="coriacea"] <- 19
# funcao para plot das sads com abundancias relativas e atributos
cont.y <- c(1,4,7,10)
cont.x <- 8:10
graf.sad <- function(com=com.rank2, cor=atri.cor$comb, ponto=atri.cor$comb2){
par(mai=c(0.24, 0.6, 0.24, 0.05), oma=c(3, 3, 0.2, 0.1))
layout(matrix(c(1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11,0), 4, 3, byrow=TRUE))
for(i in 1:10){
plot(com.rank2[[i]], log="y", ylim=c(0.0004,0.5), xlim=c(0, 63),
col=cor[order(com.cota[i,],decreasing=TRUE )][1:riq.cota[i]],
pch=ponto[order(com.cota[i,],decreasing=TRUE )][1:riq.cota[i]],
bty="l", cex=1.9, cex.axis=1.5, xlab="", ylab="", las=1,
yaxt="n", xaxt="n")
mtext(paste(LETTERS[1:10][i], paste(unique(cota)[i], "m", sep=" "), sep=". "), adj=0.05, padj=-0.5, cex=1.2, font=2)
if(i %in% cont.y){
axis(2, las=1, cex.axis=1.5, at=c(0.0005, 0.002, 0.01, 0.05, 0.2), labels=c("0.0005", "0.002", "0.01", "0.05", "0.2"))
}
else{axis(2, at=c(0.0005, 0.002, 0.01, 0.05, 0.2), labels=rep(" ", 5))}
if(i %in% cont.x){
axis(1, las=1, cex.axis=1.5) }
else{axis(1, labels=FALSE)}
}
plot(0,0, axes=FALSE, xlab="", ylab="", col=0)
legend(x=-1.155, y=0.7, c("terrestrial and membranaceous", "terrestrial and coriaceous",
"hemiepiphyte and membranaceous", "hemiepiphyte and coriaceous",
"epiphyte and membranaceous", "epiphyte and coriaceous"),
pch=rep(c(1, 19), 3), col=rep(c(cor1, cor3, cor2), each=2), cex=1.5, pt.cex=1.6, bty="n")
mtext("Species Rank", 1, outer=TRUE, cex=1.3, padj=1)
mtext("Species Relative Abundances (log)", 2, outer=TRUE, cex=1.3, padj=-1)
}
save.image("Mortaraetal.RData")