stat-smooth

Smoother statistic: fitted curve with a pointwise confidence band.

Since 0.0.1.

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

The loess band uses normal quantiles with the residual variance estimated on n - trace(L) degrees of freedom (L the smoothing operator), a close approximation to R’s t-interval construction.

Usage

stat-smooth(
  method: "lm",
  se: true,
  level: 0.95,
  span: 0.75,
  degree: 2,
)

Parameters

Parameter Default Description
method "lm" Smoother method: "lm" (weighted least squares line) or "loess" (tricube-weighted local polynomials).
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).
span 0.75 Loess neighbourhood as a fraction of the data in (0, 1]; smaller spans follow the data more closely.
degree 2 Loess local polynomial degree: 0, 1, or 2.

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.

A loess curve follows the oscillation the linear fit averages away; span tunes how locally it bends.

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

Scatter chart with x and y axes overlaid by a loess smoother and shaded 95 percent confidence band following the wave pattern in the points.

Scatter chart with x and y axes overlaid by a loess smoother and shaded 95 percent confidence band following the wave pattern in the points.

See also

geom-smooth, stat-identity.

Back to top