---
output: html_document
editor_options: 
  chunk_output_type: console
---
```{r}
logger$info("Entering trend analysis section")
```

<br/>
<br/>

## Trend analysis

For each location code and the type names and group codes specified in the settings file, the following statistics have been estimated for the period `r str_c(DATE_FROM, " to ", DATE_TO)`:

- from: the first date of the time-series;
- to: the final date of the time-series;
- [Theil-Sen slope](https://en.wikipedia.org/wiki/Theil%E2%80%93Sen_estimator) (slope): a robust non-parametric estimator of slope (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;
- the number of surveys (N);

A p-value less than an _a priori_ specified [significance level](https://en.wikipedia.org/wiki/Statistical_significance) (_e.g._, often &alpha; = 0.05), indicates a significant trend. If the p-value is greater than this significance level, we can't say that there is no trend. We can only conclude that our data do not show evidence for a significant trend (due to lack of data, noise, etc.).

The Mann-Kendall test is a non-parametric test and as such does not make distributional assumptions on the data. 

<br/>

```{r}
# check location_codes
missing_location_codes <- LOCATION_CODE %>%
  setdiff(unique(d_stats$location_code))
if (length(missing_location_codes) > 0) {
    logger$warn("The following specified location code(s) are not found and will be skipped: ",
        missing_location_codes %>% sQuote %>% enumerate)
}
LOCATION_CODE <- d_stats %>%
    chuck("location_code") %>% 
    unique %>%
    intersect(LOCATION_CODE)

# check type names
missing_type_names <- TYPE_NAME %>%
  setdiff(unique(d_stats$type_name))
if (length(missing_type_names) > 0) {
    logger$warn("The following specified type name(s) are not found and will be skipped: ",
        missing_type_names %>% sQuote %>% enumerate)
}
TYPE_NAME <- d_stats %>%
    chuck("type_name") %>% 
    unique %>%
    intersect(TYPE_NAME)

# check group codes
missing_group_codes <- GROUP_CODE %>%
  setdiff(unique(d_stats$type_name))
if (length(missing_group_codes) > 0) {
    logger$warn("The following specified group code(s) are not found and will be skipped: ",
          missing_group_codes %>% sQuote %>% enumerate)
}
GROUP_CODE <- d_stats %>%
    chuck("type_name") %>% 
    unique %>%
    intersect(GROUP_CODE)
```

<br/>

```{r}
logger$info("Creating table with trend statistics")
d_stats %>%
    filter(type_name %in% unique(c("TC", TYPE_NAME, GROUP_CODE))) %>%
    mutate(
        p_value = formatC(p_value, format = "f", digits = 4),
        slope = b1 * 365.25) %>%
    select(location_code, type_name, from, to, N = n, slope, `p-value` = p_value) %>%
    arrange(location_code, desc(abs(slope))) %>%
    rename(`type name / group code` = type_name) %>%
    mutate_if(
        is_double,
        formatC, format = "fg", digits = 4) %>%
    kable(align = "llllrrr")
logger$info("Table with trend statistics created")
```


<p><br/></p>
> Descriptive statisics and trend analysis results have been stored in file `r sQuote(basename(FILE_STATS))`.


```{r eval=length(LOCATION_CODE) == 0L}
  logger$info("No valid location_code(s) found. Therefore, time-series plot are skipped.")
```

```{r, child=system.file("app", "litter-trend-plots.Rmd", package = "litteR"), eval=length(LOCATION_CODE) != 0L}
```