Regression-js: Least Squares Data Fitting
regression-js is a JavaScript module that provides a collection of linear least-squares fitting methods for simple data analysis. It offers capabilities for linear, exponential, logarithmic, power, and polynomial regression. The current stable version is 2.0.1, last published over 8 years ago, suggesting a mature but potentially unmaintained codebase, though it remains widely used. It is a lightweight, pure JavaScript solution that runs both in Node.js and modern browsers. Unlike some broader machine learning libraries, regression-js focuses specifically on classical least-squares curve fitting, providing a straightforward API for common trend analysis tasks without external dependencies.
Common errors
-
TypeError: regression.linear is not a function
cause Attempting to call a method on `regression` when it was imported incorrectly, or the `regression` object itself is undefined due to a CJS/ESM mixup.fixEnsure you are using `import regression from 'regression';` for ESM contexts, or `const regression = require('regression');` for CommonJS. If using `regression('linear', data)`, update to `regression.linear(data)` as the old API was removed in v2.0.0. -
TypeError: data is not an array
cause The `data` argument passed to any regression method is not in the expected `[[x1, y1], [x2, y2], ...]` format.fixVerify that your input data is an array of arrays, where each inner array contains exactly two numbers representing an [x, y] coordinate. E.g., `[[1, 2], [3, 4], [5, 6]]`. -
TypeError: Cannot read properties of undefined (reading 'equation')
cause This usually occurs when trying to access properties like `equation`, `string`, or `r2` from the result of a regression call that failed or returned an unexpected value (e.g., if `data` was malformed and the function returned `undefined`).fixBefore accessing properties, check if the `result` object is valid and contains the expected properties. Ensure the input `data` format is correct and the regression function executed successfully.
Warnings
- breaking Version 2.0.0 introduced significant API changes, moving from a single `regression(type, data, options)` function to direct method calls like `regression.linear(data, options)`. Old call patterns will no longer work.
- gotcha Input data must be an array of `[x, y]` pairs (e.g., `[[0, 1], [32, 67]]`). Providing data in other formats (e.g., objects like `{ x: 0, y: 1 }` or separate X and Y arrays) will lead to errors.
- gotcha The `precision` option (defaulting to 2 significant figures) rounds the output equation coefficients. If high precision is required for intermediate calculations, be aware of this rounding or set `precision` to a higher value or `null`.
- deprecated The `lastvalue` and `linearThroughOrigin` methods were removed in version 2.0.0. Linear regression through the origin is now handled by the standard `linear` model.
- gotcha The package's last publish was over 8 years ago. While stable, this indicates that the library is in maintenance mode and unlikely to receive new features, active bug fixes, or updates for modern JavaScript language features or security concerns.
Install
-
npm install regression -
yarn add regression -
pnpm add regression
Imports
- regression
const regression = require('regression');import regression from 'regression';
- regression.linear
import { linear } from 'regression'; // Incorrect - linear is a method of the default export const result = linear(data);import regression from 'regression'; const result = regression.linear(data);
- regression.polynomial
const result = regression('polynomial', data, 3); // Old API from earlier versionsimport regression from 'regression'; const result = regression.polynomial(data, { order: 3 });
Quickstart
import regression from 'regression';
// Sample data: [[x1, y1], [x2, y2], ...]
const data = [
[0, 1],
[32, 67],
[12, 79],
[5, 10],
[15, 30],
[25, 50],
[40, 85],
[50, 100]
];
// Perform linear regression
const linearResult = regression.linear(data);
console.log('Linear Regression Result:');
console.log(' Equation:', linearResult.equation); // [m, c]
console.log(' String:', linearResult.string);
console.log(' R-squared (R²):', linearResult.r2);
console.log(' Prediction for x=45:', linearResult.predict(45)[1]);
// Perform polynomial regression with order 2
const polynomialResult = regression.polynomial(data, { order: 2 });
console.log('\nPolynomial Regression (Order 2) Result:');
console.log(' Equation:', polynomialResult.equation); // [a_n, ..., a_1, a_0]
console.log(' String:', polynomialResult.string);
console.log(' R-squared (R²):', polynomialResult.r2);
console.log(' Prediction for x=45:', polynomialResult.predict(45)[1]);