Skip to contents

The Mahalanobis distance (in squared units) measures the distance in multivariate space taking into account the covariance structure of the data. Because a few extreme outliers can skew the covariance estimate, the bootstrapped version is considered as more robust.

Usage

distance_mahalanobis(data, ci = 0.95, iterations = 1000, robust = TRUE, ...)

Arguments

data

A data frame.

ci

Confidence/Credible Interval level. If "default", then it is set to 0.95 (95% CI).

iterations

The number of draws to simulate/bootstrap (when robust is TRUE).

robust

If TRUE, will run a bootstrapped version of the function with i iterations.

...

Additional arguments (e.g., alternative) to be passed to other methods. See stats::cor.test for further details.

Value

Description of the Mahalanobis distance.

References

  • Schwarzkopf, D. S., De Haas, B., & Rees, G. (2012). Better ways to improve standards in brain-behavior correlation analysis. Frontiers in human neuroscience, 6, 200.

Examples

library(correlation)

distance_mahalanobis(iris[, 1:4])
#>       Distance     CI_low    CI_high
#> 1    2.1799257  1.5773870  3.0103486
#> 2    2.9457730  2.3046996  3.8828547
#> 3    2.1506407  1.6440310  2.8116164
#> 4    2.5325294  1.9934572  3.1978815
#> 5    2.5149781  1.8429525  3.4760443
#> 6    3.9657247  2.8747144  5.5103717
#> 7    2.9468683  2.3332488  3.7177751
#> 8    1.8639158  1.3581406  2.5804917
#> 9    3.4860917  2.7892251  4.4468571
#> 10   2.4537735  1.9908618  3.0547983
#> 11   3.3593890  2.4788184  4.5718910
#> 12   2.8425693  2.2178004  3.7292085
#> 13   2.6983058  2.1681400  3.4196374
#> 14   3.7166666  2.9815346  4.7338636
#> 15   8.9733668  6.9708260 11.9545950
#> 16   9.9467263  7.5362396 13.5663960
#> 17   5.9252088  4.4518350  7.9317621
#> 18   2.3806662  1.7136078  3.2424768
#> 19   4.6051144  3.4715641  6.1756659
#> 20   3.5271295  2.5660489  4.7947835
#> 21   2.7095779  2.1436978  3.4677051
#> 22   3.0207706  2.2606950  4.0804981
#> 23   3.7161789  2.9324896  4.7178055
#> 24   2.3238096  1.7628059  3.0206115
#> 25   5.4564414  4.3089571  6.9408854
#> 26   2.5281342  2.0042669  3.2341170
#> 27   1.8236534  1.3220168  2.4441207
#> 28   2.2489952  1.6362501  3.1051852
#> 29   2.5718080  1.9275503  3.4438628
#> 30   2.5366374  2.0151761  3.2479897
#> 31   2.0484693  1.6171286  2.6332832
#> 32   4.7269491  3.6902651  6.1724441
#> 33   8.5706975  6.5729874 11.4608677
#> 34   7.3877677  5.4255431 10.1158148
#> 35   2.0456671  1.5874368  2.6487812
#> 36   3.5345338  2.7605900  4.5041415
#> 37   5.4766686  4.2969839  7.1212419
#> 38   3.5109163  2.7042975  4.6763822
#> 39   3.2554156  2.5915287  4.1815463
#> 40   1.9387146  1.4089449  2.6520777
#> 41   2.6194170  1.9217755  3.4828131
#> 42  11.7863614  9.0524054 15.9592502
#> 43   3.4032997  2.7288752  4.2629712
#> 44   3.8613772  3.0829856  4.8962542
#> 45   4.5899426  3.5320543  6.1570303
#> 46   3.0839026  2.3792953  4.0878210
#> 47   4.5390887  3.4487699  6.0829001
#> 48   2.4261806  1.8840887  3.0705580
#> 49   3.0771802  2.2221812  4.2418618
#> 50   1.9844633  1.4656127  2.6795897
#> 51   4.6103747  3.4019005  6.4394240
#> 52   0.6795722  0.3993739  1.1149686
#> 53   3.1385767  2.2108964  4.4885294
#> 54   3.7089710  2.7614747  5.1227956
#> 55   2.1417018  1.4924896  2.9460511
#> 56   3.4191000  2.5024700  4.7291669
#> 57   1.3337254  0.8921796  2.0298744
#> 58   4.4519711  3.4653356  5.8913695
#> 59   2.8588760  2.0204329  3.9975087
#> 60   3.2781633  2.4975026  4.3280488
#> 61   7.8657602  6.1755701 10.7237504
#> 62   0.4634968  0.2412206  0.8462517
#> 63   7.7951035  6.0822511 10.4612180
#> 64   1.6243704  1.0611169  2.3701588
#> 65   1.0979633  0.7404738  1.5850582
#> 66   2.9081570  2.1142886  3.9782032
#> 67   3.5697116  2.7221373  4.8031296
#> 68   3.4848933  2.5567223  4.6120142
#> 69   7.6337844  5.9229366 10.3117740
#> 70   2.2568148  1.7241945  3.1318394
#> 71   3.3399420  2.4995800  4.6342052
#> 72   1.4377535  0.9734630  2.0381354
#> 73   2.4822089  1.8510260  3.3918806
#> 74   4.7215866  3.4291262  6.4250734
#> 75   1.6871957  1.1647610  2.4047316
#> 76   2.4333109  1.7476934  3.3402083
#> 77   4.3477249  3.2152190  5.8734513
#> 78   1.4977659  1.0007638  2.1906052
#> 79   0.3505193  0.1940155  0.5911341
#> 80   2.1828667  1.5605978  3.1522768
#> 81   2.8970522  2.2108942  4.1065785
#> 82   3.2252676  2.5104168  4.4954895
#> 83   0.8946361  0.5780441  1.3792865
#> 84   2.6070842  1.8954132  3.5635743
#> 85   6.3672633  5.0056520  8.3313371
#> 86   2.8859615  2.1742314  3.9165958
#> 87   1.8885087  1.2579573  2.7675367
#> 88   6.7902462  5.2552853  9.0728594
#> 89   1.7205175  1.2510007  2.4306643
#> 90   2.0705182  1.5347413  2.8800558
#> 91   5.1196048  3.7689186  6.8182929
#> 92   1.2482461  0.7971880  1.9012722
#> 93   1.4223322  1.0050514  2.0875850
#> 94   4.6913453  3.6062317  6.4101863
#> 95   1.5390349  1.1118849  2.1755078
#> 96   2.7850354  2.0136190  3.8482516
#> 97   1.2315192  0.8267786  1.7900246
#> 98   0.6995785  0.4090891  1.1344064
#> 99   5.0108885  3.7790722  6.9124190
#> 100  0.6664748  0.4326780  1.0263171
#> 101  9.2167262  7.0241954 12.6455407
#> 102  3.0545212  2.2656583  4.2152310
#> 103  2.5528745  1.8657537  3.5992210
#> 104  3.6633128  2.7639567  5.0483745
#> 105  2.4199144  1.7850948  3.4720458
#> 106  6.2758011  4.3896455  9.0527586
#> 107 10.4283920  8.2588373 13.5355346
#> 108  7.8433733  5.5149611 11.2300189
#> 109  3.4507908  2.6234843  4.7155419
#> 110  7.5958448  5.8313491 10.3446979
#> 111  2.3581056  1.5323609  3.6330723
#> 112  1.2937694  0.9593935  1.7619268
#> 113  2.5345987  1.7391990  3.6917365
#> 114  4.9581509  3.7979295  6.5503092
#> 115 11.8992951  9.1121328 16.0189385
#> 116  6.0823693  4.2713160  8.8965472
#> 117  2.0594187  1.4496197  3.0461121
#> 118 13.2696773  9.4820869 18.6290587
#> 119  7.4320882  5.5369120  9.9030779
#> 120  4.5449746  3.6096666  6.0258058
#> 121  4.2268802  2.9661346  6.1397664
#> 122  5.5093273  4.1865019  7.6816461
#> 123  9.0567039  6.5989927 12.8951083
#> 124  1.8808572  1.2963700  2.6404897
#> 125  2.9973252  2.2366152  4.1131613
#> 126  6.1232314  4.1470755  9.1355574
#> 127  1.5230020  1.0234961  2.2688884
#> 128  1.1781381  0.7641689  1.8479766
#> 129  1.7208109  1.2884941  2.4721891
#> 130  7.3306051  5.1483852 10.6412573
#> 131  5.9093631  4.3043252  8.2184180
#> 132 13.5974973  9.6815290 19.3457802
#> 133  2.6342064  1.8908162  3.8209506
#> 134  2.5188442  1.7253452  3.5982089
#> 135 13.4120791 10.0547728 17.8589032
#> 136  9.9872352  7.7519099 13.0973162
#> 137  8.4583659  6.3042611 11.8586293
#> 138  3.3586717  2.5153312  4.7485015
#> 139  1.5181257  0.9981914  2.3454767
#> 140  3.6351981  2.5744357  5.2238943
#> 141  6.2109366  4.3027920  9.0383628
#> 142 12.9498425  9.5390412 18.0879774
#> 143  3.0545212  2.2656583  4.2152310
#> 144  3.2139728  2.3500017  4.6364718
#> 145  7.7268304  5.4827588 11.1987243
#> 146  9.4445209  6.8135843 13.2949493
#> 147  4.2241636  3.1034430  5.6236597
#> 148  1.8589921  1.1960484  2.8909660
#> 149  7.9258215  5.8949569 11.0687071
#> 150  3.5667623  2.7057995  4.8361518
distance_mahalanobis(iris[, 1:4], robust = FALSE)
#>       Distance
#> 1    2.1344679
#> 2    2.8491187
#> 3    2.0813387
#> 4    2.4523816
#> 5    2.4621545
#> 6    3.8834177
#> 7    2.8621081
#> 8    1.8333003
#> 9    3.3840731
#> 10   2.3752179
#> 11   3.2831069
#> 12   2.7747975
#> 13   2.6132975
#> 14   3.6034324
#> 15   8.7375184
#> 16   9.7127899
#> 17   5.7605877
#> 18   2.3213894
#> 19   4.4996899
#> 20   3.4388658
#> 21   2.6360071
#> 22   2.9292496
#> 23   3.6134114
#> 24   2.2371731
#> 25   5.3023607
#> 26   2.4453103
#> 27   1.7658286
#> 28   2.1971806
#> 29   2.5027712
#> 30   2.4643980
#> 31   1.9849638
#> 32   4.5911380
#> 33   8.3583413
#> 34   7.2213139
#> 35   1.9820679
#> 36   3.4173031
#> 37   5.3372175
#> 38   3.4513350
#> 39   3.1549793
#> 40   1.8926197
#> 41   2.5485013
#> 42  11.4240288
#> 43   3.3144697
#> 44   3.7085855
#> 45   4.4840560
#> 46   2.9786602
#> 47   4.4333077
#> 48   2.3594309
#> 49   3.0076970
#> 50   1.9285641
#> 51   4.4528311
#> 52   0.6273996
#> 53   3.0186280
#> 54   3.6125278
#> 55   2.0404590
#> 56   3.3195858
#> 57   1.2763441
#> 58   4.3093305
#> 59   2.7426578
#> 60   3.1665229
#> 61   7.6832930
#> 62   0.4323176
#> 63   7.5614023
#> 64   1.5482870
#> 65   1.0372548
#> 66   2.7865631
#> 67   3.4732716
#> 68   3.3289796
#> 69   7.3923779
#> 70   2.1837666
#> 71   3.2615033
#> 72   1.3601100
#> 73   2.3885294
#> 74   4.5073307
#> 75   1.6095193
#> 76   2.3362612
#> 77   4.1982905
#> 78   1.4219664
#> 79   0.3194730
#> 80   2.1112223
#> 81   2.8147732
#> 82   3.1338323
#> 83   0.8579871
#> 84   2.5178289
#> 85   6.2164814
#> 86   2.7910313
#> 87   1.7971852
#> 88   6.5544318
#> 89   1.6677135
#> 90   2.0056841
#> 91   4.9630348
#> 92   1.1821941
#> 93   1.3713881
#> 94   4.5698858
#> 95   1.4932609
#> 96   2.6869276
#> 97   1.1870542
#> 98   0.6575354
#> 99   4.8207427
#> 100  0.6341019
#> 101  8.9395988
#> 102  2.9682833
#> 103  2.4451456
#> 104  3.5551146
#> 105  2.3541837
#> 106  6.0617111
#> 107 10.1378044
#> 108  7.5880086
#> 109  3.3301655
#> 110  7.3453376
#> 111  2.2611037
#> 112  1.2342978
#> 113  2.4316524
#> 114  4.7588845
#> 115 11.4105735
#> 116  5.8815414
#> 117  1.9743869
#> 118 12.8130732
#> 119  7.1768159
#> 120  4.3974981
#> 121  4.0399248
#> 122  5.3595987
#> 123  8.7973122
#> 124  1.7967966
#> 125  2.8868280
#> 126  5.8835660
#> 127  1.4473698
#> 128  1.1348285
#> 129  1.6688603
#> 130  7.0710485
#> 131  5.6935238
#> 132 13.1010925
#> 133  2.5441171
#> 134  2.4087367
#> 135 12.8803310
#> 136  9.6569355
#> 137  8.2028004
#> 138  3.2575803
#> 139  1.4687445
#> 140  3.4924068
#> 141  5.9713581
#> 142 12.4413843
#> 143  2.9682833
#> 144  3.1069367
#> 145  7.4592052
#> 146  9.0639497
#> 147  4.0366487
#> 148  1.7670035
#> 149  7.6824724
#> 150  3.4787688