R/correlation_distances.R

Defines functions absolute_correlation_cdist absolute_correlation_pdist absolute_correlation_rdist angular_cdist angular_pdist angular_rdist correlation_cdist correlation_pdist correlation_rdist bound

bound <- function(x, min = -1, max = 1){
  x[x > max] <- max
  x[x < min] <- min
  x
}

# correlation distances
correlation_rdist <- function(X){
  stats::as.dist(sqrt((1 - bound(stats::cor(t(X))))/2))
}
correlation_pdist <- function(X){
  sqrt((1 - bound(stats::cor(t(X))))/2)
}
correlation_cdist <- function(X, Y){
  sqrt((1 - bound(stats::cor(t(X), t(Y))))/2)
}

# angular distances
angular_rdist <- function(X){
  stats::as.dist(acos(bound(stats::cor(t(X)))))
}
angular_pdist <- function(X){
  acos(bound(stats::cor(t(X))))
}
angular_cdist <- function(X, Y){
  acos(bound(stats::cor(t(X), t(Y))))
}

# absolute correlation distances
absolute_correlation_rdist <- function(X){
  stats::as.dist(sqrt(1 - bound(stats::cor(t(X))) ^ 2))
}
absolute_correlation_pdist <- function(X){
  sqrt(1 - bound(stats::cor(t(X))) ^ 2)
}
absolute_correlation_cdist <- function(X, Y){
  sqrt(1 - bound(stats::cor(t(X), t(Y))) ^ 2)
}

Try the rdist package in your browser

Any scripts or data that you put into this service are public.

rdist documentation built on May 4, 2020, 5:13 p.m.