---
title: "Explicit Interaction Community Models"
author: Miguel Porto, Pedro Beja^[CIBIO/InBio – Centro de Investigação em Biodiversidade e Recursos Genéticos, Universidade do Porto, Campus de Vairão, 4485-661 Vairão, Portugal; Instituto Superior de Agronomia, Universidade de Lisboa, Tapada da Ajuda, 1349-017 Lisboa, Portugal]
affiliation: "CIBIO/InBio – Centro de Investigação em Biodiversidade e Recursos Genéticos, Universidade do Porto, Campus de Vairão, 4485-661 Vairão, Portugal; Instituto Superior de Agronomia, Universidade de Lisboa, Tapada da Ajuda, 1349-017 Lisboa, Portugal"
package: eicm
output: 
  rmarkdown::html_document:
    toc: TRUE
    toc_float:
      collapsed: FALSE
  rmarkdown::pdf_document: default
vignette: >
  %\VignetteIndexEntry{Explicit Interaction Community Models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}  
---

```{r setup, include=FALSE}
knitr::knit_hooks$set(timeit = local({
  now = NULL
  function(before, options) {
    if (before) {
      now <<- Sys.time()
    } else {
      res = difftime(Sys.time(), now, units="secs")
      now <<- NULL
      # use options$label if you want the chunk label as well
      if(res > 60)
	      sprintf("<p style=\"text-align:right\">-> running time of the above code chunk: %.1f minutes</p>", res / 60)
      else
	      sprintf("<p style=\"text-align:right\">-> running time of the above chunk: %.1f seconds</p>", res)
    }
  }})
)

library(eicm)
set.seed(2)		# for vignette reproducibility
```
<style>
p.caption {font.size:0.8em; font-style:italic}
</style>

# Getting started with EICMs
## The model

EICMs are an extension of binomial Generalized Linear Models for joint modelling of species communities,
that incorporate both the effects of species biotic interactions and the effects of missing covariates.
The model is a system of simultaneous equations, where species interactions are modelled explicitly as direct
effects of each species on each of the others, and are estimated alongside the effects of missing
covariates, modelled as latent factors.
The `eicm` package includes a Penalized Maximum Likelihood fitting function, and a
Genetic Algorithm for selecting the most parsimonious species interaction network topology.
There are methods for computing confidence intervals and for plotting results.

## Basic fitting workflow
### Select interaction network topology

Fitting and selecting the species interaction network topology is pretty simple.
Assuming all you have is a presence/absence data matrix in `occurrences`, you just have
to run (assuming you trust the defaults):

```{r, echo = TRUE, eval = FALSE}
# fit & select an EICM with 2 latent variables
m <- eicm(occurrences, n.latent=2)

# display estimated coefficients (note they are organized in matrices)
coef(m$selected.model)
```

This will end up with a model where the species interaction network topology has
been selected in respect to a parsimony criterion, and the respective coefficients
have been estimated.
We started with the most complex (and realistic) scenario:

* all environmental predictors are unmeasured, so we should/must estimate latent variables;
* we don't have clues of possible species interactions, so we assume all interactions are possible (the default).

If, however, you need more control on model hyperparameters (and you will need),
these are the most important arguments to define:

```{r, echo = TRUE, eval = FALSE}
m <- eicm(occurrences, n.latent=2, regularization=c(6, 0.5), penalty=4, theta.threshold=0.5)
```

Some explanation on the arguments (but see `?eicm`):

* `occurrences`: the occurrence data matrix (binary, for the time being)
* `n.latent`: how many latent variables we want to estimate
* `regularization`: the ridge regularization lambdas for penalized ML fitting. The first element applies to environmental
    coefficients and latents, the second element to species interaction coefficients
* `penalty`: the penalty applied to the number of species interactions to include (multiplier), during network selection
* `theta.threshold`: the threshold to exclude species interactions *a priori*. Species interactions whose preliminary coefficient
    (in absolute value) is lower than `theta.threshold` are excluded from the network selection stage

Note that, by default, this function will run in all available CPU cores, which
greatly speeds up the network selection stage. Set `n.cores`, if otherwise desired.

### Excluding unlikely interactions

Selecting the network topology for communities with 32 species or less, assuming that all interactions
are possible ($32*31/2=496$), takes usually less than a day.
For larger communities, hence, it becomes important to have *a priori* exclusion criteria for impossible
or unlikely interactions.
There are two ways of doing this, either with formulas or by providing a square binary matrix specifying which interactions
to include (1) or exclude (0).

#### Formula syntax

```{r, echo = TRUE, eval = FALSE}
# excluding interactions with the formula syntax
m <- eicm(occurrences, forbidden=list(
    sp3 ~ sp4 + sp5,		# sp3 must not be affected by sp4 nor sp5
    sp4 ~ .,				# sp4 must not be affected by any other
    sp1 ~ . - sp8			# sp1 must not be affected by any other except sp8
    ))

# display species interaction coefficients
# note the zeroed coefficients are those that were excluded
coef(m$fitted.model)$sp
```

#### Matrix syntax
The non-zero elements of the square binary species x species matrix are read as follows:
species A (column) affects species B (row).

