ggplot-rs 0.17.1

A Rust implementation of ggplot2's Grammar of Graphics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use crate::aes::Aesthetic;
use crate::data::{DataFrame, Value};
use crate::scale::ScaleSet;

use super::Stat;

/// Smoothing method selection.
#[derive(Clone, Debug, Default)]
pub enum SmoothMethod {
    /// Linear regression (y = mx + b).
    #[default]
    Lm,
    /// LOESS with configurable span.
    Loess { span: f64 },
    /// Generalized linear model via anofox-regression (Gaussian or Poisson).
    #[cfg(feature = "regression")]
    Glm { family: SmoothFamily },
    /// Robust linear regression (Huber M-estimator) via anofox-regression.
    #[cfg(feature = "regression")]
    Rlm,
    /// Penalized B-spline (P-spline) GAM smoother via anofox-regression —
    /// ggplot2's `method = "gam"`. λ is chosen by GCV.
    #[cfg(feature = "regression")]
    Gam,
}

/// GLM family for regression-backed smoothing (`SmoothMethod::Glm`), R's
/// `glm(family = …)`.
///
/// For every non-Gaussian family the confidence band is computed as ggplot2's
/// `predictdf.glm` does: the linear predictor and its standard error come from
/// `predict(type = "link", se.fit = TRUE)`, the interval
/// `η ± qnorm(0.975)·se(η)` is formed on the link scale and both ends are
/// mapped through the inverse link — so the band respects the response range
/// (probabilities stay in `[0, 1]`, means stay positive). Dispersion follows
/// R: fixed at 1 for binomial / Poisson / negative binomial, Pearson-estimated
/// for Gamma.
///
/// `Gaussian` keeps the ordinary-least-squares fit with its `t`-based interval
/// (identical to `method = "lm"`).
#[cfg(feature = "regression")]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum SmoothFamily {
    /// Ordinary least squares (identity link).
    #[default]
    Gaussian,
    /// Poisson regression (log link) for count responses.
    Poisson,
    /// Binomial regression for 0/1 (or proportion) responses —
    /// `binomial(link)`, R's default link is logit.
    Binomial(SmoothBinomialLink),
    /// Gamma regression for positive continuous responses — `Gamma(link)`;
    /// R's default link is the inverse.
    Gamma(SmoothGammaLink),
    /// Negative-binomial regression (log link, θ estimated by maximum
    /// likelihood) for over-dispersed counts — R's `MASS::glm.nb`.
    NegativeBinomial,
}

#[cfg(feature = "regression")]
impl SmoothFamily {
    /// `binomial("logit")` — logistic regression.
    pub fn binomial() -> Self {
        SmoothFamily::Binomial(SmoothBinomialLink::Logit)
    }

    /// `Gamma("inverse")` — R's canonical Gamma link.
    pub fn gamma() -> Self {
        SmoothFamily::Gamma(SmoothGammaLink::Inverse)
    }
}

/// Link function for [`SmoothFamily::Binomial`].
#[cfg(feature = "regression")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SmoothBinomialLink {
    /// `log(μ / (1 − μ))` (canonical).
    #[default]
    Logit,
    /// `Φ⁻¹(μ)`.
    Probit,
    /// `log(−log(1 − μ))`.
    Cloglog,
}

/// Link function for [`SmoothFamily::Gamma`].
#[cfg(feature = "regression")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SmoothGammaLink {
    /// `1 / μ` (R's canonical Gamma link).
    #[default]
    Inverse,
    /// `log(μ)`.
    Log,
}

/// Smoothing statistic — supports both linear regression and LOESS.
pub struct StatSmooth {
    /// Number of points to generate for the fitted line.
    pub n_points: usize,
    /// Whether to compute confidence interval.
    pub se: bool,
    /// Smoothing method.
    pub method: SmoothMethod,
}

impl Default for StatSmooth {
    fn default() -> Self {
        StatSmooth {
            n_points: 80,
            se: true,
            method: SmoothMethod::Lm,
        }
    }
}

