Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request (and offer for help): optionally supply derivatives of transforms and inverses #322

Closed
mjskay opened this issue Dec 28, 2021 · 2 comments · Fixed by #341
Closed
Labels
feature a feature request or enhancement

Comments

@mjskay
Copy link
Contributor

mjskay commented Dec 28, 2021

Something that comes up regularly when visualizing analytical distributions on transformed scales is the need to correct the density function for the scale transformation (the so-called "Jacobian correction"), which requires the derivative of the inverse of the scale transformation.

For example, {ggdist} allows visualizing analytical distributions on transformed scales and automatically determines the Jacobian in order to do this correctly. E.g. a log-normal distribution plotted on a log scale should look like a normal distribution (without this correction it would not):

library(ggplot2)
library(magrittr)
library(ggdist)

data.frame(dist = "lnorm") %>%
  ggplot(aes(y = 0, dist = dist, arg1 = log(10), arg2 = 2*log(10)))  
  stat_dist_halfeye()  
  scale_x_log10(breaks = 10^seq(-5,7, by = 2))

Created on 2021-12-28 by the reprex package (v2.0.1)

In {ggdist} we currently do this first by attempting to get the symbolic derivative by applying stats::D() to the scale transformation function definition (this usually fails) and then fall back to numerical derivatives. Naturally, the latter are not perfect and will fail in some cases, degrading the user experience.

The ask / offer

It occurred to me that this can't be the only application where it would be helpful to know the derivative of the $transform() and/or $inverse() of a scale transformation. So I would like to propose adding two additional fields to the trans objects in {scales}:

  • transform_grad (or grad_transform or d_transform or transform_deriv or ...) giving the derivative of transform
  • inverse_grad (or grad_inverse or d_inverse or inverse_deriv or ...) giving the derivative of inverse

These would be optional fields (may be NULL) that not all transformations are required to have. This would retain backwards compatibility, and would also work for my purposes (as I can continue falling back to symbolic and numerical differentiation as needed), but would (1) immediately improve the user experience with existing scale functions by supplying known derivatives in this package and (2) allow users to easily fix cases with manual scales when numerical methods fail by supplying the derivative manually.

If there is interest in something like this, I am happy to supply a PR that creates these fields and supplies derivatives for the existing transformations in R/trans-numeric.r. Thanks!

@hadley
Copy link
Member

hadley commented Mar 16, 2022

This would be great if you're interested in doing it 😄

@hadley hadley added the feature a feature request or enhancement label Mar 16, 2022
@mjskay
Copy link
Contributor Author

mjskay commented Mar 16, 2022

Cool! Definitely interested in doing it 😊. I will take a stab in advance of the next iteration of ggdist, probably this summer.

mjskay added a commit to mjskay/scales that referenced this issue Mar 18, 2022
mjskay added a commit to mjskay/scales that referenced this issue Mar 18, 2022
mjskay added a commit to mjskay/scales that referenced this issue Apr 3, 2022
mjskay added a commit to mjskay/scales that referenced this issue Apr 3, 2022
mjskay added a commit to mjskay/scales that referenced this issue Apr 3, 2022
mjskay added a commit to mjskay/scales that referenced this issue Apr 3, 2022
mjskay added a commit to mjskay/scales that referenced this issue Apr 3, 2022
mjskay added a commit to mjskay/scales that referenced this issue Apr 3, 2022
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
mjskay added a commit to mjskay/scales that referenced this issue Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants