File: distcalls.Rout.save

package info (click to toggle)
r-cran-proxy 0.4-27-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 372 kB
  • sloc: ansic: 1,247; sh: 12; makefile: 5
file content (203 lines) | stat: -rw-r--r-- 4,301 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

R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> #################################
> ## Test for dist calls
> ##################################
> 
> library(proxy)

Attaching package: 'proxy'

The following objects are masked from 'package:stats':

    as.dist, dist

The following object is masked from 'package:base':

    as.matrix

> 
> set.seed(20140107)
> 
> ## get all measures
> proxies = pr_DB$get_entry_names()
> 
> ## remove special cases
> proxies = setdiff(proxies, c("Mahalanobis", "Minkowski", "Stiles", "Levenshtein", "fJaccard"))
> 
> ## create test data
> x = matrix(1:100, 10)
> 
> ## test function: checks if dist(x) == dist(x,x) for all measures,
> ## and if diag(dist(x, x)) == diag(x, x, pairwise = TRUE)
> prtest <- function(...) {
+     CD <- dist(x, x, ...)
+     all(as.matrix(dist(x, ...)) == CD) &&
+       all(diag(CD) == dist(x, x, pairwise = TRUE, ...))
+ }
> 
> ## loop over all measures (except special cases)
> for (i in proxies)
+     {cat(i); prtest(i); cat(": OK.\n")}
Jaccard: OK.
Kulczynski1: OK.
Kulczynski2: OK.
Mountford: OK.
Fager: OK.
Russel: OK.
simple matching: OK.
Hamman: OK.
Faith: OK.
Tanimoto: OK.
Dice: OK.
Phi: OK.
Michael: OK.
Mozley: OK.
Yule: OK.
Yule2: OK.
Ochiai: OK.
Simpson: OK.
Braun-Blanquet: OK.
cosine: OK.
angular: OK.
eJaccard: OK.
eDice: OK.
correlation: OK.
Chi-squared: OK.
Phi-squared: OK.
Tschuprow: OK.
Cramer: OK.
Pearson: OK.
Gower: OK.
Euclidean: OK.
Bhjattacharyya: OK.
Manhattan: OK.
supremum: OK.
Canberra: OK.
Wave: OK.
divergence: OK.
Kullback: OK.
Bray: OK.
Soergel: OK.
Podani: OK.
Chord: OK.
Geodesic: OK.
Whittaker: OK.
Hellinger: OK.
> 
> ## Minkowski
> for (j in c(0.5, 1, 2, 3, Inf))
+     {cat("Minkowski: p =", j); prtest("Minkowski", p = j); cat(": OK.\n")}
Minkowski: p = 0.5: OK.
Minkowski: p = 1: OK.
Minkowski: p = 2: OK.
Minkowski: p = 3: OK.
Minkowski: p = Inf: OK.
> 
> ## Mahalanobis (need non-singular matrix)
> x = as.matrix(iris[1:50,-5])
> prtest("Mahalanobis")
[1] TRUE
> 
> ## fJaccard (needs values in unit interval)
> x = as.matrix(1:100/100, 10)
> prtest("fJaccard")
[1] TRUE
> 
> ## produce binary matrix
> x = matrix(rbinom(100,1,0.7), 10)
> 
> ## Stiles (gives a lot of warnings due to log)
> tmp = dist(x, "Stiles")
There were 27 warnings (use warnings() to see them)
> tmp = dist(x, x, "Stiles")
There were 50 or more warnings (use warnings() to see the first 50)
> 
> ## try again (almost) all measures, this time with binary data to check
> ## conversions
> for (i in proxies)
+     {cat(i); prtest(i); cat(": OK.\n")}
Jaccard: OK.
Kulczynski1: OK.
Kulczynski2: OK.
Mountford: OK.
Fager: OK.
Russel: OK.
simple matching: OK.
Hamman: OK.
Faith: OK.
Tanimoto: OK.
Dice: OK.
Phi: OK.
Michael: OK.
Mozley: OK.
Yule: OK.
Yule2: OK.
Ochiai: OK.
Simpson: OK.
Braun-Blanquet: OK.
cosine: OK.
angular: OK.
eJaccard: OK.
eDice: OK.
correlation: OK.
Chi-squared: OK.
Phi-squared: OK.
Tschuprow: OK.
Cramer: OK.
Pearson: OK.
Gower: OK.
Euclidean: OK.
Bhjattacharyya: OK.
Manhattan: OK.
supremum: OK.
Canberra: OK.
Wave: OK.
divergence: OK.
Kullback: OK.
Bray: OK.
Soergel: OK.
Podani: OK.
Chord: OK.
Geodesic: OK.
Whittaker: OK.
Hellinger: OK.
> ## Minkowski
> for (j in c(0.5, 1, 2, 3, Inf))
+     {cat("Minkowski: p =", j); prtest("Minkowski", p = j); cat(": OK.\n")}
Minkowski: p = 0.5: OK.
Minkowski: p = 1: OK.
Minkowski: p = 2: OK.
Minkowski: p = 3: OK.
Minkowski: p = Inf: OK.
> 
> ## Levenshtein distance
> s <- c("A", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog")
> all(as.matrix(dist(s, "Levenshtein")) == dist(s, s, "Levenshtein"))
[1] TRUE
> 
> ## Test auto-conversion
> x = iris[,-5]
> prtest()
[1] TRUE
> 
> 
> 
> proc.time()
   user  system elapsed 
  0.585   0.012   0.591