File: similarities.R

package info (click to toggle)
r-cran-proxy 0.4-27-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 372 kB
  • sloc: ansic: 1,247; sh: 12; makefile: 5
file content (635 lines) | stat: -rw-r--r-- 26,671 bytes parent folder | download
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
### Binary measures
pr_Jaccard <- function(a, b, c, d, n) a / (n - d)
pr_Jaccard_prefun <- function(x, y, pairwise, p, reg_entry) {
    if (!is.matrix(x)) {
        reg_entry$C_FUN <- FALSE
        reg_entry$loop <- TRUE
        reg_entry$abcd <- TRUE
        reg_entry$FUN <- "pr_Jaccard"
    } else {
        storage.mode(x) <- "logical"
        if (!is.null(y))
            storage.mode(y) <- "logical"
    }

    list(x = x, y = y, pairwise = pairwise, p = NULL, reg_entry = reg_entry)
}
pr_DB$set_entry(FUN = "R_bjaccard",
                names = c("Jaccard","binary","Reyssac","Roux"),
                distance = FALSE,
                PREFUN = "pr_Jaccard_prefun",
                convert = "pr_simil2dist",
                type = "binary",
                loop = FALSE,
                C_FUN = TRUE,
                abcd = FALSE,
                formula = "a / (a + b + c)",
                reference = "Jaccard, P. (1908). Nouvelles recherches sur la distribution florale. Bull. Soc. Vaud. Sci. Nat., 44, pp. 223--270.",
                description = "The Jaccard Similarity (C implementation) for binary data. It is the proportion of (TRUE, TRUE) pairs, but not considering (FALSE, FALSE) pairs. So it compares the intersection with the union of object sets.")

pr_Kulczynski1 <- function(a, b, c, d, n) a / (b + c)
pr_DB$set_entry(FUN = "pr_Kulczynski1",
                names = "Kulczynski1",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "a / (b + c)",
                reference = "Kurzcynski, T.W. (1970). Generalized distance and discrete variables. Biometrics, 26, pp. 525--534.",
                description = "Kulczynski Similarity for binary data. Relates the (TRUE, TRUE) pairs to discordant pairs.")

pr_Kulczynski2 <- function(a, b, c, d, n) (a / (a + b) + a / (a + c)) / 2
pr_DB$set_entry(FUN = "pr_Kulczynski2",
                names = "Kulczynski2",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "[a / (a + b) + a / (a + c)] / 2",
                reference = "Kurzcynski, T.W. (1970). Generalized distance and discrete variables. Biometrics, 26, pp. 525--534.",
                description = "Kulczynski Similarity for binary data. Relates the (TRUE, TRUE) pairs to the discordant pairs.")

pr_Mountford <- function(a, b, c, d, n) 2 * a / (a * (b + c) + 2 * b * c)
pr_DB$set_entry(FUN = "pr_Mountford",
                names = "Mountford",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "2a / (ab + ac + 2bc)",
                reference = "Mountford, M.D. (1962). An index of similarity and its application to classificatory probems. In P.W. Murphy (ed.), Progress in Soil Zoology, pp. 43--50. Butterworth, London.",
                description = "The Mountford Similarity for binary data.")

pr_fagerMcgowan <- function(a, b, c, d, n) a / sqrt((a + b) * (a + c)) - sqrt(a + c) / 2
pr_DB$set_entry(FUN = "pr_fagerMcgowan",
                names = c("Fager", "McGowan"),
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "a / sqrt((a + b)(a + c)) - sqrt(a + c) / 2",
                reference = "Fager, E. W. and McGowan, J. A. (1963). Zooplankton species groups in the North Pacific. Science, N. Y. 140: 453-460",
                description = "The Fager / McGowan distance.")


pr_RusselRao <- function(a, b, c, d, n) a / n
pr_DB$set_entry(FUN = "pr_RusselRao",
                names = c("Russel","Rao"),
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "a / n",
                reference = "Russell, P.F., and Rao T.R. (1940). On habitat and association of species of anopheline larvae in southeastern, Madras, J. Malaria Inst. India 3, pp. 153--178",
                description = "The Russel/Rao Similarity for binary data. It is just the proportion of (TRUE, TRUE) pairs.")

