Documentation

Statistics

@statili/stats·v0.0.2-beta.0·MIT

npm install @statili/stats

Regression and smoothing models returning discriminated-union results, so a fit that cannot be computed says so rather than returning a plausible wrong answer.

Install

npm install @statili/stats

Usage

Every model takes options first and data last, so it curries:

import { linear } from '@statili/stats'
import type { DataPoint } from '@statili/stats'

const data: DataPoint[] = [[1, 2], [2, 4], [3, 5], [4, 4], [5, 5]]

const result = linear({ precision: 2 }, data)

if (!result.ok) {
  console.error(result.errorType, result.message)
} else {
  result.slope        // 0.6
  result.intercept    // 2.2
  result.r2           // 0.6
  result.rmse         // 0.69
  result.predict(6)   // [6, 5.8]
}
// Curried / partial application
const fit = linear({ precision: 4 })
const result = fit(data)

Models

Model Fits Notes
linear y = mx + c Adds significance testing and intervals
polynomial y = c₀ + c₁x + … + cₙxⁿ order sets the degree
power y = a·xᵇ Requires x > 0, y > 0
exponential y = a·e^(bx) Requires y > 0
logarithmic y = a + b·ln(x) Requires x > 0
multilinear y = b₀ + b₁x₁ + … Several predictors
logistic P(y=1) = σ(…) Binary classification
loess A smoother; reports shape, not an equation

Inference

linear reports how confident the fit is:

const result = linear({ precision: 4 }, data)
if (!result.ok) return

result.seM            // standard error of the slope
result.pValueM        // two-tailed p-value
result.df             // residual degrees of freedom
result.slopeInterval  // 95% confidence interval for the slope

// Confidence or prediction band at any x
result.interval(6)                 // { fit, lower, upper }
result.interval(6, 'prediction')   // always wider

Failure

Rather than a thrown error or a silent NaN, every model returns a discriminated union:

const result = linear({}, [[1, 1], [1, 2], [1, 3]])

result.ok         // false
result.errorType  // "DegenerateInput"
result.message    // explains that all x-values are identical

errorType is one of InsufficientData, DegenerateInput, InvalidInput, MathError or NumericalStability.

See also

License

MIT

Modules