als-statistics
Version:
Modular JS statistics toolkit for Node.js and the browser: descriptive stats, correlations (Pearson/Spearman/Kendall), t-tests & ANOVA (Student/Welch), reliability (Cronbach’s alpha), regression (linear/logistic), clustering (DBSCAN/HDBSCAN), and table/co
35 lines (30 loc) • 1.65 kB
JavaScript
import { EPS } from "../settings.js";
import { Analyze } from "../../lib/index.js";
const { Regression } = Analyze;
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { nearlyEqual, hasDataOrSkip, splitGroups, toRecordFromArray } from '../helpers.js'
export function testRegression(g) {
const { X, Y, group, score, before, after, Q1, Q2, Q3, Q4 } = g.data;
describe('Regression (Linear core via wrapper)', () => {
it('simple Y ~ X (coefficients, r2)', () => {
// Build a tiny table-like object expected by wrapper
const data = { X, Y };
const reg = new Regression(data, { yName: 'Y', xNames: ['X'], type: 'linear' });
const step0 = reg.steps[0].calculate();
// Coefficients: [Intercept, β_X]
const beta0 = step0.coefficients?.[0];
const beta1 = step0.coefficients?.[1];
nearlyEqual(beta1, g.regression.linear.slope, EPS.reg, 'linear slope (β_X)');
nearlyEqual(beta0, g.regression.linear.intercept, EPS.reg, 'linear intercept');
nearlyEqual(step0.r2, g.regression.linear.r2, 5e-6, 'linear r2');
// If p-values are exposed (for slope), compare
if (Array.isArray(step0.pValues) && step0.pValues.length >= 2) {
nearlyEqual(step0.pValues[1], g.regression.linear.p, 5e-4, 'linear p-value (β_X)');
}
if (Array.isArray(step0.standardErrors) && step0.standardErrors.length >= 2) {
nearlyEqual(step0.standardErrors[1], g.regression.linear.stderr, 5e-4, 'linear stderr (β_X)');
}
});
});
}