---
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r}
logger$info("Entering descriptive statistics section")
```

## Descriptive statistics

For each spatial code and group/type name, the following statistics have been estimated for the period `r str_c(DATE_FROM, DATE_TO, sep = " to ")`:

- mean count (mean):, _i.e._, [the arithmetic mean](https://en.wikipedia.org/wiki/Arithmetic_mean) of the counts for each litter type;
- median count (median), _i.e._, the [median](https://en.wikipedia.org/wiki/Median) of the counts for each litter type;
- relative count (%TC): the contribution of each litter type to the total count of litter types (%);
- [coefficient of variation](https://en.wikipedia.org/wiki/Coefficient_of_variation) (CV): the ratio of the standard deviation to the mean of the counts for each litter type (%);
- ratio of [MAD](https://en.wikipedia.org/wiki/Median_absolute_deviation) and [median](https://en.wikipedia.org/wiki/Median) (RMAD, %);
- number of surveys (N);
- [Theil-Sen slope](https://en.wikipedia.org/wiki/Theil%E2%80%93Sen_estimator) (slope): a robust non-parametric estimator of slope (litter counts / year);
- [p-value](https://en.wikipedia.org/wiki/P-value): the p-value associated with the one-tailed [Mann-Kendall test](https://en.wikipedia.org/wiki/Kendall_rank_correlation_coefficient) to test the null hypothesis of 
    - no monotonically _increasing_ trend in case the Theil-Sen slope is greater than zero;
    - no monotonically _decreasing_ trend in case the Theil-Sen slope is smaller than zero;


These statistics will be estimated for types with the greatest counts making up `r PERCENTAGE_TOTAL_COUNT`% of the total count for each location and for all groups specified in `r sQuote(path_file(FILE_TYPES))`.

> These statistics have been stored in file `r sQuote(path_file(FILE_STATS))`.


```{r}
logger$info("Creating table with litter statistics")
d_stats <- d_ltr %>%
    group_by(spatial_code, type_name, topx) %>%
    summarise(
        from = format(min(date)),
        to = format(max(date)),
        rel_count = mean(rel_count),
        mean = mean(count),
        median = median(count),
        cv = cv(count),
        rmad = rmad(count),
        n = n(),
        ts = list(theil_sen(date, count)),
        b0 = ts %>%
          first %>%
          intercept,
        b1 = ts %>%
          first %>%
          slope,
        p_value = if_else(
            are_na(b1),
            NA_real_,
            if_else(
                b1 >  0,
                p_value(mann_kendall(count, "increasing")),
                p_value(mann_kendall(count, "decreasing"))
            )
        )) %>%
    ungroup %>%
    select(-ts)

# store stats in a CSV-file
d <- d_stats %>%
    filter(topx | are_na(topx)) %>% # only top x% or groups
    mutate(slope = b1 * 365.25) %>%
    mutate_if(is_double, signif, digits = 3) %>%
    select(spatial_code, from, to, type_name,
           rel_count, mean, median, cv, rmad, n, slope, p_value) %>%
    arrange(spatial_code, desc(rel_count)) %>%
    rename(`%TC` = rel_count)

d %>%
    write_csv(FILE_STATS)

logger$info("Table with litter statistics created")
```


The statistics for the litter groups are given in the table below.

```{r}
d %>%
    filter(type_name == str_to_upper(type_name)) %>% # group codes
    rename(group_code = type_name) %>%
    mutate_if(is_double, formatC, format = "fg", digits = 4) %>%
    kable()
```


The figures below show for each spatial code the median count for each group.

```{r fig.width=5, fig.height=5, out.width=449}
d_stats %>%
  select(spatial_code, type_name, median) %>%
  filter(type_name == str_to_upper(type_name)) %>% # group codes
  arrange(spatial_code, median) %>%
  split(.$spatial_code) %>%
  walk(function(x) {
      x$type_name <- factor(x$type_name, levels = x$type_name, ordered = TRUE)
      g <- ggplot(data = x) +
        geom_col(mapping = aes(x = type_name, y = median), fill = "blue", alpha = 0.5) +
        scale_x_discrete(name = "") +
        scale_y_continuous(name = "median count") +
        coord_flip() +
        ggtitle(x$spatial_code[1])
      print(g)
  })
```