impl Stat for StatSmooth {
    fn compute_group(&self, data: &DataFrame, scales: &ScaleSet) -> DataFrame {
        match &self.method {
            SmoothMethod::Lm => self.compute_lm(data),
            SmoothMethod::Loess { span } => {
                let loess = super::loess::StatLoess {
                    span: *span,
                    n_points: self.n_points,
                    se: self.se,
                };
                loess.compute_group(data, scales)
            }
            #[cfg(feature = "regression")]
            SmoothMethod::Glm { family } => self.compute_glm(data, Some(*family)),
            #[cfg(feature = "regression")]
            SmoothMethod::Rlm => self.compute_glm(data, None),
            #[cfg(feature = "regression")]
            SmoothMethod::Gam => self.compute_gam(data),
        }
    }

    fn required_aes(&self) -> Vec<Aesthetic> {
        vec![Aesthetic::X, Aesthetic::Y]
    }

    fn name(&self) -> &str {
        "smooth"
    }
}

impl StatSmooth {
    fn compute_lm(&self, data: &DataFrame) -> DataFrame {
        let x_col = match data.column("x") {
            Some(c) => c,
            None => return DataFrame::new(),
        };
        let y_col = match data.column("y") {
            Some(c) => c,
            None => return DataFrame::new(),
        };

        let pairs: Vec<(f64, f64)> = x_col
            .iter()
            .zip(y_col.iter())
            .filter_map(|(x, y)| Some((x.as_f64()?, y.as_f64()?)))
            .collect();

        if pairs.len() < 2 {
            return DataFrame::new();
        }

        let n = pairs.len() as f64;
        let sum_x: f64 = pairs.iter().map(|(x, _)| x).sum();
        let sum_y: f64 = pairs.iter().map(|(_, y)| y).sum();
        let sum_xy: f64 = pairs.iter().map(|(x, y)| x * y).sum();
        let sum_xx: f64 = pairs.iter().map(|(x, _)| x * x).sum();

        let mean_x = sum_x / n;
        let mean_y = sum_y / n;

        let denom = sum_xx - sum_x * sum_x / n;
        let (slope, intercept) = if denom.abs() < f64::EPSILON {
            (0.0, mean_y)
        } else {
            let m = (sum_xy - sum_x * sum_y / n) / denom;
            let b = mean_y - m * mean_x;
            (m, b)
        };

        // Generate fitted values across x range
        let x_min = pairs.iter().map(|(x, _)| *x).fold(f64::INFINITY, f64::min);
        let x_max = pairs
            .iter()
            .map(|(x, _)| *x)
            .fold(f64::NEG_INFINITY, f64::max);

        let step = (x_max - x_min) / (self.n_points - 1).max(1) as f64;

        // Compute standard error of prediction if requested
        let se_values = if self.se && pairs.len() > 2 {
            let residuals: Vec<f64> = pairs
                .iter()
                .map(|(x, y)| y - (slope * x + intercept))
                .collect();
            let sse: f64 = residuals.iter().map(|r| r * r).sum();
            let mse = sse / (n - 2.0);
            Some((mse, sum_xx, mean_x, n))
        } else {
            None
        };

        let mut x_vals = Vec::with_capacity(self.n_points);
        let mut y_vals = Vec::with_capacity(self.n_points);
        let mut ymin_vals = Vec::with_capacity(self.n_points);
        let mut ymax_vals = Vec::with_capacity(self.n_points);

        for i in 0..self.n_points {
            let x = x_min + i as f64 * step;
            let y = slope * x + intercept;
            x_vals.push(Value::Float(x));
            y_vals.push(Value::Float(y));

            if let Some((mse, sum_xx, mean_x, n)) = se_values {
                let se_pred = (mse
                    * (1.0 / n + (x - mean_x).powi(2) / (sum_xx - n * mean_x * mean_x)))
                    .sqrt();
                // 95% confidence interval for the mean response: R's
                // qt(0.975, n − 2), not the large-sample normal 1.96.
                let t_val = crate::stat::dist::qt(0.975, (n - 2.0).max(1.0));
                ymin_vals.push(Value::Float(y - t_val * se_pred));
                ymax_vals.push(Value::Float(y + t_val * se_pred));
            }
        }

        let mut result = DataFrame::new();
        result.add_column("x".to_string(), x_vals);
        result.add_column("y".to_string(), y_vals);
        if !ymin_vals.is_empty() {
            result.add_column("ymin".to_string(), ymin_vals);
            result.add_column("ymax".to_string(), ymax_vals);
        }
        result
    }

