**Peter Solymos - R related posts**, 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.

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

`devtools::install_github("borealbirds/lhreg")`

.

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

vector

with phylogenetically based (or any other known) correlations

and optionally with observation error (`SE`

), and covariate effects (`X`

).

The function is pretty bare-bones (i.e. no formula interface,

the design matrix `X`

needs to be properly specified through

e.g. `model.matrix()`

). The `lambda`

argument

is a non-negative number modifying the strength of phylogenetic effects.

`lambda = 0`

is equivalent to `lm`

with

`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 `stats::optim`

,

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

and `summary`

)

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 `phytools::contMap`

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!

**leave a comment**for the author, please follow the link and comment on their blog:

**Peter Solymos - R related posts**.

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.