---
title: "Quantitative ethnobotany analysis with ethnobotanyR"
author: "Cory Whitney"
output: rmarkdown::html_vignette
bibliography: references/Ethnobotany.bib
vignette: >
  %\VignetteIndexEntry{Quantitative ethnobotany analysis with ethnobotanyR}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

<!-- Spelling -->
<!-- The ABC √ option (upper right on the Rmarkdown console)-->

<!-- Grammar -->
<!-- devtools::install_github("ropenscilabs/gramr") -->
<!-- gramr::run_grammar_checker("vignettes/ethnobotanyr_vignette.rmd") -->

<!-- Print pdf version -->
<!-- rmarkdown::render("vignettes/ethnobotanyr_vignette.Rmd", output_format = "pdf_document") -->

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

load("ethnobotanydata.rda")
library(dplyr)
library(ethnobotanyR)
library(ggalluvial)
library(ggplot2)
library(ggridges)
library(magrittr)
# in case of rendering issues render with 
# rmarkdown::render('vignettes/ethnobotanyr_vignette.Rmd', output_file='ethnobotanyr_vignette.html', output_dir='vignettes')

```

<img src="ethnobotanyR.png" alt="ethnobotanyR logo" align="right" width = "25%" height="25%" style="background:none; border:none; box-shadow:none;"/>

The `ethnobotanyR` package calculates common quantitative ethnobotany indices to assess the cultural significance of plant species based on informant consensus. The package closely follows two papers, one on cultural importance indices [@tardioCulturalImportanceIndices2008] and another on agrobiodiversity valuation [@whitneyEthnobotanyAgrobiodiversityValuation2018]. The goal is to provide an easy-to-use platform for ethnobotanists to calculate quantitative ethnobotany indices. Users are highly encouraged to familiarize themselves with ethnobotany theory [@gaoue2017; @albuquerqueEthnobotanyOneConcept2010] and social ecological theory [@albuquerqueSocialEcologicalTheoryMaximization2019]. An overview of this theoretical background will be helpful in understanding approaches in ethnobotany and formulating useful research questions.

An example data set called `ethnobotanydata` is provided to show how standard ethnobotany data should be formatted to interface with the `ethnobotanyR` package. This is an ethnobotany data set including one column of `r length(unique(ethnobotanydata$informant))` knowledge holder identifiers `informant` and one of `r length(unique(ethnobotanydata$sp_name))` species names `sp_name`. The rest of the columns are the identified ethnobotany use categories. The data in the use categories is populated with counts of uses per person (should be 0 or 1 values). ^[The example `ethnobotanydata` is included with the `ethnobotanyR` package but can also be downloaded from GitHub <https://github.com/CWWhitney/ethnobotanyR/tree/master/data>.]

Many of the functions in `ethnobotanyR` make use of `select()` and `filter_all()` functions of the `dplyr` package [@dplyr] and pipe functions `%>%` from the `magrittr` package [@magrittr]. These are easy to use and understand and allow users the chance to pull the code for these functions and change anything they see fit.

```{r, echo= FALSE}
knitr::kable(head(ethnobotanydata), digits = 2, caption = "First six rows of the example ethnobotany data included with ethnobotanyR")
```


## `ethnobotanyR` package functions

### Use Report (UR) per species

The use report `URs()` is the most basic ethnobotany calculation. The function calculates the use report (UR) for each species in the data set. 

\begin{equation}
UR_{s} = \sum_{u=u_1}^{^uNC} \sum_{i=i_1}^{^iN} UR_{ui}
\end{equation}

`URs()` calculates the total uses for the species by all informants (from $i_1$ to $^iN$) within each use-category for that species $(s)$. It is a count of the number of informants who mention each use-category $NC$ for the species and the sum of all uses in each use-category (from $u_1$ to $^uNC$) [see @pranceQuantitativeEthnobotanyCase1987].

```{r URs}
ethnobotanyR::URs(ethnobotanydata)
```

The `URsum()` function calculates the sum of all ethnobotany use reports (UR) for all species in the data set [see @pranceQuantitativeEthnobotanyCase1987].

```{r URsum}
ethnobotanyR::URsum(ethnobotanydata)
```

### Cultural Importance (CI) index

The `CIs()` function calculates the cultural importance index (CI) for each species in the data set.

\begin{equation}
CI_{s} = \sum_{u=u_1}^{^uNC} \sum_{i=i_1}^{^iN} UR_{ui/N}.
\end{equation}

`CIs()` is essentially `URs()` divided by the number of informants to account for the diversity of uses for the species [see @tardioCulturalImportanceIndices2008].

```{r CIs}
ethnobotanyR::CIs(ethnobotanydata)
```

### Frequency of Citation (FC) per species

The `FCs()` function calculates the frequency of citation (FC) for each species in the data set. 

\begin{equation}
FC_s = \sum_{i=i_1}^{^iN}{UR_i}
\end{equation}

`FCs()` is the sum of informants that cite a use for the species [see @pranceQuantitativeEthnobotanyCase1987].

```{r FCs}
ethnobotanyR::FCs(ethnobotanydata)
```

### Number of Uses (NU) per species

The `NUs()` function calculates the number of uses (NU) for each species in the data set.

\begin{equation}
NU_s = \sum_{u=u_1}^{^uNC}
\end{equation}

$NC$ are the number of use categories. `NUs()` is the sum of all categories for which a species is considered useful [see @pranceQuantitativeEthnobotanyCase1987].

```{r NUs}
ethnobotanyR::NUs(ethnobotanydata)
```

### Relative Frequency of Citation (RFC) index

The `RFCs()` function calculates the relative frequency of citation (RFC) for each species in the data set.

\begin{equation}
RFC_s = \frac{FC_s}{N} = \frac{\sum_{i=i_1}^{^iN} UR_i}{N}
\end{equation}

$FC_s$ is the frequency of citation for each species $s$, $UR_i$ are the use reports for all informants $i$ and $N$ is the total number of informants interviewed in the survey [see @tardioCulturalImportanceIndices2008].

```{r RFCs}
ethnobotanyR::RFCs(ethnobotanydata)
```

### Relative Importance (RI) index

The `RIs()` function calculates the relative importance index (RI) for each species in the data set.

\begin{equation}
RI_s = \frac{RFC_{s(max)}+RNU_{s(max)}}{2} 
\end{equation}

$RFC_{s(max)}$ is the relative frequency of citation for the species $s$ over the maximum, $RNU_{s(max)}$ is the relative number of uses for $s$ over the maximum [see @tardioCulturalImportanceIndices2008].

```{r RIs}
ethnobotanyR::RIs(ethnobotanydata)
```

### Use Value (UV) index

The `UVs()` function calculates the use value (UV) index for each species in the data set.

\begin{equation}
UV_{s} =  \sum_{i=i_1}^{^iN} \sum_{u=u_1}^{^uNC} UR_{ui/N} 
\end{equation}

`UVs()` is essentially the same as `CIs()` except that it starts with the sum of UR groupings by informants. $U_i$ is the number of different uses mentioned by each informant $i$ and $N$ is the total number of informants interviewed in the survey [see @tardioCulturalImportanceIndices2008]. 

```{r UVs}
ethnobotanyR::UVs(ethnobotanydata)
```

The `simple_UVs()` function calculates the simplified use value (UV) index for each species in the data set. 

\begin{equation}
UV_{s} = \sum U_i/N
\end{equation}

$U_i$ is the number of different uses mentioned by each informant $i$ and $N$ is the total number of informants interviewed in the survey [see @albuquerque2006].

### Cultural Value (CVe) for ethnospecies 

The `CVe()` function calculates the cultural value (CVe) for ethnospecies. The index is one of three proposed for assessing the cultural, practical and economic dimensions (ethno) species importance. Reyes-Garcia et al. (2006) suggest several more indices but $CV_e$ is the most commonly used from that study [@ReyesGarcia2006].

\begin{equation}
CV_{e} = {Uc_{e}}  \cdot{IC_{e}}  \cdot \sum {IUc_{e}}
\end{equation}

Where $UC_e$ is the number of uses reported for ethnospecies $e$ divided by all potential uses of an ethnospecies considered in the study. $Ic_e$ expresses the number of informants who listed the ethnospecies $e$ as useful divided by the total number of informants. $IUc_e$ expresses the number of informants who mentioned each use of the ethnospecies $e$ divided by the total number of informants [see @ReyesGarcia2006].

```{r CVe}
ethnobotanyR::CVe(ethnobotanydata)
```

### Fidelity Level (FL) per species 

The `FLs()` function calculates the fidelity level (FL) per species in the study. It is a way of calculating the percentage of informants who use a plant for the same purpose as compared to all uses of all plants.

\begin{equation}
FL_{s} = \frac {N_{s}*100}{FC_{s}}
\end{equation}

where $N_s$ is the number of informants that use a particular plant for a specific purpose, and $FC_s$ is the total number of uses for the species [see @friedmanPreliminaryClassificationHealing1986]. 

```{r FLs}
ethnobotanyR::FLs(ethnobotanydata)
```

Divide FLs by 100 to get the percent FL, as it is reported in some studies.

## Visualize `ethnobotanyR` results

For quick assessments of differences between indices use the `Radial_plot` function to show ethnobotanyR results as a radial bar plot using the `ggplot2` library. The `cowplot` package [@cowplot] can be useful for comparing several `Radial_plot` results for easy comparison across indices.

```{r, fig.width=7, fig.height=7}
URs_plot <- ethnobotanyR::Radial_plot(ethnobotanydata, ethnobotanyR::URs)

