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

```{r}
logger$info("Entering outlier analysis section")
```

<br/>
<br/>

## Outlier analysis

For each `location_code`, [adjusted boxplots](https://dx.doi.org/doi:10.1016/j.csda.2007.11.008) are given of the total count for [outliers](https://en.wikipedia.org/wiki/Outlier) detection in the period `r str_c(DATE_FROM, " to ", DATE_TO)`. Outliers are given as dots (if any) in the adjusted box-and-whisker plots below. Note that outliers are not necessarily errors.

<br/>
<br/>

```{r}
d <- d_ltr %>%
    filter(type_name == "TC") %>%
    select(location_code, date, tc = count)
```

```{r width=7, height=7, out.width=600}
d %>%
    ggplot(mapping = aes(x = location_code, y = tc), alpha = 0.5) +
        stat_adj_boxplot() +
        stat_adj_boxplot_outlier() +
        scale_x_discrete(name = "") +
        scale_y_continuous(name = "total count") +
        coord_flip()
```


```{r}
d <- d %>%
    group_by(location_code) %>%
    summarise(n = n(), stats = list(adj_boxplot_stats(tc)), .groups = "drop") %>%
    split(1:nrow(.)) %>%
    map_df(function(x) {
        d %>%
            mutate(n = x %>% chuck("n")) %>%
            filter(location_code == x %>% chuck("location_code")) %>%
            filter(
                (tc < (x %>% 
                    pluck("stats") %>% 
                    unlist %>% 
                    chuck("ymin"))) |
                (tc > (x %>% 
                    pluck("stats") %>% 
                    unlist %>% 
                    chuck("ymax"))))
    })
```


Outliers, if available, are listed in the table below. In addition, also the number of surveys `n` is reported. Litter experts should decide if outliers are errors and need to be excluded from analysis. Note, however, that due to its non-parametric nature, `litteR` is fairly robust for outliers.

<br/>
<br/>

```{r, eval=nrow(d)>0}
d %>%
    arrange(location_code, tc) %>%
    select(location_code, date, n, tc) %>%
    rename("total count" = tc) %>%
    kable()
```