A JavaScript implementation of descriptive, regression, and inference statistics
—
Functions for linear regression analysis and correlation calculations.
import {
linearRegression,
linearRegressionLine,
rSquared,
sampleCorrelation,
sampleCovariance
} from "simple-statistics";interface RegressionResult {
m: number; // slope
b: number; // y-intercept
}
function linearRegression(data: Array<[number, number]>): RegressionResult;Performs simple linear regression using the least squares method to find the best-fit line through a set of points.
Parameters:
data: Array<[number, number]> - Array of [x, y] coordinate pairsReturns: RegressionResult - Object containing slope (m) and y-intercept (b)
Formula: y = mx + b
import { linearRegression } from "simple-statistics";
const data = [[1, 1], [2, 2], [3, 1.3], [4, 3.75], [5, 2.25]];
const regression = linearRegression(data);
// { m: 0.425, b: 0.785 }
// Line equation: y = 0.425x + 0.785function linearRegressionLine(mb: RegressionResult): (x: number) => number;Creates a function from regression parameters that can predict y values for given x values.
Parameters:
mb: RegressionResult - Object with slope (m) and y-intercept (b)Returns: (x: number) => number - Function that predicts y from x
import { linearRegression, linearRegressionLine } from "simple-statistics";
const data = [[1, 1], [2, 2], [3, 1.3], [4, 3.75], [5, 2.25]];
const regression = linearRegression(data);
const predictY = linearRegressionLine(regression);
const prediction = predictY(6); // 3.335
const prediction2 = predictY(0); // 0.785 (y-intercept)function rSquared(data: Array<[number, number]>, func: (x: number) => number): number;Calculates the coefficient of determination (R²) which measures how well the regression line fits the data.
Parameters:
data: Array<[number, number]> - Array of [x, y] coordinate pairsfunc: (x: number) => number - Regression function (from linearRegressionLine)Returns: number - R² value between 0 and 1 (higher is better fit)
import { linearRegression, linearRegressionLine, rSquared } from "simple-statistics";
const data = [[1, 1], [2, 2], [3, 1.3], [4, 3.75], [5, 2.25]];
const regression = linearRegression(data);
const line = linearRegressionLine(regression);
const fit = rSquared(data, line); // 0.261...
// R² = 0.261 means 26.1% of variance is explained by the regressionfunction sampleCorrelation(x: number[], y: number[]): number;Calculates the Pearson correlation coefficient between two variables, measuring linear relationship strength.
Parameters:
x: number[] - First variable valuesy: number[] - Second variable values (same length as x)Returns: number - Correlation coefficient between -1 and 1
import { sampleCorrelation } from "simple-statistics";
const heights = [60, 65, 70, 75, 80];
const weights = [120, 140, 160, 180, 200];
const correlation = sampleCorrelation(heights, weights); // 1.0 (perfect positive)
const negativeExample = [1, 2, 3, 4, 5];
const negativeY = [5, 4, 3, 2, 1];
const negCorr = sampleCorrelation(negativeExample, negativeY); // -1.0 (perfect negative)function sampleCovariance(x: number[], y: number[]): number;Calculates how much two variables change together. Covariance indicates direction of linear relationship but not strength.
Parameters:
x: number[] - First variable valuesy: number[] - Second variable values (same length as x)Returns: number - Covariance value
import { sampleCovariance } from "simple-statistics";
const x = [1, 2, 3, 4, 5];
const y = [2, 4, 6, 8, 10];
const covariance = sampleCovariance(x, y); // 5.0 (positive covariance)import {
linearRegression,
linearRegressionLine,
rSquared,
sampleCorrelation
} from "simple-statistics";
// Sales data: [advertising_spend, sales_revenue]
const salesData = [
[1000, 50000], [1500, 55000], [2000, 65000],
[2500, 70000], [3000, 80000], [3500, 85000]
];
// Perform regression
const regression = linearRegression(salesData);
console.log(`Slope: ${regression.m}`); // Revenue increase per $ of advertising
console.log(`Intercept: ${regression.b}`); // Base revenue with no advertising
// Create prediction function
const predictRevenue = linearRegressionLine(regression);
const predicted4000 = predictRevenue(4000); // Predict revenue for $4000 advertising
// Measure fit quality
const rSquaredValue = rSquared(salesData, predictRevenue);
console.log(`R²: ${rSquaredValue}`); // How well the model explains variance
// Calculate correlation
const advertising = salesData.map(d => d[0]);
const revenue = salesData.map(d => d[1]);
const correlation = sampleCorrelation(advertising, revenue);
console.log(`Correlation: ${correlation}`); // Strength of linear relationshipimport { linearRegression, linearRegressionLine, rSquared } from "simple-statistics";
const data = [[1, 2], [2, 5], [3, 7], [4, 10], [5, 15]];
// Linear model
const linearModel = linearRegression(data);
const linearPredict = linearRegressionLine(linearModel);
const linearR2 = rSquared(data, linearPredict);
console.log(`Linear R²: ${linearR2}`);
// Compare with other models to find best fitInstall with Tessl CLI
npx tessl i tessl/npm-simple-statistics