```{r, echo = TRUE, eval = FALSE}
# Excluding interactions with the matrix syntax

# create a square matrix species x species, all zeroes
mask <- matrix(0, nrow=ncol(occurrences), ncol=ncol(occurrences))

# set to 1 those interactions we want to include
mask[4, 2] <- 1		# species #2 may affect species #4
mask[6, 1] <- 1		# species #1 may affect species #6
mask[, 7] <- 1		# species #7 may affect any other
mask[2, ] <- 1		# species #2 may be affected by any other

m <- eicm(occurrences, mask.sp=mask)

# display species interaction coefficients
# note the zeroed coefficients are those that were excluded
coef(m$fitted.model)$sp
```

You can also easily exclude interactions which depart from rare species, because they will be very difficult
to detect anyway, so excluding them from the start may compensate the risk. Note that this does not exclude
the possibility that the rare species are affected by others, only that rare
species do not affect others.

```{r, echo = TRUE, eval = FALSE}
# exclude interactions departing from species with 20 or less occurrences
m <- eicm(occurrences, mask.sp=mask, exclude.prevalence=20)
```

### Fitting without selecting network

If you have already a predefined interaction network topology and only want
to estimate the respective coefficients, you may skip the selection stage (which
is the most time-consuming task). We illustrate this with 2 latent variables:

```{r, echo = TRUE, message = FALSE, eval = TRUE, results="hide", timeit=TRUE, cache=TRUE}
# Load the included true model (32 species, 30 interactions, 2 environmental predictors)
data(truemodel)

# realize the model
occurrences <- predict(truemodel, nrepetitions=1)

# Pre-define a network topology
# For illustrative purposes, let's assume we know the true network topology so we build
#  the mask from the true model coefficients:
# 0: don't estimate interaction
# 1: estimate interaction
mask <- ifelse(truemodel$model$sp == 0, 0, 1)

# And now we estimate their values
# here we discard all the predictors - note that we don't provide any predictor matrix,
# only the presence/absence data.
m <- eicm(occurrences, n.latent=2, mask.sp=mask, do.selection=FALSE,
	regularization=c(1, 0.1))
```

We can readily plot the estimated species interaction network.

```{r, echo = TRUE, eval = TRUE, message = FALSE, results="hide", fig.cap="Estimated interaction network with a given topology. Blue: positive interactions; red: negative interactions. Line thickness is proportional to interaction strength."}
# Plot estimated species interaction network
plot(m, type="network")
```

The package also provides a plotting function that allows to visually compare the
estimated model coefficients and network topology with those of the true model, when data is simulated.

```{r, echo = TRUE, eval = TRUE, message = FALSE, results="hide", fig.width=10, fig.asp=0.4, fig.fullwidth=TRUE, fig.keep="last", fig.show="hold", fig.cap="Estimated versus true coefficients of a fitted EICM model over simulated data, with the true environmental predictors dropped, and estimated as latent variables."}
# Plot the network topology comparison between true and estimated models
# (plot omitted from vignette)
plot(m, true.model=truemodel, type="network")

# Plot estimated versus true coefficients
# Note that the environmental coefficients here relate to estimated latent variables,
# not the true predictors.
plot(m, true.model=truemodel, nenv.to.plot=2, legend=FALSE)
```

```{r, echo = TRUE, message = FALSE, eval = TRUE}
# display estimated species interaction coefficients (head only)
# the zeroed coefficients are those that were excluded a priori
head(round(coef(m$fitted.model)$sp, 3))
```

### Fitting with NAs in the response

Model fitting, including estimation of latent variables, is straightforward also in the
presence of NAs anywhere in the occurrence data matrix. No cases or variables are removed or replaced by predictions,
instead, model fitting occurs normally, with the only consequence being that estimated coefficients become less accurate.
Let's illustrate this by setting one half of the observations to NA.

```{r, echo = TRUE, message = FALSE, eval = TRUE, results="hide", timeit=TRUE, cache=TRUE, fig.width=10, fig.asp=0.4, fig.fullwidth=TRUE, fig.cap="Estimated versus true coefficients of a fitted EICM model over simulated data in the presence of a large amount of NAs in the response matrix. Note that the true environmental predictors were dropped, and estimated as latent variables, and that their estimation is accurate even in the presence of a large amount of NAs in the occurrence data."}
# Randomly set 1/2 of the occurrence data to NA
# (8000 records out of 16000)
occurrences[sample(length(occurrences), 8000)] <- NA

# Fit the model
m <- eicm(occurrences, n.latent=2, mask.sp=mask, do.selection=FALSE,
	regularization=c(1, 0.1))

plot(m, true.model=truemodel, nenv.to.plot=2, legend=FALSE)
```

It is noteworthy that even when all the environmental predictors were dropped and estimated as latent variables,
there is a very good match between estimated and true environmental coefficients even in the presence of a large amount of
NAs in the occurrence data (one half of the data was set to NA in this example). The major accuracy drop happens in the
estimated interaction coefficients.

