Skip to main content

ggplot_rs/stat/
mod.rs

1pub mod bin;
2pub mod bin2d;
3pub mod bindot;
4pub mod binhex;
5pub mod boxplot;
6pub mod calendar;
7#[cfg(feature = "ggpubr")]
8pub mod compare_means;
9pub mod contour;
10pub mod contour_filled;
11#[cfg(feature = "ggpubr")]
12pub mod cor;
13pub mod count;
14pub mod density;
15pub mod density2d;
16pub mod dist;
17pub mod ecdf;
18pub mod ellipse;
19pub mod function;
20pub mod identity;
21pub mod loess;
22pub mod marching_squares;
23pub mod qq;
24#[cfg(feature = "regression")]
25pub mod quantile;
26pub mod smooth;
27pub mod sum;
28pub mod summary;
29pub mod summary2d;
30pub mod summary_bin;
31pub mod ydensity;
32
33use crate::aes::{Aes, Aesthetic};
34use crate::data::DataFrame;
35use crate::scale::ScaleSet;
36
37/// Trait for statistical transformations.
38pub trait Stat: Send + Sync {
39    /// Transform data for a single group.
40    fn compute_group(&self, data: &DataFrame, scales: &ScaleSet) -> DataFrame;
41
42    /// Required aesthetics this stat needs.
43    fn required_aes(&self) -> Vec<Aesthetic>;
44
45    /// Default aesthetic mappings this stat produces.
46    fn default_aes(&self) -> Aes {
47        Aes::default()
48    }
49
50    /// If true, the stat receives every row of a panel at once (grouped only by
51    /// facet variables) instead of once per aesthetic group. Needed for
52    /// cross-group comparisons such as `stat_compare_means`, which must see all
53    /// groups together. Defaults to per-group behaviour.
54    fn panelwise(&self) -> bool {
55        false
56    }
57
58    /// Name for debug/display.
59    fn name(&self) -> &str;
60}
61
62/// R's `bw.nrd0`: Silverman's rule of thumb, `0.9 · min(sd, IQR/1.34) · n^-0.2`,
63/// with R's fallbacks when the spread is zero — `sd`, then `|x[0]|`, then `1` —
64/// so a constant sample still gets a positive bandwidth (never 0 → NaN).
65pub(crate) fn bw_nrd0(values: &[f64]) -> f64 {
66    let n = values.len();
67    if n < 2 {
68        return 1.0;
69    }
70    let nf = n as f64;
71    let mean = values.iter().sum::<f64>() / nf;
72    let sd = (values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (nf - 1.0)).sqrt();
73    let mut sorted = values.to_vec();
74    sorted.sort_by(|a, b| a.total_cmp(b));
75    let q = |p: f64| {
76        let h = (n - 1) as f64 * p;
77        let lo = h.floor() as usize;
78        let hi = (lo + 1).min(n - 1);
79        sorted[lo] + (h - lo as f64) * (sorted[hi] - sorted[lo])
80    };
81    let iqr = q(0.75) - q(0.25);
82    let mut lo = sd.min(iqr / 1.34);
83    for fallback in [sd, values[0].abs(), 1.0] {
84        if lo > 0.0 && lo.is_finite() {
85            break;
86        }
87        lo = fallback;
88    }
89    0.9 * lo * nf.powf(-0.2)
90}