proportion 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 proportionTest function, which compares proportions: how often something happens out of a number of tries. Enter your own counts 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 proportion test ยท Demo data ยท Demo results ยท What the results mean ยท How to use it ยท Further reading
When to use a proportion test
A proportion test compares how often something happens, when each try either succeeds or doesn't, such as a coin landing heads, a visitor buying something, or a part passing inspection. There are two kinds:
- One sample: is a proportion different from a target value? For example, is a coin that landed heads 58 times in 100 flips a fair coin, with a proportion of 0.5?
- Two samples: do two groups have different proportions? For example, did 45 of 120 visitors buy after seeing one design, compared with 30 of 110 after seeing another?
statinfer's proportion test is a z-test, which relies on an approximation that works well when the counts are reasonably large. A common rule of thumb is at least 10 successes and 10 failures in each group. When comparing two groups with smaller counts, Fisher's exact test is more reliable. The confidence interval is the simple "Wald" interval, which can be inaccurate for small samples, or for proportions close to 0 or 1.
Demo data
Demo results
One-sample z-test for a proportion ================================== statistic 1.6 p-value 0.1096 estimate 0.58 95% CI [0.4833, 0.6767] alternative two-sided
What the results of a proportion test mean
The estimate from the demo data entered above is 0.58.
If the true proportion really were 0.5, the chance of getting a result at least this extreme is 0.1096. That's the p-value.
That's not below 0.05, so at the common 5% level there isn't enough evidence that the true proportion differs from 0.5. That doesn't prove it equals 0.5; the data just can't tell.
The 95% confidence interval, 0.4833 to 0.6767, is the range of values for the true proportion 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 proportion 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 { proportionTest, summarize } from "statinfer";
const result = proportionTest({
successes: 58,
trials: 100,
});
console.log(summarize(result));For every option proportionTest accepts, see its API documentation (opens in a new tab).
The raw result
proportionTest 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. For one-sided tests, the confidence interval stops at a natural limit rather than at infinity: 0 or 1 for a proportion, and โ1 or 1 for a difference between two proportions.
Show the raw result as JSON
{
"method": "One-sample z-test for a proportion",
"statistic": 1.5999999999999992,
"pValue": 0.1095985833991162,
"df": null,
"estimate": 0.58,
"confidenceInterval": [
0.4832643101683205,
0.6767356898316794
],
"confidenceLevel": 0.95,
"alternative": "two-sided"
}Further reading
- Does the proportion of defectives meet requirements? (opens in a new tab), testing one proportion, in the NIST/SEMATECH e-Handbook of Statistical Methods
- How can we determine whether two processes produce the same proportion of defectives? (opens in a new tab), comparing two proportions, including Fisher's exact test, in the same handbook
- Binomial proportion confidence interval (opens in a new tab), on Wikipedia, which explains the Wald interval and its more accurate alternatives