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
39 lines (35 loc) • 1.5 kB
JavaScript
import { htmlTable } from '../../utils/html-table.js';
import { TestBase, stats } from '../test-base/index.js';
export class CronbachAlpha extends TestBase {
#ifItemsDeleted
constructor(samples) {
super(samples, 'Cronbach Alpha', ['alpha'], { sameSize: true, minK: 2 });
this.sumOfVariances = this.samples.reduce((t, { varianceSample }) => t + varianceSample, 0)
const transposed = Array.from({ length: this.n }, (_, i) => Array.from({length:this.k},(_,j) => this.samples[j].values[i]))
this.sumColumnVariance = stats(transposed.map(arr => arr.reduce((t,v) => t+v,0))).varianceSample
this.bessel = this.k / (this.k - 1);
this.alpha = this.sumColumnVariance === 0 ? 0 : this.bessel * (1 - this.sumOfVariances / this.sumColumnVariance);
}
get ifItemsDeleted() {
if (this.#ifItemsDeleted) return this.#ifItemsDeleted
this.#ifItemsDeleted = {};
this.samples.forEach(col => {
const cols = {}
this.samples.forEach((s) => {
if (s.name !== col.name) cols[s.name] = s
})
this.#ifItemsDeleted[col.name] = new CronbachAlpha(cols).alpha;
})
return this.#ifItemsDeleted;
}
get htmlTable() {
const { alpha, ifItemsDeleted } = this
return /*html*/`
<h2>Cronbach Alpha</h2>
<hr>
Alpha: ${alpha}
<br><br>
${htmlTable(Object.entries(ifItemsDeleted),['variable','alpha if deleted'])}
`
}
}