main.js (3536B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isPlainObject = require( '@stdlib/assert/is-plain-object' ); 24 var setReadOnly = require( '@stdlib/utils/define-read-only-property' ); 25 var anova1 = require( './../../anova1' ); 26 var absMeanDiff = require( './absdiff.js' ); 27 var validate = require( './validate.js' ); 28 var unique = require( './unique.js' ); 29 var print = require( './print.js' ); // eslint-disable-line stdlib/no-redeclare 30 31 32 // MAIN // 33 34 /** 35 * Computes Levene's test for equal variances. 36 * 37 * @param {...NumericArray} arguments - either two or more number arrays or a single numeric array if provided an array of group indicators 38 * @param {Options} [options] - function options 39 * @param {number} [options.alpha=0.05] - significance level 40 * @param {Array} [options.groups] - array of group indicators 41 * @throws {TypeError} must provide array-like arguments 42 * @throws {RangeError} alpha option must be a number on the interval `[0,1]` 43 * @throws {Error} must provide at least two array-like arguments if `groups` is not provided 44 * @throws {TypeError} options must be an object 45 * @throws {TypeError} must provide valid options 46 * @returns {Object} test results 47 * 48 * @example 49 * // Data from Hollander & Wolfe (1973), p. 116: 50 * var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ]; 51 * var y = [ 3.8, 2.7, 4.0, 2.4 ]; 52 * var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ]; 53 * 54 * var out = levene( x, y, z ); 55 * // returns {...} 56 */ 57 function levene() { 58 var options; 59 var result; 60 var groups; 61 var levels; 62 var args; 63 var opts; 64 var arg; 65 var err; 66 var out; 67 var i; 68 var j; 69 var k; 70 71 k = arguments.length; 72 args = []; 73 opts = {}; 74 if ( isPlainObject( arguments[ k-1 ] ) ) { 75 options = arguments[ k-1 ]; 76 k -= 1; 77 err = validate( opts, options ); 78 if ( err ) { 79 throw err; 80 } 81 } else { 82 options = {}; 83 } 84 if ( opts.groups ) { 85 args = arguments[ 0 ]; 86 groups = opts.groups; 87 levels = unique( groups ); 88 k = levels.length; 89 if ( k < 2 ) { 90 throw new Error( 'invalid option. `groups` option must contain at least two unique elements. Value: `' + levels + '`.' ); 91 } 92 } else { 93 groups = []; 94 args = []; 95 levels = []; 96 for ( i = 0; i < k; i++ ) { 97 arg = arguments[ i ]; 98 if ( arg.length === 0 ) { 99 throw new Error( 'invalid argument. Provided arrays cannot be empty. Value: `' + arg + '`.' ); 100 } 101 args = args.concat( arg ); 102 for ( j = 0; j < arg.length; j++ ) { 103 groups.push( i ); 104 } 105 levels.push( i ); 106 } 107 } 108 args = absMeanDiff( args, groups, levels ); 109 result = anova1( args, groups, options ); 110 out = {}; 111 setReadOnly( out, 'rejected', result.rejected ); 112 setReadOnly( out, 'alpha', result.alpha ); 113 setReadOnly( out, 'pValue', result.pValue ); 114 setReadOnly( out, 'statistic', result.statistic ); 115 setReadOnly( out, 'df', [ result.treatment.df, result.error.df ] ); 116 setReadOnly( out, 'method', 'Levene\'s test for Homogeneity of Variance' ); 117 setReadOnly( out, 'print', print ); 118 return out; 119 } 120 121 122 // EXPORTS // 123 124 module.exports = levene;