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.

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):

```
# 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:

`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.

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).

```
# 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
```

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

```
# 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.

```
# exclude interactions departing from species with 20 or less occurrences
m <- eicm(occurrences, mask.sp=mask, exclude.prevalence=20)
```

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:

```
# 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))
```

-> running time of the above code chunk: 1.3 minutes

We can readily plot the estimated species interaction network.

```
# 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.

```
# 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)
```

```
# 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))
```

```
## sp001 sp002 sp003 sp004 sp005 sp006 sp007 sp008 sp009 sp010 sp011 sp012
## sp001 0 0 0 0 0 0 0 0 0 0 0.000 0
## sp002 0 0 0 0 0 0 0 0 0 0 0.000 0
## sp003 0 0 0 0 0 0 0 0 0 0 0.000 0
## sp004 0 0 0 0 0 0 0 0 0 0 -2.274 0
## sp005 0 0 0 0 0 0 0 0 0 0 0.000 0
## sp006 0 0 0 0 0 0 0 0 0 0 0.000 0
## sp013 sp014 sp015 sp016 sp017 sp018 sp019 sp020 sp021 sp022 sp023 sp024
## sp001 0 0 0 0 0 0 0 1.687 0 0 0 0
## sp002 0 0 0 0 0 0 0 0.000 0 0 0 0
## sp003 0 0 0 0 0 0 0 0.000 0 0 0 0
## sp004 0 0 0 0 0 0 0 0.000 0 0 0 0
## sp005 0 0 0 0 0 0 0 0.000 0 0 0 0
## sp006 0 0 0 0 0 0 0 0.000 0 0 0 0
## sp025 sp026 sp027 sp028 sp029 sp030 sp031 sp032
## sp001 0.000 0 0.000 0 0 0 0.000 0
## sp002 1.833 0 -0.176 0 0 0 0.000 0
## sp003 0.000 0 0.000 0 0 0 0.000 0
## sp004 0.000 0 0.000 0 0 0 -1.661 0
## sp005 0.000 0 0.000 0 0 0 0.000 0
## sp006 0.000 0 0.000 0 0 0 0.000 0
```

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.

```
# 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)
```

-> running time of the above code chunk: 1.2 minutes

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 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.

```
# 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).

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.

```
# load the included parameterized model
data(truemodel)
# for reference, plot the species interaction network
plot(truemodel, type="network")
```

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.

```
# Unconditional predictions
# let's fix environmental predictors at 0, for simplicity.
predict(truemodel, newdata=cbind(env01=0, env02=0))
```

```
## sp001 sp002 sp003 sp004 sp005 sp006 sp007 sp008 sp009 sp010 sp011
## [1,] 0.78 0.0314 0.0577 0.3961 0.0268 0.1398 0.0835 0.076 0.0089 0.3828 0.6305
## sp012 sp013 sp014 sp015 sp016 sp017 sp018 sp019 sp020 sp021
## [1,] 0.1108 0.4565 0.2051 0.0456 0.3647 0.8163 0.4128 0.5668 0.2423 0.0934
## sp022 sp023 sp024 sp025 sp026 sp027 sp028 sp029 sp030 sp031 sp032
## [1,] 0.208 0.596 0.0501 0.2575 0.8629 0.0969 0.0602 0.3339 0.6676 0.1966 0.0929
```

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).

```
# 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))
```

`## Excluded species 11`

```
## sp001 sp002 sp003 sp004 sp005 sp006 sp007 sp008 sp009 sp010
## [1,] 0.7749 0.0298 0.0567 0.2304 0.0263 0.1417 0.0802 0.0804 0.0097 0.3438
## sp011 sp012 sp013 sp014 sp015 sp016 sp017 sp018 sp019 sp020 sp021
## [1,] 1 0.1093 0.4499 0.049 0.049 0.4065 0.8111 0.4119 0.5495 0.2415 0.0973
## sp022 sp023 sp024 sp025 sp026 sp027 sp028 sp029 sp030 sp031 sp032
## [1,] 0.2041 0.5931 0.0511 0.2538 0.8645 0.0951 0.073 0.321 0.6566 0.1923 0.0928
```

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

```
# 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
```

```
## env01 env02 sp012 sp018
## [1,] 0 0 0 0
## [2,] 0 0 1 1
```

`predict(truemodel, newdat`