All files / levene-test/lib main.js

49.6% Statements 62/125
100% Branches 1/1
0% Functions 0/1
49.6% Lines 62/125

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 1261x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                               1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
var format = require( '@stdlib/string/format' );
var anova1 = require( '@stdlib/stats/anova1' );
var absMeanDiff = require( './absdiff.js' );
var validate = require( './validate.js' );
var unique = require( './unique.js' );
var print = require( './print.js' ); // eslint-disable-line stdlib/no-redeclare
 
 
// MAIN //
 
/**
* Computes Levene's test for equal variances.
*
* @param {...NumericArray} arguments - either two or more number arrays or a single numeric array if provided an array of group indicators
* @param {Options} [options] - function options
* @param {number} [options.alpha=0.05] - significance level
* @param {Array} [options.groups] - array of group indicators
* @throws {TypeError} must provide array-like arguments
* @throws {RangeError} alpha option must be a number on the interval `[0,1]`
* @throws {Error} must provide at least two array-like arguments if `groups` is not provided
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {Object} test results
*
* @example
* // Data from Hollander & Wolfe (1973), p. 116:
* var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ];
* var y = [ 3.8, 2.7, 4.0, 2.4 ];
* var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ];
*
* var out = levene( x, y, z );
* // returns {...}
*/
function levene() {
	var options;
	var result;
	var groups;
	var levels;
	var args;
	var opts;
	var arg;
	var err;
	var out;
	var i;
	var j;
	var k;

	k = arguments.length;
	args = [];
	opts = {};
	if ( isPlainObject( arguments[ k-1 ] ) ) {
		options = arguments[ k-1 ];
		k -= 1;
		err = validate( opts, options );
		if ( err ) {
			throw err;
		}
	} else {
		options = {};
	}
	if ( opts.groups ) {
		args = arguments[ 0 ];
		groups = opts.groups;
		levels = unique( groups );
		k = levels.length;
		if ( k < 2 ) {
			throw new Error( format( 'invalid option. `%s` option must contain at least two unique elements. Value: `%s`.', 'groups', levels ) );
		}
	} else {
		groups = [];
		args = [];
		levels = [];
		for ( i = 0; i < k; i++ ) {
			arg = arguments[ i ];
			if ( arg.length === 0 ) {
				throw new Error( format( 'invalid argument. Provided arrays cannot be empty. Value: `%s`.', arg ) );
			}
			args = args.concat( arg );
			for ( j = 0; j < arg.length; j++ ) {
				groups.push( i );
			}
			levels.push( i );
		}
	}
	args = absMeanDiff( args, groups, levels );
	result = anova1( args, groups, options );
	out = {};
	setReadOnly( out, 'rejected', result.rejected );
	setReadOnly( out, 'alpha', result.alpha );
	setReadOnly( out, 'pValue', result.pValue );
	setReadOnly( out, 'statistic', result.statistic );
	setReadOnly( out, 'df', [ result.treatment.df, result.error.df ] );
	setReadOnly( out, 'method', 'Levene\'s test for Homogeneity of Variance' );
	setReadOnly( out, 'print', print );
	return out;
}
 
 
// EXPORTS //
 
module.exports = levene;