pr_SimpleMatching <- function(a, b, c, d, n) (a + d) / n
pr_DB$set_entry(FUN = "pr_SimpleMatching",
                names = c("simple matching", "Sokal/Michener"),
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "(a + d) / n",
                reference = "Sokal, R.R., and Michener, C.D. (1958). A statistical method for evaluating systematic relationships. Univ. Kansas Sci. Bull., 39, pp. 1409--1438.",
                description = "The Simple Matching Similarity or binary data. It is the proportion of concordant pairs.")

pr_Hamman <- function(a, b, c, d, n) (a + d - b - c) / n
pr_DB$set_entry(FUN = "pr_Hamman",
                names = "Hamman",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "([a + d] - [b + c]) / n",
                reference = "Hamann, U. (1961). Merkmalbestand und Verwandtschaftsbeziehungen der Farinosae. Ein Beitrag zum System der Monokotyledonen. Willdenowia, 2, pp. 639-768.",
                description = "The Hamman Matching Similarity for binary data. It is the proportion difference of the concordant and discordant pairs.")

pr_Faith <- function(a, b, c, d, n) (a + d / 2) / n
pr_DB$set_entry(FUN = "pr_Faith",
                names = "Faith",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "(a + d/2) / n",
                reference = "Belbin, L., Marshall, C. & Faith, D.P. (1983). Representing relationships by automatic assignment of colour. The Australian Computing Journal 15, 160-163.",
                description = "The Faith similarity")

pr_RogersTanimoto <- function(a, b, c, d, n) (a + d) / (a + 2 * (b + c) + d)
pr_DB$set_entry(FUN = "pr_RogersTanimoto",
                names = c("Tanimoto", "Rogers"),
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "(a + d) / (a + 2b + 2c + d)",
                reference = "Rogers, D.J, and Tanimoto, T.T. (1960). A computer program for classifying plants. Science, 132, pp. 1115--1118.",
                description = "The Rogers/Tanimoto Similarity for binary data. Similar to the simple matching coefficient, but putting double weight on the discordant pairs.")

pr_Dice <- function(a, b, c, d, n) 2 * a / (2 * a + b + c)
pr_DB$set_entry(FUN = "pr_Dice",
                names = c("Dice", "Czekanowski", "Sorensen"),
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "2a / (2a + b + c)",
                reference = "Dice, L.R. (1945). Measures of the amount of ecologic association between species. Ecolology, 26, pp. 297--302.",
                description = "The Dice Similarity")

pr_Phi <- function(a, b, c, d, n)
    (a * d - b * c) / (sqrt(a + b) * sqrt(c + d) * sqrt(a + c) * sqrt(b + d))
pr_DB$set_entry(FUN = "pr_Phi",
                names = "Phi",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "(ad - bc) / sqrt[(a + b)(c + d)(a + c)(b + d)]",
                reference = "Sokal, R.R, and Sneath, P.H.A. (1963). Principles of numerical taxonomy. W.H. Freeman and Company, San Francisco.",
                description = "The Phi Similarity (= Product-Moment-Correlation for binary variables)")

pr_Stiles <- function(a, b, c, d, n)
    log(n) + 2 * log(abs(a * d - b * c) - 0.5 * n) - log(a + b) - log(c + d) - log(a + c) - log(b + d)
pr_DB$set_entry(FUN = "pr_Stiles",
                names = "Stiles",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "log(n(|ad-bc| - 0.5n)^2 / [(a + b)(c + d)(a + c)(b + d)])",
                reference = "Stiles, H.E. (1961). The association factor in information retrieval. Communictions of the ACM, 8, 1, pp. 271--279.",
                description = "The Stiles Similarity (used for information retrieval). Identical to the logarithm of Krylov's distance.")

pr_Michael <- function(a, b, c, d, n)
    4 * (a * d - b * c) / ((a + d) * (a + d) + (b + c) * (b + c))
pr_DB$set_entry(FUN = "pr_Michael",
                names = "Michael",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "4(ad - bc) / [(a + d)^2 + (b + c)^2]",
                reference = "Cox, T.F., and Cox, M.A.A. (2001). Multidimensional Scaling. Chapmann and Hall.",
                description = "The Michael Similarity")

