-
Notifications
You must be signed in to change notification settings - Fork 224
/
t_test.js
35 lines (30 loc) · 1.2 KB
/
t_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;