You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Density scaling and bandwidth in type_ridge() #271
In get_density() within type_ridge() the density estimates are scaled to the same maximum which I believe is incorrect. Also the bandwidths are by default computed separately whereas ggridges employs a joint bandwidth estimate which typically seems to be better. But maybe I'm overlooking something here, Vincent @vincentarelbundock ?
For illustration let's consider a sample with two groups with very different variances:
set.seed(0)
d <- data.frame(
y = rep(1, 100),
x = c(rnorm(50), rnorm(50, mean = 3, sd = 0.2)),
z = factor(rep(1:2, each = 50))
)
Using ggridges a joint bandwith of 0.218 is selected and the resulting densities have very different maxima - as you would expect if both should have the area 1.
ggplot(d, aes(x = x, y = z)) + geom_density_ridges() + theme_minimal()
ggplot(d, aes(x = x, y = y, group = z)) + geom_density_ridges() + theme_minimal()
In contrast, because get_density() scales the density to have the same maximum, the second group has a much smaller area than the first in tinyplot(..., type = "ridge").
tinyplot(z ~ x, data = d, type = "ridge", grid = TRUE)
tinyplot(y ~ x | z, data = d, type = "ridge", grid = TRUE)
As the code is so similar: Is the type_density code inspired by ggridges or are they both inspired by something else? Why is this simply using the mean rather than the weighted mean (so that larger groups would receive more weight)?
Should we then always enforce that the same bandwith is used throughout? Or should we allow different bandwidth per ridgeline? If the latter, how should we specify this, with an additional argument or does anyone have a better idea?
As the code is so similar: Is the type_density code inspired by ggridges or are they both inspired by something else?
I wrote (what eventually became) the type_density code a long time ago, so I can't recall my exact thinking. But I think you be be correct in that I compared what I was doing with the ggridges code to make sure they were consistent. IIRC that was prompted by your suggestion to use a common bandwith across groups. See point 2 here.
Why is this simply using the mean rather than the weighted mean (so that larger groups would receive more weight)?
I don't have a good reason. I think I just did what was expedient. Should we switch to weighted.mean (both for type_density and type_ridges)?
OK, thanks for the explanation. In any case, I think that type_density and type_ridge should use the same default bandwidth. And I lean towards weighted.mean but I haven't explored this more systematically to check whether it really works better in unbalanced data.
Note: with #284 we ultimately opted with individual bandwidths in (regular) grouped density plots... albeit with the possibility for users to override via the joint.bw argument.
Should we update the type_ridge code to do so as well?
I was looking at this again today. One discrepancy from the ggridges side is that it treats joint bandwidth selection differently across groups and facets. Specifically, a common bandwidth is used across groups but distinct bandwidths are used across facets (although every group within a facet will receive the same bandwidth). E.g., using Achim's running example from above:
ggplot(d, aes(x=x, y=y, group=z)) + geom_density_ridges()
#> Picking joint bandwidth of 0.218
ggplot(d, aes(x=x, y=y)) + geom_density_ridges() + facet_wrap(~z)
#>Picking joint bandwidth of 0.377#> Picking joint bandwidth of 0.0591
I don't really get why you'd want to treat facets and groups differently here. At the same time, I also think that some kind of joint bandwidth makes sense as a default for ridge plots (contra our decision in #284 to go with individual bandwidths as the default for type_density()).
My current inclination is thus to add to a joint.bw argument, with "owm" (observation-weighted mean) bandwidth as a default. We can offer "full" as an alternative joint bandwidth based on the the full dataset, as well as "none" if users want to opt out of joint bandwidths entirely and calculate the bandwidth for each distribution on its own. Note that this hierarchy essentially reverses the order of the equivalent type_density(joint.bw = ...) defaults. Happy to discuss and debate if either of you feel strongly._
P.S. I'm pretty sure ggridges is using a simple mean to calculate its joint bandwidth, not a weighted mean or full sample bandwidth as we discussed.
Note: As thing stand right now, the default for #270 would actually be the "none" option that is shown last here. I don't think this looks good (even if we decided to make it the default for "vanilla" type_density()).
Activity
vincentarelbundock commentedon Nov 27, 2024
Oh that's interesting. I chose this purely for visual reason, and had no principled reason. Sorry!
I'll defer to you on best defaults.
zeileis commentedon Nov 28, 2024
OK, good, thanks for the quick feedback. Removing the scaling to the same maximum is straightforward.
Regarding the bandwidth: Both
ggridgesandtype_density()seem to use the average of the individual bandwidths per group, see: https://github.com/wilkelab/ggridges/blob/master/R/stats.R#L109-L112 and https://github.com/grantmcdermott/tinyplot/blob/main/R/type_density.R#L83-L86As the code is so similar: Is the
type_densitycode inspired byggridgesor are they both inspired by something else? Why is this simply using the mean rather than the weighted mean (so that larger groups would receive more weight)?Should we then always enforce that the same bandwith is used throughout? Or should we allow different bandwidth per ridgeline? If the latter, how should we specify this, with an additional argument or does anyone have a better idea?
grantmcdermott commentedon Dec 20, 2024
I wrote (what eventually became) the
type_densitycode a long time ago, so I can't recall my exact thinking. But I think you be be correct in that I compared what I was doing with theggridgescode to make sure they were consistent. IIRC that was prompted by your suggestion to use a common bandwith across groups. See point 2 here.I don't have a good reason. I think I just did what was expedient. Should we switch to
weighted.mean(both fortype_densityandtype_ridges)?zeileis commentedon Dec 20, 2024
OK, thanks for the explanation. In any case, I think that
type_densityandtype_ridgeshould use the same default bandwidth. And I lean towardsweighted.meanbut I haven't explored this more systematically to check whether it really works better in unbalanced data.grantmcdermott commentedon Jan 9, 2025
Note: with #284 we ultimately opted with individual bandwidths in (regular) grouped density plots... albeit with the possibility for users to override via the
joint.bwargument.Should we update the
type_ridgecode to do so as well?zeileis commentedon Jan 9, 2025
Yes,
type_density()andtype_ridge()should be consistent in their handling of the bandwidths, I think.I'll try to have a closer look at the details in
type_density()later tonight and then follow-up again.grantmcdermott commentedon Jan 24, 2025
I was looking at this again today. One discrepancy from the
ggridgesside is that it treats joint bandwidth selection differently across groups and facets. Specifically, a common bandwidth is used across groups but distinct bandwidths are used across facets (although every group within a facet will receive the same bandwidth). E.g., using Achim's running example from above:I don't really get why you'd want to treat facets and groups differently here. At the same time, I also think that some kind of joint bandwidth makes sense as a default for ridge plots (contra our decision in #284 to go with individual bandwidths as the default for
type_density()).My current inclination is thus to add to a
joint.bwargument, with"owm"(observation-weighted mean) bandwidth as a default. We can offer"full"as an alternative joint bandwidth based on the the full dataset, as well as"none"if users want to opt out of joint bandwidths entirely and calculate the bandwidth for each distribution on its own. Note that this hierarchy essentially reverses the order of the equivalenttype_density(joint.bw = ...)defaults. Happy to discuss and debate if either of you feel strongly._P.S. I'm pretty sure
ggridgesis using a simple mean to calculate its joint bandwidth, not a weighted mean or full sample bandwidth as we discussed.Edits: Reworded second from last paragraph.
grantmcdermott commentedon Jan 24, 2025
Okay, here's an example of my proposed changes in action. If you are both happy, then I'll role these in to #270. (Which we can hopefully then merge.)
Note: As thing stand right now, the default for #270 would actually be the
"none"option that is shown last here. I don't think this looks good (even if we decided to make it the default for "vanilla"type_density()).