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

## Trend analysis

For each spatial 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/>
<br/>

```{r}
# check spatial codes
missing_spatial_codes <- SPATIAL_CODE %>%
  setdiff(unique(d_stats$spatial_code))
if (length(missing_spatial_codes) > 0) {
    logger$warn(str_glue("The following specified spatial code(s) are not found and will be skipped: {enumerate(sQuote(missing_spatial_codes))}"))
}
SPATIAL_CODE <- d_stats %>%
    chuck("spatial_code") %>% 
    unique %>%
    intersect(SPATIAL_CODE)

# check type names
missing_type_names <- TYPE_NAME %>%
  setdiff(unique(d_stats$type_name))
if (length(missing_type_names) > 0) {
    logger$warn(str_glue("The following specified type name(s) are not found and will be skipped: {enumerate(sQuote(missing_type_names))}"))
}
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(str_glue("The following specified group code(s) are not found and will be skipped: {sQuote(enumerate(missing_group_codes))}"))
}
GROUP_CODE <- d_stats %>%
    chuck("type_name") %>% 
    unique %>%
    intersect(GROUP_CODE)
```

```{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 = 3),
        slope = b1 * 365.25) %>%
    select(spatial_code, type_name, from, to, slope, `p-value` = p_value, N = n) %>%
    arrange(spatial_code, desc(abs(slope))) %>%
    rename(`type name / group code` = type_name) %>%
    mutate_if(
        is_double,
        formatC, format = "fg", digits = 3) %>%
    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))`.


<br/>
<br/>
```{r eval=length(SPATIAL_CODE) == 0L}
  logger$info("No valid spatial_code(s) found. Therefore, time-series plot are skipped")
```

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