Skip to content

Commit 607c7fd

Browse files
committed
Different improvment as suggeste d by Eric (effect of very rare species, changes in the look of figures in the working example, results from simulations).
2 parents 396de75 + 61cbfe6 commit 607c7fd

File tree

14 files changed

+447
-276
lines changed

14 files changed

+447
-276
lines changed

inst/doc/main_analyses.R

Lines changed: 140 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,51 @@ library(mgcv)
1010
library(FD)
1111

1212
## ------------------------------------------------------------------------
13-
f.plotFD <- function(d, title, tylab, ymin = -0.75, leg.cex = 0.8, lxtext = 3,
14-
ymax = 0.5, tticks = 0.25, yleg = c(-0.475, -0.59),
15-
xleg = c(350, 300)) {
13+
f.plotFD <- function(d, title, tylab, ymin = -0.8, leg.cex = 0.8, lxtext = 3,
14+
ymax = 0.6, tticks = 0.2, yleg = c(-0.475, -0.59),
15+
xleg = c(350, 300), confint = FALSE) {
1616
ele <- 400:2500
1717
xax <- seq(250, 2750, 250)
18-
tcol <- brewer.pal(8, "Set1")
18+
if(!confint) tcol <- brewer.pal(8, "Set1")
19+
if(confint) {
20+
tcol <- brewer.pal(4, "Paired")
21+
tcol <- paste0(tcol, c("50", "99", "50", 99))
22+
}
1923
plot(NA, ylim = c(ymin,ymax), xlim = c(250,2750), axes=F, xlab = "", ylab = "")
2024
farbe <- rep("grey80", nplots)
2125
farbe[d$dif & d$obs < d$cor] <- tcol[1]
2226
farbe[d$dif & d$obs > d$cor] <- tcol[2]
23-
points(d$elevation, d$obs, pch = 21, cex = 0.5, col = "grey50")
24-
points(d$elevation, d$obs, pch = 16, cex = 0.5, col = farbe)
27+
if(!confint) {
28+
points(d$elevation, d$obs, pch = 21, cex = 0.5, col = "grey50")
29+
points(d$elevation, d$obs, pch = 16, cex = 0.5, col = farbe)
30+
}
31+
if(confint) {
32+
nsim <- 1000
33+
tmp <- array(0, dim = c(length(ele), nsim))
34+
for(s in 1:nsim) {
35+
tmp[, s] <- predict(gam(obs ~ s(elevation),
36+
data = d[sample(1:nrow(d), nrow(d), replace = TRUE),]),
37+
newdata = data.frame(elevation=ele), type = "response")
38+
}
39+
polygon(c(ele, rev(ele)),
40+
c(apply(tmp, 1, quantile, probs = 0.025),
41+
rev(apply(tmp, 1, quantile, probs = 0.975))), col = tcol[1], border = tcol[1])
42+
tmp <- array(0, dim = c(length(ele), nsim))
43+
for(s in 1:nsim) {
44+
tmp[, s] <- predict(gam(cor ~ s(elevation),
45+
data = d[sample(1:nrow(d), nrow(d), replace = TRUE),]),
46+
newdata = data.frame(elevation=ele), type = "response")
47+
}
48+
polygon(c(ele, rev(ele)),
49+
c(apply(tmp, 1, quantile, probs = 0.025),
50+
rev(apply(tmp, 1, quantile, probs = 0.975))), col = tcol[3], border = tcol[3])
51+
}
2552
pred <- predict(gam(obs ~ s(elevation), data = d),
2653
newdata = data.frame(elevation=ele), type = "response")
27-
points(ele, pred, ty = "l", lty = 2)
54+
points(ele, pred, ty = "l", lty = 2, col = ifelse(confint, tcol[2], "black"))
2855
pred <- predict(gam(cor ~ s(elevation), data = d),
2956
newdata = data.frame(elevation=ele), type = "response")
30-
points(ele, pred, ty = "l")
57+
points(ele, pred, ty = "l", col = ifelse(confint, tcol[4], "black"))
3158
axis(side=1, at = xax, labels = rep("", length(xax)), pos=ymin)
3259
mtext(seq(500,2500,500), 1, at = seq(500,2500,500), cex = 0.7)
3360
axis(side = 2, at = seq(ymin, ymax, tticks), pos = 250, las = 1,
@@ -37,11 +64,114 @@ f.plotFD <- function(d, title, tylab, ymin = -0.75, leg.cex = 0.8, lxtext = 3,
3764
mtext(text = "Community Elevation", side = 1, line = 1, cex = 0.7)
3865
mtext(text = tylab, side = 2, line = lxtext, cex = 0.7)
3966
mtext(text = title, side = 3, at = -420, line = 1.5, cex = 0.8, adj = 0)
67+
if(!confint) {
4068
legend(xleg[1], yleg[1], c(" underestimated values",
4169
" overestimated values"), col = tcol[1:2],
4270
bty = "n", cex = leg.cex, pch = c(16,16), pt.cex = 1, y.intersp=0.8)
71+
}
4372
legend(xleg[2], yleg[2], c("prediction from observed communities",
44-
"detection corrected prediction"),
45-
bty = "n", cex = leg.cex, lty = c(2,1), y.intersp=0.8)
73+
"detection-corrected prediction"),
74+
bty = "n", cex = leg.cex, lty = c(2,1), y.intersp=0.8,
75+
col = ifelse(confint, tcol[c(2,4)], c("black", "black")))
4676
}
4777

78+
## ------------------------------------------------------------------------
79+
# Specific leaf area (SLA)
80+
f <- function(x) mean(traitmat$sla[as.logical(x)])
81+
used <- apply(commat, 1, f)
82+
f <- function(x) mean(traitmat_all$sla[as.logical(x)])
83+
allobs <- apply(commat_obs, 1, f)
84+
cor(used, allobs)
85+
86+
# Canopy height (CH)
87+
f <- function(x) mean(traitmat$ch[as.logical(x)])
88+
used <- apply(commat, 1, f)
89+
f <- function(x) mean(traitmat_all$ch[as.logical(x)])
90+
allobs <- apply(commat_obs, 1, f)
91+
cor(used, allobs)
92+
93+
# Seed mass (SM)
94+
f <- function(x) mean(traitmat$sm[as.logical(x)])
95+
used <- apply(commat, 1, f)
96+
f <- function(x) mean(traitmat_all$sm[as.logical(x)])
97+
allobs <- apply(commat_obs, 1, f)
98+
cor(used, allobs)
99+
100+
# Functional trait space (FRic)
101+
f <- function(x) convhulln(traitmat[as.logical(x),], "FA")$vol
102+
used <- apply(commat, 1, f)
103+
f <- function(x) convhulln(traitmat_all[as.logical(x),], "FA")$vol
104+
allobs <- apply(commat_obs, 1, f)
105+
cor(used, allobs)
106+
107+
## Calculate distance matrix from complete trait matrix
108+
dist_all <- as.matrix(dist(traitmat_all))
109+
110+
# Mean nearest neighbout distance (mnnd)
111+
f <- function(x) {
112+
sample.dis <- dist[as.logical(x),as.logical(x)]
113+
mean(apply(sample.dis, 1, function(x) min(x[x>0])))
114+
}
115+
used <- apply(commat, 1, f)
116+
f <- function(x) {
117+
sample.dis <- dist_all[as.logical(x),as.logical(x)]
118+
mean(apply(sample.dis, 1, function(x) min(x[x>0])))
119+
}
120+
allobs <- apply(commat_obs, 1, f)
121+
cor(used, allobs)
122+
123+
# Species richness SR
124+
used <- apply(commat, 1, sum)
125+
allobs <- apply(commat_obs, 1, sum)
126+
cor(used, allobs)
127+
128+
## ------------------------------------------------------------------------
129+
# Specific leaf area (SLA)
130+
f <- function(x) mean(traitmat$sla[as.logical(x)])
131+
used <- apply(commat, 1, f)
132+
f <- function(x) mean(traitmat_all$sla[as.logical(x)])
133+
allobs <- apply(commat_obs, 1, f)
134+
cor(used, allobs)
135+
136+
# Canopy height (CH)
137+
f <- function(x) mean(traitmat$ch[as.logical(x)])
138+
used <- apply(commat, 1, f)
139+
f <- function(x) mean(traitmat_all$ch[as.logical(x)])
140+
allobs <- apply(commat_obs, 1, f)
141+
cor(used, allobs)
142+
143+
# Seed mass (SM)
144+
f <- function(x) mean(traitmat$sm[as.logical(x)])
145+
used <- apply(commat, 1, f)
146+
f <- function(x) mean(traitmat_all$sm[as.logical(x)])
147+
allobs <- apply(commat_obs, 1, f)
148+
cor(used, allobs)
149+
150+
# Functional trait space (FRic)
151+
f <- function(x) convhulln(traitmat[as.logical(x),], "FA")$vol
152+
used <- apply(commat, 1, f)
153+
f <- function(x) convhulln(traitmat_all[as.logical(x),], "FA")$vol
154+
allobs <- apply(commat_obs, 1, f)
155+
cor(used, allobs)
156+
157+
## Calculate distance matrix from complete trait matrix
158+
dist_all <- as.matrix(dist(traitmat_all))
159+
160+
# Mean nearest neighbout distance (mnnd)
161+
f <- function(x) {
162+
sample.dis <- dist[as.logical(x),as.logical(x)]
163+
mean(apply(sample.dis, 1, function(x) min(x[x>0])))
164+
}
165+
used <- apply(commat, 1, f)
166+
f <- function(x) {
167+
sample.dis <- dist_all[as.logical(x),as.logical(x)]
168+
mean(apply(sample.dis, 1, function(x) min(x[x>0])))
169+
}
170+
allobs <- apply(commat_obs, 1, f)
171+
cor(used, allobs)
172+
173+
# Species richness SR
174+
used <- apply(commat, 1, sum)
175+
allobs <- apply(commat_obs, 1, sum)
176+
cor(used, allobs)
177+

0 commit comments

Comments
 (0)