Skip to contents

Distance of Gower (1971) between the rows of x (and y): a weighted mean over the variables of

  • numeric and logical variables: \(|x - y| / r\), where \(r\) is the range of the variable;

  • ordered factors: the absolute difference of the level ranks divided by the number of levels minus one;

  • other factors and characters: 0 if equal, 1 otherwise.

Pairs with a missing value in a variable ignore that variable. The distance is model-free and serves as a baseline for the model-derived distances.

Usage

gower_distance(x, y = NULL, weights = NULL, ranges = NULL)

Arguments

x

a data.frame

y

a second data.frame with the same columns (optional)

weights

non-negative weights of the variables: unnamed (one per column) or named (variables not named get weight 1). Default: equal weights.

ranges

named list of ranges (c(min, max)) of numeric variables. Default: the ranges in x, so that distances of new cases y do not depend on the other cases in y.

Value

a dist object of the rows of x, or an nrow(x) x nrow(y) matrix if y is given.

References

Gower, J. C. (1971). A general coefficient of similarity and some of its properties. Biometrics, 27(4), 857–871. doi:10.2307/2528823

Examples

gower_distance(iris[1:5, ])
#>            1          2          3          4
#> 2 0.24666667                                 
#> 3 0.36000000 0.24666667                      
#> 4 0.43333333 0.25333333 0.27333333           
#> 5 0.07333333 0.24000000 0.35333333 0.42666667
gower_distance(iris[1:100, -5], iris[101:103, -5])
#>             [,1]       [,2]      [,3]
#>   [1,] 0.7506675 0.62375790 0.7910705
#>   [2,] 0.7796027 0.59019309 0.7575057
#>   [3,] 0.7833855 0.63564250 0.8029551
#>   [4,] 0.7908663 0.62228997 0.7896026
#>   [5,] 0.7703435 0.64343383 0.8107465
#>   [6,] 0.7168520 0.58994235 0.7572550
#>   [7,] 0.7718413 0.64493165 0.8122443
#>   [8,] 0.7434126 0.61650294 0.7838156
#>   [9,] 0.8363157 0.62607272 0.8142187
#>  [10,] 0.7777944 0.60921808 0.7765307
#>  [11,] 0.7376255 0.61071590 0.7780285
#>  [12,] 0.7558335 0.62892389 0.7962365
#>  [13,] 0.8035679 0.61415823 0.7814709
#>  [14,] 0.8681568 0.67874721 0.8460598
#>  [15,] 0.7501312 0.62322154 0.7905342
#>  [16,] 0.7533527 0.62644302 0.7937556
#>  [17,] 0.7412422 0.61433259 0.7816452
#>  [18,] 0.7359617 0.60905202 0.7763646
#>  [19,] 0.6933634 0.56645378 0.7337664
#>  [20,] 0.7611141 0.63420446 0.8015171
#>  [21,] 0.6941804 0.56727078 0.7345834
#>  [22,] 0.7359916 0.60908191 0.7763945
#>  [23,] 0.8317708 0.70486111 0.8721737
#>  [24,] 0.6674239 0.54051424 0.7078269
#>  [25,] 0.7375408 0.61063121 0.7779438
#>  [26,] 0.7581483 0.56873871 0.7360513
#>  [27,] 0.7079032 0.58099361 0.7483062
#>  [28,] 0.7353107 0.60840108 0.7757137
#>  [29,] 0.7309916 0.60408198 0.7713946
#>  [30,] 0.7650928 0.61734982 0.7846624
#>  [31,] 0.7662502 0.59767389 0.7649865
#>  [32,] 0.6769638 0.55005413 0.7173668
#>  [33,] 0.8125166 0.68560697 0.8529196
#>  [34,] 0.7865472 0.65963753 0.8269502
#>  [35,] 0.7630885 0.59451220 0.7618248
#>  [36,] 0.7617053 0.61396229 0.7812749
#>  [37,] 0.7197281 0.59281843 0.7601311
#>  [38,] 0.7943086 0.66739897 0.8347116
#>  [39,] 0.8319966 0.64258695 0.8098996
#>  [40,] 0.7341533 0.60724368 0.7745563
#>  [41,] 0.7513185 0.62440884 0.7917215
#>  [42,] 0.8809481 0.62903847 0.8588511
#>  [43,] 0.8111633 0.66342028 0.8307329
#>  [44,] 0.6889082 0.56199851 0.7293111
#>  [45,] 0.7220180 0.59510833 0.7624210
#>  [46,] 0.7741561 0.58474647 0.7520591
#>  [47,] 0.7697224 0.64281278 0.8101254
#>  [48,] 0.7865472 0.63880420 0.8061168
#>  [49,] 0.7468848 0.61997516 0.7872878
#>  [50,] 0.7390935 0.61218383 0.7794965
#>  [51,] 0.3162645 0.26111410 0.2062045
#>  [52,] 0.2581982 0.20304778 0.2592493
#>  [53,] 0.2905209 0.21453717 0.1781461
#>  [54,] 0.4766625 0.22475291 0.4545655
#>  [55,] 0.3030265 0.16454282 0.2438925
#>  [56,] 0.3755729 0.14449659 0.3534759
#>  [57,] 0.2116212 0.17730419 0.2520242
#>  [58,] 0.6086020 0.35669237 0.5865050
#>  [59,] 0.3312809 0.21363051 0.2536283
#>  [60,] 0.4541653 0.20225570 0.4320683
#>  [61,] 0.6288143 0.37690466 0.6067173
#>  [62,] 0.3251020 0.15421084 0.3030049
#>  [63,] 0.4849006 0.27002796 0.4628035
#>  [64,] 0.3012182 0.14653077 0.2791212
#>  [65,] 0.4292935 0.21905056 0.4071965
#>  [66,] 0.3171961 0.24121234 0.2418583
#>  [67,] 0.3345871 0.14517741 0.3124900
#>  [68,] 0.4452382 0.19332855 0.4231412
#>  [69,] 0.3623648 0.18452927 0.3402678
#>  [70,] 0.4820793 0.23016964 0.4599823
#>  [71,] 0.2235656 0.09434116 0.2431353
#>  [72,] 0.3690237 0.19350291 0.3469266
#>  [73,] 0.2974653 0.13814828 0.2753683
#>  [74,] 0.3410466 0.16552586 0.3189496
#>  [75,] 0.3310551 0.21340467 0.2904395
#>  [76,] 0.3183535 0.22153641 0.2407009
#>  [77,] 0.3333151 0.19483135 0.2186255
#>  [78,] 0.2469097 0.15009266 0.1507386
#>  [79,] 0.3079667 0.13476075 0.2858697
#>  [80,] 0.5014995 0.24958984 0.4794025
#>  [81,] 0.5078528 0.25594313 0.4857558
#>  [82,] 0.5286562 0.27674657 0.5065592
#>  [83,] 0.4280215 0.17611191 0.4059245
#>  [84,] 0.2775088 0.06263617 0.2554118
#>  [85,] 0.3531056 0.16369593 0.3310086
#>  [86,] 0.2620108 0.17213820 0.3024138
#>  [87,] 0.2841975 0.20821377 0.2088597
#>  [88,] 0.3781982 0.21888118 0.3561012
#>  [89,] 0.3883891 0.19897942 0.3662920
#>  [90,] 0.4558292 0.20391958 0.4337322
#>  [91,] 0.4357282 0.18381855 0.4136312
#>  [92,] 0.2968991 0.16304499 0.2748021
#>  [93,] 0.4323407 0.18043101 0.4102436
#>  [94,] 0.6097594 0.35784978 0.5876624
#>  [95,] 0.4135415 0.16163186 0.3914445
#>  [96,] 0.3877381 0.19832848 0.3656411
#>  [97,] 0.3834489 0.17320594 0.3613519
#>  [98,] 0.3310551 0.19488615 0.3089580
#>  [99,] 0.5832536 0.33134399 0.5611566
#> [100,] 0.3999631 0.16888683 0.3778661