{"id":14150,"library":"ttest","title":"Student's t-test Statistical Hypothesis Testing","description":"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.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/AndreasMadsen/ttest","tags":["javascript","hypothesis","student t"],"install":[{"cmd":"npm install ttest","lang":"bash","label":"npm"},{"cmd":"yarn add ttest","lang":"bash","label":"yarn"},{"cmd":"pnpm add ttest","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is the primary CommonJS import method as documented in the package's README.","wrong":"import { ttest } from 'ttest'","symbol":"ttest","correct":"const ttest = require('ttest')"},{"note":"While primarily a CommonJS module, Node.js allows this syntax for default-exported CJS modules when used in an ESM context.","wrong":"import { ttest } from 'ttest'","symbol":"ttest","correct":"import ttest from 'ttest'"}],"quickstart":{"code":"import ttest from 'ttest';\n\n// Example 1: One-sample t-test\n// Testing if the mean of the sample [1, 2, 3, 4, 5] is significantly different from 3.\nconst sample1 = [1, 2, 3, 4, 5];\nconst oneSampleTest = ttest(sample1, { mu: 3, alpha: 0.05, alternative: \"not equal\" });\n\nconsole.log('--- One-sample t-test ---');\nconsole.log(`Sample: [${sample1}]`);\nconsole.log(`Hypothesized mean (mu): 3`);\nconsole.log(`t-value: ${oneSampleTest.testValue().toFixed(3)}`);\nconsole.log(`p-value: ${oneSampleTest.pValue().toFixed(3)}`);\nconsole.log(`Confidence Interval (95%): [${oneSampleTest.confidence()[0].toFixed(3)}, ${oneSampleTest.confidence()[1].toFixed(3)}]`);\nconsole.log(`Valid (p >= alpha): ${oneSampleTest.valid()}`); // Note: valid() returns true if p >= alpha\nconsole.log(`Degrees of freedom: ${oneSampleTest.freedom()}`);\nconsole.log('');\n\n// Example 2: Two-sample t-test (Welch's t-test for unequal variances)\n// Testing if the mean of sampleA is significantly greater than sampleB,\n// with a hypothesized difference (mu) of 0.\nconst sampleA = [10, 12, 11, 13, 15];\nconst sampleB = [8, 9, 10, 11, 12];\nconst twoSampleTest = ttest(sampleA, sampleB, { mu: 0, varEqual: false, alpha: 0.01, alternative: \"greater\" });\n\nconsole.log('--- Two-sample t-test (Welch) ---');\nconsole.log(`Sample A: [${sampleA}]`);\nconsole.log(`Sample B: [${sampleB}]`);\nconsole.log(`Hypothesized difference (mu): 0`);\nconsole.log(`Assume equal variance (varEqual): ${false}`);\nconsole.log(`t-value: ${twoSampleTest.testValue().toFixed(3)}`);\nconsole.log(`p-value: ${twoSampleTest.pValue().toFixed(3)}`);\nconsole.log(`Valid (p >= alpha): ${twoSampleTest.valid()}`);\nconsole.log(`Degrees of freedom: ${twoSampleTest.freedom().toFixed(2)}`);","lang":"typescript","description":"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'."},"warnings":[{"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.","message":"The `.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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If you specifically require Student's two-sample t-test (assuming equal variances), ensure to pass `{ varEqual: true }` in the options object.","message":"For 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always ensure your `alpha` parameter (e.g., `0.05` for 95% confidence) is within the valid range `[0, 1]`.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'4.0.0':111 'accept':65 'accommod':37 'along':140 'alpha':96,149 'altern':99 'api':116,156 'array':67 'case':57 'check':146 'comput':163 'concis':155 'confid':132,134 'configur':82 'contain':75 'critic':83 'current':107 'data':63 'default':46 'degre':136 'differenti':152 'e.g':101 'equal':41,105 'flexibl':61 'focus':12 'freedom':138,139 'greater':103 'hypothesi':7,21,90,100,165 'includ':122 'input':64 'interv':133 'javascript':13,164 'key':119 'less':102 'level':95 'librari':14,59 'main':151 'mean':76,91 'method':144 'mu':92 'null':89 'object':71,74 'offer':60 'one':27 'one-sampl':26 'outcom':121 'p':129 'p-valu':128 'paramet':85 'perform':16 'plain':73 'programmat':115 'provid':113 'pvalu':131 'raw':66 'retriev':118 'sampl':28,32,56 'scenario':38 'signific':94,147 'size':79 'specif':157 'stabl':108 'statist':6,120 'student':1,17,166 'summari':70 'support':24 't-hypothesi':19 't-test':3,33,50,160 't-valu':124 'tailor':158 'test':5,8,22,35,52,84,162 'testvalu':127 'ttest':9 'two':31,55 'two-sampl':30,54 'unequ':43 'user':80 'valid':143 'valu':69,126,130 'varianc':44,77 'version':109 'welch':48","created_at":"2026-04-20T01:58:11.237299+00:00","updated_at":"2026-04-20T01:58:11.237299+00:00","problems":[{"fix":"Use `const ttest = require('ttest')` for CommonJS environments or `import ttest from 'ttest'` for ESM contexts to correctly import the default export.","cause":"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.","error":"TypeError: ttest is not a function"},{"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.","cause":"Input data provided to the `ttest` constructor is not in a supported format as specified by the documentation.","error":"Error: Invalid data format for ttest."}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/AndreasMadsen/ttest","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/ttest","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","data","ai-ml"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}