README.md (4875B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # ranks 22 23 > Compute ranks for values of an array-like object. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var ranks = require( '@stdlib/stats/ranks' ); 31 ``` 32 33 #### ranks( arr\[, opts] ) 34 35 Returns the sample ranks of the elements in `arr`, which can be either an [`array`][mdn-array] or [`typed array`][mdn-typed-array]. 36 37 ```javascript 38 var arr = [ 1.1, 2.0, 3.5, 0.0, 2.4 ]; 39 var out = ranks( arr ); 40 // returns [ 2, 3, 5, 1, 4 ] 41 42 // Ties are averaged: 43 arr = [ 2, 2, 1, 4, 3 ]; 44 out = ranks( arr ); 45 // returns [ 2.5, 2.5, 1, 5, 4 ]; 46 47 // Missing values are placed last: 48 arr = [ null, 2, 2, 1, 4, 3, NaN, NaN ]; 49 out = ranks( arr ); 50 // returns [ 6, 2.5, 2.5, 1, 5, 4, 7 ,8 ] 51 ``` 52 53 The function accepts the following options: 54 55 - **method**: `string` indicating how ties are handled. Can be one of the following values: `'average'`, `'min'`, `'max'`, `'ordinal'` and `'dense'`. Default: `'average'`. 56 - **missing**: `string` specifying how missing values are handled. Must be either `'last'`, `'first'` or `'remove'`. Default: `'last'`. 57 - **encoding**: `array` holding all values which will be regarded as missing values. Default: `[ NaN, null]`. 58 59 When all elements of the `array` are different, the ranks are uniquely determined. When there are equal elements (called _ties_), the `method` option determines how they are handled. The default, `'average'`, replace the ranks of the ties by their mean. Other possible options are `'min'` and `'max'`, which replace the ranks of the ties by their minimum and maximum, respectively. `'dense'` works like `'min'`, with the difference that the next highest element after a tie is assigned the next smallest integer. Finally, `ordinal` gives each element in `arr` a distinct rank, according to the position they appear in. 60 61 ```javascript 62 var data = [ 2, 2, 1, 4, 3 ]; 63 64 // Max method: 65 var out = ranks( data, { 66 'method': 'max' 67 }); 68 // returns [ 3, 3, 1, 5, 4 ] 69 70 // Min method: 71 out = ranks( data, { 72 'method': 'min' 73 }); 74 // returns [ 2, 2, 1, 5, 4 ] 75 76 // Ordinal method 77 out = ranks( data, { 78 'method': 'ordinal' 79 }); 80 // returns [ 2, 3, 1, 5, 4 ] 81 82 // Dense method: 83 out = [ 2, 2, 1, 4, 3 ]; 84 out = ranks( data, { 85 'method': 'dense' 86 }); 87 // returns [ 2, 2, 1, 4, 3 ] 88 ``` 89 90 The `missing` option is used to specify how to handle missing data. By default, `NaN` or `null` are treated as missing values. `'last'`specifies that missing values are placed last, `'first'` that the are assigned the lowest ranks and `'remove'` means that they are removed from the array before the ranks are calculated. 91 92 ```javascript 93 var data = [ NaN, 2, 2, 1, 4, 3, null, null ]; 94 95 var out = ranks( data, { 96 'missing': 'first' 97 }); 98 // returns [ 1, 5.5, 5.5, 4, 8, 7, 2, 3 ] 99 100 out = ranks( data, { 101 'missing': 'last' 102 }); 103 // returns [ 6, 2.5, 2.5, 1, 5, 4, 7 ,8 ] 104 105 out = ranks( data, { 106 'missing': 'remove' 107 }); 108 // returns [ 2.5, 2.5, 1, 5, 4 ] 109 ``` 110 111 Custom encoding for missing values is supported via the `encoding` option, which allows to supply the function with an `array` of values which should be treated as missing. 112 113 ```javascript 114 var Int32Array = require( '@stdlib/array/int32' ); 115 116 var data = new Int32Array( [ 2, 1, -999, 3, 4 ] ); 117 118 var out = ranks( data, { 119 'encoding': [ -999 ] 120 }); 121 // returns [ 2, 1, 5, 3, 4 ] 122 ``` 123 124 </section> 125 126 <!-- /.usage --> 127 128 <section class="examples"> 129 130 ## Examples 131 132 <!-- eslint no-undef: "error" --> 133 134 ```javascript 135 var Int32Array = require( '@stdlib/array/int32' ); 136 var round = require( '@stdlib/math/base/special/round' ); 137 var randu = require( '@stdlib/random/base/randu' ); 138 var ranks = require( '@stdlib/stats/ranks' ); 139 140 var data; 141 var out; 142 var i; 143 144 // Plain arrays... 145 data = new Array( 10 ); 146 for ( i = 0; i < data.length; i++ ) { 147 data[ i ] = round( randu()*10.0 ); 148 } 149 150 out = ranks( data ); 151 // returns <array> 152 153 // Typed arrays... 154 data = new Int32Array( 10 ); 155 for ( i = 0; i < data.length; i++ ) { 156 data[ i ] = randu() * 10.0; 157 } 158 159 out = ranks( data ); 160 // returns <array> 161 ``` 162 163 </section> 164 165 <!-- /.examples --> 166 167 <section class="references"> 168 169 </section> 170 171 <!-- /.references --> 172 173 <section class="links"> 174 175 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 176 177 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays 178 179 </section> 180 181 <!-- /.links -->