regression-simple-linear

Ordinary least squares simple linear regression for JavaScript and TypeScript, with fit, predict, scoring, and JSON serialization built in.

Library
npm
v3.0.1
31stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
28/100Needs Attention
Development Activity0
Maintenance20
Community20
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
65/100Good
Architecture60
Code Quality85
Innovation80
Learning Curve35

ml-regression-simple-linear is a lightweight TypeScript implementation of ordinary least squares (OLS) simple linear regression, part of the mljs machine-learning ecosystem for JavaScript. Given paired x/y arrays, it computes the best-fit line’s slope and intercept in a single pass, then exposes that model as a class instance with prediction, inverse prediction, string/LaTeX formatting, and goodness-of-fit scoring.

Because it extends the shared BaseRegression class from ml-regression-base, it plugs directly into the rest of the mljs regression family, sharing serialization (toJSON/load) and scoring conventions with sibling packages like polynomial and multivariate regression. This makes it a natural building block for quick statistical analysis, data preprocessing, or as a baseline model when experimenting with more complex regression techniques in a Node.js or browser environment.

What You Get

  • A SimpleLinearRegression class that fits slope and intercept from x/y arrays in a single O(n) pass
  • predict() and computeX() methods for forward and inverse evaluation of the fitted line
  • A score() method returning r, r2, chi2, and rmsd goodness-of-fit metrics
  • toJSON()/static load() for serializing a fitted model and reconstructing it later without refitting
  • toString()/toLaTeX() for human-readable equation formatting with configurable precision

Common Use Cases

  • Fitting a trend line to a small paired dataset for quick exploratory analysis
  • Establishing a baseline linear model before trying more complex regression techniques
  • Serializing a fitted model to JSON for storage or transfer, then reloading it later
  • Computing R² and RMSD to evaluate how well a linear relationship explains observed data

Under The Hood

Architecture The entire implementation lives in a single src/index.ts: a SimpleLinearRegression class extends BaseRegression (imported from the sibling ml-regression-base package) and delegates the actual math to a small internal regress() function that computes slope and intercept via the closed-form OLS formula in one loop over the input arrays. The constructor doubles as both the fitting entry point and the deserialization path used by the static load() method, distinguished by a sentinel boolean first argument (marked with @ts-expect-error at each use) — a compact but slightly brittle overload trick that keeps the public API to a single class without a separate factory function. There are no other internal layers: validation (checkArrayLength) and shared scoring/formatting helpers come from the base package, keeping this package itself extremely small and focused.

Tech Stack Written in TypeScript and built as dual CommonJS/ESM output via two separate tsc passes (tsconfig.cjs.json and tsconfig.esm.json). Testing uses Vitest with @vitest/coverage-v8 for coverage reporting. Linting and formatting are enforced via eslint-config-cheminfo-typescript and Prettier, both run as part of the test script alongside type-checking (tsc --noEmit). The only runtime dependencies are ml-regression-base (the shared regression base class) and cheminfo-types (shared numeric array typings) — both maintained within the same mljs organization. CI and npm publishing run through reusable zakodium/workflows GitHub Actions, and API docs are auto-generated with TypeDoc and deployed to GitHub Pages on release.

Code Quality The test suite in src/__tests__/test.test.ts is compact but reasonably thorough for the package’s scope: it checks a basic fit, a real-world textbook example (with expected r/r² values), a perfect-fit case, a constant (zero-slope) line, negative slope/intercept formatting, mismatched-array-length error handling, and a full JSON serialize/deserialize round trip including a rejection path for a malformed model object. Types are strict throughout via cheminfo-types, and the test npm script gates on tests, lint, formatting, and type-checking together, so none of those can silently regress. No obvious swallowed errors — invalid input throws explicit, tested error messages.

API Design The public surface is intentionally tiny: construct with two arrays, then call predict, computeX, score, toString/toLaTeX, or toJSON. Method names map directly to statistical concepts, so there is very little boilerplate or configuration to learn before getting a result. Because it shares its base class and serialization contract with sibling mljs regression packages (polynomial, power, exponential, etc.), switching between regression types in an application requires minimal call-site changes — a deliberate consistency choice across the family rather than a one-off design. The README usage example covers the full API in a few lines, and generated TypeDoc reference is published for anyone needing method-level detail beyond the JSDoc comments already in source.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search