print.js (2893B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); 24 var isObject = require( '@stdlib/assert/is-plain-object' ); 25 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; 26 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 27 var roundn = require( '@stdlib/math/base/special/roundn' ); 28 29 30 // MAIN // 31 32 /** 33 * Pretty-print output of test. 34 * 35 * @param {Object} [opts] - options object 36 * @param {PositiveInteger} [opts.digits=4] - number of digits after the decimal point 37 * @param {boolean} [opts.decision=true] - boolean indicating whether to print the test decision 38 * @throws {TypeError} options argument must be an object 39 * @throws {TypeError} must provide valid options 40 * @returns {string} formatted output 41 */ 42 function print( opts ) { // eslint-disable-line stdlib/no-redeclare 43 /* eslint-disable no-invalid-this */ 44 var decision; 45 var dgts; 46 var str; 47 48 dgts = 4; 49 decision = true; 50 if ( arguments.length > 0 ) { 51 if ( !isObject( opts ) ) { 52 throw new TypeError( 'invalid argument. First argument must be an options object. Value: `' + opts + '`.' ); 53 } 54 if ( hasOwnProp( opts, 'digits' ) ) { 55 if ( !isPositiveInteger( opts.digits ) ) { 56 throw new TypeError( 'invalid option. `digits` option must be a positive integer. Option: `' + opts.digits + '`.' ); 57 } 58 dgts = opts.digits; 59 } 60 if ( hasOwnProp( opts, 'decision' ) ) { 61 if ( !isBoolean( opts.decision ) ) { 62 throw new TypeError( 'invalid option. `decision` option must be a boolean primitive. Option: `' + opts.decision + '`.' ); 63 } 64 decision = opts.decision; 65 } 66 } 67 68 str = ''; 69 str += this.method; 70 str += '\n\n'; 71 str += 'Null hypothesis: the medians of all groups are the same'; 72 str += '\n\n'; 73 str += ' pValue: ' + roundn( this.pValue, -dgts ) + '\n'; 74 str += ' statistic: ' + roundn( this.statistic, -dgts ); 75 str += ' df: ' + this.df; 76 str += '\n\n'; 77 if ( decision ) { 78 str += 'Test Decision: '; 79 if ( this.rejected ) { 80 str += 'Reject null in favor of alternative at ' + (this.alpha*100) + '% significance level'; 81 } else { 82 str += 'Fail to reject null in favor of alternative at ' + (this.alpha*100) + '% significance level'; 83 } 84 str += '\n'; 85 } 86 return str; 87 } 88 89 90 // EXPORTS // 91 92 module.exports = print;