multivariate.R 3.1 KB
Newer Older
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
#'
#' @import MASS
#'
NULL

library(MASS)

#' Project a distance matrix in a euclidean space (NMDS).
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
nmds = function(distances,
                maxit = 50, trace = TRUE,
                tol = 0.001, p = 2) {
  if (inherits(distances,"matrix"))
    distances=as.dist(distances)

  stopifnot(inherits(distances,"dist"))

  k = attr(distances,"Size") - 1

  n = isoMDS(distances,
             k=k,
             maxit = maxit,
             trace = trace,
             tol = tol,
             p=p)

  p = n$points
  attr(p,"stress") = n$stress
  attr(x,"method")="nmds"

  return(p)
}

#' Project a distance matrix in a euclidean space (PCOA).
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
pcoa = function(distances) {
  if (inherits(distances,"matrix"))
    distances=as.dist(distances)

  stopifnot(inherits(distances,"dist"))

  k = attr(distances,"Size") - 1
  x = cmdscale(distances,k=k)
  attr(x,"method")="pcoa"

  return()
}

#' Project a set of points in a euclidean space (PCA).
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
pca = function(data,scale=FALSE) {
  k = min(nrow(data)-1,ncol(data))
  p = prcomp(data,
             retx = TRUE,
             center = TRUE,
             scale. = scale,
             tol=0)

  x = p$x
  attr(x,"method")="pca"

  return(x)
}

#' Double centering of a matrix.
#'
#' colSums and rowSums of the returned matrix are all equal to zero.
#'
#' Inspired from the algorithm described in stackoverflow
#' \url{https://stackoverflow.com/questions/43639063/double-centering-in-r}
#'
#' @export
bicentered = function(m) {

  # compute the row-wise and column-wise mean matrices
  R = m*0 + rowMeans(m)
  C = t(m*0 + colMeans(m))

  # substract them and add the grand mean
  return(m - R - C + mean(m[]))
}

#' Test if the distance matrix is euclidean.
#'
#' Actually a simplified version of the ADE4 implementation
#' (\code{\link[ade4]{is.euclid}}).
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
is.euclid = function (distances, tol = 1e-07)
{
  if (!inherits(distances, "dist"))
    stop("Object of class 'dist' expected")

  if (any(distances < tol))
    warning("Zero distance(s)")

  distances <- as.matrix(distances)
  n <- ncol(distances)
  delta <- -0.5 * ProcMod::bicenter(distances * distances)
  lambda <- eigen(delta, symmetric = TRUE, only.values = TRUE)$values
  w0 <- lambda[n]/lambda[1]
  return((w0 > -tol))
}

#' Project a distance matrix in a euclidean space.
#'
#' Project a set of points defined by a distance matrix in
#' an eucleadean space. If the used distance is a metric,
#' this is done using the \code{\link[ProcMod]{pcoa}} function,
#' otherwise the \code{\link[ProcMod]{nmds}} is used.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
dist2orthospace = function(distances, tol = 1e-07) {
  if (inherits(distances,"matrix"))
    distances=as.dist(distances)

  if (ProcMod::is.euclid(distances,tol = tol))
    return(ProcMod::pcoa(distances))
  else
    return(ProcMod::nmds(distances))
}