pr_MozleyMargalef <- function(a, b, c, d, n)
    a * n / ((a + b) * (a + c))
pr_DB$set_entry(FUN = "pr_MozleyMargalef",
                names = c("Mozley","Margalef"),
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "an / (a + b)(a + c)",
                reference = "Margalef, D.R. (1958). Information theory in ecology. Gen. Systems, 3, pp. 36--71.",
                description = "The Mozley/Margalef Similarity")

pr_Yule<- function(a, b, c, d, n) {
    ad <- a * d
    bc <- b * c
    (ad - bc) / (ad + bc)
}
pr_DB$set_entry(FUN = "pr_Yule",
                names = "Yule",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "(ad - bc) / (ad + bc)",
                reference = "Yule, G.U. (1912). On measuring associations between attributes. J. Roy. Stat. Soc., 75, pp. 579--642.",
                description = "Yule Similarity")

pr_Yule2<- function(a, b, c, d, n) {
    ad <- a * d
    bc <- b * c
    (sqrt(ad) - sqrt(bc)) / (sqrt(ad) + sqrt(bc))
}
pr_DB$set_entry(FUN = "pr_Yule2",
                names = "Yule2",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "(sqrt(ad) - sqrt(bc)) / (sqrt(ad) + sqrt(bc))",
                reference = "Yule, G.U. (1912). On measuring associations between attributes. J. Roy. Stat. Soc., 75, pp. 579--642.",
                description = "Yule Similarity")

pr_Ochiai <- function(a, b, c, d, n)
    a / sqrt((a + b) * (a + c))
pr_DB$set_entry(FUN = "pr_Ochiai",
                names = "Ochiai",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "a / sqrt[(a + b)(a + c)]",
                reference = "Sokal, R.R, and Sneath, P.H.A. (1963). Principles of numerical taxonomy. W.H. Freeman and Company, San Francisco.",
                description = "The Ochiai Similarity")

pr_Simpson <- function(a, b, c, d, n)
    a / min((a + b), (a + c))
pr_DB$set_entry(FUN = "pr_Simpson",
                names = "Simpson",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "a / min{(a + b), (a + c)}",
                reference = "Simpson, G.G. (1960). Notes on the measurement of faunal resemblance. American Journal of Science 258-A: 300-311.",
                description = "The Simpson Similarity (used in Zoology).")

pr_BraunBlanquet <- function(a, b, c, d, n)
    a / max((a + b), (a + c))
pr_DB$set_entry(FUN = "pr_BraunBlanquet",
                names = c("Braun-Blanquet"),
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "binary",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = TRUE,
                formula = "a / max{(a + b), (a + c)}",
                reference = "Braun-Blanquet, J. (1964): Pflanzensoziologie. Springer Verlag, Wien and New York.",
                description = "The Braun-Blanquet Similarity (used in Biology).")


pr_cos <- function(x, y) crossprod(x, y) / sqrt(crossprod(x) * crossprod(y))
pr_cos_prefun <- function(x, y, pairwise, p, reg_entry) {
    if (!is.matrix(x)) {
        reg_entry$C_FUN <- FALSE
        reg_entry$loop <- TRUE
        reg_entry$FUN <- "pr_cos"
    }
    list(x = x, y = y, pairwise = pairwise, p = NULL, reg_entry = reg_entry)
}
pr_DB$set_entry(FUN = "R_cosine",
                names = "cosine",
                PREFUN = "pr_cos_prefun",
                distance = FALSE,
                convert = function (x) 1 - x,
                type = "metric",
                loop = FALSE,
                C_FUN = TRUE,
                abcd = FALSE,
                formula = "xy / sqrt(xx * yy)",
                reference = "Anderberg, M.R. (1973). Cluster Analysis for Applicaitons. Academic Press.",
                description = "The cos Similarity (C implementation)")

