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
151 lines (109 loc) • 5.07 kB
Markdown
<title>Statistics — Managing Multiple Tables</title>
<description>Manage multiple tables, combine columns across them, and feed results into Analyze. Useful for before/after, multi-group, or split pipelines.</description>
<keywords>statistics manager, multi table, before after, splitBy, combine columns</keywords>
# Statistics (multi-table manager)
`Statistics` is a lightweight coordinator for **multiple** `Table`s. It lets you:
- register tables (`addTable`),
- compute the union of available column names (`colNames`),
- **combine the same columns from different tables** into a new `Table` (`columns(...)`),
- remove tables (`deleteTable`),
- and access the module namespace (static): `Statistics.Table`, `Statistics.Stats`, `Statistics.Analyze`, `Statistics.Column`.
> It’s especially handy for **before/after** designs, or when you **split** one table by a factor and then want to analyze the resulting groups together.
---
## API
```ts
new Statistics(name?: string)
statistics.addTable(obj: Record<string, number[]>, options?: { name?: string, minK?: number, alignColumns?: boolean }): Table
statistics.deleteTable(tableName: string): void
// set of distinct column names across all registered tables
statistics.colNames: string[]
// Combine selected columns (from *every* table that has them) into a new Table.
// Result columns are named `${tableName}_${colName}`.
statistics.columns(name: string, ...colFilter: (string|RegExp)[]): Table
// Static accessors (namespaces)
Statistics.Table
Statistics.Stats
Statistics.Analyze
Statistics.Column
```
### Column selection (`colFilter`)
`columns(name, ...colFilter)` uses the same filtering helper as `Table`:
- pass exact names: `columns('X', 'score')`
- pass regex: `columns('X', /^score|age$/)`
- exclude by prefixing with `-`: `columns('X', 'score', '-score_z')`
---
## Examples
### 1) Before/After (paired)
```js
import Statistics from 'als-statistics';
const { CompareMeans } = Statistics.Analyze;
const S = new Statistics('A/B');
// register two tables with the same column name "score"
S.addTable({ score: [62, 71, 69, 73, 75] }, { name: 'before' });
S.addTable({ score: [70, 76, 70, 78, 79] }, { name: 'after' });
// collect score columns from all tables into one Table
const merged = S.columns('Scores', 'score'); // -> columns: before_score, after_score
// run paired t-test using the Table shortcut
const paired = merged.compareMeans('before_score', 'after_score').paired();
console.log({ t: paired.t, df: paired.df, p: paired.p });
```
### 2) Split → Combine → Independent Welch
```js
import { Table } from 'als-statistics';
import Statistics from 'als-statistics';
const { CompareMeans } = Statistics.Analyze;
const t = new Table(
{ group: [0,1,0,1,0,1], score: [62,75,70,81,64,78] },
{ name: 'Survey' }
);
// split by "group" → returns a Statistics instance with one table per group
const S = t.splitBy('group', { 0: 'control', 1: 'treat' });
// bring the "score" columns from each split table into ONE Table
const merged = S.columns('scored', 'score'); // control_score, treat_score
const test = merged.compareMeans('control_score','treat_score').independentWelch();
console.log({ t: test.t, df: test.df, p: test.p });
```
### 3) Cross-table correlation
```js
const merged = S.columns('ab', 'score'); // e.g., before_score, after_score
const corr = merged.correlate('before_score','after_score').pearson();
console.log({ r: corr.r, p: corr.p });
```
## Scenarios
### A) Before/After (pre→post) in separate tables
```js
import Statistics from 'als-statistics';
const S = new Statistics();
S.addTable('pre', preTable);
S.addTable('post', postTable);
// Merge the same column name from multiple tables
const merged = S.columns('merged', 'score'); // pre_score, post_score
const cm = merged.compareMeans('pre_score','post_score').paired();
console.log({ t: cm.t, df: cm.df, p: cm.p });
```
### B) Split → Combine workflow
```js
const S = new Statistics();
S.addTable('raw', rawTable);
// Split by factor into two new tables
const { control, treat } = S.split('raw', by => by.group === 'A' ? 'control' : 'treat');
// Combine same-named columns for cross-table analysis
const merged = S.columns('combined', 'score'); // control_score, treat_score
const res = merged.compareMeans('control_score','treat_score').independentWelch();
```
## How‑to recipes
- **Compute cross-table correlation** between `before_score` and `after_score`
```js
const merged = S.columns('ab', 'score');
merged.correlate('before_score','after_score').pearson();
```
- **Build a summary sheet** for multiple tables (mean, sd, n)
```js
const names = S.colNames();
const rows = names.map(col => {
const t = S.columns('tmp', col);
const d = t.describe(`${col}_0`); // first
return { col, mean: d.mean, sd: d.stdDevSample, n: d.n };
});
```
> Live CodePen demos: _add your links here_.