[Vegan-commits] r2279 - in branches/2.0: R inst man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Sep 10 14:38:28 CEST 2012
Author: jarioksa
Date: 2012-09-10 14:38:27 +0200 (Mon, 10 Sep 2012)
New Revision: 2279
Modified:
branches/2.0/R/mantel.R
branches/2.0/R/mantel.partial.R
branches/2.0/inst/ChangeLog
branches/2.0/man/mantel.Rd
Log:
merge 2062,2068 thru 2070: mantel[.partial] gained arg 'na.rm' plus internal code changes (r1950)
Modified: branches/2.0/R/mantel.R
===================================================================
--- branches/2.0/R/mantel.R 2012-09-10 12:05:42 UTC (rev 2278)
+++ branches/2.0/R/mantel.R 2012-09-10 12:38:27 UTC (rev 2279)
@@ -1,24 +1,46 @@
"mantel" <-
function (xdis, ydis, method = "pearson", permutations = 999,
- strata)
+ strata, na.rm = FALSE)
{
xdis <- as.dist(xdis)
ydis <- as.vector(as.dist(ydis))
- tmp <- cor.test(as.vector(xdis), ydis, method = method)
- statistic <- as.numeric(tmp$estimate)
- variant <- tmp$method
+ ## Handle missing values
+ if (na.rm)
+ use <- "complete.obs"
+ else
+ use <- "all.obs"
+ statistic <- cor(as.vector(xdis), ydis, method = method, use = use)
+ variant <- match.arg(method, eval(formals(cor)$method))
+ variant <- switch(variant,
+ pearson = "Pearson's product-moment correlation",
+ kendall = "Kendall's rank correlation tau",
+ spearman = "Spearman's rank correlation rho",
+ variant)
+ N <- attr(xdis, "Size")
+ if (length(permutations) == 1) {
+ if (permutations > 0) {
+ arg <- if (missing(strata)) NULL else strata
+ permat <- t(replicate(permutations,
+ permuted.index(N, strata = arg)))
+ }
+ } else {
+ permat <- as.matrix(permutations)
+ if (ncol(permat) != N)
+ stop(gettextf("'permutations' have %d columns, but data have %d observations",
+ ncol(permat), N))
+ permutations <- nrow(permutations)
+ }
if (permutations) {
- N <- attr(xdis, "Size")
- perm <- rep(0, permutations)
- ## asdist asn an index selects lower diagonal like as.dist,
- ## but is faster since it does not seet 'dist' attributes
+ perm <- numeric(permutations)
+ ## asdist as an index selects lower diagonal like as.dist,
+ ## but is faster since it does not set 'dist' attributes
xmat <- as.matrix(xdis)
asdist <- row(xmat) > col(xmat)
- for (i in 1:permutations) {
- take <- permuted.index(N, strata)
+ ptest <- function(take, ...) {
permvec <- (xmat[take, take])[asdist]
- perm[i] <- cor(permvec, ydis, method = method)
+ drop(cor(permvec, ydis, method = method, use = use))
}
+ perm <- sapply(1:permutations, function(i, ...) ptest(permat[i,], ...) )
signif <- (sum(perm >= statistic) + 1)/(permutations + 1)
}
else {
Modified: branches/2.0/R/mantel.partial.R
===================================================================
--- branches/2.0/R/mantel.partial.R 2012-09-10 12:05:42 UTC (rev 2278)
+++ branches/2.0/R/mantel.partial.R 2012-09-10 12:38:27 UTC (rev 2279)
@@ -1,6 +1,6 @@
"mantel.partial" <-
function (xdis, ydis, zdis, method = "pearson", permutations = 999,
- strata)
+ strata, na.rm = FALSE)
{
part.cor <- function(rxy, rxz, ryz) {
(rxy - rxz * ryz)/sqrt(1-rxz*rxz)/sqrt(1-ryz*ryz)
@@ -8,11 +8,20 @@
xdis <- as.dist(xdis)
ydis <- as.vector(as.dist(ydis))
zdis <- as.vector(as.dist(zdis))
- rxy <- cor.test(as.vector(xdis), ydis, method = method)
- rxz <- cor(as.vector(xdis), zdis, method = method)
- ryz <- cor(ydis, zdis, method = method)
- variant <- rxy$method
- rxy <- rxy$estimate
+ ## Handle missing values
+ if (na.rm)
+ use <- "complete.obs"
+ else
+ use <- "all.obs"
+ rxy <- cor(as.vector(xdis), ydis, method = method, use = use)
+ rxz <- cor(as.vector(xdis), zdis, method = method, use = use)
+ ryz <- cor(ydis, zdis, method = method, use = use)
+ variant <- match.arg(method, eval(formals(cor)$method))
+ variant <- switch(variant,
+ pearson = "Pearson's product-moment correlation",
+ kendall = "Kendall's rank correlation tau",
+ spearman = "Spearman's rank correlation rho",
+ variant)
statistic <- part.cor(rxy, rxz, ryz)
if (permutations) {
N <- attr(xdis, "Size")
@@ -22,8 +31,8 @@
for (i in 1:permutations) {
take <- permuted.index(N, strata)
permvec <- (xmat[take, take])[asdist]
- rxy <- cor(permvec, ydis, method = method)
- rxz <- cor(permvec, zdis, method = method)
+ rxy <- cor(permvec, ydis, method = method, use = use)
+ rxz <- cor(permvec, zdis, method = method, use = use)
perm[i] <- part.cor(rxy, rxz, ryz)
}
signif <- (sum(perm >= statistic)+1)/(permutations + 1)
Modified: branches/2.0/inst/ChangeLog
===================================================================
--- branches/2.0/inst/ChangeLog 2012-09-10 12:05:42 UTC (rev 2278)
+++ branches/2.0/inst/ChangeLog 2012-09-10 12:38:27 UTC (rev 2279)
@@ -4,6 +4,9 @@
Version 2.0-5 (opened June 18, 2012)
+ * merge r2262, 2268:2270, and also r1950: mantel and
+ mantel.partial gained argument na.rm = FALSE. This needed hand
+ editing of merges, and also merging old r1950: beware and test.
* merge r2271: tweak varpart.Rd. (plot coloured circles).
* merge r2267: tweak vegdist.Rd (ref to vegdist).
* merge r2260: streamline adonis (internal changes).
Modified: branches/2.0/man/mantel.Rd
===================================================================
--- branches/2.0/man/mantel.Rd 2012-09-10 12:05:42 UTC (rev 2278)
+++ branches/2.0/man/mantel.Rd 2012-09-10 12:38:27 UTC (rev 2279)
@@ -13,9 +13,10 @@
}
\usage{
-mantel(xdis, ydis, method="pearson", permutations=999, strata)
+mantel(xdis, ydis, method="pearson", permutations=999, strata,
+ na.rm = FALSE)
mantel.partial(xdis, ydis, zdis, method = "pearson", permutations = 999,
- strata)
+ strata, na.rm = FALSE)
}
\arguments{
@@ -26,6 +27,10 @@
\item{strata}{An integer vector or factor specifying the strata for
permutation. If supplied, observations are permuted only within the
specified strata.}
+ \item{na.rm}{Remove missing values in calculation of Mantel
+ correlation. Use this option with care: Permutation tests can
+ be biased, in particular if two matrices had missing values in
+ matching positions.}
}
\details{
Mantel statistic is simply a correlation between entries of two
More information about the Vegan-commits
mailing list