---
title: "Package overview"
author: "Francois Keck"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Package overview}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---


### Data
First, we load the package `phylosignal` and the dataset `carnivora` from `adephylo`.

```{r, message = FALSE, warning = FALSE}
library(phylosignal)
library(adephylo)
library(ape)
library(phylobase)
data(carni19)
```

Here is a phylogenetic tree of 19 carnivora species.
```{r}
tre <- read.tree(text=carni19$tre)
```

And we create a dataframe of 3 traits for the 19 carnivora species.

- Body mass
- Random values
- Simulated values under a Brownian Motion model along the tree

```{r}
dat <- list()
dat$mass <- carni19$bm
dat$random <- rnorm(19, sd = 10)
dat$bm <- rTraitCont(tre)
dat <- as.data.frame(dat)
```

We can combine phylogeny and traits into a `phylo4d` object.
```{r}
p4d <- phylo4d(tre, dat)
```

### Visualizing the data
```{r fig.width=8, fig.height=5}
barplot.phylo4d(p4d, tree.type = "phylo", tree.ladderize = TRUE)
```

### Measuring and testing the signal for each trait and different methods
```{r}
phyloSignal(p4d = p4d, method = "all")
```

### Assessing the behavior of these methods with this phylogeny along a Brownian-Motion influence gradient
```{r message=FALSE, warning=FALSE, results='hide'}
phylosim <- phyloSim(tree = tre, method = "all", nsim = 100, reps = 99)
```
```{r  fig.width=12, fig.height=5}
plot(phylosim, stacked.methods = FALSE, quantiles = c(0.05, 0.95))
```
```{r  fig.width=5, fig.height=4}
plot.phylosim(phylosim, what = "pval", stacked.methods = TRUE)
```

### Assessing the signal depth with correlograms
```{r fig.width=5, fig.height=4}
mass.crlg <- phyloCorrelogram(p4d, trait = "mass")
random.crlg <- phyloCorrelogram(p4d, trait = "random")
bm.crlg <- phyloCorrelogram(p4d, trait = "bm")

plot(mass.crlg)
plot(random.crlg)
plot(bm.crlg)
```

### Locating the signal with LIPA
```{r fig.width=6, fig.height=5}
carni.lipa <- lipaMoran(p4d)
carni.lipa.p4d <- lipaMoran(p4d, as.p4d = TRUE)

barplot.phylo4d(p4d, bar.col=(carni.lipa$p.value < 0.05) + 1, center = FALSE , scale = FALSE)
barplot.phylo4d(carni.lipa.p4d, bar.col = (carni.lipa$p.value < 0.05) + 1, center = FALSE, scale = FALSE)
```
