Commit 748acd07 by Eric Coissac

Add the new ortho method

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