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):
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!
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):
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 thetrans
objects in {scales}:transform_grad
(orgrad_transform
ord_transform
ortransform_deriv
or ...) giving the derivative oftransform
inverse_grad
(orgrad_inverse
ord_inverse
orinverse_deriv
or ...) giving the derivative ofinverse
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!