NUs_plot <- ethnobotanyR::Radial_plot(ethnobotanydata, ethnobotanyR::NUs)

FCs_plot <- ethnobotanyR::Radial_plot(ethnobotanydata, ethnobotanyR::FCs)

CIs_plot <- ethnobotanyR::Radial_plot(ethnobotanydata, ethnobotanyR::CIs)

cowplot::plot_grid(URs_plot, NUs_plot, FCs_plot, CIs_plot, 
    labels = c('URs', 'NUs', 'FCs', 'CIs'), 
    nrow = 2, 
    align="hv",
    label_size = 12)
```

## Chord diagrams with `circlize`

The following chord plots are made using functions from the `circlize` package [@circlize]. An example of the application of chord plots in ethnobotany is described in a study on agrobiodiversity in Uganda [@whitneyEthnobotanyAgrobiodiversityValuation2018]. 

The `ethnoChord()` function creates a chord diagram of ethnobotany uses and species.

```{r, fig.width=7, fig.height=7}
Chord_sp <- ethnobotanyR::ethnoChord(ethnobotanydata, by = "sp_name")
```

The `ethnoChord()` function can also be used to create a chord diagram of ethnobotany uses and informants.

```{r, fig.width=7, fig.height=7}
Chord_informant <- ethnobotanyR::ethnoChord(ethnobotanydata, by = "informant")
```

## Flow diagrams with `ggalluvial`

The `ethno_alluvial()` function uses the `ggplot2` extension `ggalluvial` to make flow diagrams. This may be a useful way to visualize frequency distributions across uses, experts and use categories. 

```{r, fig.width=7, fig.height=7}
ethnobotanyR::ethno_alluvial(ethnobotanydata)
```

Generate the same plot with labels on the strata and without the legend.

```{r, fig.width=7, fig.height=7}