pr_angular <- function(x, y) 1 - acos(crossprod(x, y) / sqrt(crossprod(x) * crossprod(y))) / pi
pr_DB$set_entry(FUN = pr_angular,
                names = "angular",
                distance = FALSE,
                convert = function (x) 1 - x,
                type = "metric",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = FALSE,
                formula = "1 - acos(xy / sqrt(xx * yy)) / pi",
                reference = "Anderberg, M.R. (1973). Cluster Analysis for Applicaitons. Academic Press.",
                description = "The angular similarity")


pr_eJaccard <- function(x, y) {
    tmp <- crossprod(x, y)
    tmp / (crossprod(x) + crossprod(y) - tmp)
}
pr_eJaccard_prefun <- function(x, y, pairwise, p, reg_entry) {
    if (!is.matrix(x)) {
        reg_entry$C_FUN <- FALSE
        reg_entry$loop <- TRUE
        reg_entry$FUN <- "pr_eJaccard"
    }
    list(x = 0 + x,
         y = if (!is.null(y)) 0 + y else NULL,
         pairwise = pairwise,
         p = NULL, reg_entry = reg_entry)
}
pr_DB$set_entry(FUN = "R_ejaccard",
                names = c("eJaccard", "extended_Jaccard"),
                PREFUN = "pr_eJaccard_prefun",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "metric",
                loop = FALSE,
                C_FUN = TRUE,
                abcd = FALSE,
                formula = "xy / (xx + yy - xy)",
                reference = "Strehl A. and Ghosh J. (2000).
Value-based customer grouping from large retail data-sets.
In Proc. SPIE Conference on Data Mining and Knowledge Discovery, Orlando, volume 4057, pages 33-42. SPIE.",
                description = "The extended Jaccard Similarity (C implementation; yields Jaccard for binary x,y).")

pr_eDice <- function(x, y) {
    tmp <- crossprod(x, y)
    tmp / (crossprod(x) + crossprod(y))
}
pr_eDice_prefun <- function(x, y, pairwise, p, reg_entry) {
    if (!is.matrix(x)) {
        reg_entry$C_FUN <- FALSE
        reg_entry$loop <- TRUE
        reg_entry$FUN <- "pr_eDice"
    }
    list(x = 0 + x,
         y = if (!is.null(y)) 0 + y else NULL,
         pairwise = pairwise,
         p = NULL, reg_entry = reg_entry)
}
pr_DB$set_entry(FUN = "R_edice",
                names = c("eDice", "extended_Dice","eSorensen"),
                PREFUN = "pr_eDice_prefun",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "metric",
                loop = FALSE,
                C_FUN = TRUE,
                abcd = FALSE,
                formula = "2xy / (xx + yy)",
		## FIXME
                reference = "Alexander Strehl. Relationship-based Clustering and Cluster Ensembles for High-dimensional Data Mining. PhD thesis, The University of Texas at Austin, May 2002.",
                description = "The extended Dice Similarity (C implementation; yields Dice for binary x,y).")

pr_cor <- function(x, y) {
    X <- x - mean(x)
    Y <- y - mean(y)
    crossprod(X, Y) / sqrt(crossprod(X) * crossprod(Y))
}
pr_DB$set_entry(FUN = "pr_cor",
                names = "correlation",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "metric",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = FALSE,
                formula = "xy / sqrt(xx * yy) for centered x,y",
                reference = "Anderberg, M.R. (1973). Cluster Analysis for Applicaitons. Academic Press.",
                description = "correlation (taking n instead of n-1 for the variance)")

pr_ChiSquared <- function(x, y) {
    tab <- table(x,y)
    exp <- rowSums(tab) %o% colSums(tab) / sum(tab)
    sum((tab - exp) ^ 2 / exp)
}
pr_DB$set_entry(FUN = "pr_ChiSquared",
                names = "Chi-squared",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "nominal",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = FALSE,
                formula = "sum_ij (o_i - e_i)^2 / e_i",
                reference = "Anderberg, M.R. (1973). Cluster Analysis for Applicaitons. Academic Press.",
                description = "Sum of standardized squared deviations from observed and expected values in a cross-tab for x and y.")

