UNPKG

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

68 lines (58 loc) 3.01 kB
import { EPS } from "../settings.js"; import { Analyze } from "../../lib/index.js"; const { CompareMeans } = Analyze import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { nearlyEqual, hasDataOrSkip, splitGroups, toRecordFromArray } from '../helpers.js' export function testCompareMeans(g) { const { X, Y, group, score, before, after, Q1, Q2, Q3, Q4 } = g.data; describe('Compare Means (CompareMeans)', () => { const groupsObj = splitGroups(score, group, 3); const cm = new CompareMeans(groupsObj); it('Independent Student (0 vs 1)', () => { const res = cm.independent('0', '1'); const g0 = g.compare_means.independent_student; nearlyEqual(res.t, g0.t, 5e-6, 'ttest student t'); nearlyEqual(res.p, g0.p, 5e-6, 'ttest student p'); nearlyEqual(res.df, g0.df, EPS.df, 'ttest student df'); }); it('Independent Welch (0 vs 1)', () => { const res = cm.independentWelch('0', '1'); const g0 = g.compare_means.independent_welch; nearlyEqual(res.t, g0.t, 5e-6, 'ttest welch t'); nearlyEqual(res.p, g0.p, 5e-6, 'ttest welch p'); nearlyEqual(res.df, g0.df, 5e-4, 'ttest welch df'); // Satterthwaite df can vary slightly }); it('Paired (before vs after)', () => { const c2 = new CompareMeans({ before, after }); const res = c2.paired('before', 'after'); const g0 = g.compare_means.paired; nearlyEqual(res.t, g0.t, 5e-6, 'paired t'); nearlyEqual(res.p, g0.p, 5e-6, 'paired p'); assert.equal(res.df, g0.df, 'paired df'); }); it('One-sample (X vs mu0)', () => { const mu0 = g.compare_means.one_sample.mu0; const c3 = new CompareMeans({ X }); const res = c3.oneSample('X', mu0); const g0 = g.compare_means.one_sample; nearlyEqual(res.t, g0.t, 5e-6, 'one-sample t'); nearlyEqual(res.p, g0.p, 5e-6, 'one-sample p'); assert.equal(res.df, g0.df, 'one-sample df'); }); it('One-way ANOVA (classic & Welch)', () => { const aClassic = cm.anova('0', '1', '2'); const aWelch = cm.anovaWelch('0', '1', '2'); const gC = g.compare_means.anova; const gW = g.compare_means.anova_welch; nearlyEqual(aClassic.F, gC.F, EPS.anovaF, 'anova F'); nearlyEqual(aClassic.p, gC.p, EPS.p, 'anova p'); assert.equal(aClassic.dfBetween, gC.dfBetween, 'anova dfBetween'); assert.equal(aClassic.dfWithin, gC.dfWithin, 'anova dfWithin'); nearlyEqual(aWelch.F, gW.F, 5e-6, 'welch anova F'); nearlyEqual(aWelch.p, gW.p, 5e-6, 'welch anova p'); nearlyEqual(aWelch.dfBetween, gW.dfBetween, 5e-6, 'welch anova dfBetween'); nearlyEqual(aWelch.dfWithin, gW.dfWithin, 1e-3, 'welch anova dfWithin'); // df2 is approximate }); }); }