<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/phylogeny-and-species-traits-predict-bird-detectability/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://peter.solymos.org/code/2018/02/09/phylogeny-and-species-traits-predict-bird-detectability.html"> Peter Solymos - R related posts</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It all started with <a rel="nofollow" href="http://onlinelibrary.wiley.com/doi/10.1111/2041-210X.12106/abstract" target="_blank">this</a> paper in <em>Methods in Ecol. Evol.</em> where we looked at<br>
detectability of many species. So we wanted to use life history<br>
traits to validate our results. But we had to cut the manuscript,<br>
and there was this leftover with some neat patterns, but without much focus.<br>
It took a few years, and the <a rel="nofollow" href="https://twitter.com/psolymos/status/903634823906033664" target="_blank">most positive peer-review experience ever</a>,<br>
and the paper is now early view in <a rel="nofollow" href="http://onlinelibrary.wiley.com/doi/10.1111/ecog.03415/abstract" target="_blank"><em>Ecography</em></a>. This post is a quick summary of the goodies stuffed inside the <a rel="nofollow" href="https://github.com/borealbirds/lhreg#readme" target="_blank"><strong>lhreg</strong></a> R package that makes the whole analysis reproducible, and provides some functions for similar PGLMM models.</p>
<p>The R package is hosted on <a rel="nofollow" href="https://github.com/borealbirds/lhreg" target="_blank">GitHub</a><br>
(no CRAN version yet),<br>
please submit any issues <a rel="nofollow" href="https://github.com/borealbirds/lhreg/issues" target="_blank">here</a>.<br>
The package is also archived on Zenodo with DOI <a rel="nofollow" href="http://doi.org/10.5281/zenodo.596410" target="_blank">10.5281/zenodo.596410</a>.<br>
To install the package, use<br>
<code>devtools::install_github(&quot;borealbirds/lhreg&quot;)</code>.</p>
<p>Here, I am going to skim the implementation based on the more<br>
complete supporting information of the paper which has all the<br>
reproducible code (try <code>vignette(topic = &quot;lhreg&quot;, package = &quot;lhreg&quot;)</code> after<br>
installing and loading the package).<br>
Here is the rendered <a rel="nofollow" href="https://borealbirds.github.io/lhreg/" target="_blank">html</a> version.</p>
<p>The most important function is <code>lhreg</code> which takes the following main arguments:</p>
<ul>
<li><code>Y</code>: response vector,</li>
<li><code>X</code>: model matrix for the mean.</li>
<li><code>SE</code>: standard error estimate (observation error) for the response,</li>
<li><code>V</code>: correlation matrix,</li>
</ul>
<p>and fits a Multivariate Normal model to the observed <code>Y</code> vector<br>
with phylogenetically based (or any other known) correlations<br>
and optionally with observation error (<code>SE</code>), and covariate effects (<code>X</code>).<br>
The function is pretty bare-bones (i.e. no formula interface,<br>
the design matrix <code>X</code> needs to be properly specified through<br>
e.g. <code>model.matrix()</code>). The <code>lambda</code> argument<br>
is a non-negative number modifying the strength of phylogenetic effects.<br>
<code>lambda = 0</code> is equivalent to <code>lm</code> with<br>
<code>weights = 1/(SE^2)</code>, <code>lambda = 1</code> implies Brownian motion evolution,<br>
<code>lambda = NA</code> lets the function estimate it based on the data.</p>
<p>In terms of optimization, besides the algorithms from <code>stats::optim</code>,<br>
we also have differential evolution algorithm based on the<br>
<a rel="nofollow" href="https://cran.r-project.org/package=DEoptim" target="_blank"><strong>DEoptim</strong></a> package (a bit time consuming but very reliable).<br>
The output object class has some methods defined (like <code>logLik</code> and <code>summary</code>)<br>
and as a result AIC/BIC will work out of the box.The vignette also<br>
describes a few techniques which are pretty nice to have in<br>
a multivariate setting (i.e. profile likelihood, parametric bootstrap)<br>
to support avanced hypothesis testing and model selection.</p>
<p>We used leave one out cross-validation to see how well we could predict the<br>
values based on data from the other species, traits and phylogeny.<br>
The conditional distribution we used for that is described in the paper which<br>
made this exercise very straightforward.<br>
Maybe it is just ignorance, but I couldn’t find another paper<br>
that would have described it in a nice and useful manner,<br>
however, if one wishes to make trait/phylogeny based<br>
predictions for detectability, this formula is going to be<br>
very useful (look inside the <code>loo2</code> function for implementation).</p>
<p>At the end of the vignette, there is a hack based on <code>phytools::contMap</code><br>
function to produce <em>non-rainbow</em> colors.<br>
(It was surprisingly <em>non-straightforward</em> to hack the code —<br>
<a rel="nofollow" href="https://en.wikipedia.org/wiki/Unix_philosophy#Doug_McIlroy_on_Unix_programming" target="_blank">modular code</a> please!)<br>
The following figure shows the two input data vectors mirrored side-by-side:</p>
<p><img alt="lhreg inputs" src="https://i1.wp.com/github.com/borealbirds/lhreg/raw/master/tree.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I realize this is not a very detailed post, but the paper<br>
and the vignette should satisfy your curiosity.<br>
If you still have unanswered questions, feel free to ask them below!</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://peter.solymos.org/code/2018/02/09/phylogeny-and-species-traits-predict-bird-detectability.html"> Peter Solymos - R related posts</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/iPWw6EPZmz0" height="1"></a></div>