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
52 lines (42 loc) • 2.18 kB
JavaScript
import Column from '../column/index.js'
import SimpleTable from './simple-table/index.js'
import Analyze from '../analyze/index.js'
const { CompareMeans, Correlate, Clustering: { Dbscan, Hdbscan }, Regression } = Analyze
import { filterKeys } from '../utils/filter-keys.js'
class Table extends SimpleTable {
Table = Table
constructor(samples, options = {}) { super(samples, options) }
recode(colName, fn, newColName) {
const recoded = this.columns[colName].values.map(fn)
if (newColName) this.addColumn(newColName, recoded)
else this.columns[colName].values = recoded
}
compute(fn, name) {
const computed = this.rows(true).map((row, i) => fn(row, i))
return name ? this.addColumn(name, computed) : new Column(computed, name)
}
correlate(...colFilter) { return new Correlate(this.#filterCols(colFilter)) }
compareMeans(...colFilter) { return new CompareMeans(this.#filterCols(colFilter)) }
dbscan(colFilter, options = {}) { return new Dbscan(this.#filterCols(colFilter), options) }
hdbscan(colFilter, options = {}) { return new Hdbscan(this.#filterCols(colFilter), options) }
regression(yName, xNames, type = 'linear') { return new Regression(this.columns, { yName, xNames, type }) }
linear(yName, xNames) { return this.regression(this.columns, { yName, xNames, type: 'linear' }) }
logistic(yName, xNames) { return this.regression(this.columns, { yName, xNames, type: 'logistic' }) }
where(fn) { return this.rows().map((row, i) => fn(row, i) ? i : null).filter((v) => v !== null) }
filterRowsBy(colName, fn) {
return this.filterRows(this.columns[colName].values.map((v, i) => (fn(v) ? null : i)).filter(v => v !== null));
}
filterRows(indexes = []) {
for (let colName in this.columns) {
this.columns[colName].values = this.columns[colName].values.filter((v, i) => !indexes.includes(i))
}
this.alignColumns()
return this
}
#filterCols(colFilter) {
const cols = {}
filterKeys(this.colNames, colFilter).forEach(n => cols[n] = this.columns[n])
return cols
}
}
export default Table;