Commit 748acd07 by Eric Coissac

Add the new ortho method

parent 2344af7a
#' @include procmod.frame.R
#' @include multivariate.R
#'
NULL
......@@ -38,7 +39,7 @@ mvar = function(...) {
Xnames=names(Xs)
Xs <- as.procmod.frame(lapply(Xs,scale,scale = FALSE))
Xs <- ortho(Xs)
nX = length(Xs)
......
#' @include procmod.frame.R
#' @import MASS
#'
NULL
......@@ -26,7 +27,7 @@ nmds = function(distances,
stopifnot(inherits(distances,"dist"))
k = attr(distances,"Size") - 1
k = attributes(distances)$Size - 1
y = suppressWarnings(cmdscale(distances,k))
k = ncol(y)
......@@ -40,8 +41,8 @@ nmds = function(distances,
p=p)
p = n$points
attr(p,"stress") = n$stress
attr(x,"method")="nmds"
attributes(p)$stress = n$stress
attributes(p)$projected='nmds'
return(p)
}
......@@ -67,9 +68,9 @@ pcoa = function(distances) {
stopifnot(inherits(distances,"dist"))
k = attr(distances,"Size") - 1
k = attributes(distances)$Size - 1
x = suppressWarnings(cmdscale(distances,k=k))
attr(x,"method")="pcoa"
attributes(x)$projected='pcoa'
return(x)
}
......@@ -88,8 +89,7 @@ pca = function(data,scale=FALSE) {
tol=0)
x = p$x
attr(x,"method")="pca"
attributes(x)$projected='pca'
return(x)
}
......@@ -135,22 +135,74 @@ is.euclid = function (distances, tol = 1e-07)
return((w0 > -tol))
}
#' Project a distance matrix in a euclidean space.
is.orthogonal = function(x) {
stopifnot(is.procmod.frame(x))
return(!is.null(attr(x,'projected')))
}
#' Project a dataset 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.
#' Project a set of points defined by a distance matrix
#' or a set of variables in an eucleadean space.
#' If the distance matrix is a metric, this is done using
#' the \code{\link[ProcMod]{pcoa}} function,
#' for other distance the \code{\link[ProcMod]{nmds}} is used.
#' When points are described by a set of variable 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)
ortho = function(data,...) {
UseMethod("ortho",data)
}
if (ProcMod::is.euclid(distances,tol = tol))
return(ProcMod::pcoa(distances))
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
ortho.dist = function(x,tol = 1e-7) {
if (ProcMod::is.euclid(x,tol = tol))
return(ProcMod::pcoa(x))
else
return(ProcMod::nmds(distances))
return(ProcMod::nmds(x))
}
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
ortho.matrix = function(x,scale=FALSE) {
pca(x,scale = scale)
}
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
ortho.data.frame = function(x,scale=FALSE) {
if (!is.null(attributes(x)$is.dist) &&
attributes(x)$is.dist==TRUE)
return(ortho(as.dist(as.matrix(x))))
pca(as.matrix(x),scale = scale)
}
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
ortho.procmod.frame = function(x) {
if (is.orthogonal(x))
return(x)
n = ncol(x)
p = vector(mode = 'character', length = n)
for (i in seq_len(n)) {
xt=ortho(x[[i]])
x[[i]]=xt
p[i]=attributes(xt)$projected
}
names(p) = names(x)
attributes(x)$projected=p
return(x)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/multivariate.R
\name{dist2orthospace}
\alias{dist2orthospace}
\title{Project a distance matrix in a euclidean space.}
\name{ortho}
\alias{ortho}
\title{Project a dataset in a euclidean space.}
\usage{
dist2orthospace(distances, tol = 1e-07)
ortho(data, ...)
}
\description{
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.
Project a set of points defined by a distance matrix
or a set of variables in an eucleadean space.
If the distance matrix is a metric, this is done using
the \code{\link[ProcMod]{pcoa}} function,
for other distance the \code{\link[ProcMod]{nmds}} is used.
When points are described by a set of variable the
\code{\link[ProcMod]{nmds}} is used.
}
\author{
Eric Coissac
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment