simple-statistics
Version:
36 lines (30 loc) • 1.23 kB
JavaScript
import mean from "./mean.js";
import standardDeviation from "./standard_deviation.js";
/**
* This is to compute [a one-sample t-test](https://en.wikipedia.org/wiki/Student%27s_t-test#One-sample_t-test), comparing the mean
* of a sample to a known value, x.
*
* in this case, we're trying to determine whether the
* population mean is equal to the value that we know, which is `x`
* here. Usually the results here are used to look up a
* [p-value](http://en.wikipedia.org/wiki/P-value), which, for
* a certain level of significance, will let you determine that the
* null hypothesis can or cannot be rejected.
*
* @param {Array<number>} x sample of one or more numbers
* @param {number} expectedValue expected value of the population mean
* @returns {number} value
* @example
* tTest([1, 2, 3, 4, 5, 6], 3.385).toFixed(2); // => '0.16'
*/
function tTest(x, expectedValue) {
// The mean of the sample
const sampleMean = mean(x);
// The standard deviation of the sample
const sd = standardDeviation(x);
// Square root the length of the sample
const rootN = Math.sqrt(x.length);
// returning the t value
return (sampleMean - expectedValue) / (sd / rootN);
}
export default tTest;