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
48 lines (41 loc) • 1.82 kB
JavaScript
import { stats } from './stats.js';
import { htmlTable } from '../../utils/html-table.js'
export class TestBaseSimple {
constructor(samples, testName, options = {}) {
const { min = 2, sameSize = false } = options
const entries = Object.entries(samples)
if (entries.length < min) throw new Error(`${testName} requires at least 2 groups`);
this.samples = entries.map(([name, values]) => stats(values, name, { min }))
if(sameSize) {
const nMin = Math.min(...this.samples.map(s => s.n));
this.samples.forEach(s => { if (s.n !== nMin) s.values = s.values.slice(0, nMin); });
}
if (sameSize || min === 1) this.n = this.samples[0].n
this.k = this.samples.length;
this.testName = testName
}
}
export class TestBase extends TestBaseSimple {
constructor(samples, testName, resultKeys, options = {}) {
super(samples, testName, options)
this.resultKeys = resultKeys
}
get descriptive() { return this.samples.map(({ name, mean, stdDev, n }) => ({ name, mean, stdDev, n })) }
get htmlTable() {
const { resultKeys, descriptive, testName, samples } = this
const headers = resultKeys;
const rows = [headers.map(k => this[k])];
const anovaTable = htmlTable(rows, headers, { firstColHeader: false, header: testName });
const dRows = descriptive.map(({ name, mean, stdDev, n }) => [name, mean, stdDev, n]);
const descriptiveTable = htmlTable(dRows, ['name', 'mean', 'stdDev', 'n'], {
header: 'Descriptive statistics'
});
return /*html*/`<div>
<h2>${testName} between ${samples.map(({ name }) => name).join(' , ')}</h2>
<hr>
${descriptiveTable}
${anovaTable}
</div>`;
}
}
export { stats as stats }