Registry / testing / ttest
library4.0.0jsnpmunverified

ttest is a focused JavaScript library for performing Student's t-hypothesis tests. It supports both one-sample and two-sample t-tests, and accommodates scenarios with both equal and unequal variances by defaulting to Welch's t-test for two-sample cases. The library offers flexibility in data input, accepting raw arrays of values, `Summary` objects, or plain objects containing `mean`, `variance`, and `size`. Users can configure critical test parameters such as the null hypothesis mean (`mu`), the significance level (`alpha`), and the alternative hypothesis (e.g., 'less', 'greater', 'not equal'). The current stable version is 4.0.0. It provides a programmatic API to retrieve key statistical outcomes including the t-value (`testValue`), p-value (`pValue`), confidence interval (`confidence`), and degrees of freedom (`freedom`), along with a `valid()` method to check significance against alpha. Its main differentiator is its concise API specifically tailored for t-test computations.

npm install ttest
INSTALL
IMPORT
SIG · TTEST
T
ttest
testingjavascriptv4.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ttest
✓ const ttest = require('ttest')
✗ import { ttest } from 'ttest'
This is the primary CommonJS import method as documented in the package's README.
ttest
✓ import ttest from 'ttest'
✗ import { ttest } from 'ttest'
While primarily a CommonJS module, Node.js allows this syntax for default-exported CJS modules when used in an ESM context.

Demonstrates both one-sample and two-sample t-tests, showing how to construct tests with various options and retrieve key statistical results like t-value, p-value, and confidence intervals. It highlights usage of different options such as 'mu', 'alpha', 'alternative', and 'varEqual'.

import ttest from 'ttest'; // Example 1: One-sample t-test // Testing if the mean of the sample [1, 2, 3, 4, 5] is significantly different from 3. const sample1 = [1, 2, 3, 4, 5]; const oneSampleTest = ttest(sample1, { mu: 3, alpha: 0.05, alternative: "not equal" }); console.log('--- One-sample t-test ---'); console.log(`Sample: [${sample1}]`); console.log(`Hypothesized mean (mu): 3`); console.log(`t-value: ${oneSampleTest.testValue().toFixed(3)}`); console.log(`p-value: ${oneSampleTest.pValue().toFixed(3)}`); console.log(`Confidence Interval (95%): [${oneSampleTest.confidence()[0].toFixed(3)}, ${oneSampleTest.confidence()[1].toFixed(3)}]`); console.log(`Valid (p >= alpha): ${oneSampleTest.valid()}`); // Note: valid() returns true if p >= alpha console.log(`Degrees of freedom: ${oneSampleTest.freedom()}`); console.log(''); // Example 2: Two-sample t-test (Welch's t-test for unequal variances) // Testing if the mean of sampleA is significantly greater than sampleB, // with a hypothesized difference (mu) of 0. const sampleA = [10, 12, 11, 13, 15]; const sampleB = [8, 9, 10, 11, 12]; const twoSampleTest = ttest(sampleA, sampleB, { mu: 0, varEqual: false, alpha: 0.01, alternative: "greater" }); console.log('--- Two-sample t-test (Welch) ---'); console.log(`Sample A: [${sampleA}]`); console.log(`Sample B: [${sampleB}]`); console.log(`Hypothesized difference (mu): 0`); console.log(`Assume equal variance (varEqual): ${false}`); console.log(`t-value: ${twoSampleTest.testValue().toFixed(3)}`); console.log(`p-value: ${twoSampleTest.pValue().toFixed(3)}`); console.log(`Valid (p >= alpha): ${twoSampleTest.valid()}`); console.log(`Degrees of freedom: ${twoSampleTest.freedom().toFixed(2)}`);
Debug
Known issues
gotchaThe `.valid()` method returns `true` if the p-value is GREATER OR EQUAL to the `alpha` level. This indicates a failure to reject the null hypothesis, which might be counter-intuitive for users expecting `true` to signify statistical significance (where p < alpha). Always check the p-value directly for clarity.
fix
Instead of relying solely on `.valid()`, compare `.pValue()` directly against your chosen `alpha` level (e.g., `ttest.pValue() < options.alpha`) to determine statistical significance.
affects: >=1.0.0
gotchaFor two-sample t-tests, the `varEqual` option defaults to `false`. This means the library performs Welch's t-test, which does not assume equal variances between samples. While robust, ensure this aligns with your statistical assumptions; if equal variances are known or assumed, explicitly set `varEqual: true` for Student's two-sample t-test.
fix
If you specifically require Student's two-sample t-test (assuming equal variances), ensure to pass `{ varEqual: true }` in the options object.
affects: >=1.0.0
gotchaThe `alpha` (significance level) option must be a number between 0 and 1, inclusive. Providing values outside this range will likely lead to incorrect statistical interpretations or internal errors, as the library may not perform explicit validation for all edge cases.
fix
Always ensure your `alpha` parameter (e.g., `0.05` for 95% confidence) is within the valid range `[0, 1]`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: ttest is not a function
Attempting to use a named import (e.g., `import { ttest } from 'ttest'`) for a CommonJS module that exports via `module.exports = ttest` or a module that uses a default export in ESM.
fix
Use `const ttest = require('ttest')` for CommonJS environments or `import ttest from 'ttest'` for ESM contexts to correctly import the default export.
Error: Invalid data format for ttest.
Input data provided to the `ttest` constructor is not in a supported format as specified by the documentation.
fix
Ensure data is either an array of numbers (e.g., `[1, 2, 3]`), a `Summary` object (if `summary` package is used), or a plain object with `{mean: Number, variance: Number, size: Number}` properties.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources
ttest — npm install ttest · libregistry