{"id":11926,"library":"regression","title":"Regression-js: Least Squares Data Fitting","description":"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.","status":"maintenance","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/tom-alexander/regression-js","tags":["javascript","regression","data","fiting","modeling","analysis"],"install":[{"cmd":"npm install regression","lang":"bash","label":"npm"},{"cmd":"yarn add regression","lang":"bash","label":"yarn"},{"cmd":"pnpm add regression","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses a default export pattern. The 'regression' package provides a single object containing all regression methods. While CommonJS `require` works in Node.js environments, ESM `import` is the recommended standard for modern JavaScript.","wrong":"const regression = require('regression');","symbol":"regression","correct":"import regression from 'regression';"},{"note":"Individual regression methods like `linear` are properties of the default `regression` object, not named exports themselves. They must be accessed via the imported default object.","wrong":"import { linear } from 'regression'; // Incorrect - linear is a method of the default export\nconst result = linear(data);","symbol":"regression.linear","correct":"import regression from 'regression'; const result = regression.linear(data);"},{"note":"Since version 2.0.0, the API for calling regression methods changed from a single `regression(type, data, options)` function to directly calling `regression.type(data, options)`. Also note the `order` option for polynomial regression.","wrong":"const result = regression('polynomial', data, 3); // Old API from earlier versions","symbol":"regression.polynomial","correct":"import regression from 'regression'; const result = regression.polynomial(data, { order: 3 });"}],"quickstart":{"code":"import regression from 'regression';\n\n// Sample data: [[x1, y1], [x2, y2], ...]\nconst data = [\n  [0, 1],\n  [32, 67],\n  [12, 79],\n  [5, 10],\n  [15, 30],\n  [25, 50],\n  [40, 85],\n  [50, 100]\n];\n\n// Perform linear regression\nconst linearResult = regression.linear(data);\nconsole.log('Linear Regression Result:');\nconsole.log('  Equation:', linearResult.equation); // [m, c]\nconsole.log('  String:', linearResult.string);\nconsole.log('  R-squared (R²):', linearResult.r2);\nconsole.log('  Prediction for x=45:', linearResult.predict(45)[1]);\n\n// Perform polynomial regression with order 2\nconst polynomialResult = regression.polynomial(data, { order: 2 });\nconsole.log('\\nPolynomial Regression (Order 2) Result:');\nconsole.log('  Equation:', polynomialResult.equation); // [a_n, ..., a_1, a_0]\nconsole.log('  String:', polynomialResult.string);\nconsole.log('  R-squared (R²):', polynomialResult.r2);\nconsole.log('  Prediction for x=45:', polynomialResult.predict(45)[1]);","lang":"javascript","description":"Demonstrates how to import the `regression` library, perform linear and polynomial regression on sample data, and predict values using the generated models."},"warnings":[{"fix":"Update usage to call specific regression methods directly on the imported `regression` object, e.g., `regression.linear(data)` instead of `regression('linear', data)`.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure all input data is formatted as `Array<[number, number]>`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Set the `precision` option in the configuration object to a higher number or `null` for full precision, e.g., `{ precision: 10 }` or `{ precision: null }`.","message":"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`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For linear regression through the origin, use `regression.linear()` and interpret the `yIntercept` as zero if that is your model assumption. The `lastvalue` functionality is no longer available directly.","message":"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.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Consider its long-term viability for new projects. For active development, assess if an actively maintained alternative might be more suitable, or be prepared to fork and maintain if specific updates are needed.","message":"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.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure 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.","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.","error":"TypeError: regression.linear is not a function"},{"fix":"Verify 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]]`.","cause":"The `data` argument passed to any regression method is not in the expected `[[x1, y1], [x2, y2], ...]` format.","error":"TypeError: data is not an array"},{"fix":"Before 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.","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`).","error":"TypeError: Cannot read properties of undefined (reading 'equation')"}],"ecosystem":"npm"}