Skip to content

Add skew-lognormal (skewlnorm) distribution family#128

Merged
nehill197 merged 19 commits into
mainfrom
add-skewlnorm
Jul 3, 2026
Merged

Add skew-lognormal (skewlnorm) distribution family#128
nehill197 merged 19 commits into
mainfrom
add-skewlnorm

Conversation

@joethorley

Copy link
Copy Markdown
Member

Closes #127.

Adds a skew-lognormal distribution family, where $X = \exp(Y)$ and $\log(X) = Y$ follows a skew-normal distribution with location meanlog, scale sdlog, and shape.
It mirrors the existing skewnorm family and reduces exactly to the log-normal when shape = 0, just as skewnorm reduces to norm.

Added

  • Base distribution functions in R/skewlnorm.R: dskewlnorm(), pskewlnorm(), qskewlnorm(), rskewlnorm() (parameterised meanlog / sdlog / shape, delegating to the sn package).
  • Helper family integration: ran_skewlnorm(), sens_skewlnorm(), quant_skewlnorm(), dev_skewlnorm(), res_skewlnorm(), log_lik_skewlnorm(), prob_skewlnorm().
  • Tests in tests/testthat/test-skewlnorm.R plus family blocks in the existing test files.
  • _pkgdown.yml reference index and NEWS.md entries.

Implementation notes for review

  • Density $f(x) = f_{SN}(\log x)/x$ for $x > 0$, $0$ otherwise; CDF $F(x) = F_{SN}(\log x)$; quantile $\exp(Q_{SN}(p))$. Each base function mirrors the edge-case handling (NULL / character / NA / length / sdlog checks) of R/skewnorm.R.
  • dev_skewlnorm() mirrors dev_lnorm() (log-transform then dev_skewnorm()).
  • res_skewlnorm() and sens_skewlnorm() use the closed-form skew-lognormal moments derived from the skew-normal MGF: $E[X]=2e^{\mu+\sigma^2/2}\Phi(\delta\sigma)$, $E[X^2]=2e^{2\mu+2\sigma^2}\Phi(2\delta\sigma)$, with $\delta=\alpha/\sqrt{1+\alpha^2}$ (verified against simulation).
  • Design decision to confirm: sens_skewlnorm() preserves the natural-scale mean and scales the SD by sd_mult via a 1-D root solve (holding shape fixed), consistent with sens_lnorm() and reducing to it at shape = 0. Flagging in case a log-scale adjustment is preferred instead.

All affected test files pass locally (sn-gated tests skipped when sn is unavailable).

🤖 Generated with Claude Code

Add a skew-lognormal distribution where log(X) follows a skew-normal
distribution, mirroring the existing skewnorm family and reducing to the
log-normal when shape = 0.

* Base functions in R/skewlnorm.R: dskewlnorm, pskewlnorm, qskewlnorm,
  rskewlnorm (parameterised meanlog/sdlog/shape, delegating to sn).
* Family helpers: ran_skewlnorm, sens_skewlnorm, quant_skewlnorm,
  dev_skewlnorm, res_skewlnorm, log_lik_skewlnorm, prob_skewlnorm.
* sens_skewlnorm preserves the natural-scale mean and scales the SD via a
  1-D solve using the skew-lognormal moments; reduces to sens_lnorm at
  shape = 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@joethorley joethorley marked this pull request as ready for review June 20, 2026 00:28
@joethorley joethorley requested a review from nehill197 as a code owner June 20, 2026 00:28
@aylapear

Copy link
Copy Markdown
Member

@nehill197 I have an analysis that needs this. If you are able to review and assuming no big issues, merging by the end of next week would be helpful.

@aylapear

Copy link
Copy Markdown
Member

@nadinehussein This is the PR I was mentioning that adds the skew normal code that we need.

@nehill197 nehill197 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay just one thing I think was wrong (the raw residual calculation).
Then I've suggested a bunch of changes/additions to the tests, and one question about the news file update and how it'll work with fledge.

Comment thread NEWS.md
Comment thread R/res.R Outdated
Comment thread tests/testthat/test-dev.R
Comment thread tests/testthat/test-res.R
Comment thread tests/testthat/test-res.R Outdated
Comment thread tests/testthat/test-ran.R Outdated
Comment thread tests/testthat/test-ran.R
Comment thread tests/testthat/test-ran.R Outdated
Comment thread tests/testthat/test-ran.R
Comment thread tests/testthat/test-prob.R
joethorley and others added 9 commits July 2, 2026 16:25
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
@joethorley

Copy link
Copy Markdown
Member Author

@nehill197 - I've accepted all your changes except changing our NEWS files as our fledge system is still in development.

@joethorley joethorley requested a review from nehill197 July 2, 2026 23:30
nehill197
nehill197 previously approved these changes Jul 2, 2026
@nehill197 nehill197 self-requested a review July 2, 2026 23:47
@nehill197

Copy link
Copy Markdown
Member

Sorry @joethorley I approved but then when I was going to merge I realized some of my suggestions were unresolved and you hadn't committed them! Can you take another look at these ones that are still open?

Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
joethorley and others added 6 commits July 2, 2026 17:18
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
Co-authored-by: Nicole Hill <nicole@poissonconsulting.ca>
@nehill197 nehill197 merged commit fc4e8df into main Jul 3, 2026
9 checks passed
@nehill197 nehill197 deleted the add-skewlnorm branch July 3, 2026 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add skew-lognormal (skewlnorm) distribution family

3 participants