[Vegan-commits] r1095 - in pkg/permute: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jan 4 14:46:39 CET 2010


Author: gsimpson
Date: 2010-01-04 14:46:39 +0100 (Mon, 04 Jan 2010)
New Revision: 1095

Added:
   pkg/permute/R/permuted.index.R
   pkg/permute/man/permuted.index.Rd
Removed:
   pkg/permute/R/permuted.index2.R
   pkg/permute/man/permuted.index2.Rd
Log:
Clean-up name of main function

Added: pkg/permute/R/permuted.index.R
===================================================================
--- pkg/permute/R/permuted.index.R	                        (rev 0)
+++ pkg/permute/R/permuted.index.R	2010-01-04 13:46:39 UTC (rev 1095)
@@ -0,0 +1,155 @@
+`permuted.index` <-
+    function (n, control = permControl())
+{
+    `pStrata` <- function(strata, type, mirror = FALSE, start = NULL,
+                          flip = NULL, nrow, ncol, start.row = NULL,
+                          start.col = NULL) {
+            lev <- length(levels(strata))
+            ngr <- length(strata) / lev
+            sp <- split(seq(along = strata), strata)
+            if(type == "free") {
+                unname(do.call(c, sp[pFree(lev, lev)]))
+            } else if(type == "series") {
+                unname(do.call(c,
+                               sp[pSeries(seq_len(lev),
+                                          mirror = mirror,
+                                          start = start,
+                                          flip = flip)]))
+            } else if(type == "grid") {
+                unname(do.call(c,
+                               sp[pGrid(nrow = nrow, ncol = ncol,
+                                        mirror = mirror,
+                                        start.row = start.row,
+                                        start.col = start.col,
+                                        flip = flip)]))
+            } else {
+                stop("Invalid permutation type.")
+            }
+        }
+    `pGrid` <- function(nrow, ncol, mirror = FALSE, start.row = NULL,
+                        start.col = NULL, flip = NULL) {
+            if(is.null(start.row))
+                start.row <- pFree(nrow, 1L)
+            if(is.null(start.col))
+                start.col <- pFree(ncol, 1L)
+            ir <- seq(start.row, length=nrow) %% nrow
+            ic <- seq(start.col, length=ncol) %% ncol
+            if(!is.null(flip)) {
+                if(any(flip)) {
+                    if(flip[1L])
+                        ir <- rev(ir)
+                    if(flip[2L])
+                        ic <- rev(ic)
+                }
+            } else {
+                if (mirror) {
+                    if (runif(1L) < 0.5)
+                        ir <- rev(ir)
+                    if (runif(1L) < 0.5)
+                        ic <- rev(ic)
+                }
+            }
+            rep(ic, each=nrow) * nrow + rep(ir, len=nrow*ncol) + 1L
+        }
+    `pSeries` <- function(inds, mirror = FALSE, start = NULL,
+                          flip = NULL) {
+        n <- length(inds)
+        if(is.null(start))
+            start <- pFree(n, 1L)
+        out <- seq(start, length = n) %% n + 1L
+        if(!is.null(flip)) {
+            if(flip)
+                out <- rev(out)
+        } else {
+            if(mirror && runif(1L) < 0.5)
+                out <- rev(out)
+        }
+        inds[out]
+    }
+    `pFree` <- function(x, size)
+        .Internal(sample(x, size, FALSE, NULL))
+    ## END in-line Functions ##
+
+    ## If no strata then permute all samples using stated scheme
+    if(is.null(control$strata)) {
+        out <-
+            switch(control$within$type,
+                   "free" = pFree(n, n),
+                   "series" =
+                   pSeries(1:n, mirror = control$within$mirror),
+                   "grid" =
+                   pGrid(nrow = control$within$nrow,
+                         ncol = control$within$ncol,
+                         mirror = control$within$mirror)
+                   )
+    } else {
+        ## If strata present, either permute samples, strata or both
+
+        ## permute strata?
+        if(control$blocks$type == "none") {
+            out <- 1:n
+        } else {
+            flip <- runif(1L) < 0.5
+            out <- pStrata(control$strata,
+                           type = control$blocks$type,
+                           mirror = control$blocks$mirror,
+                           flip = flip,
+                           nrow = control$blocks$nrow,
+                           ncol = control$blocks$ncol)
+        }
+        ## permute the samples within strata?
+        if(control$within$type != "none") {
+            tab <- table(control$strata[out])
+            ## the levels of the strata
+            inds <- names(tab)
+            ## same permutation within each level of strata?
+            if(control$within$constant) {
+                if(control$within$type == "free") {
+                    n <- unique(tab)[1L]
+                    same.rand <- pFree(n, n)
+                } else if(control$within$type == "series") {
+                    start <- pFree(n / length(inds), 1L)
+                    flip <- runif(1L) < 0.5
+                } else if(control$within$type == "grid") {
+                    start.row <- pFree(control$within$nrow, 1L)
+                    start.col <- pFree(control$within$ncol, 1L)
+                    flip <- runif(2L) < 0.5
+                }
+            } else {
+                start <- start.row <- start.col <- flip <- NULL
+            }
+            tmp <- out
+            ## for each level of strata, permute
+            for (is in inds) {
+                ## must re-order strata here on basis of out as they
+                ## may have been permuted above
+                MATCH <- control$strata[out] == is
+                gr <- out[MATCH]
+                if ((n.gr <- length(gr)) > 1) {
+                    tmp[which(MATCH)] <-
+                        switch(control$within$type,
+                               "free" =
+                               if(control$within$constant) {
+                                   gr[same.rand]
+                               } else {
+                                   out[gr][pFree(n.gr, n.gr)]
+                               },
+                               "series" =
+                               pSeries(gr,
+                                       mirror = control$within$mirror,
+                                       start = start, flip = flip),
+                               "grid" =
+                               gr[pGrid(nrow = control$within$nrow,
+                                        ncol = control$within$ncol,
+                                        mirror = control$within$mirror,
+                                        start.row = start.row,
+                                        start.col = start.col,
+                                        flip = flip)]
+                               )
+                }
+            }
+            out <- tmp
+        }
+    }
+    out
+}

