scales icon indicating copy to clipboard operation
scales copied to clipboard

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

Open mjskay opened this issue 3 years ago • 2 comments
trafficstars

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!

mjskay avatar Dec 28 '21 21:12 mjskay

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

hadley avatar Mar 16 '22 14:03 hadley

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

mjskay avatar Mar 16 '22 23:03 mjskay