# correct internal assignment for stat = "stratum" 
  StatStratum <- ggalluvial::StatStratum

ethnobotanyR::ethno_alluvial(ethnobotanydata, alpha = 0.2) + 
  ggplot2::theme(legend.position = "none") +  
             ggplot2::geom_label(stat = "stratum", 
                      ggplot2::aes(label = ggplot2::after_stat(stratum)))

```


### Confidence in responses

The indices are probably too narrow a tool for a proper assessment but they can be a useful entry way into understanding some aspects human and nature interactions. These steps required to calculate these indices offer a way to quantify intangible factors of how human communities interact with the world. They can come in handy as additive pieces for more holistic assessments and analyses. 

One such procedure is the non-parametric Bayesian bootstrap. The `ethno_boot` function runs such a bootstrap and returns a sample of size 'n1' representing the posterior distribution of the chose statistic (i.e. 'mean').

The function uses the Dirichlet distribution as a way to model the randomness of a probability mass function (PMF) with unlimited options for finite sets (e.g. an unlimited amount of dice in a bag). It is the conjugate prior of the categorical distribution and multinomial distribution. 

A probability mass function (PMF) is also called a frequency function, it gives probabilities for random variables that are discrete such as UR (there can be only 1 or 0 UR, this also works for discrete counts like plant uses where there can only be max 'n' people interviewed).

The Dirichlet distribution creates n positive numbers (a set of random vectors X1…Xn) that add up to 1. It is closely related to the multinomial distribution, which also requires n numbers that sum to 1.

Here we are interested in the differences in use (either '0' no use, or '1' use) between species 'a' and species 'b'.  

```{r ethno_boot_uses}
sp_a_data <- ethnobotanydata %>% filter(sp_name == "sp_a") 