Note that, however, there must always be 0s and 1s in the data in a per-species basis, irrespectively of the
amount of NAs. Species which only have presences and NAs, cannot have their coefficients estimated,
for "obvious" statistical reasons (flat likelihood).

## Confidence intervals

Confidence intervals are computed by penalized profile likelihood, with `confint`.
Also note that confidence intervals should not be computed on a model whose terms
have been selected.
```{r, echo = TRUE, message = FALSE, eval = FALSE, timed=TRUE, fig.asp=3}
# Confidence intervals are appended to the model object.
m <- confint(m$fitted.model)
plot(m)		# by default, plots CIs
```
It may take some time with many parameters, so be sure to do it in multicore (the default).

# Prediction

Any model whose species interaction network is a Directed Acyclic Graph can be used for prediction. Note
that a selected model may not be a DAG.
For reference, let's plot the species interaction network that will be used for prediction.

```{r, echo = TRUE, message = FALSE, eval = TRUE}
# load the included parameterized model
data(truemodel)

# for reference, plot the species interaction network
plot(truemodel, type="network")
```

## Unconditional predictions

Unconditional predictions are those that are made only with environmental predictors. If the model
was fitted with no regularization, they should be numerically equal to binomial GLM predictions.

```{r, echo = TRUE, message = FALSE, eval = TRUE}
# Unconditional predictions
# let's fix environmental predictors at 0, for simplicity.
predict(truemodel, newdata=cbind(env01=0, env02=0))
```

## Conditional predictions

Conditional predictions incorporate the effects of other species that may have been observed or
proven absent. To provide data on observed species, add columns to the new data matrix with the names
of the species for which there is available data. In the current version, NAs cannot be mixed with 0s or 1s,
and latents are not estimated in new data (but in a future version both will be possible).

```{r, echo = TRUE, message = FALSE, eval = TRUE}
# Conditional predictions
# predict probabilities for all species conditional on the
# known presence of sp011 (compare sp014 and sp004 with the above)
predict(truemodel, newdata=cbind(env01=0, env02=0, sp011=1))
```

### Indirect effects

Indirect effects of species are propagated through the interaction network. This illustrates it:

```{r, echo = TRUE, message = FALSE, eval = TRUE}
# Propagation of indirect species effects
# predict probabilities for all species conditional on the known 
# absence (first line) and known presence (second line) of sp005 and sp023
newdata <- cbind(env01=0, env02=0, sp012=c(0, 1), sp018=c(0, 1))
newdata
predict(truemodel, newdata=newdata, nrep=100000)
```

Notice the effects on sp026 and the effect propagation to those species which depend on it (sp013, sp008).
Also compare with unconditional predictions.

# Simulating species communities

## Simple basic simulation
For testing purposes, you may need to simulate communities with known parameters.
We first simulate some occurrence data using a **naive** model where we set the parameters
manually, to illustrate the basics. The core function is `as.eicm`, which constructs
a model from given coefficients.

```{r, echo = TRUE, eval = FALSE}
nenv <- 2			# 2 environmental predictors
nsp <- 20			# 20 species
nsamples <- 400		# 400 samples

# random responses to environment
env.coefs <- matrix(runif((nenv + 1) * nsp, -4, 4), nrow=nsp)

# let's define some true species interactions
sp.coefs <- matrix(0, nrow=nsp, ncol=nsp)
sp.coefs[3, 5] <- 2	 # this reads: species #5 affects species #3 with a "strength" of 2
sp.coefs[4, 1] <- 3
sp.coefs[1, 2] <- -2

# random environmental predictors
env <- matrix(rnorm(nenv * nsamples), ncol=nenv, nrow=nsamples)

# define the true model
true <- as.eicm(env=env, env.coefs=env.coefs, sp.coefs=sp.coefs)

# realize the model
occurrences <- predict(true, nrepetitions=1)

# we now have a plain presence/absence data matrix that follows from the true model
```

## Simulating realistic communities

However, in real communities, *most species are rare*. That is to say, the frequency
distribution of species in samples approximately follows a Beta distribution. 
Simulating realistic communities then requires that this constraint is
taken into account. `eicm` provides a function for this purpose.

```{r, echo = TRUE, eval = FALSE}
# Generate a model that, when realized, shows a frequency distribution that
# follows a Beta distribution, yet accounting for all the desired effects
# (including species interactions)

truemodel <- generateEICM(nspecies=32, nsamples=500, nenv=2,
    ninteractions=30, shape1=1.5, shape2=3)

```
Now plot some realizations.
```{r, echo = TRUE, eval = TRUE, fig.width=7, fig.height=7, fig.align="center", fig.cap="Frequency disrtibutions of four realizations of the same model, generated with generateEICM(...)"}
# plot frequency histogram of 4 realizations of truemodel:
# should approx. follow a Beta distribution.
par(mfrow=c(2, 2))
for(i in 1:4) {
    occurrences <- predict(truemodel, nrepetitions=1)
    spcounts <- apply(occurrences, 2, sum)

    hist(spcounts / nrow(occurrences), breaks=seq(0, 1, by=0.1), xlim=c(0, 1),
        main="Frequency distribution of one realization", xlab=
        "Frequency in samples",ylab="Number of species")
}
```

