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
45 lines (40 loc) • 1.86 kB
JavaScript
import LinearRegression from './linear.js';
import LogisticRegression from './logistic.js'
import { TestBaseSimple } from '../test-base/index.js'
export default class Regression extends TestBaseSimple {
static LinearRegression = LinearRegression
static LogisticRegression = LogisticRegression
regressions = { linear: LinearRegression, logistic: LogisticRegression }
constructor(data,options) {
super(data, 'Multiple Regression', { min: 2 })
let { yName, xNames = [], type = 'linear' } = options
this.Regression = this.regressions[type]
this.yName = yName
const colValues = {}
this.samples.forEach(({ values, name }) => colValues[name] = values)
if (xNames.length === 0) xNames = this.samples.map(({name}) => name).filter(n => n !== yName)
this.steps = [new this.Regression(colValues, yName, xNames, 0).calculate()]
}
next(newPredictors = []) {
let { columns, xNames, step, yName } = this.steps[this.steps.length - 1]
const newTable = {};
for (let col in columns) newTable[col] = (columns[col] || columns[col].values).slice()
newPredictors.forEach(xName => {
if (xName.includes('*')) {
const [colName1, colName2] = xName.split('*')
newTable[xName] = columns[colName1].map((v, i) => v * newTable[colName2][i])
}
})
const reg = new this.Regression(newTable, yName, [...new Set([...xNames, ...newPredictors])], step + 1)
this.steps.push(reg.calculate());
return this
}
get results() { return this.steps.map(({ result }) => result) }
get htmlTables() {
return /*html*/`<div>
<h2>Linear regression to predict ${this.yName}</h2>
<hr>
${this.steps.map(({ htmlTable }) => htmlTable).join('<hr>') }
</div>`
}
}