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
21 lines (18 loc) • 708 B
JavaScript
import { TestBase } from '../test-base/index.js'
import CDF from '../cdf/index.js';
export class OneSampleTTest extends TestBase {
constructor(samples, mu0 = 0) {
super(samples, 'One Sample T-Test', ['t', 'df', 'p'], { min: 1 })
if (this.n < 2) throw new Error('OneSampleTTest: need at least 2 observations');
this.mu0 = mu0;
this.#calc();
}
#calc() {
const { n, mean, varianceSample } = this.samples[0]
this.sd = Math.sqrt(varianceSample);
this.se = this.sd / Math.sqrt(n);
this.df = n - 1;
this.t = this.se === 0 ? 0 : (mean - this.mu0) / this.se;
}
get p() { return 2 * (1 - CDF.t(Math.abs(this.t), this.df)); }
}