pr_PhiSquared <- function(x, y) {
    tab <- table(x,y)
    exp <- rowSums(tab) %o% colSums(tab) / sum(tab)
    sum((tab - exp) ^ 2 / exp) / sum(tab)
}
pr_DB$set_entry(FUN = "pr_PhiSquared",
                names = "Phi-squared",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "nominal",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = FALSE,
                formula = "[sum_ij (o_i - e_i)^2 / e_i] / n",
                reference = "Anderberg, M.R. (1973). Cluster Analysis for Applicaitons. Academic Press.",
                description = "Standardized Chi-Squared (= Chi / n).")

pr_Tschuprow <- function(x, y) {
    tab <- table(x,y)
    exp <- rowSums(tab) %o% colSums(tab) / sum(tab)
    sqrt(sum((tab - exp) ^ 2 / exp) / sum(tab) / sqrt((nrow(tab) - 1) * (ncol(tab) - 1)))
}
pr_DB$set_entry(FUN = "pr_Tschuprow",
                names = "Tschuprow",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "nominal",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = FALSE,
                formula = "sqrt{[sum_ij (o_i - e_i)^2 / e_i] / n / sqrt((p - 1)(q - 1))}",
                reference = "Tschuprow, A.A. (1925). Grundbegriffe und Grundprobleme der Korrelationstheorie. Springer.",
                description = "Tschuprow-standardization of Chi-Squared.")

pr_Cramer <- function(x, y) {
    tab <- table(x,y)
    exp <- rowSums(tab) %o% colSums(tab) / sum(tab)
    sqrt(sum((tab - exp) ^ 2 / exp) / sum(tab) / min((nrow(tab) - 1), (ncol(tab) - 1)))
}
pr_DB$set_entry(FUN = "pr_Cramer",
                names = "Cramer",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "nominal",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = FALSE,
                formula = "sqrt{[Chi / n)] / min[(p - 1), (q - 1)]}",
                reference = "Cramer, H. (1946). The elements of probability theory and some of its applications. Wiley, New York. ",
                description = "Cramer-standization of Chi-Squared.")

pr_Pearson <- function(x, y) {
    tab <- table(x,y)
    exp <- rowSums(tab) %o% colSums(tab) / sum(tab)
    Chi <- sum((tab - exp) ^ 2 / exp)
    sqrt(Chi / (sum(tab) + Chi))
}
pr_DB$set_entry(FUN = "pr_Pearson",
                names = c("Pearson","contingency"),
                distance = FALSE,
                convert = "pr_simil2dist",
                type = "nominal",
                loop = TRUE,
                C_FUN = FALSE,
                abcd = FALSE,
                formula = "sqrt{Chi / (n + Chi)}",
                reference = "Anderberg, M.R. (1973). Cluster Analysis for Applicaitons. Academic Press.",
                description = "Contingency Coefficient. Chi is the Chi-Squared statistic.")


