ANOVA demo

statinfer (opens in a new tab) is a JavaScript package for statistical tests, with TypeScript types included. It runs in Node.js and in the browser.

This page demonstrates its anova function, which compares the averages of several groups at once. Enter your own groups of numbers below: statinfer calculates the result right here in your browser, explains what it means, and shows the JavaScript code that produces it.

On this page: When to use ANOVA · Demo data · Demo results · What the results mean · How to use it · Further reading

When to use ANOVA

A one-way ANOVA (analysis of variance) asks whether three or more groups have the same average. For example: do three fertilisers give the same average plant height, or do students taught with four different methods get the same average score? For just two groups, use the t-test instead.

It works by comparing two kinds of variation: how far apart the group averages are (between groups), and how spread out the values are inside each group (within groups). If the averages are far apart compared with the spread inside each group, the groups probably differ.

ANOVA works best when the values are independent, roughly bell-shaped within each group, and similarly spread out in every group. For skewed data or outliers, the Kruskal-Wallis test is more reliable, and it's planned for a future version of statinfer.

A significant result tells you that at least one group's average differs, but not which one. Follow-up ("post-hoc") tests such as Tukey's HSD answer that, and are also planned.

Demo data

Demo results

One-way ANOVA
=============
statistic  16.6
p-value    0.00035
df         2, 12

The statistic is the F statistic: the between-group variation divided by the within-group variation. The larger it is, the more the group averages differ relative to the spread inside each group. An ANOVA has two degrees of freedom: the first comes from the number of groups, and the second from the total number of values.

What the results of ANOVA mean

The F statistic from the demo data entered above is 16.6, with 2 and 12 degrees of freedom.

If the groups all come from populations with the same mean, the chance of getting a result at least this extreme is less than 0.001. That's the p-value.

That's below 0.05, so at the common 5% level this counts as evidence that at least one group's population mean is different.

How to use statinfer's ANOVA in your JavaScript code

Install statinfer from npm:

npm install statinfer

This JavaScript code reproduces the result above. It also works unchanged in TypeScript, where statinfer's built-in types give you autocompletion and type checking.

import { anova, summarize } from "statinfer";

const result = anova({
  groups: [
    [23, 25, 21, 27, 24],
    [30, 28, 33, 29],
    [22, 20, 24, 23, 21, 25],
  ],
});

console.log(summarize(result));

For every option anova accepts, see its API documentation (opens in a new tab).

The raw result

anova returns a plain JavaScript object, which your code can use directly. For example, result.pValue is the p-value, and result.df is a pair of numbers, [between, within]. Below, the same object is shown as JSON (JavaScript Object Notation), a standard text format for data, which you can save or send to other programs. ANOVA has no estimate, confidence interval or direction, so those fields are null.

Show the raw result as JSON
{
  "method": "One-way ANOVA",
  "statistic": 16.601941747572816,
  "pValue": 0.00034997175443143857,
  "df": [
    2,
    12
  ],
  "estimate": null,
  "confidenceInterval": null,
  "confidenceLevel": null,
  "alternative": null
}

Further reading