CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-simple-statistics

A JavaScript implementation of descriptive, regression, and inference statistics

Pending
Overview
Eval results
Files

regression.mddocs/

Regression Analysis

Functions for linear regression analysis and correlation calculations.

Core Imports

import { 
  linearRegression, 
  linearRegressionLine, 
  rSquared,
  sampleCorrelation,
  sampleCovariance 
} from "simple-statistics";

Linear Regression

linearRegression { .api }

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 pairs

Returns: 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.785

linearRegressionLine { .api }

function 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)

rSquared { .api }

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 pairs
  • func: (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 regression

Correlation Analysis

sampleCorrelation { .api }

function sampleCorrelation(x: number[], y: number[]): number;

Calculates the Pearson correlation coefficient between two variables, measuring linear relationship strength.

Parameters:

  • x: number[] - First variable values
  • y: number[] - Second variable values (same length as x)

Returns: number - Correlation coefficient between -1 and 1

  • 1: Perfect positive correlation
  • 0: No linear correlation
  • -1: Perfect negative correlation
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)

sampleCovariance { .api }

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 values
  • y: number[] - Second variable values (same length as x)

Returns: number - Covariance value

  • Positive: Variables tend to increase together
  • Negative: One increases as other decreases
  • Zero: No linear relationship
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)

Usage Examples

Complete Regression Analysis

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 relationship

Comparing Regression Models

import { 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 fit

Install with Tessl CLI

npx tessl i tessl/npm-simple-statistics

docs

array-operations.md

combinatorics.md

data-manipulation.md

descriptive-statistics.md

distributions.md

index.md

machine-learning.md

math-utilities.md

quantiles.md

regression.md

testing.md

tile.json