R version 3.4.2 (2017-09-28) -- "Short Summer" Copyright (C) 2017 The R Foundation for Statistical Computing Platform: i686-pc-linux-gnu (32-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. > > ### ceeboo 2007 > > ## todo: special values NaN, NA, and Inf > > 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(20070630) > > ## > > x <- matrix(runif(20),5,4) > inherits(x, "matrix") [1] TRUE > rownames(x) <- LETTERS[1:5] > y <- x > y[2,] <- x[1,] <- 0 > > x [,1] [,2] [,3] [,4] A 0.0000000 0.000000000 0.0000000 0.0000000 B 0.8390691 0.002929016 0.3449861 0.4751930 C 0.0377036 0.300615279 0.1399626 0.5143060 D 0.7621676 0.963274350 0.4358104 0.7934745 E 0.1211588 0.452265898 0.8005272 0.3062947 > y [,1] [,2] [,3] [,4] A 0.4595829 0.2462831 0.6662151 0.2658000 B 0.0000000 0.0000000 0.0000000 0.0000000 C 0.0377036 0.3006153 0.1399626 0.5143060 D 0.7621676 0.9632744 0.4358104 0.7934745 E 0.1211588 0.4522659 0.8005272 0.3062947 > > ## user interfaces > > r <- .Call("R_minkowski_dist", x, NULL, FALSE, 1, PACKAGE = "proxy") > all.equal(c(r), c(stats::dist(x, method = "minkowski", p = 1))) [1] TRUE > r A B C D B 1.6621772 C 0.9925875 1.3431883 D 2.9547267 1.4463526 1.9621392 E 1.6802466 1.7916865 1.1036817 2.0039138 > .Call("R_minkowski_dist", x, x, FALSE, 1, PACKAGE = "proxy") A B C D E A 0.0000000 1.662177 0.9925875 2.954727 1.680247 B 1.6621772 0.000000 1.3431883 1.446353 1.791686 C 0.9925875 1.343188 0.0000000 1.962139 1.103682 D 2.9547267 1.446353 1.9621392 0.000000 2.003914 E 1.6802466 1.791686 1.1036817 2.003914 0.000000 > .Call("R_minkowski_dist", x, y, FALSE, 1, PACKAGE = "proxy") A B C D E A 1.6378810 0.0000000 0.9925875 2.954727 1.680247 B 1.1534623 1.6621772 1.3431883 1.446353 1.791686 C 1.2509699 0.9925875 0.0000000 1.962139 1.103682 D 1.7776551 2.9547267 1.9621392 0.000000 2.003914 E 0.7192137 1.6802466 1.1036817 2.003914 0.000000 > .Call("R_minkowski_dist", x, y, TRUE, 1, PACKAGE = "proxy") [1] 1.637881 1.662177 0.000000 0.000000 0.000000 > > dfun <- paste("R",c("euclidean", "maximum", "manhattan", "canberra", "binary", "matching", "fuzzy", "mutual"),"dist", sep = "_") > > for (f in dfun) { + cat("\nTesting ",f,"\n\n",sep="") + r <- try(do.call(".Call", list(f, x, NULL, FALSE, PACKAGE = "proxy"))) + if ( inherits(r, "try-error")) + next + s <- try(stats::dist(x, method = gsub("R_|_dist", "", f))) + if (!inherits(s, "try-error")) + print(all.equal(c(r), c(s))) + print(r) + print(do.call(".Call", list(f, x, x, FALSE, PACKAGE = "proxy"))) + print(do.call(".Call", list(f, x, y, FALSE, PACKAGE = "proxy"))) + print(do.call(".Call", list(f, x, y, TRUE, PACKAGE = "proxy"))) + } Testing R_euclidean_dist [1] TRUE A B C D B 1.0241432 C 0.6130998 0.8799820 D 1.5258864 1.0186899 1.0627446 E 0.9766699 0.9763929 0.7138466 1.0209722 A B C D E A 0.0000000 1.0241432 0.6130998 1.525886 0.9766699 B 1.0241432 0.0000000 0.8799820 1.018690 0.9763929 C 0.6130998 0.8799820 0.0000000 1.062745 0.7138466 D 1.5258864 1.0186899 1.0627446 0.000000 1.0209722 E 0.9766699 0.9763929 0.7138466 1.020972 0.0000000 A B C D E A 0.8867716 0.0000000 0.6130998 1.525886 0.9766699 B 0.5918315 1.0241432 0.8799820 1.018690 0.9763929 C 0.7208543 0.6130998 0.0000000 1.062745 0.7138466 D 0.9680706 1.5258864 1.0627446 0.000000 1.0209722 E 0.4202848 0.9766699 0.7138466 1.020972 0.0000000 [1] 0.8867716 1.0241432 0.0000000 0.0000000 0.0000000 Testing R_maximum_dist [1] TRUE A B C D B 0.8390691 C 0.5143060 0.8013655 D 0.9632744 0.9603453 0.7244640 E 0.8005272 0.7179103 0.6605646 0.6410087 A B C D E A 0.0000000 0.8390691 0.5143060 0.9632744 0.8005272 B 0.8390691 0.0000000 0.8013655 0.9603453 0.7179103 C 0.5143060 0.8013655 0.0000000 0.7244640 0.6605646 D 0.9632744 0.9603453 0.7244640 0.0000000 0.6410087 E 0.8005272 0.7179103 0.6605646 0.6410087 0.0000000 A B C D E A 0.6662151 0.0000000 0.5143060 0.9632744 0.8005272 B 0.3794862 0.8390691 0.8013655 0.9603453 0.7179103 C 0.5262524 0.5143060 0.0000000 0.7244640 0.6605646 D 0.7169912 0.9632744 0.7244640 0.0000000 0.6410087 E 0.3384240 0.8005272 0.6605646 0.6410087 0.0000000 [1] 0.6662151 0.8390691 0.0000000 0.0000000 0.0000000 Testing R_manhattan_dist [1] TRUE A B C D B 1.6621772 C 0.9925875 1.3431883 D 2.9547267 1.4463526 1.9621392 E 1.6802466 1.7916865 1.1036817 2.0039138 A B C D E A 0.0000000 1.662177 0.9925875 2.954727 1.680247 B 1.6621772 0.000000 1.3431883 1.446353 1.791686 C 0.9925875 1.343188 0.0000000 1.962139 1.103682 D 2.9547267 1.446353 1.9621392 0.000000 2.003914 E 1.6802466 1.791686 1.1036817 2.003914 0.000000 A B C D E A 1.6378810 0.0000000 0.9925875 2.954727 1.680247 B 1.1534623 1.6621772 1.3431883 1.446353 1.791686 C 1.2509699 0.9925875 0.0000000 1.962139 1.103682 D 1.7776551 2.9547267 1.9621392 0.000000 2.003914 E 0.7192137 1.6802466 1.1036817 2.003914 0.000000 [1] 1.637881 1.662177 0.000000 0.000000 0.000000 Testing R_canberra_dist [1] TRUE A B C D B 4.000000 C 4.000000 2.356997 D 4.000000 1.409165 2.157322 E 4.000000 2.348575 1.682606 1.824656 A B C D E A NA 4.000000 4.000000 4.000000 4.000000 B 4 0.000000 2.356997 1.409165 2.348575 C 4 2.356997 0.000000 2.157322 1.682606 D 4 1.409165 2.157322 0.000000 1.824656 E 4 2.348575 1.682606 1.824656 0.000000 A B C D E A 4.000000 NA 4.000000 4.000000 4.000000 B 1.868964 4 2.356997 1.409165 2.348575 C 1.919038 4 0.000000 2.157322 1.682606 D 1.547657 4 2.157322 0.000000 1.824656 E 1.039972 4 1.682606 1.824656 0.000000 [1] 4 4 0 0 0 Testing R_binary_dist [1] TRUE A B C D B 1 C 1 0 D 1 0 0 E 1 0 0 0 A B C D E A 0 1 1 1 1 B 1 0 0 0 0 C 1 0 0 0 0 D 1 0 0 0 0 E 1 0 0 0 0 A B C D E A 1 0 1 1 1 B 0 1 0 0 0 C 0 1 0 0 0 D 0 1 0 0 0 E 0 1 0 0 0 [1] 1 1 0 0 0 Testing R_matching_dist Error in .Call("R_matching_dist", structure(c(0, 0.839069103589281, 0.037703595822677, : "R_matching_dist" not available for .Call() for package "proxy" Testing R_fuzzy_dist Error in stats::dist(x, method = gsub("R_|_dist", "", f)) : invalid distance method A B C D B 1.0000000 C 1.0000000 0.6719380 D 1.0000000 0.4770877 0.6640679 E 1.0000000 0.6979540 0.5844973 0.6036897 A B C D E A 0 1.0000000 1.0000000 1.0000000 1.0000000 B 1 0.0000000 0.6719380 0.4770877 0.6979540 C 1 0.6719380 0.0000000 0.6640679 0.5844973 D 1 0.4770877 0.6640679 0.0000000 0.6036897 E 1 0.6979540 0.5844973 0.6036897 0.0000000 A B C D E A 1.0000000 0 1.0000000 1.0000000 1.0000000 B 0.5180002 1 0.6719380 0.4770877 0.6979540 C 0.6445909 1 0.0000000 0.6640679 0.5844973 D 0.5581104 1 0.6640679 0.0000000 0.6036897 E 0.3562808 1 0.5844973 0.6036897 0.0000000 [1] 1 1 0 0 0 Testing R_mutual_dist Error in .Call("R_mutual_dist", structure(c(0, 0.839069103589281, 0.037703595822677, : "R_mutual_dist" not available for .Call() for package "proxy" > > ## no longer optimized > > .Call("R_ejaccard", x, NULL, FALSE, PACKAGE = "proxy") A B C D B 0.0000000 C 0.0000000 0.2957500 D 0.0000000 0.5298998 0.4107824 E 0.0000000 0.3549987 0.4459170 0.5179277 > .Call("R_ejaccard", x, x, FALSE, PACKAGE = "proxy") A B C D E A 1 0.0000000 0.0000000 0.0000000 0.0000000 B 0 1.0000000 0.2957500 0.5298998 0.3549987 C 0 0.2957500 1.0000000 0.4107824 0.4459170 D 0 0.5298998 0.4107824 1.0000000 0.5179277 E 0 0.3549987 0.4459170 0.5179277 1.0000000 > .Call("R_ejaccard", x, y, FALSE, PACKAGE = "proxy") A B C D E A 0.0000000 1 0.0000000 0.0000000 0.0000000 B 0.6794648 0 0.2957500 0.5298998 0.3549987 C 0.3820854 0 1.0000000 0.4107824 0.4459170 D 0.5374164 0 0.4107824 1.0000000 0.5179277 E 0.8157020 0 0.4459170 0.5179277 1.0000000 > .Call("R_ejaccard", x, y, TRUE, PACKAGE = "proxy") [1] 0 0 1 1 1 > > .Call("R_cosine", x, NULL, FALSE, PACKAGE = "proxy") A B C D B 0.0000000 C 0.0000000 0.5179092 D 0.0000000 0.7485219 0.8416675 E 0.0000000 0.5245741 0.6848730 0.7514756 > .Call("R_cosine", x, x, FALSE, PACKAGE = "proxy") A B C D E A 1 0.0000000 0.0000000 0.0000000 0.0000000 B 0 1.0000000 0.5179092 0.7485219 0.5245741 C 0 0.5179092 1.0000000 0.8416675 0.6848730 D 0 0.7485219 0.8416675 1.0000000 0.7514756 E 0 0.5245741 0.6848730 0.7514756 1.0000000 > .Call("R_cosine", x, y, FALSE, PACKAGE = "proxy") A B C D E A 0.0000000 1 0.0000000 0.0000000 0.0000000 B 0.8175510 0 0.5179092 0.7485219 0.5245741 C 0.5909955 0 1.0000000 0.8416675 0.6848730 D 0.8046383 0 0.8416675 1.0000000 0.7514756 E 0.9026897 0 0.6848730 0.7514756 1.0000000 > .Call("R_cosine", x, y, TRUE, PACKAGE = "proxy") [1] 0 0 1 1 1 > > x <- matrix(x > 0.5, 5,4) > y <- matrix(y > 0.5, 5,4) > > .Call("R_bjaccard", x, NULL, FALSE, PACKAGE = "proxy") 1 2 3 4 2 0.0000000 3 0.0000000 0.0000000 4 0.0000000 0.3333333 0.3333333 5 0.0000000 0.0000000 0.0000000 0.0000000 > .Call("R_bjaccard", x, x, FALSE, PACKAGE = "proxy") [,1] [,2] [,3] [,4] [,5] [1,] 1 0.0000000 0.0000000 0.0000000 0 [2,] 0 1.0000000 0.0000000 0.3333333 0 [3,] 0 0.0000000 1.0000000 0.3333333 0 [4,] 0 0.3333333 0.3333333 1.0000000 0 [5,] 0 0.0000000 0.0000000 0.0000000 1 > .Call("R_bjaccard", x, y, FALSE, PACKAGE = "proxy") [,1] [,2] [,3] [,4] [,5] [1,] 0 1 0.0000000 0.0000000 0 [2,] 0 0 0.0000000 0.3333333 0 [3,] 0 0 1.0000000 0.3333333 0 [4,] 0 0 0.3333333 1.0000000 0 [5,] 1 0 0.0000000 0.0000000 1 > .Call("R_bjaccard", x, y, TRUE, PACKAGE = "proxy") [1] 0 0 1 1 1 > > ### > > proc.time() user system elapsed 0.288 0.040 0.325
Generated by dwww version 1.15 on Sun Jun 23 05:32:30 CEST 2024.