    /// GLM / robust-linear smoothing backed by anofox-regression. `family = None`
    /// selects the robust (Huber) fit; `Some(..)` selects a GLM family. A
    /// confidence-interval ribbon (ymin/ymax) is emitted when `self.se` is set.
    #[cfg(feature = "regression")]
    fn compute_glm(&self, data: &DataFrame, family: Option<SmoothFamily>) -> DataFrame {
        use anofox_regression::solvers::{
            FittedRegressor, HuberRegressor, OlsRegressor, Regressor,
        };
        use anofox_regression::{IntervalType, RegressionOptions};
        use faer::{Col, Mat};

        let (x_col, y_col) = match (data.column("x"), data.column("y")) {
            (Some(x), Some(y)) => (x, y),
            _ => return DataFrame::new(),
        };
        let pairs: Vec<(f64, f64)> = x_col
            .iter()
            .zip(y_col.iter())
            .filter_map(|(x, y)| Some((x.as_f64()?, y.as_f64()?)))
            .collect();
        if pairs.len() < 2 {
            return DataFrame::new();
        }

        let n = pairs.len();
        let x = Mat::from_fn(n, 1, |i, _| pairs[i].0);
        let y = Col::from_fn(n, |i| pairs[i].1);
        let x_min = pairs.iter().map(|p| p.0).fold(f64::INFINITY, f64::min);
        let x_max = pairs.iter().map(|p| p.0).fold(f64::NEG_INFINITY, f64::max);
        let steps = self.n_points.max(2);
        let grid = Mat::from_fn(steps, 1, |k, _| {
            x_min + (x_max - x_min) * k as f64 / (steps - 1) as f64
        });
        let interval = if self.se {
            Some(IntervalType::Confidence)
        } else {
            None
        };

        // Fit the requested model and predict (with interval) over the grid.
        let pred = match family {
            None => match HuberRegressor::new().fit(&x, &y) {
                Ok(f) => f.predict_with_interval(&grid, interval, 0.95),
                Err(_) => return DataFrame::new(),
            },
            Some(SmoothFamily::Gaussian) => {
                match OlsRegressor::new(RegressionOptions::default()).fit(&x, &y) {
                    Ok(f) => f.predict_with_interval(&grid, interval, 0.95),
                    Err(_) => return DataFrame::new(),
                }
            }
            Some(family) => match glm_link_prediction(family, &x, &y, &grid, self.se) {
                Some(p) => p,
                None => return DataFrame::new(),
            },
        };

        let mut x_vals = Vec::with_capacity(steps);
        let mut y_vals = Vec::with_capacity(steps);
        let mut ymin_vals = Vec::with_capacity(steps);
        let mut ymax_vals = Vec::with_capacity(steps);
        for k in 0..steps {
            x_vals.push(Value::Float(grid[(k, 0)]));
            y_vals.push(Value::Float(pred.fit[k]));
            if self.se {
                // An inverse link can swap the ends (e.g. Gamma's 1/η).
                let (a, b) = (pred.lower[k], pred.upper[k]);
                ymin_vals.push(Value::Float(a.min(b)));
                ymax_vals.push(Value::Float(a.max(b)));
            }
        }

        let mut result = DataFrame::new();
        result.add_column("x".to_string(), x_vals);
        result.add_column("y".to_string(), y_vals);
        if self.se {
            result.add_column("ymin".to_string(), ymin_vals);
            result.add_column("ymax".to_string(), ymax_vals);
        }
        for col_name in &["color", "fill", "group"] {
            if let Some(col) = data.column(col_name) {
                if let Some(first) = col.first() {
                    result.add_column(col_name.to_string(), vec![first.clone(); steps]);
                }
            }
        }
        result
    }

