stat-smooth

Smoother statistic: closed-form linear fit with a pointwise confidence band.

Returns a dense grid of (x, y, ymin, ymax) rows where y is the fitted value and ymin/ymax bound a pointwise confidence band.

Usage

stat-smooth(
  method: "lm",
  se: true,
  level: 0.95,
)

Parameters

Parameter Default Description
method "lm" Smoother method. "lm" is the only supported value in v1.
se true Whether to compute the confidence band. When false, ymin == ymax == y.
level 0.95 Confidence level for the band (e.g., 0.95).

Returns

Statistic object with name: "smooth", consumed by geom layers.

Outputs

  • x.
  • y.
  • ymin.
  • ymax.

Examples

Linear fit with the default 95% confidence band.

#let d = range(0, 20).map(i => (
  x: i,
  y: i * 0.5 + calc.sin(i * 0.4) * 2,
))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (
    geom-point(size: 2pt),
    geom-smooth(method: "lm"),
  ),
  width: 10cm,
  height: 6cm,
)

Scatter chart with x and y axes overlaid by a linear smoother and shaded 95 percent confidence band tracking the upward trend in the points.

Scatter chart with x and y axes overlaid by a linear smoother and shaded 95 percent confidence band tracking the upward trend in the points.

Constructor form: stat: stat-smooth() is equivalent to stat: "smooth" with defaults. Use the constructor on any geom and to customise method, se, or level. Both forms honour colour grouping.

#let d = ()
#for grp in ("a", "b") {
  for i in range(0, 20) {
    d.push((x: i, y: i * 0.5 + (if grp == "b" { 1.5 } else { 0 }) + calc.sin(i * 0.4), grp: grp))
  }
}
#plot(
  data: d,
  mapping: aes(x: "x", y: "y", colour: "grp"),
  layers: (
    geom-point(size: 2pt),
    geom-line(stat: stat-smooth(method: "lm", se: false)),
  ),
  width: 10cm,
  height: 6cm,
)

Scatter chart with x and y axes split into two colour groups a and b, each overlaid by its own linear smoother line drawn without a confidence band.

Scatter chart with x and y axes split into two colour groups a and b, each overlaid by its own linear smoother line drawn without a confidence band.

See also

geom-smooth, stat-identity.

Back to top