---
title: "Compute functional dendogram from the ecological parameters."
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Compute functional dendogram from the ecological parameters.}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = identical(Sys.getenv("NOT_CRAN"), "true")
)
```
Functional dendrograms measure functional diversity (Petchey & Gaston, 2007). The dendograms cluster species with similar traits in the same or closely linked clusters, a precursor to under the species interactions at the species-species level. In **fwtraits**, these interactions can be examined by providing the species' ecological parameters or traits.


```{r setup}
library(fwtraits)
```

### Data collation
The data was obtained from the www.freshwaterecology.info database using the **`fw_fetchdata`** function.

```{r data}

data("speciesdata")

set.seed(1135)
speciesdata$abund <- rnorm(n = nrow(speciesdata), 4.3, 1.2)

#species with geographical coordinates

geospdata <- speciesdata |> 
  sf::st_as_sf(coords = c('decimalLongitude', 'decimalLatitude'), 
               crs = sf::st_crs(4326))

```

### Data retrieval from the database

```{r dataretrieval}

fishtraits <- fw_fetchdata(data = speciesdata, 
                         ecoparams = c('rheophily habitat', 'spawning habitat',
                                       'feeding diet adult'), 
                         taxonomic_column = 'scientificName',
                         organismgroup = 'fi')

```

### Data visualistion and compute the functional dendogram.

```{r visualisation, fig.width = 7, fig.height= 5.5, fig.align='center', warning=FALSE}


fdendoclust <- fw_fdendro(fwdata = fishtraits, plot = TRUE)

head(fdendoclust, 3)

table(fdendoclust$cluster)

```

### References

Petchey, O. L., & Gaston, K. J. (2007). Dendrograms and measuring functional diversity. Oikos, 116(8), 1422-1426.