sp_a_use <- ethno_boot(sp_a_data$Use_3, statistic = mean, n1 = 1000)

sp_b_data <- ethnobotanydata %>% filter(sp_name == "sp_b") 

sp_b_use <- ethno_boot(sp_b_data$Use_3, statistic = mean, n1 = 1000)

```

We can calculate the 90% credible interval to determine the lower bound of `r round(quantile(sp_a_use, c(0.05, 0.95)), 2)[1]` and upper bound of `r round(quantile(sp_a_use, c(0.05, 0.95)), 2)[2]` for species 'a' and `r round(quantile(sp_b_use, c(0.05, 0.95)), 2)[1]` and upper bound of `r round(quantile(sp_b_use, c(0.05, 0.95)), 2)[2]` for species 'b'.

```{r ethno_boot_URs}
quantile(sp_a_use, c(0.05, 0.95))
quantile(sp_b_use, c(0.05, 0.95))
```

Running `ethno_boot` returns a posterior distribution of the result. Plotting these can give some visual probability estimation of differences between the species or informants according to the various indices. 

Create a data frame and use the `melt` function to reshape data for the `ggplot2` plotting functions.

```{r fig.width=7, fig.height=7}
boot_data <- data.frame(sp_a_use, sp_b_use)

ethno_boot_melt <- reshape2::melt(boot_data)
```

Use the `ggplot2` and `ggridges` libraries to plot the data as smooth histograms.

```{r plot_boot_ridges, fig.width=7, fig.height=4}
ggplot2::ggplot(ethno_boot_melt, aes(x = value, 
                y = variable, fill = variable)) +
                ggridges::geom_density_ridges() +
                ggridges::theme_ridges() + 
                theme(legend.position = "none") +
                labs(y= "", x = "Example Bayesian bootstraps of three use categories")
```

The `ethno_bayes_consensus` function is inspired by `AnthroTools` package [@AnthroTools]. It gives us a measure of the confidence we can have in the reported uses by creating a matrix of probability values. These represent the probability that informant citations for a given use are 'correct' [see @oravecz2014; @romney1986]. 

The inputs to the function are informant responses to the use category for each plant, an estimate of informant's \code{prior_for_answers} with the plant, and the number of possible answers. This can be calculated with `URsum` or given as a value.

Depending on the size of the data this function can return a rather large set of probabilities. There are several ways to perform simple visualizations of these probabilities. Here we use the base R function `heatmap` [@R-base] and the the `dplyr` function`filter`  [@dplyr] to subset to a single species and create a ridge plot. 

```{r ethno_sp_a }
ethno_sp_a <- dplyr::filter(ethnobotanydata, sp_name == "sp_a")
```

Generate prior probabilities for all answers as a matrix. If this is not provided the function assumes a uniform distribution `(prior = -1)`. The probability table should have the same number of columns as uses in the provided ethnobotany data and the same number of rows as there are possible answers for the consensus.

First we set the number of possible answers to '2'. This means informants can either agree it is 'used' or 'not used'.
```{r answers}
  answers <- 2
```

It is also possible to build the probability table manually using `prop.table` [@R-base]. This can be easier if there are many answers or if there is not always a clear preference about where the higher probability should be for the various answers. This matrix must sum up to 100% chance for either 'use' or 'no use'.

Here we use the `dplyr` function `recode` to reset the informant name factor variable as numeric [@dplyr]. This way we can set a prior for the informants skill for the `prior_for_answers` input. Assuming that informants have a varying degree of skill that we can assign as a prior for the likelihood that the data we have are correct for `sp_a`.

```{r ethno_compet_sp_a}
ethno_compet_sp_a <- dplyr::recode(ethno_sp_a$informant, 
    inform_a = 0.9,inform_b = 0.5,inform_c = 0.5,
    inform_d = 0.9, inform_e = 0.9, inform_f = 0.5,
    inform_g = 0.7,inform_h = 0.5,inform_i = 0.9,
    inform_j= 0.9, inform_eight = 0.9,inform_five = 0.6,
    inform_four = 0.5,inform_nine = 0.9, 
    inform_one = 0.5, inform_seven = 0.5,
    inform_six= 0.9, inform_ten = 0.9, 
    inform_three = 0.9, inform_two = 0.5)
