main.js (3793B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 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 isProbabilityArray = require( '@stdlib/assert/is-probability-array' ); 24 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; 25 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 26 var bonferroni = require( './bonferroni.js' ); 27 var hommel = require( './hommel.js' ); 28 var holm = require( './holm.js' ); 29 var bh = require( './bh.js' ); 30 var by = require( './by.js' ); 31 32 33 // VARIABLES // 34 35 var METHODS = [ 'bh', 'bonferroni', 'by', 'holm', 'hommel' ]; 36 37 38 // MAIN // 39 40 /** 41 * Adjusts supplied p-values for multiple comparisons via a specified method. 42 * 43 * @param {ProbabilityArray} pvals - p-values to be adjusted 44 * @param {string} method - correction method 45 * @param {PositiveInteger} [comparisons=pvals.length] - number of comparisons 46 * @throws {TypeError} first argument has to be an array-like object 47 * @throws {TypeError} second argument must be a string primitive 48 * @throws {Error} second argument must be `bh`, `bonferroni`, `by`, `holm`, or `hommel` 49 * @throws {RangeError} comparisons must be greater or equal to the number of elements in `pvals` 50 * @returns {ProbabilityArray} array containing the corrected p-values 51 * 52 * @example 53 * var pvalues = [ 0.008, 0.03, 0.123, 0.6, 0.2 ]; 54 * var out = padjust( pvalues, 'bonferroni' ); 55 * // returns [ 0.04, 0.15, ..., 1, 1 ] 56 * 57 * @example 58 * var pvalues = [ 0.008, 0.03, 0.123, 0.6, 0.2 ]; 59 * var out = padjust( pvalues, 'by' ); 60 * // returns [ ~0.091, ~0.171, ..., 1, ~0.571 ] 61 * 62 * @example 63 * var pvalues = [ 0.008, 0.03, 0.123, 0.6, 0.2 ]; 64 * var out = padjust( pvalues, 'bh' ); 65 * // returns [ 0.04, 0.075, ..., 0.6, 0.25 ] 66 * 67 * @example 68 * var pvalues = [ 0.008, 0.03, 0.123, 0.6, 0.2 ]; 69 * var out = padjust( pvalues, 'holm' ); 70 * // returns [ 0.04, 0.12, ..., 0.6, 0.4 ] 71 * 72 * @example 73 * var pvalues = [ 0.008, 0.03, 0.123, 0.6, 0.2 ]; 74 * var out = padjust( pvalues, 'hommel' ); 75 * // returns [ 0.032, 0.12, ..., 0.6, 0.4 ] 76 */ 77 function padjust( pvals, method, comparisons ) { 78 if ( !isProbabilityArray( pvals ) ) { 79 throw new TypeError( 'invalid argument. First argument must be an array of probabilities. Value: `' + pvals + '`.' ); 80 } 81 if ( !isString( method ) ) { 82 throw new TypeError( 'invalid argument. Second argument must be a string primitive. Value: `' + method + '`.' ); 83 } 84 if ( arguments.length > 2 ) { 85 if ( !isInteger( comparisons ) ) { 86 throw new TypeError( 'invalid argument. `comparisons` must be an integer. Value: `' + comparisons + '`.' ); 87 } 88 if ( comparisons < pvals.length ) { 89 throw new RangeError( 'invalid argument. When specified, `comparisons` arguments must have at least a length of '+pvals.length+'. Value: `' + comparisons + '`.' ); 90 } 91 } 92 comparisons = comparisons || pvals.length; 93 switch ( method ) { 94 case 'bonferroni': 95 return bonferroni( pvals, comparisons ); 96 case 'by': 97 return by( pvals, comparisons ); 98 case 'bh': 99 return bh( pvals, comparisons ); 100 case 'holm': 101 return holm( pvals, comparisons ); 102 case 'hommel': 103 return hommel( pvals, comparisons ); 104 default: 105 throw new Error( 'invalid argument. Second argument must be one of '+METHODS.join( ', ' )+' Value: `' + method + '`.' ); 106 } 107 } 108 109 110 // EXPORTS // 111 112 module.exports = padjust;