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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 9x 9x 32x 9x 9x 14x 32x 1x 1x 13x 32x 6x 6x 2x 2x 6x 32x 10x 32x 1x 1x 32x 1x 1x 32x 9x 32x 1x 1x 32x 8x 32x 2x 2x 10x 10x 10x 10x 10x 32x 9x 9x 9x 9x 9x 9x 1x 1x 9x 1x 1x 9x 9x 7x 7x 9x 32x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 32x 1x 1x 32x 1x 1x 32x 32x 8x 8x 8x 32x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 32x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 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 isNumberArray = require( '@stdlib/assert/is-number-array' ).primitives;
var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
var quantileFactory = require( '@stdlib/stats/base/dists/normal/quantile' ).factory;
var cdfFactory = require( '@stdlib/stats/base/dists/normal/cdf' ).factory;
var format = require( '@stdlib/string/format' );
var atanh = require( '@stdlib/math/base/special/atanh' );
var tanh = require( '@stdlib/math/base/special/tanh' );
var tCDF = require( '@stdlib/stats/base/dists/t/cdf' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var min = require( '@stdlib/math/base/special/min' );
var print = require( './print.js' ); // eslint-disable-line stdlib/no-redeclare
var scorr = require( './scorr.js' );
var validate = require( './validate.js' );
// VARIABLES //
var normQuantile = quantileFactory( 0.0, 1.0 );
var normCDF = cdfFactory( 0.0, 1.0 );
// MAIN //
/**
* Computes a Spearman rank correlation test between paired samples.
*
* ## Notes
*
* - The Spearman rank correlation coefficient is a non-parametric measure of rank correlation that assesses how well the relationship between two variables can be described using a monotonic function.
* - When `rho` is zero, the test statistic follows a t-distribution with `n-2` degrees of freedom (for large samples). When `rho` is non-zero, Fisher's z transform is used.
*
* @param {NumericArray} x - first data array
* @param {NumericArray} y - second data array
* @param {Options} [options] - function options
* @param {number} [options.alpha=0.05] - significance level
* @param {string} [options.alternative='two-sided'] - alternative hypothesis (`two-sided`, `less` or `greater`)
* @param {number} [options.rho=0.0] - correlation under H0
* @throws {TypeError} first argument has to be a typed array or array of numbers
* @throws {TypeError} second argument has to be a typed array or array of numbers
* @throws {RangeError} first and second arguments must have the same length
* @throws {Error} first and second arguments must contain at least four elements
* @throws {TypeError} options must be an object
* @throws {TypeError} must provide valid options
* @returns {Object} test result object
*
* @example
* var x = [ 2, 4, 3, 1, 2, 3 ];
* var y = [ 3, 2, 4, 1, 2, 4 ];
* var out = spearmanTest( x, y );
*
* @example
* var x = [ 2, 4, 3, 1, 2, 3 ];
* var y = [ 3, 2, 4, 1, 2, 4 ];
* var out = spearmanTest( x, y, {
* 'alternative': 'greater'
* });
*/
function spearmanTest( x, y, options ) {
var method;
var alpha;
var result;
var cint;
var opts;
var pval;
var stat;
var alt;
var err;
var out;
var rho;
var val;
var df;
var se;
var n;
var rs;
var z;
if ( !isTypedArrayLike( x ) && !isNumberArray( x ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a numeric array. Value: `%s`.', x ) );
}
if ( !isTypedArrayLike( y ) && !isNumberArray( y ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a numeric array. Value: `%s`.', y ) );
}
n = x.length;
if ( n !== y.length ) {
throw new RangeError( 'invalid arguments. First and second arguments must be arrays having the same length.' );
}
opts = {};
if ( options ) {
err = validate( opts, options );
if ( err ) {
throw err;
}
}
if ( opts.alpha === void 0 ) {
alpha = 0.05;
} else {
alpha = opts.alpha;
}
if ( n < 4 ) {
throw new Error( 'invalid arguments. Not enough observations. First and second arguments must contain at least four observations.' );
}
if ( opts.rho === void 0 ) {
rho = 0.0;
} else {
rho = opts.rho;
}
if ( opts.alternative === void 0 ) {
alt = 'two-sided';
} else {
alt = opts.alternative;
}
result = scorr( x, y );
rs = result.rs;
z = atanh( rs );
se = 1.0 / sqrt( n - 3 );
if ( rho === 0.0 ) {
// Use t-test for H0: rho = 0.0 vs H1: rho != 0.0...
method = 't-test for Spearman\'s rank correlation coefficient';
df = n - 2;
stat = sqrt( df ) * rs / sqrt( 1.0 - (rs*rs) );
switch ( alt ) {
case 'greater':
pval = 1.0 - tCDF( stat, df );
break;
case 'less':
pval = tCDF( stat, df );
break;
case 'two-sided':
default:
pval = 2.0 * min( tCDF( stat, df ), 1.0 - tCDF( stat, df ) );
break;
}
} else {
// Use large-sample normality to calculate p-value based on Fisher's z transform...
method = 'Fisher\'s z transform test for Spearman\'s rank correlation coefficient';
stat = ( z - atanh( rho ) ) * sqrt( n - 3 );
switch ( alt ) {
case 'greater':
pval = normCDF( -stat );
break;
case 'less':
pval = 1.0 - normCDF( -stat );
break;
case 'two-sided':
default:
pval = 2.0 * min( normCDF( -stat ), 1.0 - normCDF( -stat ) );
break;
}
}
switch ( alt ) {
case 'greater':
cint = [ tanh( z - ( se*normQuantile( 1.0 - alpha ) ) ), 1.0 ];
break;
case 'less':
cint = [ -1.0, tanh( z + ( se*normQuantile( 1.0 - alpha ) ) ) ];
break;
case 'two-sided':
default:
val = se * normQuantile( 1.0 - ( alpha/2.0 ) );
cint = [ tanh( z - val ), tanh( z + val ) ];
break;
}
out = {};
setReadOnly( out, 'rejected', pval <= alpha );
setReadOnly( out, 'alpha', alpha );
setReadOnly( out, 'pValue', pval );
setReadOnly( out, 'statistic', stat );
setReadOnly( out, 'ci', cint );
setReadOnly( out, 'alternative', alt );
setReadOnly( out, 'method', method );
setReadOnly( out, 'nullValue', rho );
setReadOnly( out, 'scorr', rs );
setReadOnly( out, 'print', print );
return out;
}
// EXPORTS //
module.exports = spearmanTest;
|