# Dietary differentiation between *Chlidonias niger* and *C. leucopterus* — R scripts and structure

This repository contains **reproducible R scripts** for analyses of chick diet composition in two sympatric terns:
the Black Tern (*Chlidonias niger*) and White-winged Tern (*C. leucopterus*).

## How to use
1. Place your data files in `data/` (see format below).
2. Open R (≥ 4.3) and run `scripts/utils_packages.R` once to install/load packages.
3. Run scripts in order (01 → 05). Outputs will appear in `outputs/`.

## Data format (you add your Excel/CSV):
- **Option A (LONG)** — one row = one prey item delivered:
  - Required columns: `id` (nest ID), `species` ("leucopterus" or "niger"), `prey` (prey order as text),
    `day`, `age_chicks`, `n_chicks`.
  - Optional: `temperature`, `wind` (not used in the final PERMANOVA model).
  - Unidentified prey should be coded as: `"unidentified"`, `"nieoznaczone"`, `"niezidentyfikowane"`, `"unknown"`, or `"na"`.
  - **Odonata**: already combined (no separate A/Z) recommended.

- **Option B (WIDE)** — one row = one nest:
  - Required columns: `id`, `species`, `day`, `age_chicks`, `n_chicks` and prey-order count columns:
    `diptera`, `odonata`, `trichoptera`, `orthoptera`, `coleoptera`, `ephemeroptera`, `pisces`, `lepidoptera`, `hymenoptera`.
  - Unidentified prey column (`unidentified`) will be ignored if present.

Save your Excel as CSV (UTF-8) or load via `readxl` and write CSVs into `data/` as shown in the scripts.

## Scripts
- `01_PERMANOVA_no_weather.R` — PERMANOVA (Bray–Curtis) on nest-by-taxon relative composition; predictors: species + day_z + age_z + n_z; VIF + PERMDISP; writes summaries.
- `02_NMDS_plot.R` — NMDS (Bray–Curtis) on relative composition; convex hulls by species; saves Figure (stress printed).
- `03_Niche_indices.R` — Levin’s niche breadth; Pianka and Schoener overlap indices; writes a small summary.
- `04_GLMM_by_taxon.R` — GLMMs per prey order (negative binomial, log link), `count ~ species + offset(log(total_prey)) + (1|id)`; Wald test; IRR + 95% CI.
- `05_Prey_proportion_plot.R` — Mean nest-level prey proportions (95% CI) per species × taxon; saves Figure.
- `utils_packages.R` — optional helper for installing required packages (`vegan`, `glmmTMB`, `dplyr`, `tidyr`, `ggplot2`, `ggrepel`, `forcats`, `purrr`, `performance`, `readr`, `readxl`, `stringr`, `scales`).

## Outputs
- `outputs/PERMANOVA_summary.txt`
- `outputs/GLMM_by_taxon_results.csv`
- `outputs/diet_indices_summary.txt`
- `outputs/NMDS_scores.csv`
- `outputs/figures/Fig1_prey_composition.png`, `Fig2_NMDS_plot.png`

## Software
R (≥ 4.3), packages: vegan, glmmTMB, dplyr, tidyr, ggplot2, ggrepel, forcats, purrr, performance, readr, readxl, stringr, scales.

## Citation
Please cite the associated manuscript once available.