    /// GAM smoothing via anofox-regression's penalized B-spline (P-spline) with
    /// GCV-selected smoothing — ggplot2's `method = "gam"`. Emits a t-based
    /// confidence ribbon (ymin/ymax) when `self.se` is set.
    #[cfg(feature = "regression")]
    fn compute_gam(&self, data: &DataFrame) -> DataFrame {
        use anofox_regression::solvers::{FittedRegressor, PSplineRegressor, Regressor};
        use anofox_regression::IntervalType;
        use faer::{Col, Mat};

        let (x_col, y_col) = match (data.column("x"), data.column("y")) {
            (Some(x), Some(y)) => (x, y),
            _ => return DataFrame::new(),
        };
        let pairs: Vec<(f64, f64)> = x_col
            .iter()
            .zip(y_col.iter())
            .filter_map(|(x, y)| Some((x.as_f64()?, y.as_f64()?)))
            .collect();
        // The P-spline needs enough distinct support to build a basis; fall back
        // to a straight line for tiny groups.
        if pairs.len() < 6 {
            return self.compute_lm(data);
        }

        let n = pairs.len();
        let x = Mat::from_fn(n, 1, |i, _| pairs[i].0);
        let y = Col::from_fn(n, |i| pairs[i].1);
        let x_min = pairs.iter().map(|p| p.0).fold(f64::INFINITY, f64::min);
        let x_max = pairs.iter().map(|p| p.0).fold(f64::NEG_INFINITY, f64::max);
        let steps = self.n_points.max(2);
        let grid = Mat::from_fn(steps, 1, |k, _| {
            x_min + (x_max - x_min) * k as f64 / (steps - 1) as f64
        });
        let interval = if self.se {
            Some(IntervalType::Confidence)
        } else {
            None
        };

        let pred = match PSplineRegressor::new().fit(&x, &y) {
            Ok(f) => f.predict_with_interval(&grid, interval, 0.95),
            // Degenerate input (e.g. collinear x) — fall back to a line.
            Err(_) => return self.compute_lm(data),
        };

        let mut x_vals = Vec::with_capacity(steps);
        let mut y_vals = Vec::with_capacity(steps);
        let mut ymin_vals = Vec::with_capacity(steps);
        let mut ymax_vals = Vec::with_capacity(steps);
        for k in 0..steps {
            x_vals.push(Value::Float(grid[(k, 0)]));
            y_vals.push(Value::Float(pred.fit[k]));
            if self.se {
                ymin_vals.push(Value::Float(pred.lower[k]));
                ymax_vals.push(Value::Float(pred.upper[k]));
            }
        }

        let mut result = DataFrame::new();
        result.add_column("x".to_string(), x_vals);
        result.add_column("y".to_string(), y_vals);
        if self.se {
            result.add_column("ymin".to_string(), ymin_vals);
            result.add_column("ymax".to_string(), ymax_vals);
        }
        for col_name in &["color", "fill", "group"] {
            if let Some(col) = data.column(col_name) {
                if let Some(first) = col.first() {
                    result.add_column(col_name.to_string(), vec![first.clone(); steps]);
                }
            }
        }
        result
    }
}

/// IRLS convergence settings shared with the anofox-statistics DuckDB
/// extension's GLM aggregates (R's `glm.control(epsilon = 1e-8)`), so a fit in
/// SQL and the plotted smooth agree numerically.
#[cfg(feature = "regression")]
const GLM_TOL: f64 = 1e-8;
#[cfg(feature = "regression")]
const GLM_MAX_ITER: usize = 100;

