repl.txt (5665B)
1 2 {{alias}}( x, y[, options] ) 3 Computes a two-sample Student's t test. 4 5 By default, the function performs a two-sample t-test for the null 6 hypothesis that the data in arrays or typed arrays `x` and `y` is 7 independently drawn from normal distributions with equal means. 8 9 The returned object comes with a `.print()` method which when invoked will 10 print a formatted output of the results of the hypothesis test. 11 12 Parameters 13 ---------- 14 x: Array<number> 15 First data array. 16 17 y: Array<number> 18 Second data array. 19 20 options: Object (optional) 21 Options. 22 23 options.alpha: number (optional) 24 Number in the interval `[0,1]` giving the significance level of the 25 hypothesis test. Default: `0.05`. 26 27 options.alternative: string (optional) 28 Either `two-sided`, `less` or `greater`. Indicates whether the 29 alternative hypothesis is that `x` has a larger mean than `y` 30 (`greater`), `x` has a smaller mean than `y` (`less`) or the means are 31 the same (`two-sided`). Default: `'two-sided'`. 32 33 options.difference: number (optional) 34 Number denoting the difference in means under the null hypothesis. 35 Default: `0`. 36 37 options.variance: string (optional) 38 String indicating if the test should be conducted under the assumption 39 that the unknown variances of the normal distributions are `equal` or 40 `unequal`. As a default choice, the function carries out the Welch test 41 (using the Satterthwaite approximation for the degrees of freedom), 42 which does not have the requirement that the variances of the underlying 43 distributions are equal. If the equal variances assumption seems 44 warranted, set the option to `equal`. Default: `unequal`. 45 46 Returns 47 ------- 48 out: Object 49 Test result object. 50 51 out.alpha: number 52 Used significance level. 53 54 out.rejected: boolean 55 Test decision. 56 57 out.pValue: number 58 p-value of the test. 59 60 out.statistic: number 61 Value of test statistic. 62 63 out.ci: Array<number> 64 1-alpha confidence interval for the mean. 65 66 out.nullValue: number 67 Assumed difference in means under H0. 68 69 out.xmean: number 70 Sample mean of `x`. 71 72 out.ymean: number 73 Sample mean of `y`. 74 75 out.alternative: string 76 Alternative hypothesis (`two-sided`, `less` or `greater`). 77 78 out.df: number 79 Degrees of freedom. 80 81 out.method: string 82 Name of test. 83 84 out.print: Function 85 Function to print formatted output. 86 87 Examples 88 -------- 89 // Student's sleep data: 90 > var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ]; 91 > var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ]; 92 > var out = {{alias}}( x, y ) 93 { 94 rejected: false, 95 pValue: ~0.079, 96 statistic: ~-1.861, 97 ci: [ ~-3.365, ~0.205 ], 98 // ... 99 } 100 101 // Print table output: 102 > var table = out.print() 103 Welch two-sample t-test 104 105 Alternative hypothesis: True difference in means is not equal to 0 106 107 pValue: 0.0794 108 statistic: -1.8608 109 95% confidence interval: [-3.3655,0.2055] 110 111 Test Decision: Fail to reject null in favor of alternative at 5% 112 significance level 113 114 // Choose a different significance level than `0.05`: 115 > out = {{alias}}( x, y, { 'alpha': 0.1 }); 116 > table = out.print() 117 Welch two-sample t-test 118 119 Alternative hypothesis: True difference in means is not equal to 0 120 121 pValue: 0.0794 122 statistic: -1.8608 123 90% confidence interval: [-3.0534,-0.1066] 124 125 Test Decision: Reject null in favor of alternative at 10% significance level 126 127 // Perform one-sided tests: 128 > out = {{alias}}( x, y, { 'alternative': 'less' }); 129 > table = out.print() 130 Welch two-sample t-test 131 132 Alternative hypothesis: True difference in means is less than 0 133 134 pValue: 0.0397 135 statistic: -1.8608 136 df: 17.7765 137 95% confidence interval: [-Infinity,-0.1066] 138 139 Test Decision: Reject null in favor of alternative at 5% significance level 140 141 > out = {{alias}}( x, y, { 'alternative': 'greater' }); 142 > table = out.print() 143 Welch two-sample t-test 144 145 Alternative hypothesis: True difference in means is greater than 0 146 147 pValue: 0.9603 148 statistic: -1.8608 149 df: 17.7765 150 95% confidence interval: [-3.0534,Infinity] 151 152 Test Decision: Fail to reject null in favor of alternative at 5% 153 significance level 154 155 // Run tests with equal variances assumption: 156 > x = [ 2, 3, 1, 4 ]; 157 > y = [ 1, 2, 3, 1, 2, 5, 3, 4 ]; 158 > out = {{alias}}( x, y, { 'variance': 'equal' }); 159 > table = out.print() 160 Two-sample t-test 161 162 Alternative hypothesis: True difference in means is not equal to 0 163 164 pValue: 0.8848 165 statistic: -0.1486 166 df: 10 167 95% confidence interval: [-1.9996,1.7496] 168 169 Test Decision: Fail to reject null in favor of alternative at 5% 170 significance level 171 172 // Test for a difference in means besides zero: 173 > var rnorm = {{alias:@stdlib/random/base/normal}}.factory({ 'seed': 372 }); 174 > x = new Array( 100 ); 175 > for ( i = 0; i < x.length; i++ ) { 176 ... x[ i ] = rnorm( 2.0, 3.0 ); 177 ... } 178 > y = new Array( 100 ); 179 > for ( i = 0; i < x.length; i++ ) { 180 ... y[ i ] = rnorm( 1.0, 3.0 ); 181 ... } 182 > out = {{alias}}( x, y, { 'difference': 1.0, 'variance': 'equal' }) 183 { 184 rejected: false, 185 pValue: ~0.642, 186 statistic: ~-0.466, 187 ci: [ ~-0.0455, ~1.646 ], 188 // ... 189 } 190 191 See Also 192 -------- 193