Skip to content

Biology: sex ratio by nursery and season with a binomial mixed model (#117) - #121

Merged
Luen merged 1 commit into
mainfrom
sex-ratio-glmm
Nov 3, 2026
Merged

Luen merged 1 commit into
mainfrom
sex-ratio-glmm

Conversation

@Luen

@Luen Luen commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Closes #117.

What

A mixed model of sex for individually identified sharks:

  • Unit: each shark is counted once, at its first capture.

  • Random effects: the nursery and the season of that capture, as crossed random intercepts, fitted per species.

  • Model type: because the outcome is female yes/no, this is the logistic (binomial) form of a linear mixed model.

  • analytics/glmm.ts is a new, generic fitter:

    • it uses the Laplace approximation, finding fixed and random effects together by penalised IRLS (as lme4's glmer(nAGQ = 0)) and choosing the standard deviations by Nelder–Mead;
    • each random effect is tested by refitting with its SD fixed at 0, using a likelihood-ratio test with the p-value halved for the boundary.
  • Validation (glmm.test.ts): R isn't available in CI, so the fitter is checked against:

    • the exact logistic-regression answer when the random effects are switched off (intercept, SE and log-likelihood);
    • SD recovery and correct tests on simulated single and crossed designs.

    I also checked recovery over six simulated seeds per scenario. For true SDs of 1/0, 0.5/0.3 and 0/0, the estimates were 0.70–1.36/0, 0.37–0.67/0.25–0.42 and about 0/0, with the tests matching each case.

  • analytics/sex.ts runs the model per species and reports:

    • the female share at a typical nursery in a typical season;
    • the nursery and season SDs, each with its test;
    • each nursery's shrunken estimate with an approximate 95% interval.
  • /morphometrics#sex:

    • a model summary;
    • a "Share of females by nursery" chart per species (nurseries with ≥ 20 sharks);
    • the season test as a note on the existing per-season charts.
  • Sex-ratio findings now say whether nurseries differ, from the model.

Results on the real data (as the page computes them)

  • Blacktips: about 49% female at a typical nursery. Nurseries differ (SD ≈ 0.19 on the log-odds scale, p ≈ 0.006): Papetoai leans female (≈ 58%) and Tiki leans male (≈ 44%). Seasons don't (SD 0, p = 1).
  • Sicklefin lemons: about 51% female. Neither nurseries (p ≈ 0.42) nor seasons (p = 1) differ beyond chance.
  • The fit takes about 0.4 s and is cached per import.

Checks

  • pnpm lint, pnpm check
  • pnpm test: 80 passed, including 3 new GLMM tests
  • Opt-in real-data test
  • Viewed /morphometrics#sex on the real data in the dev server

🤖 Generated with Claude Code

…117)

- analytics/glmm.ts: logistic regression with crossed random intercepts,
  Laplace approximation (PIRLS as lme4's nAGQ = 0, SDs by Nelder-Mead), and a
  boundary-corrected likelihood-ratio test per random effect. Tested on an
  exact logistic case and simulated crossed designs.
- analytics/sex.ts: one row per individual at its first capture, nursery and
  season as random effects, per species; typical share, SDs, tests and
  each nursery's estimate.
- /morphometrics#sex: model summary, female share by nursery (nurseries with
  >= 20 sharks), and the season test on the per-season charts.
- Sex-ratio findings say whether nurseries differ, from the model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Luen Luen added enhancement New feature or request analytics Charts, statistics and analyses labels Nov 3, 2026
@Luen
Luen merged commit 7baf53b into main Nov 3, 2026
5 checks passed
@Luen
Luen deleted the sex-ratio-glmm branch November 3, 2026 23:16
Luen added a commit that referenced this pull request Nov 3, 2026
Keep both findings imports and repo-map lines. The mixed-model tests on
the /morphometrics sex charts move from notes to the ChartCard stats line,
and the per-nursery charts get one too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Luen Luen mentioned this pull request Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

analytics Charts, statistics and analyses enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analysis: sex ratio by nursery and season (mixed model)

1 participant