/// Fitted mean and (optionally) a 95% confidence band for a non-Gaussian GLM
/// family over `grid`, computed like ggplot2's `predictdf.glm`: the band is
/// `linkinv(η ± qnorm(0.975)·se(η))`. `None` when the fit fails (degenerate
/// or out-of-range responses, e.g. a negative count).
#[cfg(feature = "regression")]
fn glm_link_prediction(
    family: SmoothFamily,
    x: &faer::Mat<f64>,
    y: &faer::Col<f64>,
    grid: &faer::Mat<f64>,
    se: bool,
) -> Option<anofox_regression::PredictionResult> {
    use anofox_regression::core::PredictionType;
    use anofox_regression::solvers::{
        BinomialRegressor, GammaRegressor, NegativeBinomialRegressor, PoissonRegressor, Regressor,
    };
    use anofox_regression::{BinomialLink, PredictionResult};
    use faer::Col;

    // Link-scale prediction (η and se(η)) plus the inverse link.
    let (link_pred, linkinv): (PredictionResult, Box<dyn Fn(f64) -> f64>) = match family {
        SmoothFamily::Gaussian => return None,
        SmoothFamily::Poisson => {
            let f = PoissonRegressor::log()
                .tolerance(GLM_TOL)
                .max_iterations(GLM_MAX_ITER)
                .build()
                .fit(x, y)
                .ok()?;
            (
                f.predict_with_se(grid, PredictionType::Link, None, 0.95),
                Box::new(f64::exp),
            )
        }
        SmoothFamily::NegativeBinomial => {
            let f = NegativeBinomialRegressor::builder()
                .tolerance(GLM_TOL)
                .max_iterations(GLM_MAX_ITER)
                .build()
                .fit(x, y)
                .ok()?;
            (
                f.predict_with_se(grid, PredictionType::Link, None, 0.95),
                Box::new(f64::exp),
            )
        }
        SmoothFamily::Binomial(link) => {
            let link = match link {
                SmoothBinomialLink::Logit => BinomialLink::Logit,
                SmoothBinomialLink::Probit => BinomialLink::Probit,
                SmoothBinomialLink::Cloglog => BinomialLink::Cloglog,
            };
            let f = BinomialRegressor::builder()
                .link(link)
                .tolerance(GLM_TOL)
                .max_iterations(GLM_MAX_ITER)
                .build()
                .fit(x, y)
                .ok()?;
            (
                f.predict_with_se(grid, PredictionType::Link, None, 0.95),
                Box::new(move |eta| link.link_inverse(eta)),
            )
        }
        SmoothFamily::Gamma(link) => {
            let (power, inv): (f64, Box<dyn Fn(f64) -> f64>) = match link {
                SmoothGammaLink::Inverse => (-1.0, Box::new(|eta: f64| 1.0 / eta)),
                SmoothGammaLink::Log => (0.0, Box::new(f64::exp)),
            };
            let f = GammaRegressor::builder()
                .link_power(power)
                .tolerance(GLM_TOL)
                .max_iterations(GLM_MAX_ITER)
                .build()
                .fit(x, y)
                .ok()?;
            (
                f.inner()
                    .predict_with_se(grid, PredictionType::Link, None, 0.95),
                inv,
            )
        }
    };

    let n = grid.nrows();
    let eta = &link_pred.fit;
    let fit = Col::from_fn(n, |k| linkinv(eta[k]));
    if !se {
        return Some(PredictionResult::with_intervals(
            fit,
            Col::zeros(n),
            Col::zeros(n),
            Col::zeros(n),
        ));
    }
    let z = crate::stat::dist::qnorm(0.975);
    let lower = Col::from_fn(n, |k| linkinv(eta[k] - z * link_pred.se[k]));
    let upper = Col::from_fn(n, |k| linkinv(eta[k] + z * link_pred.se[k]));
    Some(PredictionResult::with_intervals(
        fit,
        lower,
        upper,
        link_pred.se.clone(),
    ))
}