Deleted: pkg/permute/R/permuted.index2.R
===================================================================
--- pkg/permute/R/permuted.index2.R	2010-01-04 13:44:44 UTC (rev 1094)
+++ pkg/permute/R/permuted.index2.R	2010-01-04 13:46:39 UTC (rev 1095)
@@ -1,155 +0,0 @@
-`permuted.index` <-
-    function (n, control = permControl())
-{
-    `pStrata` <- function(strata, type, mirror = FALSE, start = NULL,
-                          flip = NULL, nrow, ncol, start.row = NULL,
-                          start.col = NULL) {
-            lev <- length(levels(strata))
-            ngr <- length(strata) / lev
-            sp <- split(seq(along = strata), strata)
-            if(type == "free") {
-                unname(do.call(c, sp[pFree(lev, lev)]))
-            } else if(type == "series") {
-                unname(do.call(c,
-                               sp[pSeries(seq_len(lev),
-                                          mirror = mirror,
-                                          start = start,
-                                          flip = flip)]))
-            } else if(type == "grid") {
-                unname(do.call(c,
-                               sp[pGrid(nrow = nrow, ncol = ncol,
-                                        mirror = mirror,
-                                        start.row = start.row,
-                                        start.col = start.col,
-                                        flip = flip)]))
-            } else {
-                stop("Invalid permutation type.")
-            }
-        }
-    `pGrid` <- function(nrow, ncol, mirror = FALSE, start.row = NULL,
-                        start.col = NULL, flip = NULL) {
-            if(is.null(start.row))
-                start.row <- pFree(nrow, 1L)
-            if(is.null(start.col))
-                start.col <- pFree(ncol, 1L)
-            ir <- seq(start.row, length=nrow) %% nrow
-            ic <- seq(start.col, length=ncol) %% ncol
-            if(!is.null(flip)) {
-                if(any(flip)) {
-                    if(flip[1L])
-                        ir <- rev(ir)
-                    if(flip[2L])
-                        ic <- rev(ic)
-                }
-            } else {
-                if (mirror) {
-                    if (runif(1L) < 0.5)
-                        ir <- rev(ir)
-                    if (runif(1L) < 0.5)
-                        ic <- rev(ic)
-                }
-            }
-            rep(ic, each=nrow) * nrow + rep(ir, len=nrow*ncol) + 1L
-        }
-    `pSeries` <- function(inds, mirror = FALSE, start = NULL,
-                          flip = NULL) {
-        n <- length(inds)
-        if(is.null(start))
-            start <- pFree(n, 1L)
-        out <- seq(start, length = n) %% n + 1L
-        if(!is.null(flip)) {
-            if(flip)
-                out <- rev(out)
-        } else {
-            if(mirror && runif(1L) < 0.5)
-                out <- rev(out)
-        }
-        inds[out]
-    }
-    `pFree` <- function(x, size)
-        .Internal(sample(x, size, FALSE, NULL))
-    ## END in-line Functions ##
-
-    ## If no strata then permute all samples using stated scheme
-    if(is.null(control$strata)) {
-        out <-
-            switch(control$within$type,
-                   "free" = pFree(n, n),
-                   "series" =
-                   pSeries(1:n, mirror = control$within$mirror),
-                   "grid" =
-                   pGrid(nrow = control$within$nrow,
-                         ncol = control$within$ncol,
-                         mirror = control$within$mirror)
-                   )
-    } else {
-        ## If strata present, either permute samples, strata or both
-
-        ## permute strata?
-        if(control$blocks$type == "none") {
-            out <- 1:n
-        } else {
-            flip <- runif(1L) < 0.5
-            out <- pStrata(control$strata,
-                           type = control$blocks$type,
-                           mirror = control$blocks$mirror,
-                           flip = flip,
-                           nrow = control$blocks$nrow,
-                           ncol = control$blocks$ncol)
-        }
-        ## permute the samples within strata?
-        if(control$within$type != "none") {
-            tab <- table(control$strata[out])
-            ## the levels of the strata
-            inds <- names(tab)
-            ## same permutation within each level of strata?
-            if(control$within$constant) {
-                if(control$within$type == "free") {
-                    n <- unique(tab)[1L]
-                    same.rand <- pFree(n, n)
-                } else if(control$within$type == "series") {
-                    start <- pFree(n / length(inds), 1L)
-                    flip <- runif(1L) < 0.5
-                } else if(control$within$type == "grid") {
-                    start.row <- pFree(control$within$nrow, 1L)
-                    start.col <- pFree(control$within$ncol, 1L)
-                    flip <- runif(2L) < 0.5
-                }
-            } else {
-                start <- start.row <- start.col <- flip <- NULL
-            }
-            tmp <- out
-            ## for each level of strata, permute
-            for (is in inds) {
-                ## must re-order strata here on basis of out as they
-                ## may have been permuted above
-                MATCH <- control$strata[out] == is
-                gr <- out[MATCH]
-                if ((n.gr <- length(gr)) > 1) {
-                    tmp[which(MATCH)] <-
-                        switch(control$within$type,
-                               "free" =
-                               if(control$within$constant) {
-                                   gr[same.rand]
-                               } else {
-                                   out[gr][pFree(n.gr, n.gr)]
-                               },
-                               "series" =
-                               pSeries(gr,
-                                       mirror = control$within$mirror,
-                                       start = start, flip = flip),
-                               "grid" =
-                               gr[pGrid(nrow = control$within$nrow,
-                                        ncol = control$within$ncol,
-                                        mirror = control$within$mirror,
-                                        start.row = start.row,
-                                        start.col = start.col,
-                                        flip = flip)]
-                               )
-                }
-            }
-            out <- tmp
-        }
-    }
-    out
-}

