Multilevel modeling of community composition with imperfect detection

[This article was first published on Ecology in silico, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This is a guest post generously provided by Joe Mihaljevic.

A common goal of community ecology is to understand how and why species composition shifts across space. Common techniques to determine which environmental covariates might lead to such shifts typically rely on ordination of community data to reduce the amount of data. These techniques include redundancy analysis (RDA), canonical correspondence analysis (CCA), and nonmetric multi-dimensional scaling (NMDS), each paired with permutation tests. However, as brought to light by Jackson et al. (2012: Ecosphere), these ordination techniques do not discern species-level covariate effects, making it difficult to attribute community-level pattern shifts to species-level changes. Jackson et al. (2012) propose a hierarchical modeling framework as an alternative, which we extend in this post to correct for imperfect detection.

Multilevel models can estimate species-level random and fixed covariate effects to determine the relative contribution of environmental covariates to changing composition across space (Jackson et al. 2012). For presence/absence data, such models are often formulated as:

Here $y_q$ is a vector of presences/absence of each species at each site ($q=1, … , nm,$ where $n$ is the number of species and $m$ the number of sites). This model can be extended to incorporate multiple covariates.

We are interested in whether species respond differently to environmental gradients (e.g. elevation, temperature, precipitation). If this is the case, then we expect community composition changes along such gradients. Concretely, we are interested in whether $\sigma_{slope}^2$ for any covariate differs from zero.

Jackson et al. (2012) provide code for a maximum likelihood implementation of their model with data from Southern Appalachian understory herbs using the R package lme4. Here we present a simple extension of Jackson and colleague’s work, correcting for detection error with repeat surveys (i.e. multi-species occupancy modeling). Specifically, the above model could be changed slightly to:

Now $y_q$ is the number of times each species is observed at each site over $j$ surveys. $p_{spp[q]}$ represents the species-specific probability of detection when the species is present, and $z_q$ represents the ‘true’ occurence of the species, a Bernoulli random variable with probability, $\psi_q$.

To demonstrate the method, we simulate data for a 20 species community across 100 sites with 4 repeat surveys. We assume that three site-level environmental covariates were measured, two of which have variable affects on occurrence probabilities (i.e. random effects), and one of which has consistent effects for all species (i.e. a fixed effect). We also assumed that species-specific detection probabilities varied, but were independent of environmental covariates.

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
<span class="line"><span class="c1">################################################</span>
</span><span class="line"><span class="c1"># Simulate data</span>
</span><span class="line"><span class="c1">################################################</span>
</span><span class="line">
</span><span class="line">Nsite <span class="o"><-</span> <span class="m">100</span>
</span><span class="line">Ncov <span class="o"><-</span> <span class="m">3</span>
</span><span class="line">Nspecies <span class="o"><-</span> <span class="m">20</span>
</span><span class="line">J <span class="o"><-</span> <span class="m">4</span>
</span><span class="line">
</span><span class="line"><span class="c1"># species-specific intercepts:</span>
</span><span class="line">alpha <span class="o"><-</span> rnorm<span class="p">(</span>Nspecies<span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">1</span><span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="c1"># covariate values</span>
</span><span class="line">Xcov <span class="o"><-</span> matrix<span class="p">(</span>rnorm<span class="p">(</span>Nsite<span class="o">*</span>Ncov<span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">2</span><span class="p">),</span>
</span><span class="line">               nrow<span class="o">=</span>Nsite<span class="p">,</span> ncol<span class="o">=</span>Ncov<span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="c1"># I'll assume 2 of the 3 covariates have effects that vary among species</span>
</span><span class="line">Beta <span class="o"><-</span> array<span class="p">(</span>c<span class="p">(</span>rnorm<span class="p">(</span>Nspecies<span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">2</span><span class="p">),</span>
</span><span class="line">                rnorm<span class="p">(</span>Nspecies<span class="p">,</span> <span class="m">-1</span><span class="p">,</span> <span class="m">1</span><span class="p">),</span>
</span><span class="line">                rep<span class="p">(</span><span class="m">1</span><span class="p">,</span> Nspecies<span class="p">)</span>
</span><span class="line">                <span class="p">),</span>
</span><span class="line">              dim<span class="o">=</span>c<span class="p">(</span>Nspecies<span class="p">,</span> Ncov<span class="p">)</span>
</span><span class="line">              <span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="c1"># species-specific detection probs</span>
</span><span class="line">p0 <span class="o"><-</span> plogis<span class="p">(</span>rnorm<span class="p">(</span>Nspecies<span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0.5</span><span class="p">))</span>
</span><span class="line">p0
</span><span class="line">
</span><span class="line"><span class="c1">#### Occupancy states ####</span>
</span><span class="line">Yobs <span class="o"><-</span> array<span class="p">(</span><span class="m">0</span><span class="p">,</span> dim <span class="o">=</span> c<span class="p">(</span>Nspecies<span class="p">,</span> Nsite<span class="p">))</span> <span class="c1"># Simulated observations</span>
</span><span class="line">
</span><span class="line"><span class="kr">for</span><span class="p">(</span>n <span class="kr">in</span> <span class="m">1</span><span class="o">:</span>Nspecies<span class="p">){</span>
</span><span class="line">  <span class="kr">for</span><span class="p">(</span>k <span class="kr">in</span> <span class="m">1</span><span class="o">:</span>Nsite<span class="p">){</span>
</span><span class="line">    lpsi <span class="o"><-</span> alpha<span class="p">[</span>n<span class="p">]</span> <span class="o">+</span> Beta<span class="p">[</span>n<span class="p">,</span> <span class="p">]</span> <span class="o">%*%</span> Xcov<span class="p">[</span>k<span class="p">,</span> <span class="p">]</span> <span class="c1"># Covariate effects on occurrence </span>
</span><span class="line">    psi <span class="o"><-</span> <span class="m">1</span><span class="o">/</span><span class="p">(</span><span class="m">1</span><span class="o">+</span>exp<span class="p">(</span><span class="o">-</span>lpsi<span class="p">))</span> <span class="c1">#anti-logit</span>
</span><span class="line">
</span><span class="line">    z <span class="o"><-</span> rbinom<span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> psi<span class="p">)</span> <span class="c1"># True Occupancy</span>
</span><span class="line">    Yobs<span class="p">[</span>n<span class="p">,</span> k<span class="p">]</span> <span class="o"><-</span> rbinom<span class="p">(</span><span class="m">1</span><span class="p">,</span> J<span class="p">,</span> p0<span class="p">[</span>n<span class="p">]</span> <span class="o">*</span> z<span class="p">)</span> <span class="c1"># Observed Occupancy</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="c1">################################################</span>
</span><span class="line"><span class="c1"># Format data for model</span>
</span><span class="line"><span class="c1">################################################</span>
</span><span class="line"><span class="c1"># X needs to have repeated covariates for each species, long form</span>
</span><span class="line">X <span class="o"><-</span> array<span class="p">(</span><span class="m">0</span><span class="p">,</span> dim<span class="o">=</span>c<span class="p">(</span>Nsite<span class="o">*</span>Nspecies<span class="p">,</span> Ncov<span class="p">))</span>
</span><span class="line">t <span class="o"><-</span> <span class="m">1</span><span class="p">;</span> i <span class="o"><-</span> <span class="m">1</span>
</span><span class="line">TT <span class="o"><-</span> Nsite
</span><span class="line"><span class="kr">while</span><span class="p">(</span>i <span class="o"><=</span> Nspecies<span class="p">){</span>
</span><span class="line">  X<span class="p">[</span>t<span class="o">:</span>TT<span class="p">,</span> <span class="p">]</span> <span class="o"><-</span> Xcov
</span><span class="line">  t <span class="o"><-</span> t<span class="o">+</span>Nsite
</span><span class="line">  TT <span class="o"><-</span> TT <span class="o">+</span> Nsite
</span><span class="line">  i <span class="o"><-</span> i<span class="m">+1</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="c1"># Species</span>
</span><span class="line">Species <span class="o"><-</span> rep<span class="p">(</span>c<span class="p">(</span><span class="m">1</span><span class="o">:</span>Nspecies<span class="p">),</span> each<span class="o">=</span>Nsite<span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="c1"># Observations/data:</span>
</span><span class="line">Y <span class="o"><-</span> <span class="kc">NULL</span>
</span><span class="line"><span class="kr">for</span><span class="p">(</span>i <span class="kr">in</span> <span class="m">1</span><span class="o">:</span>Nspecies<span class="p">){</span>
</span><span class="line">  Y <span class="o"><-</span> c<span class="p">(</span>Y<span class="p">,</span> Yobs<span class="p">[</span>i<span class="p">,</span> <span class="p">])</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="c1"># All sites surveyed same # times:</span>
</span><span class="line">J <span class="o"><-</span> rep<span class="p">(</span>J<span class="p">,</span> times<span class="o">=</span>Nspecies<span class="o">*</span>Nsite<span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="c1"># Number of total observations</span>
</span><span class="line">Nobs <span class="o"><-</span> Nspecies<span class="o">*</span>Nsite
</span>

We fit the following model with JAGS with vague priors.

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
<span class="line">model <span class="p">{</span>
</span><span class="line">  <span class="c1"># Priors</span>
</span><span class="line">  psi.mean <span class="o">~</span> dbeta<span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">)</span>
</span><span class="line">  p.detect.mean <span class="o">~</span> dbeta<span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">  sd.psi <span class="o">~</span> dunif<span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">10</span><span class="p">)</span>
</span><span class="line">  psi.tau <span class="o"><-</span> pow<span class="p">(</span>sd.psi<span class="p">,</span> <span class="m">-2</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">  sd.p.detect <span class="o">~</span> dunif<span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">10</span><span class="p">)</span>
</span><span class="line">  p.detect.tau <span class="o"><-</span> pow<span class="p">(</span>sd.p.detect<span class="p">,</span> <span class="m">-2</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">  <span class="kr">for</span><span class="p">(</span>i <span class="kr">in</span> <span class="m">1</span><span class="o">:</span>Nspecies<span class="p">){</span>
</span><span class="line">    alpha<span class="p">[</span>i<span class="p">]</span> <span class="o">~</span> dnorm<span class="p">(</span>logit<span class="p">(</span>psi.mean<span class="p">),</span> psi.tau<span class="p">)</span><span class="k-Variable">T</span><span class="p">(</span><span class="m">-12</span><span class="p">,</span><span class="m">12</span><span class="p">)</span>
</span><span class="line">    lp.detect<span class="p">[</span>i<span class="p">]</span> <span class="o">~</span> dnorm<span class="p">(</span>logit<span class="p">(</span>p.detect.mean<span class="p">),</span> p.detect.tau<span class="p">)</span><span class="k-Variable">T</span><span class="p">(</span><span class="m">-12</span><span class="p">,</span><span class="m">12</span><span class="p">)</span>
</span><span class="line">    p.detect<span class="p">[</span>i<span class="p">]</span> <span class="o"><-</span> exp<span class="p">(</span>lp.detect<span class="p">[</span>i<span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="m">1</span> <span class="o">+</span> exp<span class="p">(</span>lp.detect<span class="p">[</span>i<span class="p">]))</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line">  <span class="kr">for</span><span class="p">(</span>j <span class="kr">in</span> <span class="m">1</span><span class="o">:</span>Ncov<span class="p">){</span>
</span><span class="line">    mean.beta<span class="p">[</span>j<span class="p">]</span> <span class="o">~</span> dnorm<span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">0.01</span><span class="p">)</span>
</span><span class="line">    sd.beta<span class="p">[</span>j<span class="p">]</span> <span class="o">~</span> dunif<span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">10</span><span class="p">)</span>
</span><span class="line">    tau.beta<span class="p">[</span>j<span class="p">]</span> <span class="o"><-</span> pow<span class="p">(</span>sd.beta<span class="p">[</span>j<span class="p">]</span><span class="m">+0.001</span><span class="p">,</span> <span class="m">-2</span><span class="p">)</span>
</span><span class="line">    <span class="kr">for</span><span class="p">(</span>i <span class="kr">in</span> <span class="m">1</span><span class="o">:</span>Nspecies<span class="p">){</span>
</span><span class="line">      betas<span class="p">[</span>i<span class="p">,</span>j<span class="p">]</span> <span class="o">~</span> dnorm<span class="p">(</span>mean.beta<span class="p">[</span>j<span class="p">],</span> tau.beta<span class="p">[</span>j<span class="p">])</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line">  <span class="c1"># Likelihood</span>
</span><span class="line">  <span class="kr">for</span><span class="p">(</span>i <span class="kr">in</span> <span class="m">1</span><span class="o">:</span>Nobs<span class="p">){</span>
</span><span class="line">    logit<span class="p">(</span>psi<span class="p">[</span>i<span class="p">])</span> <span class="o"><-</span> alpha<span class="p">[</span>Species<span class="p">[</span>i<span class="p">]]</span> <span class="o">+</span> inprod<span class="p">(</span>betas<span class="p">[</span>Species<span class="p">[</span>i<span class="p">],],</span> X<span class="p">[</span>i<span class="p">,</span> <span class="p">])</span>
</span><span class="line">    z<span class="p">[</span>i<span class="p">]</span> <span class="o">~</span> dbern<span class="p">(</span>psi<span class="p">[</span>i<span class="p">])</span>
</span><span class="line">    Y<span class="p">[</span>i<span class="p">]</span> <span class="o">~</span> dbinom<span class="p">(</span>z<span class="p">[</span>i<span class="p">]</span> <span class="o">*</span> p.detect<span class="p">[</span>Species<span class="p">[</span>i<span class="p">]],</span> J<span class="p">[</span>i<span class="p">])</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span>

Using information theory, specifically Watanabe-Akaike information criteria (WAIC), we compared this model, which assumes all covariates have random effects, to all combination of models varying whether each covariate has fixed or random effects. See this GitHub repository for all model statements and code.

A model that assumes all covariates have random effects, and the data-generating model, in which only covariates 1 and 2 have random effects, performed the best, but were indistinguishable from one another:

This result makes sense because the model with all random effects is able to recover species-specific responses to site-level covariates very well:

However, this model estimates that the 95% HDI of $\sigma_{slope}$ of covariate 3 includes zero, indicating that this covariate effectively has a fixed, rather than random effect among species.

Thus, we could conclude that the first two covariates have random effects, while the third covariate has a fixed effect. This means that composition shifts along gradients of covariates 1 and 2. We can visualize the relative contribution of covariate 1 and 2’s random effects to composition using ordination, as discussed in Jackson et al. (2012). To do this, we compare the linear predictor (i.e. $logit^{-1}(\psi_q)$) of the best model that includes only significant random effects to a model that does not have any random effects.

The code to extract linear predictors and ordinate the community is provided on GitHub:

To leave a comment for the author, please follow the link and comment on their blog: Ecology in silico.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)