pr_Gower <- function(x, y, l = NA, f = NA, m = NA, weights = NA) {
    ## prepare vectors
    len <- length(x)
    d <- logical(len)
    s <- double(len)

    ## compute scores groupwise:

    ## logical
    if (any(l)) {
        s[l] <- x[l] & y[l]
        d[l] <- x[l] | y[l]
    }

    ## factor
    if (any(f)) {
        s[f] <- x[f] == y[f]
        d[f] <- TRUE
    }

    ## metric
    if (any(m)) {
        s[m] <- 1 - abs(as.double(x[m]) - as.double(y[m]))
        d[m] <- TRUE
    }

    ## do not count missings
    d[is.na(s)] <- FALSE
    s[is.na(s)] <- 0

    drop(crossprod(s, weights)) / drop(crossprod(d, weights))
}
pr_Gower_prefun <- function(x, y, pairwise, p, reg_entry) {
    ## transform x and y
    x <- as.data.frame(x)
    if (!is.null(y))
        y <- as.data.frame(y)

    ## determine types
    l <- sapply(x, is.logical)
    f <- sapply(x, is.factor)
    o <- sapply(x, is.ordered)
    f <- f & !o
    m <- !(l | f | o)

    ## Transform ordinal variables
    if (any(o)) {
        ##FIXME: Gower uses ranks, but daisy just uses internal codes??
        x[o] <- lapply(x[o], as.integer)
        x[o] <- lapply(x[o], function(i) (i - 1) / (max(i) - 1))
        if (!is.null(y)) {
            y[o] <- lapply(y[o], as.integer)
            y[o] <- lapply(y[o], function(i) (i - 1) / (max(i) - 1))
        }
        m <- m | o
    }

    ## scale metric types
    RANGE <- function(x, y = NULL) {
        ## compute scale
        MAX <- sapply(x, max, na.rm = TRUE)
        MIN <- sapply(x, min, na.rm = TRUE)
        if (!is.null(y)) {
            MAX <- pmax(MAX, sapply(y, max, na.rm = TRUE))
            MIN <- pmin(MIN, sapply(y, min, na.rm = TRUE))
        }
        ret <- MAX - MIN
        ## do not scale when range == 0
        ret[ret == 0] <- 1
        ret
    }
    if (any(m)) {
        if (!is.null(p$ranges) && is.null(p$ranges.x))
            p$ranges.x <- p$ranges
        r <- if(is.null(p$ranges.x))
            RANGE(x[m], y[m])
        else
            rep_len(p$ranges.x, length.out = sum(m))

        if (!is.null(p$min) && is.null(p$min.x))
            p$min.x <- p$min
        MIN <- if (is.null(p$min.x)) {
            if (is.null(y))
                sapply(x[m], min, na.rm = TRUE)
            else
                pmin(sapply(x[m], min, na.rm = TRUE),
                     sapply(y[m], min, na.rm = TRUE))
        } else
            rep_len(p$min.x, length.out = sum(m))

        x[m] <- sweep(sweep(x[m], 2, MIN), 2, r, FUN = "/")
        if (!is.null(y)) {
            if (!is.null(p$min) && is.null(p$min.y))
                p$min.y <- p$min
            if (!is.null(p$min.y))
                MIN <- rep_len(p$min.y, length.out = sum(m))

            if (!is.null(p$ranges) && is.null(p$ranges.y))
                p$ranges.y <- p$ranges
            if (!is.null(p$ranges.y))
                r <- rep_len(p$ranges.y, length.out = sum(m))

            y[m] <- sweep(sweep(y[m], 2, MIN), 2, r, FUN = "/")
        }
    }

    ## weights
    weights <- rep_len(if (is.null(p$weights)) 1 else p$weights,
                   length.out = ncol(x))

    p <- list(l = l, f = f, m = m, weights = weights)
    list(x = x, y = y, pairwise = pairwise, p = p, reg_entry = reg_entry)
}
pr_DB$set_entry(FUN = "pr_Gower",
                names = "Gower",
                PREFUN = "pr_Gower_prefun",
                distance = FALSE,
                convert = "pr_simil2dist",
                type = NA,
                loop = TRUE,
                C_FUN = FALSE,
                abcd = FALSE,
                formula = "Sum_k (s_ijk * w_k) / Sum_k (d_ijk * w_k)",
                reference = "Gower, J.C. (1971). A general coefficient of similarity and some of its properties. Biometrics, 27, pp. 857--871.",
                description = "The Gower Similarity for mixed variable types.
w_k are variable weights. d_ijk is 0 for missings or a pair of FALSE logicals, and 1 else.
s_ijk is 1 for a pair of TRUE logicals or matching factor levels,
and the absolute difference for metric variables.
Each metric variable is scaled with its corresponding range,
provided the latter is not 0.
Ordinal variables are converted to ranks r_i and
the scores z_i = (r_i - 1) / (max r_i - 1) are taken as metric variables.
Note that in the latter case, unlike the definition of Gower, just the
internal integer codes are taken as the ranks, and not what rank() would
return. This is for compatibility with daisy() of the cluster package, and
will make a slight difference in case of ties. The weights w_k
can be specified by passing a numeric vector (recycled as needed) to
the 'weights' argument. Ranges (minimum) for scaling the columns of x and y
can be specified using the 'ranges.x'/'ranges.y' ('min.x' / 'min.y')
arguments (or simply 'ranges' ('min') for both x and y). In case of cross-proximities,
if not specified via these arguments, both data frames are standardized together.")