Added: pkg/permute/man/permuted.index.Rd
===================================================================
--- pkg/permute/man/permuted.index.Rd	                        (rev 0)
+++ pkg/permute/man/permuted.index.Rd	2010-01-04 13:46:39 UTC (rev 1095)
@@ -0,0 +1,258 @@
+\name{permuted.index}
+\alias{permuted.index}
+\alias{permControl}
+\alias{Blocks}
+\alias{Within}
+\alias{print.permControl}
+\alias{permute}
+
+\title{Unrestricted and restricted permutations}
+\description{
+  Unrestricted and restricted permutation designs for time series,
+  line transects, spatial grids and blocking factors.
+}
+\usage{
+permuted.index(n, control = permControl())
+
+permControl(strata = NULL, nperm = 199, complete = FALSE,
+            within = Within(),
+            blocks = Blocks(),
+            maxperm = 9999, minperm = 99,
+            all.perms = NULL)
+
+Within(type = c("free","series","grid","none"),
+       constant = FALSE, mirror = FALSE,
+       ncol = NULL, nrow = NULL)
+
+Blocks(type = c("free","series","grid","none"),
+       mirror = FALSE, ncol = NULL, nrow = NULL)
+
+permute(i, n, control)
+}
+
+\arguments{
+  \item{n}{numeric; the length of the returned vector of permuted
+    values. Usually the number of observations under consideration.}
+  \item{control}{a list of control values describing properties of the
+    permutation design, as returned by a call to \code{permControl}.}
+  \item{strata}{An integer vector or factor specifying the strata for
+    permutation. If supplied, observations are permuted only within the
+    specified strata.}
+  \item{nperm}{the number of permutations.}
+  \item{complete}{logical; should complete enumeration of all
+    permutations be performed?}
+  \item{within, blocks}{Permutation designs for samples within the
+    levels of \code{strata} (\code{within}) or for the blocks (strata)
+    themselves.}
+  \item{type}{the type of permutations required. One of \code{"free"},
+    \code{"series"}, \code{"grid"} or \code{"none"}. See Details.}
+  \item{maxperm}{the maximum number of permutations to
+    perform. Currently unused.}
+  \item{minperm}{the lower limit to the number of possible permutations
+    at which complete enumeration is performed. See argument
+    \code{complete} and Details, below.}
+  \item{constant}{logical; should the same permutation be used within
+    each level of strata? If \code{FALSE} a separate, possibly restricted,
+    permutation is produced for each level of \code{strata}.}
+  \item{mirror}{logical; should mirroring of sequences be allowed?}
+  \item{ncol, nrow}{numeric; the number of columns and rows of samples
+    in the spatial grid respectiavly.}
+  \item{all.perms}{an object of class \code{allPerms}, the result of a
+    call to \code{\link{allPerms}}.}
+  \item{i}{integer; row of \code{control$all.perms} to return.}
+}
+\details{
+  \code{permuted.index} can generate permutations for a wide range of
+  restricted permutation schemes. A small selection of the available
+  combinations of options is provided in the Examples section below.
+
+  Argument \code{mirror} determines whether grid or series permutations
+  can be mirrored. Consider the sequence 1,2,3,4. The relationship
+  between consecutive observations is preserved if we reverse the
+  sequence to 4,3,2,1. If there is no inherent direction in your
+  experimental design, mirrored permutations can be considered
+  part of the Null model, and as such increase the number of possible
+  permutations. The default is to not use mirroring so you must
+  explicitly turn this on using \code{mirror = TRUE} in
+  \code{permControl}.
+
+  To permute \code{strata} rather than the observations within the
+  levels of \code{strata}, use \code{permute.strata = TRUE}. However, note
+  that the number of observations within each level of strata
+  \strong{must} be equal! 
+
+  For some experiments, such as BACI designs, one might wish to use the
+  same permutation within each level of strata. This is controlled by
+  argument \code{constant}. If \code{constant = TRUE} then the same
+  permutation will be generated for each level of \code{strata}. The
+  default is \code{constant = FALSE}.
+
+  \code{permute} is a higher level utility function for use in a loop
+  within a function implementing a permutation test. The main purpose of
+  \code{permute} is to return the correct permutation in each iteration
+  of the loop, either a random permutation from the current design or
+  the next permutation from \code{control$all.perms} if it is not
+  \code{NULL} and \code{control$complete} is \code{TRUE}.
+}
+\value{
+  For \code{permuted.index} a vector of length \code{n} containing a
+  permutation of the observations 1, \ldots, n using the permutation
+  scheme described by argument \code{control}.
+  
+  For \code{permControl} a list with components for each of the possible
+  arguments.
+}
+\note{
+  \code{permuted.index} is currently used in one Vegan function;
+  \code{\link[vegan]{permutest.betadisper}}. Over time, the other functions
+  that currently use the older \code{\link[vegan]{permuted.index}} will be
+  updated to use \code{permuted.index}.
+}
+%\references{
+%}
+\author{Gavin Simpson}
+\seealso{\code{\link{permCheck}}, a utility function for checking
+  permutation scheme described by \code{\link{permControl}}.}
+
+\examples{
+set.seed(1234)
+
+## unrestricted permutations
+permuted.index(20)
+
+## observations represent a time series of line transect
+CTRL <- permControl(within = Within(type = "series"))
+permuted.index(20, control = CTRL)
+
+## observations represent a time series of line transect
+## but with mirroring allowed
+CTRL <- permControl(within = Within(type = "series",
+                                    mirror = TRUE))
+permuted.index(20, control = CTRL)
+
+## observations represent a spatial grid, 5rx4c
+nr <- 5
+nc <- 4
+CTRL <- permControl(within = Within(type = "grid", ncol = nc,
+                                     nrow = nr))
+perms <- permuted.index(20, control = CTRL)
+## view the permutation as a grid
+matrix(matrix(1:20, nrow = nr, ncol = nc)[perms],
+       ncol = nc, nrow = nr)
+
+## random permutations in presence of strata
+block <- gl(4, 5)
+CTRL <- permControl(strata = block,
+                    within = Within(type = "free"))
+permuted.index(20, CTRL)
+## as above but same random permutation within strata
+CTRL <- permControl(strata = block,
+                    within = Within(type = "free", constant = TRUE))
+permuted.index(20, CTRL)
+
+## time series within each level of block
+CTRL <- permControl(strata = block,
+                    within = Within(type = "series"))
+permuted.index(20, CTRL)
+## as above, but  with same permutation for each level
+CTRL <- permControl(strata = block,
+                    within = Within(type = "series",
+                                    constant = TRUE))
+permuted.index(20, CTRL)
+
+## spatial grids within each level of block, 4 x (5r x 5c)
+nr <- 5
+nc <- 5
+nb <- 4 ## number of blocks
+block <- gl(nb, 25)
+CTRL <- permControl(strata = block,
+                    within = Within(type = "grid",
+                                    ncol = nc,
+                                    nrow = nr))
+permuted.index(100, CTRL)
+## as above, but with same permutation for each level
+CTRL <- permControl(strata = block,
+                    within = Within(type = "grid",
+                                    ncol = nc,
+                                    nrow = nr,
+                                    constant = TRUE))
+permuted.index(100, CTRL)
+
+## permuting levels of block instead of observations
+block <- gl(4, 5)
+CTRL <- permControl(strata = block,
+                    blocks = Blocks(type = "free"),
+                    within = Within(type = "none"))
+permuted.index(20, CTRL)
+## permuting levels of block instead of observations
+## but blocks represent a time series
+CTRL <- permControl(strata = block,
+                    blocks = Blocks(type = "series"),
+                    within = Within(type = "none"))
+permuted.index(20, CTRL)
+
+## permuting levels of block but blocks represent a time series
+## free permutation within blocks
+CTRL <- permControl(strata = block,
+                    blocks = Blocks(type = "series"),
+                    within = Within(type = "free"))
+permuted.index(20, CTRL)
+
+## Simple function using permute() to assess significance
+## of a t.test  
+pt.test <- function(x, group, control) {
+    ## function to calculate t
+    t.statistic <- function(x, y) {
+        m <- length(x)
+        n <- length(y)
+        ## means and variances, but for speed
+        xbar <- mean(x)
+        ybar <- mean(y)
+        xvar <- var(x)
+        yvar <- var(y)
+        pooled <- sqrt(((m-1)*xvar + (n-1)*yvar) / (m+n-2))
+        (xbar - ybar) / (pooled * sqrt(1/m + 1/n))
+    }
+    ## check the control object
+    control <- permCheck(x, control)$control
+    ## number of observations
+    nobs <- getNumObs(x)
+    ## group names
+    lev <- names(table(group))
+    ## vector to hold results, +1 because of observed t
+    t.permu <- numeric(length = control$nperm) + 1
+    ## calculate observed t
+    t.permu[1] <- t.statistic(x[group == lev[1]], x[group == lev[2]])
+    ## generate randomisation distribution of t
+    for(i in seq_along(t.permu)) {
+        ## return a permutation
+        want <- permute(i, nobs, control)
+        ## calculate permuted t
+        t.permu[i+1] <- t.statistic(x[want][group == lev[1]],
+                                    x[want][group == lev[2]])
+    }
+    ## pval from permutation test
+    pval <- sum(abs(t.permu) >= abs(t.permu[1])) / (control$nperm + 1)
+    ## return value
+    return(list(t.stat = t.permu[1], pval = pval))
+}
+
+## generate some data with slightly different means
+set.seed(1234)
+gr1 <- rnorm(20, mean = 9)
+gr2 <- rnorm(20, mean = 10)
+dat <- c(gr1, gr2)
+## grouping variable
+grp <- gl(2, 20, labels = paste("Group", 1:2))
+## create the permutation design
+control <- permControl(nperm = 999,
+                       within = Within(type = "free"))
+## perform permutation t test
+perm.val <- pt.test(dat, grp, control)
+perm.val
+
+## compare perm.val with the p-value from t.test()
+t.test(dat ~ grp, var.equal = TRUE)
+}
+\keyword{ htest }
+\keyword{ design }

