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

40 lines (34 loc) 1.4 kB
import { htmlTable } from "../../utils/html-table.js"; export class RegressionBase { constructor(columns, yName, xNames = [], step) { this.columns = columns; this.yName = yName; this.xNames = xNames this.step = step } calculate() { const { columns, yName, xNames } = this this.y = columns[yName]; this.X = columns[xNames[0]].map((_, i) => [1, ...xNames.map(name => columns[name][i])]); this.n = this.X.length; this.k = this.X[0].length; this.computeCoefficients(); this.coefficients.forEach(c => { if (!isFinite(c)) throw new Error(`Regression failed: singular matrix or constant predictors ${c}`); }) this.yHat = this.predict(this.X); } computeCoefficients() { } predict() { } get result() { if (!this.coefficients) this.calculate() return { step: this.step, n: this.n, Variable: ['Intercept', ...this.xNames], Coefficient: this.coefficients }; } get htmlTable() { const { result } = this, { step, n } = result; const headers = Object.keys(result).filter(k => k !== 'step' && k !== 'n'); const rows = headers.map((k) => result[k]) const header = `Step:${step} (${this.xNames.length} predictors, n=${n})` return htmlTable(rows, headers, { header, firstColHeader: true, transposeValues:true }) } }