```

Run the `ethno_bayes_consensus` function on the subset data of `sp_a`. 
```{r ethno_sp_a_bayes}
ethno_sp_a_bayes <- ethnobotanyR::ethno_bayes_consensus(ethno_sp_a, 
                    answers = 2,
                    #here we keep the default normal distribution with `prior = -1`
                    prior_for_answers = ethno_compet_sp_a) 
```

Create a simple heatmap of the results. The `heatmap` function in R [@R-base] provides a good initial assessment of the results and can be a nice first look at the probability matrix that comes out of the `ethno_bayes_consensus` function. It includes the `hclust` hierarchical cluster analysis using euclidean distance for relationships among both the answers and the uses. This may be useful for looking for similarities among a number of uses or possible answers when there are more than just 'use' and 'non use' (see below). 

```{r heatmap}
heatmap(ethno_sp_a_bayes)
```

Here the '1' and '2' represent 'use' and 'no use' (y-axis). The colors are the probabilities (darker is greater). The `hclust` for these is not very informative since there are only 2. However, the `hclust` for the various uses (x-axis) might be helpful in thinking about how the strength of the information about different use categories for `sp_a` are grouped together. 

### Richer response data

Users often have a large number of counts in cells of the data set after categorization (i.e one user cites ten different 'food' uses but this is just one category). Let's say that the theoretical maximum number of use reports in one category, for one species by one informant is 10. It may be useful to work with these richer datasets for the Bayes consensus analysis. The `ggplot2` and `ggridges` libraries can be used to plot the data as smooth histograms. Here we generate some ethnobotany data with up to 10 citations in a single use category for a species by one informant.

```{r ethno_sp_a_rich}
set.seed(123) #make random number reproducible
ethno_sp_a_rich <- data.frame(replicate(3,sample(0:10,20,rep=TRUE)))
  names(ethno_sp_a_rich) <- 
  gsub(x = names(ethno_sp_a_rich), 
  pattern = "X", replacement = "Use_")  
  ethno_sp_a_rich$informant <- sample(c('User_1', 'User_2'), 
  20, replace=TRUE)
  ethno_sp_a_rich$sp_name <- sample(c('sp_a'), 
  20, replace=TRUE)
```

Define the `prior_for_answers` of the data from these new informants in the simulated ethnobotany data. With `User_1` we have high confidence because perhaps we gather this information through 'walk in the woods' or another method we feel good about. With `User_2` we assign less confidence. Maybe did our work in a rush or gathered in another way that gives us less confidence. 

```{r}
ethno_compet_sp_a_rich <- 
          dplyr::recode(ethno_sp_a_rich$informant,
          User_1 = 0.9, User_2 = 0.5)
```

We keep a normal prior for the data and the knowledge of the informants. 

```{r fig.width=7, fig.height=7}
ethno_sp_a_bayes <- ethnobotanyR::ethno_bayes_consensus(ethno_sp_a_rich,
       answers = 10, 
       prior_for_answers = ethno_compet_sp_a_rich, 
       prior=-1) #keep a normal prior in this example with -1
```

Create a data frame and melt for the `ggplot2` plotting functions.

```{r fig.width=7, fig.height=7}
ethno_sp_a_bayes_melt <-  ethno_sp_a_bayes %>%
  as.data.frame() %>%
  reshape2::melt()
```

Use the `ggplot2` and `ggridges` libraries to plot the data as smooth histograms.

```{r fig.width=7, fig.height=4}
ggplot2::ggplot(ethno_sp_a_bayes_melt, aes(x = value, 
                y = variable, fill = variable)) +
                ggridges::geom_density_ridges() +
                ggridges::theme_ridges() + 
                theme(legend.position = "none")+
                labs(y= "", x = "Example ethno_bayes_consensus of use categories for sp_a")
```

Visualizing the variation in outcomes can be useful for assessing the amount of confidence we have in the cultural use of the plant across categories. 

## References

