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

22 lines (18 loc) 791 B
import CDF from '../cdf/index.js'; import { TestBase } from '../test-base/index.js' export class PairedTTest extends TestBase { constructor(samples) { super(samples, 'Paired T-Test', ['t', 'p', 'meanDelta', 'sdDelta', 'n', 'df'], { min: 2, sameSize: true }) this.#calc() } #calc() { this.diffs = this.samples[0].values.map((v, i) => v - this.samples[1].values[i]) this.meanDelta = this.diffs.reduce((t, v) => t + v, 0) / this.n let ss = 0; for (let i = 0; i < this.n; i++) ss += Math.pow(this.diffs[i] - this.meanDelta, 2); this.sdDelta = Math.sqrt(ss / (this.n - 1)); this.t = this.meanDelta / (this.sdDelta / Math.sqrt(this.n)); this.df = this.n - 1; } get p() { return 2 * (1 - CDF.t(Math.abs(this.t), this.df)) } }