---
title: Supplementary material
date: last-modified
format: 
  html:
    html-table-processing: none
    toc: true
---


# S1 Data preparation

## Data filtering

-   `runLen` > 3
-   exclude detections in the middle of nowhere
-   remove manually classified false positives

## Flight parameter determination

:::{.callout-note}

## Flight definition

Starting east of 8°E and north of 54°N with a distance of > 50 km between first and last receiver.

:::

-   `flightID` flight identifier
-   `flightStart` start time (max signal strength at first receiver within last half hour)
-   `flightEnd` end time (last detection)
-   `flightDate` sunset date of departure
-   `minStopover` minimum stopover duration in days

Find departures without detected flights: movements of > 50 km within one day.

-   `minLength` minimum movement length

## Validate data

Run Shiny app `data/app.R` to:

-   manually remove false positives
-   validate flight parameters 

![Data filtering shiny app.](../data/shiny.png)

## Model flight paths

```{r table}
library(tidyverse)
library(kableExtra)

setwd("..")
files <- list.files(pattern = "flightpaths-", recursive = TRUE)
est <- do.call(rbind, lapply(files, read.csv))

tmp <- est |>
  group_by(speciesEN) |>
  summarise(n = length(unique(flightID)))
    
bind_rows(tmp, tibble(speciesEN = "", n = sum(tmp$n))) |>
  kable() |>
  kable_styling(full_width = FALSE)
```

![Modelled flight paths per species.](../figures/flightpaths.png)

## Flight classification

![Map showing the 10 km coastal buffer (blue area) used as threshold for classifying a flight as 'offshore'.](../figures/map_10km.png)

![Map showing the resulting flight classification. Red lines represent flights classified as 'offshore', blue lines the ones classified as 'onshore'.](../figures/map_cat.png)

# S2 Simulation

## Prior predictive simulations

### Slopes

```{r}
#| label: fig-slopes
#| fig-cap: Prior predictive simulation for slope parameters.
N <- 20 # number of simulations

alpha <- qlogis(rbeta(N, 2, 2))
beta <- rnorm(N, 0, 0.5)

plot(NULL, xlim = c(-2, 2), ylim = c(0, 1), xlab = "scaled")
for (i in 1:N) {
  curve(plogis(alpha[i] + beta[i] * x), from = -2, to = 2, add = TRUE)
}
```

### Measurement error

```{r}
#| label: fig-measurement
#| fig-cap: Prior predictive simulation for the measurement error.
plot(NULL, xlim = c(0, 600), ylim = c(0, 0.05), xlab = "lengthW")
abline(v = c(50, 300), lty = 2)
for (i in 1:N) {
  lines(density(rlnorm(1e4, log(50), 0.2)))
  lines(density(rlnorm(1e4, log(300), 0.2)))
}
```

## Generative model

::: callout-note
The code to simulate and analyse data from the generative model can be found in `4_mixture_simulation.R`.
:::

![Comparison between the posterior distributions and the known parameter values.](../figures/sim_recover.png)

![Comparison between the posterior distribution and the simulated raw data for flight distances over water.](../figures/sim_D.png)

# S3 Results

## Multi-state model

### Departure probability

![Posterior distributions of mean departure probability per species](../figures/ms_psi.png)

![Effect of eastward (u) and southward (v) wind on departure probability, as predicted by the multi-state model with all other variables held at their mean values. The left panels show posterior mean predictions for each species, while the right panels present the overall posterior mean, 100 lines sampled from the posterior, and the raw data distributions by flight category.](../figures/ms_psi_wind.png)

### Routing decision

![Effect of northward wind on offshore flight probability, as predicted by the multi-state model with all other variables held at their mean values. The left panel shows posterior mean predictions for each species, while the right panel presents the overall posterior mean, 100 lines sampled from the posterior, and the raw data distributions by flight category.](../figures/ms_chi_v10.png)

## State-based mixture model

![Posterior predictive checks for flight distance over water per species.](../figures/y_rep.png)

![Posterior distributions of the mean flight distances over water per species.](../figures/D_species.png)

![Posterior distributions of the mean probability of being in state 1 per species.](../figures/p_species.png)

![Effect of eastward wind on the probability for being in state 1, as predicted by the state-based mixture model for 200 km flights, with all other variables held at their mean values. The left panel shows posterior mean predictions for each species, while the right panels present the overall posterior mean, 100 lines sampled from the posterior.](../figures/p_u10.png)

![Effect of departure longitude on the probability for being in state 1 shown as 500 lines sampled from the posterior.](../figures/p_lon.png)

![Map showing the posterior predictions for being in state 1 per flight.](../figures/map_p.png)
