t-test 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 tTest function, which compares averages. Enter your own 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 a t-test ยท Demo data ยท Demo results ยท What the results mean ยท How to use it ยท Further reading
When to use a t-test
A t-test compares averages (means). It asks whether a difference is bigger than you'd expect from random variation alone. There are four kinds:
- One-sample: is the average of one group different from a target value? For example, does a machine fill bottles with 500 ml on average?
- Paired: did the same subjects change between two measurements? For example, blood pressure before and after a treatment, for the same patients.
- Two-sample (Welch): do two separate groups have different averages? For example, test scores of two classes. This is the usual choice, because it doesn't assume the groups are equally spread out.
- Two-sample (pooled variance): the same question, but assuming both groups are equally spread out. Only use it when you have a good reason to believe that.
t-tests work best when the measurements are independent of each other, and either roughly bell-shaped or from reasonably large samples. For small samples with outliers or very skewed data, a rank-based test is more reliable. The Wilcoxon test is planned for a future version of statinfer.
Demo data
Demo results
One-sample t-test ================= statistic 3.504 p-value 0.009944 df 7 estimate 5.55 95% CI [5.179, 5.921] alternative two-sided
What the results of a t-test mean
The estimate from the demo data entered above is 5.55.
If the true mean really were 5, the chance of getting a result at least this extreme is 0.009944. That's the p-value.
That's below 0.05, so at the common 5% level this counts as evidence that the true mean differs from 5.
The 95% confidence interval, 5.179 to 5.921, is the range of values for the true mean that are consistent with the demo data entered above.
"True" here means the value for the whole population that the demo data entered above are a sample from, rather than just for those numbers.
How to use statinfer's t-test 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 { summarize, tTest } from "statinfer";
const result = tTest({
x: [5.1, 4.9, 5.6, 5.8, 6, 5.5, 5.3, 6.2],
mu: 5,
});
console.log(summarize(result));For every option tTest accepts, see its API documentation (opens in a new tab).
The raw result
tTest returns a plain JavaScript object, which your code can use directly. For example, result.pValue is the p-value. 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. One thing to know: JSON can't represent infinity, so an open-ended confidence interval shows null for its missing end.
Show the raw result as JSON
{
"method": "One-sample t-test",
"statistic": 3.5036213149883326,
"pValue": 0.009944414335751602,
"df": 7,
"estimate": 5.55,
"confidenceInterval": [
5.178800255092534,
5.921199744907466
],
"confidenceLevel": 0.95,
"alternative": "two-sided"
}Further reading
- One-sample t-test (opens in a new tab), in the NIST/SEMATECH e-Handbook of Statistical Methods
- Two-sample t-test for equal means (opens in a new tab), in the same handbook, which covers both equal and unequal variances
- Student's t-test (opens in a new tab) and Welch's t-test (opens in a new tab), on Wikipedia