It all started with this paper in Methods in Ecol. Evol. where we looked at detectability of many species. So we wanted to use life history traits to validate our results. But we had to cut the manuscript, and there was this leftover with some neat patterns, but without much focus. It took a few years, and the most positive peer-review experience ever, and the paper is now early view in Ecography. This post is a quick summary of the goodies stuffed inside the lhreg R package that makes the whole analysis reproducible, and provides some functions for similar PGLMM models.
The R package is hosted on GitHub
(no CRAN version yet),
please submit any issues here.
The package is also archived on Zenodo with DOI 10.5281/zenodo.596410.
To install the package, use
Here, I am going to skim the implementation based on the more
complete supporting information of the paper which has all the
reproducible code (try
vignette(topic = "lhreg", package = "lhreg") after
installing and loading the package).
Here is the rendered html version.
The most important function is
lhreg which takes the following main arguments:
Y: response vector,
X: model matrix for the mean.
SE: standard error estimate (observation error) for the response,
V: correlation matrix,
and fits a Multivariate Normal model to the observed
with phylogenetically based (or any other known) correlations
and optionally with observation error (
SE), and covariate effects (
The function is pretty bare-bones (i.e. no formula interface,
the design matrix
X needs to be properly specified through
is a non-negative number modifying the strength of phylogenetic effects.
lambda = 0 is equivalent to
weights = 1/(SE^2),
lambda = 1 implies Brownian motion evolution,
lambda = NA lets the function estimate it based on the data.
In terms of optimization, besides the algorithms from
we also have differential evolution algorithm based on the
DEoptim package (a bit time consuming but very reliable).
The output object class has some methods defined (like
and as a result AIC/BIC will work out of the box.The vignette also
describes a few techniques which are pretty nice to have in
a multivariate setting (i.e. profile likelihood, parametric bootstrap)
to support avanced hypothesis testing and model selection.
We used leave one out cross-validation to see how well we could predict the
values based on data from the other species, traits and phylogeny.
The conditional distribution we used for that is described in the paper which
made this exercise very straightforward.
Maybe it is just ignorance, but I couldn’t find another paper
that would have described it in a nice and useful manner,
however, if one wishes to make trait/phylogeny based
predictions for detectability, this formula is going to be
very useful (look inside the
loo2 function for implementation).
At the end of the vignette, there is a hack based on
function to produce non-rainbow colors.
(It was surprisingly non-straightforward to hack the code —
modular code please!)
The following figure shows the two input data vectors mirrored side-by-side:
I realize this is not a very detailed post, but the paper and the vignette should satisfy your curiosity. If you still have unanswered questions, feel free to ask them below!