[Vegan-commits] r1637 - in pkg/permute: . R inst man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jun 17 23:35:14 CEST 2011
Author: gsimpson
Date: 2011-06-17 23:35:12 +0200 (Fri, 17 Jun 2011)
New Revision: 1637
Added:
pkg/permute/R/shuffle.R
pkg/permute/man/shuffle.Rd
Removed:
pkg/permute/R/permuted.index.R
pkg/permute/man/permuted.index.Rd
Modified:
pkg/permute/DESCRIPTION
pkg/permute/NAMESPACE
pkg/permute/R/allPerms.R
pkg/permute/R/permControl.R
pkg/permute/R/permuplot.R
pkg/permute/R/permute.R
pkg/permute/inst/ChangeLog
pkg/permute/man/numPerms.Rd
pkg/permute/man/permCheck.Rd
Log:
permuted.index() become shuffle()
Modified: pkg/permute/DESCRIPTION
===================================================================
--- pkg/permute/DESCRIPTION 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/DESCRIPTION 2011-06-17 21:35:12 UTC (rev 1637)
@@ -1,6 +1,6 @@
Package: permute
Title: Functions for generating restricted permutations of data
-Version: 0.0-3
+Version: 0.5-0
Date: $Date$
Author: Gavin L. Simpson
Maintainer: Gavin L. Simpson <gavin.simpson at ucl.ac.uk>
Modified: pkg/permute/NAMESPACE
===================================================================
--- pkg/permute/NAMESPACE 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/NAMESPACE 2011-06-17 21:35:12 UTC (rev 1637)
@@ -1,6 +1,6 @@
## Visible functions:
export(`allPerms`, `Blocks`, `getNumObs`, `numPerms`, `permCheck`,
- `permControl`, `permute`, `permuted.index`, `Within`)
+ `permControl`, `permute`, `shuffle`, `Within`)
## S3 Methods
# print methods
Modified: pkg/permute/R/allPerms.R
===================================================================
--- pkg/permute/R/allPerms.R 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/R/allPerms.R 2011-06-17 21:35:12 UTC (rev 1637)
@@ -139,15 +139,15 @@
storage.mode(res) <- "integer"
if(!observed) {
obs.v <- seq_len(n)
- ##obs.row <- apply(res, 1, function(x, v) {identical(x, v)}, v)
- obs.row <- apply(res, 1, function(x, v) {identical(x, v)}, obs.v)
+ ##obs.row <- apply(res, 1, function(x, v) {identical(x, v)}, obs.v)
+ obs.row <- apply(res, 1, function(x, obs.v) all(x == obs.v), obs.v)
res <- res[!obs.row, ]
## reduce the number of permutations to get rid of the
## observed ordering
control$nperm <- control$nperm - 1
}
class(res) <- "allPerms"
- attr(res, "control") <- control
+ ##attr(res, "control") <- control
attr(res, "observed") <- observed
return(res)
}
Modified: pkg/permute/R/permControl.R
===================================================================
--- pkg/permute/R/permControl.R 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/R/permControl.R 2011-06-17 21:35:12 UTC (rev 1637)
@@ -2,12 +2,13 @@
within = Within(),
blocks = Blocks(),
maxperm = 9999, minperm = 99,
- all.perms = NULL)
+ all.perms = NULL,
+ observed = FALSE)
{
out <- list(strata = strata, nperm = nperm, complete = complete,
within = within, blocks = blocks,
maxperm = maxperm, minperm = minperm,
- all.perms = all.perms,
+ all.perms = all.perms, observed = observed,
name.strata = deparse(substitute(strata)))
class(out) <- "permControl"
return(out)
Modified: pkg/permute/R/permuplot.R
===================================================================
--- pkg/permute/R/permuplot.R 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/R/permuplot.R 2011-06-17 21:35:12 UTC (rev 1637)
@@ -1,3 +1,5 @@
+## This is totally wrong and needs updating to match the new
+## code in permute...
`permuplot` <- function(n, control = permControl(),
col = par("col"),
hcol = "red",
@@ -103,7 +105,7 @@
}),
stop("Unsupport permutation 'type'"))
}
- perms <- permuted.index2(n, control = control)
+ perms <- shuffle2(n, control = control)
perms <- tapply(perms, control$strata, function(x) x)
if(is.null(main))
main <- paste("Stratum:", names(tab))
@@ -173,7 +175,7 @@
sep = " ")
plot.new()
plot.window(xlim, ylim, asp = 1, ...)
- labs <- permuted.index2(n, control=control)
+ labs <- shuffle(n, control=control)
cols <- switch(control$type,
free = rep(col, n),
series = c(hcol, rep(col, n-1)),
Modified: pkg/permute/R/permute.R
===================================================================
--- pkg/permute/R/permute.R 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/R/permute.R 2011-06-17 21:35:12 UTC (rev 1637)
@@ -4,7 +4,7 @@
else {
if(control$complete)
warning("'$all.perms' is NULL, yet '$complete = TRUE'.\nReturning a random permutation.")
- perm <- permuted.index(n, control)
+ perm <- shuffle(n, control)
}
return(perm)
}
Deleted: pkg/permute/R/permuted.index.R
===================================================================
--- pkg/permute/R/permuted.index.R 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/R/permuted.index.R 2011-06-17 21:35:12 UTC (rev 1637)
@@ -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/R/shuffle.R
===================================================================
--- pkg/permute/R/shuffle.R (rev 0)
+++ pkg/permute/R/shuffle.R 2011-06-17 21:35:12 UTC (rev 1637)
@@ -0,0 +1,155 @@
+`shuffle` <-
+ 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
+}
Modified: pkg/permute/inst/ChangeLog
===================================================================
--- pkg/permute/inst/ChangeLog 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/inst/ChangeLog 2011-06-17 21:35:12 UTC (rev 1637)
@@ -2,8 +2,12 @@
permute ChangeLog
-Version 0.0-3 (opened December 09, 2010)
+Version 0.5-0 (opened June 17, 2011)
+ * shuffle: renamed permuted.index() to shuffle()
+
+Version 0.0-3 (closed June 17, 2011)
+
* NAMESPACE: permute gets a NAMESPACE. Needed to shield
functions form ones of similar name in vegan.
Modified: pkg/permute/man/numPerms.Rd
===================================================================
--- pkg/permute/man/numPerms.Rd 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/man/numPerms.Rd 2011-06-17 21:35:12 UTC (rev 1637)
@@ -60,7 +60,7 @@
\emph{grid} is not an issue here.
}
\author{Gavin Simpson}
-\seealso{\code{\link{permuted.index}} and \code{\link{permControl}}.}
+\seealso{\code{\link{shuffle}} and \code{\link{permControl}}.}
\examples{
## permutation design --- see ?permControl
ctrl <- permControl() ## defaults to freely exchangeable
Modified: pkg/permute/man/permCheck.Rd
===================================================================
--- pkg/permute/man/permCheck.Rd 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/man/permCheck.Rd 2011-06-17 21:35:12 UTC (rev 1637)
@@ -55,7 +55,7 @@
\details{
\code{permCheck}, \code{allPerms}, \code{numPerms} and
\code{permuplot} are utility functions for working with the new
- permutation schemes available in \code{\link{permuted.index}}.
+ permutation schemes available in \code{\link{shuffle}}.
\code{permCheck} is used to check the current permutation schemes
against the object to which it will be applied. It calculates the
@@ -109,7 +109,7 @@
%\references{
%}
\author{Gavin Simpson}
-\seealso{\code{\link{permuted.index}} and \code{\link{permControl}}.}
+\seealso{\code{\link{shuffle}} and \code{\link{permControl}}.}
\examples{
\dontrun{
Deleted: pkg/permute/man/permuted.index.Rd
===================================================================
--- pkg/permute/man/permuted.index.Rd 2011-06-16 09:08:15 UTC (rev 1636)
+++ pkg/permute/man/permuted.index.Rd 2011-06-17 21:35:12 UTC (rev 1637)
@@ -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)
-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 }
Added: pkg/permute/man/shuffle.Rd
===================================================================
--- pkg/permute/man/shuffle.Rd (rev 0)
+++ pkg/permute/man/shuffle.Rd 2011-06-17 21:35:12 UTC (rev 1637)
@@ -0,0 +1,260 @@
+\name{shuffle}
+\alias{shuffle}
+\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{
+shuffle(n, control = permControl())
+
+permControl(strata = NULL, nperm = 199, complete = FALSE,
+ within = Within(),
+ blocks = Blocks(),
+ maxperm = 9999, minperm = 99,
+ all.perms = NULL, observed = FALSE)
+
+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{all.perms}{an object of class \code{allPerms}, the result of a
+ call to \code{\link{allPerms}}.}
+ \item{observed}{logical; should the observed permutation be returned
+ as part of the set of all permutations?}
+ \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{i}{integer; row of \code{control$all.perms} to return.}
+}
+\details{
+ \code{shuffle} 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
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/vegan -r 1637
More information about the Vegan-commits
mailing list