Deleted: pkg/permute/man/permuted.index2.Rd
===================================================================
--- pkg/permute/man/permuted.index2.Rd	2010-01-04 13:44:44 UTC (rev 1094)
+++ pkg/permute/man/permuted.index2.Rd	2010-01-04 13:46:39 UTC (rev 1095)
@@ -1,258 +0,0 @@
-\name{permuted.index}
-\alias{permuted.index}
-\alias{permControl}
-\alias{Blocks}
-\alias{Within}
-\alias{print.permControl}
-\alias{permute}
-
-\title{Unrestricted and restricted permutations}
-\description{
-  Unrestricted and restricted permutation designs for time series,
-  line transects, spatial grids and blocking factors.
-}
-\usage{
-permuted.index(n, control = permControl())
-
-permControl(strata = NULL, nperm = 199, complete = FALSE,
-            within = Within(),
-            blocks = Blocks(),
-            maxperm = 9999, minperm = 99,
-            all.perms = NULL)
-
-Within(type = c("free","series","grid","none"),
-       constant = FALSE, mirror = FALSE,
-       ncol = NULL, nrow = NULL)
-
-Blocks(type = c("free","series","grid","none"),
-       mirror = FALSE, ncol = NULL, nrow = NULL)
-
-permute(i, n, control)
-}
-
-\arguments{
-  \item{n}{numeric; the length of the returned vector of permuted
-    values. Usually the number of observations under consideration.}
-  \item{control}{a list of control values describing properties of the
-    permutation design, as returned by a call to \code{permControl}.}
-  \item{strata}{An integer vector or factor specifying the strata for
-    permutation. If supplied, observations are permuted only within the
-    specified strata.}
-  \item{nperm}{the number of permutations.}
-  \item{complete}{logical; should complete enumeration of all
-    permutations be performed?}
-  \item{within, blocks}{Permutation designs for samples within the
-    levels of \code{strata} (\code{within}) or for the blocks (strata)
-    themselves.}
-  \item{type}{the type of permutations required. One of \code{"free"},
-    \code{"series"}, \code{"grid"} or \code{"none"}. See Details.}
-  \item{maxperm}{the maximum number of permutations to
-    perform. Currently unused.}
-  \item{minperm}{the lower limit to the number of possible permutations
-    at which complete enumeration is performed. See argument
-    \code{complete} and Details, below.}
-  \item{constant}{logical; should the same permutation be used within
-    each level of strata? If \code{FALSE} a separate, possibly restricted,
-    permutation is produced for each level of \code{strata}.}
-  \item{mirror}{logical; should mirroring of sequences be allowed?}
-  \item{ncol, nrow}{numeric; the number of columns and rows of samples
-    in the spatial grid respectiavly.}
-  \item{all.perms}{an object of class \code{allPerms}, the result of a
-    call to \code{\link{allPerms}}.}
-  \item{i}{integer; row of \code{control$all.perms} to return.}
-}
-\details{
-  \code{permuted.index} can generate permutations for a wide range of
-  restricted permutation schemes. A small selection of the available
-  combinations of options is provided in the Examples section below.
-
-  Argument \code{mirror} determines whether grid or series permutations
-  can be mirrored. Consider the sequence 1,2,3,4. The relationship
-  between consecutive observations is preserved if we reverse the
-  sequence to 4,3,2,1. If there is no inherent direction in your
-  experimental design, mirrored permutations can be considered
-  part of the Null model, and as such increase the number of possible
-  permutations. The default is to not use mirroring so you must
-  explicitly turn this on using \code{mirror = TRUE} in
-  \code{permControl}.
-
-  To permute \code{strata} rather than the observations within the
-  levels of \code{strata}, use \code{permute.strata = TRUE}. However, note
-  that the number of observations within each level of strata
-  \strong{must} be equal! 
-
-  For some experiments, such as BACI designs, one might wish to use the
-  same permutation within each level of strata. This is controlled by
-  argument \code{constant}. If \code{constant = TRUE} then the same
-  permutation will be generated for each level of \code{strata}. The
-  default is \code{constant = FALSE}.
-
-  \code{permute} is a higher level utility function for use in a loop
-  within a function implementing a permutation test. The main purpose of
-  \code{permute} is to return the correct permutation in each iteration
-  of the loop, either a random permutation from the current design or
-  the next permutation from \code{control$all.perms} if it is not
-  \code{NULL} and \code{control$complete} is \code{TRUE}.
-}
-\value{
-  For \code{permuted.index} a vector of length \code{n} containing a
-  permutation of the observations 1, \ldots, n using the permutation
-  scheme described by argument \code{control}.
-  
-  For \code{permControl} a list with components for each of the possible
-  arguments.
-}
-\note{
-  \code{permuted.index} is currently used in one Vegan function;
-  \code{\link[vegan]{permutest.betadisper}}. Over time, the other functions
-  that currently use the older \code{\link[vegan]{permuted.index}} will be
-  updated to use \code{permuted.index}.
-}
-%\references{
-%}
-\author{Gavin Simpson}
-\seealso{\code{\link{permCheck}}, a utility function for checking
-  permutation scheme described by \code{\link{permControl}}.}
-
-\examples{
-set.seed(1234)
-
-## unrestricted permutations
-permuted.index(20)
-
-## observations represent a time series of line transect
-CTRL <- permControl(within = Within(type = "series"))
-permuted.index(20, control = CTRL)
-
-## observations represent a time series of line transect
-## but with mirroring allowed
-CTRL <- permControl(within = Within(type = "series",
-                                    mirror = TRUE))
-permuted.index(20, control = CTRL)
-
-## observations represent a spatial grid, 5rx4c
-nr <- 5
-nc <- 4
-CTRL <- permControl(within = Within(type = "grid", ncol = nc,
-                                     nrow = nr))
-perms <- permuted.index(20, control = CTRL)
-## view the permutation as a grid
-matrix(matrix(1:20, nrow = nr, ncol = nc)[perms],
-       ncol = nc, nrow = nr)
-
-## random permutations in presence of strata
-block <- gl(4, 5)
-CTRL <- permControl(strata = block,
-                    within = Within(type = "free"))
-permuted.index(20, CTRL)
-## as above but same random permutation within strata
-CTRL <- permControl(strata = block,
-                    within = Within(type = "free", constant = TRUE))
-permuted.index(20, CTRL)
-
-## time series within each level of block
-CTRL <- permControl(strata = block,
-                    within = Within(type = "series"))
-permuted.index(20, CTRL)
-## as above, but  with same permutation for each level
-CTRL <- permControl(strata = block,
-                    within = Within(type = "series",
-                                    constant = TRUE))
-permuted.index(20, CTRL)
-
-## spatial grids within each level of block, 4 x (5r x 5c)
-nr <- 5
-nc <- 5
-nb <- 4 ## number of blocks
-block <- gl(nb, 25)
-CTRL <- permControl(strata = block,
-                    within = Within(type = "grid",
-                                    ncol = nc,
-                                    nrow = nr))
-permuted.index(100, CTRL)
-## as above, but with same permutation for each level
-CTRL <- permControl(strata = block,
-                    within = Within(type = "grid",
-                                    ncol = nc,
-                                    nrow = nr,
-                                    constant = TRUE))
-permuted.index(100, CTRL)
-
-## permuting levels of block instead of observations
-block <- gl(4, 5)
-CTRL <- permControl(strata = block,
-                    blocks = Blocks(type = "free"),
-                    within = Within(type = "none"))
-permuted.index(20, CTRL)
-## permuting levels of block instead of observations
-## but blocks represent a time series
-CTRL <- permControl(strata = block,
-                    blocks = Blocks(type = "series"),
-                    within = Within(type = "none"))
-permuted.index(20, CTRL)
-
-## permuting levels of block but blocks represent a time series
-## free permutation within blocks
-CTRL <- permControl(strata = block,
-                    blocks = Blocks(type = "series"),
-                    within = Within(type = "free"))
-permuted.index(20, CTRL)
-
-## Simple function using permute() to assess significance
-## of a t.test  
-pt.test <- function(x, group, control) {
-    ## function to calculate t
-    t.statistic <- function(x, y) {
-        m <- length(x)
-        n <- length(y)
-        ## means and variances, but for speed
-        xbar <- mean(x)
-        ybar <- mean(y)
-        xvar <- var(x)
-        yvar <- var(y)
-        pooled <- sqrt(((m-1)*xvar + (n-1)*yvar) / (m+n-2))
-        (xbar - ybar) / (pooled * sqrt(1/m + 1/n))
-    }
-    ## check the control object
-    control <- permCheck(x, control)$control
-    ## number of observations
-    nobs <- getNumObs(x)
-    ## group names
-    lev <- names(table(group))
-    ## vector to hold results, +1 because of observed t
-    t.permu <- numeric(length = control$nperm) + 1
-    ## calculate observed t
-    t.permu[1] <- t.statistic(x[group == lev[1]], x[group == lev[2]])
-    ## generate randomisation distribution of t
-    for(i in seq_along(t.permu)) {
-        ## return a permutation
-        want <- permute(i, nobs, control)
-        ## calculate permuted t
-        t.permu[i+1] <- t.statistic(x[want][group == lev[1]],
-                                    x[want][group == lev[2]])
-    }
-    ## pval from permutation test
-    pval <- sum(abs(t.permu) >= abs(t.permu[1])) / (control$nperm + 1)
-    ## return value
-    return(list(t.stat = t.permu[1], pval = pval))
-}
-
-## generate some data with slightly different means
-set.seed(1234)
-gr1 <- rnorm(20, mean = 9)
-gr2 <- rnorm(20, mean = 10)
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/vegan -r 1095


More information about the Vegan-commits mailing list