MyNixOS website logo
Description

Usual Operations for Distance Matrices in R.

It provides the subset operator for dist objects and a function to compute medoid(s) that are fully parallelized leveraging the 'RcppParallel' package. It also provides functions for package developers to easily implement their own parallelized dist() function using a custom 'C++'-based distance function.

distops

R-CMD-check Codecov testcoverage pkgdown test-coverage CRANstatus

The goal of distops is to provide a set of functions to compute distances between observations in a sample and to perform operations on distance matrices.

Installation

You can install the development version of distops from GitHub with:

# install.packages("devtools")
devtools::install_github("LMJL-Alea/distops")

Features

library(distops)

Package developement

We provide two functions for package developers to help with defining efficient implementation of the dist functions for custom distances. Namely:

  • use_distops() setups a package to use distops for computing distances. In particular, it creates a src/ directory with a Makevars file and a Makevars.win file. It also creates a R/distops-package.R file with the appropriate roxygen2 tags so that the NAMESPACE file is modified to add the importFrom() directives for the Rcpp and RcppParallel packages and the useDynLib() directive for packages with compiled code. It finally modifies the DESCRIPTION file to add Rcpp, RcppParallel and distops to the Imports and LinkingTo fields and GNU make to the SystemRequirements field.
  • use_distance() creates R and C++ files for easy implementation of custom distances.

Subset operator

Let us compute the Euclidean distance matrix for the iris dataset:

D <- dist(iris[, 1:4], method = "euclidean")

We can subset this matrix using the [ operator. We can either provide the same indices for rows and columns in which case it return another object of class dist:

D[1:3, 1:3]
#>           1         2
#> 2 0.5385165          
#> 3 0.5099020 0.3000000

Or we can provide different indices for rows and columns in which case it returns a dense matrix:

D[2:3, 7:12]
#>           7         8         9        10        11        12
#> 2 0.5099020 0.4242641 0.5099020 0.1732051 0.8660254 0.4582576
#> 3 0.2645751 0.4123106 0.4358899 0.3162278 0.8831761 0.3741657

The subsetting operation is fully parallelized using the RcppParallel package. It is also memory efficient as it does not copy the original distance matrix.

Medoid computation

The medoid of a sample is the observation that minimizes the sum of distances to all other observations. The find_medoids() function computes the medoid of a sample for a given distance. It takes advantage of the RcppParallel package to compute the medoid in parallel.

find_medoids(D)
#> [1] 62

If the memberships argument is provided, it returns the medoid for each cluster.

find_medoids(D, memberships = as.factor(rep(1:3, each = 50L)))
#>   1   2   3 
#>   8  97 113

Future work

  • Pass a list instead of a matrix to be more general?
  • Use Arrow parquet format to store distance matrix in multiple files when sample size exceeds 10,000 or something like that.
  • Use Arrow connection to read in large data.
  • Add Progress bar.
Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows