permute/0000755000176200001440000000000014174503352011737 5ustar liggesuserspermute/NAMESPACE0000644000176200001440000001172513537510676013175 0ustar liggesusers### Visible functions: export("allPerms", "as.allPerms", "Blocks", "check", "getBlocks", "getWithin", "getStrata", "getType", "getMirror", "getConstant", "getPlots", "getRow", "getCol", "getDim", "getNperm", "getMaxperm", "getMinperm", "getComplete", "getMake", "getObserved", "getAllperms", "getControl", "getHow", "how", "numPerms", "permute", "Plots", "setBlocks<-", "setWithin<-", "setStrata<-", "setType<-", "setMirror<-", "setConstant<-", "setPlots<-", "setRow<-", "setCol<-", "setDim<-", "setNperm<-", "setMaxperm<-", "setMinperm<-", "setComplete<-", "setMake<-", "setObserved<-", "setAllperms<-", "shuffle", "shuffleFree", "shuffleGrid", "shuffleSeries", "shuffleSet", "shuffleStrata", "Within" ) ### Imports: nobs() only exists in R 2.13.0 for import. We define the ### same nobs() generic in permute for export in older R. if (getRversion() >= "2.13.0") { importFrom("stats", "nobs") } else { export(nobs) } ### After R 3.2.x, R-Devel required all non-Base functions to be ### imported importFrom("stats", "getCall", "runif", "update") ### S3 Methods S3method("as.matrix", "allPerms") S3method("as.matrix", "permutationMatrix") S3method("update", "how") S3method("update", "Plots") ## print methods S3method("print", "allPerms") S3method("print", "check") S3method("print", "how") S3method("print", "permutationMatrix") S3method("print", "summary.allPerms") S3method("print", "summary.check") ## summary methods S3method("summary", "allPerms") S3method("summary", "check") ## nobs() methods S3method("nobs", "numeric") S3method("nobs", "integer") S3method("nobs", "matrix") S3method("nobs", "data.frame") S3method("nobs", "factor") S3method("nobs", "character") ## getFoo methods S3method("getBlocks", "default") S3method("getBlocks", "how") S3method("getPlots", "default") S3method("getPlots", "how") S3method("getWithin", "default") S3method("getWithin", "how") S3method("getStrata", "default") S3method("getStrata", "how") S3method("getStrata", "Plots") S3method("getType", "default") S3method("getType", "how") S3method("getType", "Plots") S3method("getType", "Within") S3method("getMirror", "default") S3method("getMirror", "how") S3method("getMirror", "Plots") S3method("getMirror", "Within") S3method("getConstant", "default") S3method("getConstant", "how") S3method("getConstant", "Within") S3method("getNperm", "default") S3method("getNperm", "how") S3method("getMaxperm", "default") S3method("getMaxperm", "how") S3method("getMinperm", "default") S3method("getMinperm", "how") S3method("getComplete", "default") S3method("getComplete", "how") S3method("getRow", "default") S3method("getRow", "how") S3method("getRow", "Plots") S3method("getRow", "Within") S3method("getCol", "default") S3method("getCol", "how") S3method("getCol", "Plots") S3method("getCol", "Within") S3method("getDim", "default") S3method("getDim", "how") S3method("getDim", "Plots") S3method("getDim", "Within") S3method("getMake", "default") S3method("getMake", "how") S3method("getObserved", "default") S3method("getObserved", "how") S3method("getAllperms", "default") S3method("getAllperms", "how") S3method("getControl", "default") S3method("getControl", "allPerms") ## setFoo methods S3method("setBlocks<-", "default") S3method("setBlocks<-", "how") S3method("setPlots<-", "default") S3method("setPlots<-", "how") S3method("setWithin<-", "default") S3method("setWithin<-", "how") S3method("setStrata<-", "default") S3method("setStrata<-", "how") S3method("setStrata<-", "Plots") S3method("setType<-", "default") S3method("setType<-", "how") S3method("setType<-", "Within") S3method("setType<-", "Plots") S3method("setMirror<-", "default") S3method("setMirror<-", "how") S3method("setMirror<-", "Within") S3method("setMirror<-", "Plots") S3method("setConstant<-", "default") S3method("setConstant<-", "how") S3method("setConstant<-", "Within") S3method("setConstant<-", "Plots") S3method("setNperm<-", "default") S3method("setNperm<-", "how") S3method("setMaxperm<-", "default") S3method("setMaxperm<-", "how") S3method("setMinperm<-", "default") S3method("setMinperm<-", "how") S3method("setComplete<-", "default") S3method("setComplete<-", "how") S3method("setRow<-", "default") S3method("setRow<-", "how") S3method("setRow<-", "Within") S3method("setRow<-", "Plots") S3method("setCol<-", "default") S3method("setCol<-", "how") S3method("setCol<-", "Within") S3method("setCol<-", "Plots") S3method("setDim<-", "default") S3method("setDim<-", "how") S3method("setDim<-", "Within") S3method("setDim<-", "Plots") S3method("setMake<-", "default") S3method("setMake<-", "how") S3method("setObserved<-", "default") S3method("setObserved<-", "how") S3method("setAllperms<-", "default") S3method("setAllperms<-", "how") permute/README.md0000644000176200001440000000320114141243517013210 0ustar liggesusers## Restricted permutations with R [![CRAN version](http://www.r-pkg.org/badges/version/permute)](https://cran.r-project.org/package=permute) [![](http://cranlogs.r-pkg.org/badges/grand-total/permute)](https://cran.r-project.org/package=permute) [![R-CMD-check](https://github.com/gavinsimpson/permute/workflows/R-CMD-check/badge.svg)](https://github.com/gavinsimpson/cocorresp/actions) [![codecov.io](https://codecov.io/github/gavinsimpson/permute/coverage.svg?branch=master)](https://codecov.io/github/gavinsimpson/permute?branch=master) ## What is permute? **permute** generates permutations from a range of restricted permutation designs. Permute provides an R implementation of the permutation schemes developed by Cajo ter Braak and made available in the Canoco software, version 3.1 (ter Braak, 1990). These permutation schemes draw upon ideas from an earlier paper by Besag & Clifford (1989). Several types of permutation are available in **permute**: * Free permutation of objects * Time series or line transect designs, where the temporal or spatial ordering is preserved. * Spatial grid designs, where the spatial ordering is preserved in both coordinate directions * Permutation of plots or groups of samples. * Blocking factors which restrict permutations to within blocks. The preceding designs can be nested within blocks, allowing analysis of hierachical designs (e.g. split plot designs) ### References Besag, J. and Clifford, P. (1989) Generalized Monte Carlo significance tests. *Biometrika* **76**; 633–642. ter Braak, C. J. F. (1990). *Update notes: CANOCO version 3.1*. Wageningen: Agricultural Mathematics Group. (UR). permute/data/0000755000176200001440000000000013537510676012661 5ustar liggesuserspermute/data/jackal.rda0000644000176200001440000000035113537510676014575 0ustar liggesusers‹ r‰0âŠàb```b`â’Ì@& `bN fËJLÎNÌa``«b`àb‡80pˆ:¡£ t,T<ʆÊÇ8@åPåcPõÃÔÃÍE“r@•BÓ¥îå¹êb0q5trRËRsŠ,¨ (‹obN*L…[j.˜‡¢59'±¦ nXZbrI~õ]y^bn*º-l>©yé%Pspjš.΢ür=˜NPh0Âêÿÿÿo ™ÿ£9€+%±$Q/­¨â†Ù3 !permute/man/0000755000176200001440000000000014174476773012532 5ustar liggesuserspermute/man/how.Rd0000644000176200001440000001420613537510676013612 0ustar liggesusers\name{how} \alias{how} \alias{print.how} \alias{Blocks} \alias{Within} \alias{Plots} \title{How to define a permutation design?} \description{ Utility functions to describe unrestricted and restricted permutation designs for time series, line transects, spatial grids and blocking factors. } \usage{ how(within = Within(), plots = Plots(), blocks = NULL, nperm = 199, complete = FALSE, maxperm = 9999, minperm = 5040, all.perms = NULL, make = TRUE, observed = FALSE) Within(type = c("free","series","grid","none"), constant = FALSE, mirror = FALSE, ncol = NULL, nrow = NULL) Plots(strata = NULL, type = c("none","free","series","grid"), mirror = FALSE, ncol = NULL, nrow = NULL) } \arguments{ \item{within, plots, blocks}{Permutation designs for samples within the levels of \code{plots} (\code{within}), permutation of \code{plots} themselves, or for the definition of blocking structures which further restrict permutations (\code{blocks}). \code{within} and \code{plots} each require a named list as produced by \code{Within} and \code{Plots} respectively. \code{blocks} takes a factor (or an object coercible to a factor via \code{as.factor}), the levels of which define the blocking structure.} \item{nperm}{numeric; the number of permutations.} \item{complete}{logical; should complete enumeration of all permutations be performed?} \item{type}{character; the type of permutations required. One of \code{"free"}, \code{"series"}, \code{"grid"} or \code{"none"}. See Details.} \item{maxperm}{numeric; the maximum number of permutations to perform. Currently unused.} \item{minperm}{numeric; the lower limit to the number of possible permutations at which complete enumeration is performed. When \code{nperm} is lower than \code{minperm}, sampling is performed from the set of complete permutations to avoid duplicate permutations. 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{make}{logical; should \code{check} generate all possible permutations? Useful if want to check permutation design but not produce the matrix of all permutations, or to circumvent the heuristics governing when complete enumeration is activated.} \item{observed}{logical; should the observed permutation be returned as part of the set of all permutations? Default is \code{FALSE} to facilitate usage in higher level functions.} \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 respectively.} \item{strata}{A factor, or an object that can be coerced to a factor via \code{as.factor}, specifying the strata for permutation.} } \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 type controls how samples are actually permuted; \code{"free"} indicates randomization, \code{"series"} indicates permutation via cyclic shifts (suitable for evenly-spaced line transect or time series data), \code{"grid"} indicates permutation via toroidal shifts (suitable for samples on a regular grid), and \code{"none"} indicates no permutation of samples. See the package vignette (\code{browseVignettes("permute")}) for additional information on each of these types of permutation. 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{how}. To permute plots rather than the observations within plots (the levels of \code{strata}), use \code{Within(type = "none")} and \code{Plots(type = foo)}, where \code{foo} is how you want the plots to be permuted. However, note that the number of observations within each plot \strong{must} be equal! For some experiments, such as BACI designs, one might wish to use the same permutation within each plot. 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}. } \value{ For \code{how} a list with components for each of the possible arguments. } \references{ \code{shuffle()} is modelled after the permutation schemes of Canoco 3.1 (ter Braak, 1990); see also Besag & Clifford (1989). Besag, J. and Clifford, P. (1989) Generalized Monte Carlo significance tests. \emph{Biometrika} \strong{76}; 633--642. ter Braak, C. J. F. (1990). \emph{Update notes: CANOCO version 3.1}. Wageningen: Agricultural Mathematics Group. (UR). } \author{Gavin Simpson} \seealso{\code{\link{shuffle}} and \code{\link{shuffleSet}} for permuting from a design, and \code{\link{check}}, a utility function for checking permutation design described by \code{how}.} \examples{ ## Set up factors for the Plots and Blocks plts <- gl(4, 10) ## 4 Plots of 10 samples each blks <- gl(2, 20) ## 2 Blocks of 20 samples each ## permutation design h1 <- how(within = Within(type = "series", mirror = TRUE), plots = Plots(strata = plts, type = "series"), blocks = blks) ## The design can be updated... ## ... remove the blocking: update(h1, blocks = NULL) ## ... or switch the type of shuffling at a level: #update(h1, plots = update(getPlots(h1), type = "none")) plots2 <- update(getPlots(h1), type = "none") update(h1, plots = plots2) } \keyword{ utils }permute/man/numPerms.Rd0000644000176200001440000000603613537510676014625 0ustar liggesusers\name{numPerms} \alias{numPerms} \title{Number of possible permutations for a given object} \description{ \code{numPerms} calculates the maximum number of permutations possible under the current permutation scheme. } \usage{ numPerms(object, control = how()) } \arguments{ \item{object}{any object handled by \code{\link{nobs}}.} \item{control}{a list of control values describing properties of the permutation design, as returned by a call to \code{\link{how}}.} } \details{ Function \code{numPerms} returns the number of permutations for the passed \code{object} and the selected permutation scheme. \code{object} can be one of a data frame, matrix, an object for which a scores method exists, or a numeric or integer vector. In the case of a numeric or integer vector, a vector of length 1 can be used and it will be expanded to a vector of length \code{object} (i.e., \code{1:object}) before computing the number of permutations. As such, \code{object} can be the number of observations not just the object containing the observations. } \value{ The (numeric) number of possible permutations of observations in \code{object}. } \note{ In general, mirroring \code{"series"} or \code{"grid"} designs doubles or quadruples, respectively, the number of permutations without mirroring (within levels of strata if present). This is \strong{not} true in two special cases: \enumerate{ \item In \code{"grid"} designs where the number of columns is equal to 2, and \item In \code{"series"} designs where the number of observations in a series is equal to 2. } For example, with 2 observations there are 2 permutations for \code{"series"} designs: \enumerate{ \item 1-2, and \item 2-1. } If these two permutations were mirrored, we would have: \enumerate{ \item 2-1, and \item 1-2. } It is immediately clear that this is the same set of permutations without mirroring (if one reorders the rows). A similar situation arises in \code{"grid"} designs where the number of \strong{columns} per \emph{grid} is equal to 2. Note that the number of rows per \emph{grid} is not an issue here. } \author{Gavin Simpson} \seealso{\code{\link{shuffle}} and \code{\link{how}}. Additional \code{\link{nobs}} methods are provide, see \code{\link{nobs-methods}}.} \examples{ ## permutation design --- see ?how ctrl <- how() ## defaults to freely exchangeable ## vector input v <- 1:10 (obs <- nobs(v)) numPerms(v, control = ctrl) ## integer input len <- length(v) (obs <- nobs(len)) numPerms(len, control = ctrl) ## new design, objects are a time series ctrl <- how(within = Within(type = "series")) numPerms(v, control = ctrl) ## number of permutations possible drastically reduced... ## ...turn on mirroring ctrl <- how(within = Within(type = "series", mirror = TRUE)) numPerms(v, control = ctrl) ## Try blocking --- 2 groups of 5 bl <- numPerms(v, control = how(blocks = gl(2,5))) bl ## should be same as pl <- numPerms(v, control = how(plots = Plots(strata = gl(2,5)))) pl stopifnot(all.equal(bl, pl)) } permute/man/allUtils.Rd0000644000176200001440000000262613537510676014611 0ustar liggesusers\name{allUtils} \alias{allFree} \alias{allSeries} \alias{allGrid} \alias{allStrata} \title{Utility functions for complete enumeration of all possible permutations} \description{ Utility functions to return the set of all permutations under different designs. For most practical applications, i.e. to combine designs permuting blocks and/or within blocks function \code{\link{allPerms}} will be required. } \usage{ allFree(n, v = seq_len(n)) allSeries(n, nperms, mirror = FALSE) allGrid(n, nperms, nr, nc, mirror, constant) allStrata(n, control) } \arguments{ \item{n}{the number of observations.} \item{v}{numeric; vector of indices. Default is \code{1:n}.} \item{nperms}{numeric; number of possible permutations.} \item{mirror}{logical; mirroring of permutations allowed?} \item{nr,nc}{integer; number of rows and columns of grid designs.} \item{constant}{logical; same permutation within each block?} \item{control}{a list of control values describing properties of the permutation design, as returned by a call to \code{\link{how}}.} } \details{ These are utility functions and aren't designed for casual use. \code{\link{allPerms}} should be used instead. Details on usage of these functions can be found in \code{\link{allPerms}}. } \value{ A matrix of all possible permutations of \code{n} observations or of \code{v}, given the provided options. } \author{Gavin Simpson} permute/man/shuffle-utils.Rd0000644000176200001440000000724613537510676015615 0ustar liggesusers\name{shuffle-utils} \alias{shuffle-utils} \alias{shuffleFree} \alias{shuffleGrid} \alias{shuffleSeries} \alias{shuffleStrata} \title{Utility functions for unrestricted and restricted permutations} \description{ Unrestricted and restricted permutations for time series, line transects, spatial grids and blocking factors. } \usage{ shuffleFree(x, size) shuffleSeries(x, mirror = FALSE, start = NULL, flip = NULL) shuffleGrid(nrow, ncol, mirror = FALSE, start.row = NULL, start.col = NULL, flip = NULL) shuffleStrata(strata, type, mirror = FALSE, start = NULL, flip = NULL, nrow, ncol, start.row = NULL, start.col = NULL) } \arguments{ \item{x}{vector of indices to permute.} \item{size}{number of random permutations required} \item{mirror}{logical; should mirroring of sequences be allowed?} \item{start}{integer; the starting point for time series permutations. If missing, a random starting point is determined.} \item{flip}{logical, length 1 (\code{shuffleSeries}) or length 2 (\code{shuffleGrid}); force mirroring of permutation. This will always return the reverse of the computed permutation. For \code{shuffleGrid}, the first element pertains to flipping rows, the second to flipping columns of the grid.} \item{nrow, ncol}{numeric; the number of rows and columns in the grid.} \item{start.row, start.col}{numeric; the starting row and column for the shifted grid permutation. If non supplied, a random starting row and column will be selected.} \item{strata}{factor; the blocks to permute.} \item{type}{character; the type of permutation used to shuffle the \code{strata}. One of \code{"free"}, \code{"grid"} or \code{"series"}.} } \details{ These are developer-level functions for generating permuted indexes from one of several restricted and unrestricted designs. \code{shuffleFree} is a wrapper to code underlying \code{\link{sample}}, but without the extra over head of sanity checks. It is defined as \code{sample.int(x, size, replace = FALSE)}. You must arrange for the correct values to be supplied, where \code{x} is a vector of indices to sample from, and \code{size} is the number of indices to sample. Sampling is done without replacement and without regard to prior probabilities. Argument \code{size} is allowed so that one can draw a single observation at random from the indices \code{x}. In general use, \code{size} would be set equal to \code{length{x}}. } \value{ A integer vector of permuted indices. } %\note{ % \code{shuffle} 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]{shuffle}} will be % updated to use \code{shuffle}. %} %\references{ %} \author{Gavin Simpson} \seealso{\code{\link{check}}, a utility function for checking permutation scheme described by \code{\link{how}}. \code{\link{shuffle}} as a user-oriented wrapper to these functions.} \examples{ \dontshow{suppressWarnings(RNGversion("3.5.0"))} set.seed(3) ## draw 1 value at random from the set 1:10 shuffleFree(1:10, 1) ## permute the series 1:10 x <- 1:10 shuffleSeries(x) ## with random starting point shuffleSeries(x, start = 5L) ## known starting point shuffleSeries(x, flip = TRUE) ## random start, forced mirror shuffleSeries(x, mirror = TRUE) ## random start, possibly mirror ## permute a grid of size 3x3 shuffleGrid(3, 3) ## random starting row/col shuffleGrid(3, 3, start.row = 2, start.col = 3) ## with known row/col shuffleGrid(3, 3, flip = rep(TRUE, 2)) ## random start, forced mirror } \keyword{ htest } \keyword{ design } permute/man/set-methods.Rd0000644000176200001440000001037313537510676015252 0ustar liggesusers\name{set-methods} \alias{set-methods} \alias{setBlocks<-} \alias{setBlocks<-.default} \alias{setBlocks<-.how} \alias{setBlocks<-.permControl} \alias{setWithin<-} \alias{setWithin<-.default} \alias{setWithin<-.how} \alias{setStrata<-} \alias{setStrata<-.default} \alias{setStrata<-.how} \alias{setStrata<-.Plots} \alias{setType<-} \alias{setType<-.default} \alias{setType<-.how} \alias{setType<-.Plots} \alias{setType<-.Within} \alias{setMirror<-} \alias{setMirror<-.default} \alias{setMirror<-.how} \alias{setMirror<-.Plots} \alias{setMirror<-.Within} \alias{setConstant<-} \alias{setConstant<-.default} \alias{setConstant<-.how} \alias{setConstant<-.Plots} \alias{setConstant<-.Within} \alias{setPlots<-} \alias{setPlots<-.default} \alias{setPlots<-.how} \alias{setRow<-} \alias{setRow<-.default} \alias{setRow<-.how} \alias{setRow<-.Plots} \alias{setRow<-.Within} \alias{setCol<-} \alias{setCol<-.default} \alias{setCol<-.how} \alias{setCol<-.Plots} \alias{setCol<-.Within} \alias{setDim<-} \alias{setDim<-.default} \alias{setDim<-.how} \alias{setDim<-.Plots} \alias{setDim<-.Within} \alias{setNperm<-} \alias{setNperm<-.default} \alias{setNperm<-.how} \alias{setNperm<-.permControl} \alias{setAllperms<-} \alias{setAllperms<-.default} \alias{setAllperms<-.how} \alias{setAllperms<-.permControl} \alias{setMaxperm<-} \alias{setMaxperm<-.default} \alias{setMaxperm<-.how} \alias{setMaxperm<-.permControl} \alias{setMinperm<-} \alias{setMinperm<-.default} \alias{setMinperm<-.how} \alias{setMinperm<-.permControl} \alias{setComplete<-} \alias{setComplete<-.default} \alias{setComplete<-.how} \alias{setComplete<-.permControl} \alias{setMake<-} \alias{setMake<-.default} \alias{setMake<-.how} \alias{setObserved<-} \alias{setObserved<-.default} \alias{setObserved<-.how} \title{Replacement functions to set components of a permutation design} \description{ Simple functions to allow abstracted replacement of components of a permutation design, for example as returned by \code{\link{how}}. In addition to performing replacement of components of the list returned by \code{\link{how}}, these replacement function also update the matched calls stored within the list to facilitate the use of \code{\link{update}} by users. } \usage{ setBlocks(object) <- value setPlots(object) <- value setWithin(object) <- value setStrata(object) <- value setNperm(object) <- value setAllperms(object) <- value setMaxperm(object) <- value setMinperm(object) <- value setComplete(object) <- value setMake(object) <- value setObserved(object) <- value setRow(object) <- value setCol(object) <- value setDim(object) <- value setType(object) <- value setMirror(object) <- value setConstant(object) <- value } \arguments{ \item{object}{An R object to dispatch on.} \item{value}{The replacement value/object.} } \details{ These are replacement functions for working with permutation design objects created by \code{\link{how}}. They should be used in preference to directly updating the permutation design in case the internal structure of object changes as \pkg{permute} is developed and because the matched call also needs to be updated to facilitate use of \code{\link{update}} on the \code{\link{how}} object. } \section{Note}{ \code{setStrata<-} has methods for objects of class \code{"how"} and \code{"Plots"}. The former sets the \code{blocks} component of the \code{\link{how}} object, whilst the latter sets the \code{strata} component of the \code{\link{Plots}} object. \code{setDim<-}, \code{setRow<-}, and \code{setCol<-} cannot be used on an object of class \code{"how"}. Instead, extract the \code{Plots} or \code{Within} components with \code{\link{getPlots}} or \code{\link{getWithin}} and alter those components, then use the resulting object to replace the \code{plots} or \code{within} components using \code{setPlots} or \code{setWithin}. } \value{ These replacement functions return \code{object} suitably modified. } \author{Gavin Simpson} \seealso{\code{\link{check}}, a utility function for checking permutation scheme described by \code{\link{how}}. Comparable extractor functions are also available; see \code{\link{get-methods}}. } \examples{ ## extract components from a "how" object hh <- how() getNperm(hh) setNperm(hh) <- 999 getNperm(hh) } \keyword{ methods } \keyword{ utils }permute/man/check.Rd0000644000176200001440000001344313537510676014074 0ustar liggesusers\name{check} \alias{check} \alias{print.check} \alias{print.summary.check} \alias{summary.check} \title{Utility functions for permutation schemes} \description{ \code{check} provides checking of permutation schemes for validity. \code{permuplot} produces a graphical representation of the selected permutation design. } \usage{ check(object, control = how(), quietly = FALSE) \method{summary}{check}(object, \dots) } \arguments{ \item{object}{an R object. See Details for a complete description, especially for \code{\link{numPerms}}. For \code{\link{summary.check}} an object of class \code{"check"}.} \item{control}{a list of control values describing properties of the permutation design, as returned by a call to \code{\link{how}}.} \item{quietly}{logical; should messages by suppressed?} \item{\dots}{arguments to other methods.} } \details{ \code{check} is a utility functions for working with the new permutation schemes available in \code{\link{shuffle}}. \code{check} is used to check the current permutation schemes against the object to which it will be applied. It calculates the maximum number of possible permutations for the number of observations in \code{object} and the permutation scheme described by \code{control}. The returned object contains component \code{control}, an object of class \code{"how"} suitably modified if \code{check} identifies a problem. The main problem is requesting more permutations than is possible with the number of observations and the permutation design. In such cases, \code{nperm} is reduced to equal the number of possible permutations, and complete enumeration of all permutations is turned on (\code{control$complete} is set to \code{TRUE}). Alternatively, if the number of possible permutations is low, and less than \code{control$minperm}, it is better to enumerate all possible permutations, and as such complete enumeration of all permutations is turned on (\code{control$complete} is set to \code{TRUE}). This guarantees that permutations are all unique and there are no duplicates. } \value{ For \code{check} a list containing the maximum number of permutations possible and an object of class \code{"\link{how}"}. } \author{Gavin L. Simpson} \seealso{\code{\link{shuffle}} and \code{\link{how}}.} \examples{ ## only run this example if vegan is available if (suppressPackageStartupMessages(require("vegan"))) { ## use example data from ?pyrifos in package vegan example(pyrifos) ## Demonstrate the maximum number of permutations for the pyrifos data ## under a series of permutation schemes ## no restrictions - lots of perms CONTROL <- how(within = Within(type = "free")) (check1 <- check(pyrifos, CONTROL)) ## summary(check1) ## no strata but data are series with no mirroring, so 132 permutations CONTROL <- how(within = Within(type = "series", mirror = FALSE)) check(pyrifos, CONTROL) ## no strata but data are series with mirroring, so 264 permutations CONTROL <- how(within = Within(type = "series", mirror = TRUE)) check(pyrifos, control = CONTROL) ## unrestricted within strata check(pyrifos, control = how(plots = Plots(strata = ditch), within = Within(type = "free"))) ## time series within strata, no mirroring check(pyrifos, control = how(plots = Plots(strata = ditch), within = Within(type = "series", mirror = FALSE))) ## time series within strata, with mirroring check(pyrifos, control = how(plots = Plots(strata = ditch), within = Within(type = "series", mirror = TRUE))) ## time series within strata, no mirroring, same permutation ## within strata check(pyrifos, control = how(plots = Plots(strata = ditch), within = Within(type = "series", constant = TRUE))) ## time series within strata, with mirroring, same permutation ## within strata check(pyrifos, control = how(plots = Plots(strata = ditch), within = Within(type = "series", mirror = TRUE, constant = TRUE))) ## permute strata check(pyrifos, how(plots = Plots(strata = ditch, type = "free"), within = Within(type = "none"))) } ## this should also also for arbitrary vectors vec1 <- check(1:100) vec2 <- check(1:100, how()) all.equal(vec1, vec2) vec3 <- check(1:100, how(within = Within(type = "series"))) all.equal(100, vec3$n) vec4 <- check(1:100, how(within = Within(type= "series", mirror = TRUE))) all.equal(vec4$n, 200) ## enumerate all possible permutations fac <- gl(2,6) ctrl <- how(plots = Plots(strata = fac), within = Within(type = "grid", mirror = FALSE, constant = TRUE, nrow = 3, ncol = 2)) check(1:12, ctrl) numPerms(1:12, control = ctrl) (tmp <- allPerms(12, control = update(ctrl, observed = TRUE))) (tmp2 <- allPerms(12, control = ctrl)) ## turn on mirroring ctrl <- update(ctrl, within = update(getWithin(ctrl), mirror = TRUE)) numPerms(1:12, control = ctrl) (tmp3 <- allPerms(12, control = update(ctrl, observed = TRUE))) (tmp4 <- allPerms(12, control = ctrl)) ## prints out details of the permutation scheme as ## well as the matrix of permutations summary(tmp) summary(tmp2) ## different numbers of observations per level of strata fac <- factor(rep(1:3, times = c(3,2,2))) ## free permutations in levels of strata numPerms(7, how(within = Within(type = "free"), plots = Plots(strata = fac, type = "none"))) allPerms(7, how(within = Within(type = "free"), plots = Plots(strata = fac))) ## series permutations in levels of strata ctrl <- how(within = Within(type = "series"), plots = Plots(strata = fac)) numPerms(7, control = ctrl) allPerms(7, control = ctrl) } \keyword{ utilities } \keyword{ design } \keyword{ methods } permute/man/allPerms.Rd0000644000176200001440000001011313537510676014565 0ustar liggesusers\name{allPerms} \alias{allPerms} \alias{print.allPerms} \alias{summary.allPerms} \alias{print.summary.allPerms} \alias{as.matrix.allPerms} \alias{as.allPerms} \title{Complete enumeration of all possible permutations} \description{ \code{allPerms} is a utility function to return the set of permutations for a given R object and a specified permutation design. } \usage{ allPerms(n, control = how(), check = TRUE) \method{summary}{allPerms}(object, \dots) \method{as.matrix}{allPerms}(x, \dots) as.allPerms(object, control) } \arguments{ \item{n}{the number of observations or an 'object' from which the number of observations can be determined via \code{getNumObs}.} \item{control}{a list of control values describing properties of the permutation design, as returned by a call to \code{\link{how}}.} \item{check}{logical; should \code{allPerms} check the design? The default is to check, but this can be skipped, for example if a function checked the design earlier.} \item{object}{for \code{summary.allPerms}, an object of class \code{"allPerms"}. For \code{as.allPerms} a matrix or something that can be coerced to a matrix by \code{\link{as.matrix}}.} \item{\dots}{arguments to other methods.} \item{x}{ an object of class \code{"allPerms"}, as returned by \code{allPerms}. } } \details{ Function \code{allPerms} enumerates all possible permutations for the number of observations and the selected permutation scheme. It has \code{\link{print}} and \code{\link{summary}} methods. \code{allPerms} returns a matrix containing all possible permutations, possibly containing the observed ordering (if argument \code{observed} is \code{TRUE}). The rows of this matrix are the various permutations and the columns reflect the number of samples. With free permutation designs, and restricted permutation schemes with large numbers of observations, there are a potentially huge number of possible permutations of the samples. It would be inefficient, not to mention incredibly time consuming, to enumerate them all. Storing all possible permutations would also become problematic in such cases. To control this and guard against trying to evaluate too large a number of permutations, if the number of possible permutations is larger than \code{getMaxperm(control)}, \code{allPerms} exits with an error. The \code{as.matrix} method sets the \code{control} and \code{seed} attributes to \code{NULL} and removes the \code{"permutationMatrix"} class, resulting in a standard matrix object. } \value{ For \code{allPerms}, and object of class \code{"allPerms"}, a matrix whose rows are the set of all possible permutations for the supplies number of observations and permutation scheme selected. The matrix has two additional attributes \code{control} and \code{observed}. Attribute \code{control} contains the argument \code{control} (possibly updated via \code{check}). Attribute \code{observed} contains argument \code{observed}. } \section{Warning}{ If permuting the strata themselves, a balanced design is required (the same number of observations in each level of \code{strata}. This is common to all functions in the package. } \author{Gavin Simpson} \examples{ ## allPerms can work with a vector vec <- c(3,4,5) allPerms(vec) ## free permutation ## enumerate all possible permutations for a more complicated ## design fac <- gl(2,6) ctrl <- how(within = Within(type = "grid", mirror = FALSE, constant = TRUE, nrow = 3, ncol = 2), plots = Plots(strata = fac)) Nobs <- length(fac) numPerms(seq_len(Nobs), control = ctrl) ## 6 (tmp <- allPerms(Nobs, control = update(ctrl, observed = TRUE))) (tmp2 <- allPerms(Nobs, control = ctrl)) ## turn on mirroring ##ctrl$within$mirror <- TRUE ctrl <- update(ctrl, within = update(getWithin(ctrl), mirror = TRUE)) numPerms(seq_len(Nobs), control = ctrl) (tmp3 <- allPerms(Nobs, control = update(ctrl, observed = TRUE))) (tmp4 <- allPerms(Nobs, control = ctrl)) ## prints out details of the permutation scheme as ## well as the matrix of permutations summary(tmp3) summary(tmp4) } permute/man/get-methods.Rd0000644000176200001440000001216713537510676015241 0ustar liggesusers\name{get-methods} \alias{get-methods} \alias{getBlocks} \alias{getBlocks.default} \alias{getBlocks.how} \alias{getBlocks.permControl} \alias{getWithin} \alias{getWithin.default} \alias{getWithin.how} \alias{getWithin.permControl} \alias{getStrata} \alias{getStrata.default} \alias{getStrata.how} \alias{getStrata.permControl} \alias{getStrata.Plots} \alias{getType} \alias{getType.default} \alias{getType.how} \alias{getType.permControl} \alias{getType.Plots} \alias{getType.Within} \alias{getMirror} \alias{getMirror.default} \alias{getMirror.how} \alias{getMirror.permControl} \alias{getMirror.Plots} \alias{getMirror.Within} \alias{getConstant} \alias{getConstant.default} \alias{getConstant.how} \alias{getConstant.permControl} \alias{getConstant.Within} \alias{getPlots} \alias{getPlots.default} \alias{getPlots.how} \alias{getPlots.permControl} \alias{getRow} \alias{getRow.default} \alias{getRow.how} \alias{getRow.permControl} \alias{getRow.Plots} \alias{getRow.Within} \alias{getCol} \alias{getCol.default} \alias{getCol.how} \alias{getCol.permControl} \alias{getCol.Plots} \alias{getCol.Within} \alias{getDim} \alias{getDim.default} \alias{getDim.how} \alias{getDim.permControl} \alias{getDim.Plots} \alias{getDim.Within} \alias{getNperm} \alias{getNperm.default} \alias{getNperm.how} \alias{getNperm.permControl} \alias{getMaxperm} \alias{getMaxperm.default} \alias{getMaxperm.how} \alias{getMaxperm.permControl} \alias{getMinperm} \alias{getMinperm.default} \alias{getMinperm.how} \alias{getMinperm.permControl} \alias{getComplete} \alias{getComplete.default} \alias{getComplete.how} \alias{getComplete.permControl} \alias{getMake} \alias{getMake.default} \alias{getMake.how} \alias{getObserved} \alias{getObserved.default} \alias{getObserved.how} \alias{getAllperms} \alias{getAllperms.default} \alias{getAllperms.how} \alias{getControl} \alias{getHow} \alias{getControl.default} \alias{getControl.allPerms} \title{Extractor functions to access components of a permutation design} \description{ Simple functions to allow abstracted access to components of a permutation design, for example as returned by \code{\link{how}}. Whilst many of these are very simple index opertations on a list, using these rather than directly accessing that list allows the internal representation of the permutation design to change without breaking code. } \usage{ getAllperms(object, ...) getBlocks(object, ...) getComplete(object, ...) getConstant(object, ...) getCol(object, ...) getDim(object, ...) getMake(object, ...) getMaxperm(object, ...) getMinperm(object, ...) getMirror(object, ...) getNperm(object, ...) getObserved(object, ...) getPlots(object, ...) getRow(object, ...) getStrata(object, ...) getType(object, ...) getWithin(object, ...) getControl(object, ...) getHow(object, ...) \method{getAllperms}{how}(object, ...) \method{getBlocks}{how}(object, ...) \method{getCol}{how}(object, which = c("plots", "within"), ...) \method{getCol}{Plots}(object, ...) \method{getCol}{Within}(object, ...) \method{getComplete}{how}(object, ...) \method{getConstant}{how}(object, ...) \method{getConstant}{Within}(object, ...) \method{getDim}{how}(object, which = c("plots", "within"), ...) \method{getDim}{Plots}(object, ...) \method{getDim}{Within}(object, ...) \method{getMake}{how}(object, ...) \method{getMaxperm}{how}(object, ...) \method{getMinperm}{how}(object, ...) \method{getMirror}{how}(object, which = c("plots", "within"), ...) \method{getMirror}{Plots}(object, ...) \method{getMirror}{Within}(object, ...) \method{getNperm}{how}(object, ...) \method{getObserved}{how}(object, ...) \method{getPlots}{how}(object, ...) \method{getRow}{how}(object, which = c("plots", "within"), ...) \method{getRow}{Plots}(object, ...) \method{getRow}{Within}(object, ...) \method{getStrata}{how}(object, which = c("plots", "blocks"), drop = TRUE, ...) \method{getStrata}{Plots}(object, drop = TRUE, ...) \method{getType}{how}(object, which = c("plots", "within"), ...) \method{getType}{Plots}(object, ...) \method{getType}{Within}(object, ...) \method{getWithin}{how}(object, ...) \method{getControl}{allPerms}(object, ...) } \arguments{ \item{object}{An R object to dispatch on.} \item{which}{character; which level of restriction to extract information for.} \item{drop}{logical; should un-used factor levels be dropped?} \item{\dots}{Arguments passed on to other methods.} } \details{ These are extractor functions for working with permutation design objects created by \code{\link{how}}. They should be used in preference to directly subsetting the permutation design in case the internal structure of object changes as \pkg{permute} is developed. \code{getHow} is an alias for \code{getControl}; specific methods are implemented for \code{getControl} if you are debugging. } \value{ These are simple extractor functions and return the contents of the corresponding components of \code{object}. } \author{Gavin Simpson} \seealso{\code{\link{check}}, a utility function for checking permutation scheme described by \code{\link{how}}. } \examples{ ## extract components from a "how" object hh <- how() getWithin(hh) getNperm(hh) } \keyword{ methods } \keyword{ utils }permute/man/shuffle.Rd0000644000176200001440000001525413537510676014455 0ustar liggesusers\name{shuffle} \alias{shuffle} \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 = how()) permute(i, n, control) } \arguments{ \item{n}{numeric; the length of the returned vector of permuted values. Usually the number of observations under consideration. May also be any object that \code{nobs} knows about; see \code{\link{nobs-methods}}.} \item{control}{a list of control values describing properties of the permutation design, as returned by a call to \code{how}.} \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. \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{shuffle} 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{permute} the \code{i}th permutation from the set of all permutations, or a random permutation from the design. } %\note{ % \code{shuffle} 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]{shuffle}} will be % updated to use \code{shuffle}. %} \references{ \code{shuffle()} is modelled after the permutation schemes of Canoco 3.1 (ter Braak, 1990); see also Besag & Clifford (1989). Besag, J. and Clifford, P. (1989) Generalized Monte Carlo significance tests. \emph{Biometrika} \strong{76}; 633--642. ter Braak, C. J. F. (1990). \emph{Update notes: CANOCO version 3.1}. Wageningen: Agricultural Mathematics Group. (UR). } \author{Gavin Simpson} \seealso{\code{\link{check}}, a utility function for checking permutation scheme described by \code{\link{how}}.} \examples{ \dontshow{suppressWarnings(RNGversion("3.5.0"))} set.seed(1234) ## unrestricted permutations shuffle(20) ## observations represent a time series of line transect CTRL <- how(within = Within(type = "series")) shuffle(20, control = CTRL) ## observations represent a time series of line transect ## but with mirroring allowed CTRL <- how(within = Within(type = "series", mirror = TRUE)) shuffle(20, control = CTRL) ## observations represent a spatial grid, 5rx4c nr <- 5 nc <- 4 CTRL <- how(within = Within(type = "grid", ncol = nc, nrow = nr)) perms <- shuffle(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 plots <- Plots(strata = gl(4, 5)) CTRL <- how(plots = plots, within = Within(type = "free")) shuffle(20, CTRL) ## as above but same random permutation within strata CTRL <- how(plots = plots, within = Within(type = "free", constant = TRUE)) shuffle(20, CTRL) ## time series within each level of block CTRL <- how(plots = plots, within = Within(type = "series")) shuffle(20, CTRL) ## as above, but with same permutation for each level CTRL <- how(plots = plots, within = Within(type = "series", constant = TRUE)) shuffle(20, CTRL) ## spatial grids within each level of block, 4 x (5r x 5c) nr <- 5 nc <- 5 nb <- 4 ## number of blocks plots <- Plots(gl(nb, 25)) CTRL <- how(plots = plots, within = Within(type = "grid", ncol = nc, nrow = nr)) shuffle(100, CTRL) ## as above, but with same permutation for each level CTRL <- how(plots = plots, within = Within(type = "grid", ncol = nc, nrow = nr, constant = TRUE)) shuffle(100, CTRL) ## permuting levels of plots instead of observations CTRL <- how(plots = Plots(gl(4, 5), type = "free"), within = Within(type = "none")) shuffle(20, CTRL) ## permuting levels of plots instead of observations ## but plots represent a time series CTRL <- how(plots = Plots(gl(4, 5), type = "series"), within = Within(type = "none")) shuffle(20, CTRL) ## permuting levels of plots but plots represent a time series ## free permutation within plots CTRL <- how(plots = Plots(gl(4, 5), type = "series"), within = Within(type = "free")) shuffle(20, CTRL) ## permuting within blocks grp <- gl(2, 10) # 2 groups of 10 samples each CTRL <- how(blocks = grp) shuffle(length(grp), control = 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 <- check(x, control)$control ## FIXME ## number of observations Nobs <- nobs(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 <- how(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 } permute/man/nobs.Rd0000644000176200001440000000254013537510676013754 0ustar liggesusers\name{nobs-methods} \alias{nobs-methods} \alias{nobs.numeric} \alias{nobs.integer} \alias{nobs.matrix} \alias{nobs.data.frame} \alias{nobs.factor} \alias{nobs.character} \title{Number of observations in a given object} \description{ \code{\link{nobs}} is a generic function to return the number of observations from a model. \code{shuffle} provides a few methods for other types of data object in R. } \usage{ \method{nobs}{numeric}(object, \dots) \method{nobs}{integer}(object, \dots) \method{nobs}{matrix}(object, \dots) \method{nobs}{data.frame}(object, \dots) \method{nobs}{character}(object, \dots) \method{nobs}{factor}(object, \dots) } \arguments{ \item{object}{a data frame or matrix, or a numeric, integer, character, or factor vector.} \item{\dots}{arguments to other methods.} } \details{ Function \code{nobs} is a simple generic function to return the number of observations in a range of R model objects. Methods are provided to work with a variety of R objects. } \value{ The (numeric) number of observations in \code{object}. } \author{Gavin Simpson} \examples{ \dontshow{suppressWarnings(RNGversion("3.5.0"))} set.seed(1) ## numeric vector len <- sample(1:10, 1) v <- as.numeric(sample(1:100, len)) len obs <- nobs(v) isTRUE(all.equal(len, obs)) ## integer len <- sample(1L:10L, 1) obs <- nobs(len) isTRUE(all.equal(len, obs)) } permute/man/shuffleSet.Rd0000644000176200001440000001312313537510676015122 0ustar liggesusers\name{shuffleSet} \alias{shuffleSet} \alias{as.matrix.permutationMatrix} \title{ Generate a set of permutations from the specified design. } \description{ \code{shuffleSet} returns a set of \code{nset} permutations from the specified design. The main purpose of the function is to circumvent the overhead of repeatedly calling \code{\link{shuffle}} to generate a set of permutations. } \usage{ shuffleSet(n, nset, control = how(), check = TRUE, quietly = FALSE) \method{as.matrix}{permutationMatrix}(x, \dots) } %- maybe also 'usage' for other objects documented here. \arguments{ \item{n}{ numeric; the number of observations in the sample set. May also be any object that \code{nobs} knows about; see \code{\link{nobs-methods}}. } \item{nset}{ numeric; the number of permutations to generate for the set. Can be missing, the default, in which case \code{nset} is determined from \code{control}. } \item{control}{ an object of class \code{"how"} describing a valid permutation design. } \item{check}{ logical; should the design be checked for various problems via function \code{\link{check}}? The default is to check the design for the stated number of observations and update \code{control} accordingly. See Details. } \item{quietly}{ logical; should messages by suppressed? } \item{x}{ an object of class \code{"permutationMatrix"}, as returned by \code{shuffleSet}. } \item{\dots}{ arguments passed to other methods. For the \code{as.matrix} method only. } } \details{ \code{shuffleSet} is designed to generate a set of \code{nset} permutation indices over which a function can iterate as part of a permutation test. It is only slightly more efficient than calling \code{\link{shuffle}} \code{nset} times, but it is far more practical than the simpler function because a set of permutations can be worked on by applying a function to the rows of the returned object. This simplifies the function applied, and facilitates the use of parallel processing functions, thus enabling a larger number of permutations to be evaluated in reasonable time. By default, \code{shuffleSet} will check the permutations design following a few simple heuristics. See \code{\link{check}} for details of these. Whether some of the heuristics are activiated or not can be controlled via \code{\link{how}}, essentialy via its argument \code{minperm}. In particular, if there are fewer than \code{minperm} permutations, \code{shuffleSet} will generate and return \strong{all possible permutations}, which may differ from the number requested via argument \code{nset}. The \code{check} argument to \code{shuffleSet} controls whether checking is performed in the permutation design. If you set \code{check = FALSE} then exactly \code{nset} permutations will be returned. However, do be aware that there is no guarantee that the set of permutations returned will be unique, especially so for designs and data sets where there are few possible permutations relative to the number requested. The \code{as.matrix} method sets the \code{control} and \code{seed} attributes to \code{NULL} and removes the \code{"permutationMatrix"} class, resulting in a standard matrix object. } \value{ Returns a matrix of permutations, where each row is a separate permutation. As such, the returned matrix has \code{nset} rows and \code{n} columns. } \author{ Gavin L. Simpson } %\note{ %% ~~further notes~~ %} %% ~Make other sections like Warning with \section{Warning }{....} ~ \references{ \code{shuffleSet()} is modelled after the permutation schemes of Canoco 3.1 (ter Braak, 1990); see also Besag & Clifford (1989). Besag, J. and Clifford, P. (1989) Generalized Monte Carlo significance tests. \emph{Biometrika} \strong{76}; 633--642. ter Braak, C. J. F. (1990). \emph{Update notes: CANOCO version 3.1}. Wageningen: Agricultural Mathematics Group. (UR). } \seealso{ See \code{\link{shuffle}} for generating a single permutation, and \code{\link{how}} for setting up permutation designs. } \examples{ \dontshow{suppressWarnings(RNGversion("3.5.0"))} set.seed(1) ## simple random permutations, 5 permutations in set shuffleSet(n = 10, nset = 5) ## series random permutations, 5 permutations in set shuffleSet(10, 5, how(within = Within(type = "series"))) ## series random permutations, 10 permutations in set, ## with possible mirroring CTRL <- how(within = Within(type = "series", mirror = TRUE)) shuffleSet(10, 10, CTRL) ## Permuting strata ## 4 groups of 5 observations CTRL <- how(within = Within(type = "none"), plots = Plots(strata = gl(4,5), type = "free")) shuffleSet(20, 10, control = CTRL) ## 10 random permutations in presence of Plot-level strata plotStrata <- Plots(strata = gl(4,5)) CTRL <- how(plots = plotStrata, within = Within(type = "free")) numPerms(20, control = CTRL) shuffleSet(20, 10, control = CTRL) ## as above but same random permutation within Plot-level strata CTRL <- how(plots = plotStrata, within = Within(type = "free", constant = TRUE)) numPerms(20, control = CTRL) shuffleSet(20, 10, CTRL) ## check this. ## time series within each level of Plot strata CTRL <- how(plots = plotStrata, within = Within(type = "series")) shuffleSet(20, 10, CTRL) ## as above, but with same permutation for each Plot-level stratum CTRL <- how(plots = plotStrata, within = Within(type = "series", constant = TRUE)) shuffleSet(20, 10, CTRL) } % Add one or more standard keywords, see file 'KEYWORDS' in the % R documentation directory. \keyword{ htest } \keyword{ design } permute/man/jackal.Rd0000644000176200001440000000206613537510676014243 0ustar liggesusers\name{jackal} \alias{jackal} \docType{data} \title{ Mandible lengths of male and female golden jackals } \description{ Mandible lengths (in mm) for male and female golden jackals (\emph{Canis aureus}) from a collection of specimens in the British Museum of Natural History, London, UK. } \usage{data(jackal)} \format{ A data frame with 20 observations on the following 2 variables. \describe{ \item{\code{Length}}{a numeric vector} \item{\code{Sex}}{a factor with levels \code{Male} \code{Female}} } } %\details{ % %} \source{ The data were manually transcribed from Manly (2007). } \references{ Higham, C.F.W., Kijngam, A., and Manly, B.F.J. (1980) An analysis of prehistoric canid remains from Thailand. \emph{Journal of Archaeological Science} \strong{7}:149-165. Manly, B.F.J. (2007) \emph{Randomization, bootstrap and Monte Carlo methods in biology. Third Edition}. Chapman \& Hall/CRC, Boca Raton. } \examples{ data(jackal) str(jackal) ## boxplot of mandible length vs sex plot(Length ~ Sex, data = jackal) } \keyword{datasets} permute/DESCRIPTION0000644000176200001440000000316614174503352013453 0ustar liggesusersPackage: permute Title: Functions for Generating Restricted Permutations of Data Version: 0.9-7 Date: 2022-01-27 Authors@R: c(person(given = "Gavin L.", family = "Simpson", email = "ucfagls@gmail.com", role = c("aut", "cph", "cre"), comment = c(ORCID = "0000-0002-9084-8413")), person(given = "R Core Team", role = "cph"), person(given = "Douglas M.", family = "Bates", role = "ctb"), person(given = "Jari", family = "Oksanen", role = "ctb")) Depends: R (>= 2.14.0) Imports: stats Suggests: vegan (>= 2.0-0), testthat (>= 0.5), parallel, knitr, rmarkdown, bookdown, sessioninfo Description: A set of restricted permutation designs for freely exchangeable, line transects (time series), and spatial grid designs plus permutation of blocks (groups of samples) is provided. 'permute' also allows split-plot designs, in which the whole-plots or split-plots or both can be freely-exchangeable or one of the restricted designs. The 'permute' package is modelled after the permutation schemes of 'Canoco 3.1' (and later) by Cajo ter Braak. License: GPL-2 ByteCompile: true URL: https://github.com/gavinsimpson/permute BugReports: https://github.com/gavinsimpson/permute/issues Copyright: see file COPYRIGHTS VignetteBuilder: knitr NeedsCompilation: no Packaged: 2022-01-27 11:11:56 UTC; au690221 Author: Gavin L. Simpson [aut, cph, cre] (), R Core Team [cph], Douglas M. Bates [ctb], Jari Oksanen [ctb] Maintainer: Gavin L. Simpson Repository: CRAN Date/Publication: 2022-01-27 11:50:02 UTC permute/build/0000755000176200001440000000000014174476773013056 5ustar liggesuserspermute/build/vignette.rds0000644000176200001440000000034414174476773015416 0ustar liggesusers‹mQÍ‚0 ?¢h4¼OÀÁ«WÂÅ‹!¼.Pa‘¿l#Ä›O.Ó&íºõûÚ¯ÙÅ!„èÄÔt¢˜. }‹®“Ø}^/I%«JáGE¢Ô÷ÉY,!ñ¦Ðƒ×V¦žÌ`xÇ“Æ7š‚Òa7›É"W›¹Ô쾫š©OЋ’ ”Ëj(“þùùŸ¯u eu„{[ñ‘3ÃXÆ YãÜ3“ß‹q Â!Õ"UÃOÿ™~›W­?î°îè¡CSs*ÔE„Jê_9ò{Ý/1&Jäãpermute/tests/0000755000176200001440000000000013537510676013112 5ustar liggesuserspermute/tests/Examples/0000755000176200001440000000000013537510676014670 5ustar liggesuserspermute/tests/Examples/permute-Ex.Rout.save0000644000176200001440000012115014174476666020543 0ustar liggesusers R version 4.1.2 (2021-11-01) -- "Bird Hippie" Copyright (C) 2021 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > pkgname <- "permute" > source(file.path(R.home("share"), "R", "examples-header.R")) > options(warn = 1) > library('permute') > > base::assign(".oldSearch", base::search(), pos = 'CheckExEnv') > base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv') > cleanEx() > nameEx("allPerms") > ### * allPerms > > flush(stderr()); flush(stdout()) > > ### Name: allPerms > ### Title: Complete enumeration of all possible permutations > ### Aliases: allPerms print.allPerms summary.allPerms > ### print.summary.allPerms as.matrix.allPerms as.allPerms > > ### ** Examples > > ## allPerms can work with a vector > vec <- c(3,4,5) > allPerms(vec) ## free permutation [,1] [,2] [,3] [1,] 1 3 2 [2,] 2 1 3 [3,] 2 3 1 [4,] 3 1 2 [5,] 3 2 1 > > ## enumerate all possible permutations for a more complicated > ## design > fac <- gl(2,6) > ctrl <- how(within = Within(type = "grid", mirror = FALSE, + constant = TRUE, nrow = 3, ncol = 2), + plots = Plots(strata = fac)) > Nobs <- length(fac) > numPerms(seq_len(Nobs), control = ctrl) ## 6 [1] 6 > (tmp <- allPerms(Nobs, control = update(ctrl, observed = TRUE))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 [6,] 1 2 3 4 5 6 7 8 9 10 11 12 > (tmp2 <- allPerms(Nobs, control = ctrl)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 > > ## turn on mirroring > ##ctrl$within$mirror <- TRUE > ctrl <- update(ctrl, within = update(getWithin(ctrl), mirror = TRUE)) > numPerms(seq_len(Nobs), control = ctrl) [1] 12 > (tmp3 <- allPerms(Nobs, control = update(ctrl, observed = TRUE))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 [6,] 1 2 3 4 5 6 7 8 9 10 11 12 [7,] 1 6 5 4 3 2 7 12 11 10 9 8 [8,] 2 1 6 5 4 3 8 7 12 11 10 9 [9,] 3 2 1 6 5 4 9 8 7 12 11 10 [10,] 4 3 2 1 6 5 10 9 8 7 12 11 [11,] 5 4 3 2 1 6 11 10 9 8 7 12 [12,] 6 5 4 3 2 1 12 11 10 9 8 7 > (tmp4 <- allPerms(Nobs, control = ctrl)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 [6,] 1 6 5 4 3 2 7 12 11 10 9 8 [7,] 2 1 6 5 4 3 8 7 12 11 10 9 [8,] 3 2 1 6 5 4 9 8 7 12 11 10 [9,] 4 3 2 1 6 5 10 9 8 7 12 11 [10,] 5 4 3 2 1 6 11 10 9 8 7 12 [11,] 6 5 4 3 2 1 12 11 10 9 8 7 > > ## prints out details of the permutation scheme as > ## well as the matrix of permutations > summary(tmp3) Complete enumeration of permutations Permutation Design: Blocks: Defined by: none Plots: Plots: fac Permutation type: none Mirrored?: No Within Plots: Permutation type: grid Mirrored?: Yes Different permutation within each Plot?: No Grid dimensions: 3 rows 2 cols Permutation details: Number of permutations: 199 Max. number of permutations allowed: 9999 Evaluate all permutations?: No. Activation limit: 5040 All permutations: Contains observed ordering?: Yes [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 [6,] 1 2 3 4 5 6 7 8 9 10 11 12 [7,] 1 6 5 4 3 2 7 12 11 10 9 8 [8,] 2 1 6 5 4 3 8 7 12 11 10 9 [9,] 3 2 1 6 5 4 9 8 7 12 11 10 [10,] 4 3 2 1 6 5 10 9 8 7 12 11 [11,] 5 4 3 2 1 6 11 10 9 8 7 12 [12,] 6 5 4 3 2 1 12 11 10 9 8 7 > summary(tmp4) Complete enumeration of permutations Permutation Design: Blocks: Defined by: none Plots: Plots: fac Permutation type: none Mirrored?: No Within Plots: Permutation type: grid Mirrored?: Yes Different permutation within each Plot?: No Grid dimensions: 3 rows 2 cols Permutation details: Number of permutations: 198 Max. number of permutations allowed: 9999 Evaluate all permutations?: No. Activation limit: 5040 All permutations: Contains observed ordering?: No [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 [6,] 1 6 5 4 3 2 7 12 11 10 9 8 [7,] 2 1 6 5 4 3 8 7 12 11 10 9 [8,] 3 2 1 6 5 4 9 8 7 12 11 10 [9,] 4 3 2 1 6 5 10 9 8 7 12 11 [10,] 5 4 3 2 1 6 11 10 9 8 7 12 [11,] 6 5 4 3 2 1 12 11 10 9 8 7 > > > > cleanEx() > nameEx("check") > ### * check > > flush(stderr()); flush(stdout()) > > ### Name: check > ### Title: Utility functions for permutation schemes > ### Aliases: check print.check print.summary.check summary.check > ### Keywords: utilities design methods > > ### ** Examples > > ## only run this example if vegan is available > if (suppressPackageStartupMessages(require("vegan"))) { + ## use example data from ?pyrifos in package vegan + example(pyrifos) + + ## Demonstrate the maximum number of permutations for the pyrifos data + ## under a series of permutation schemes + + ## no restrictions - lots of perms + CONTROL <- how(within = Within(type = "free")) + (check1 <- check(pyrifos, CONTROL)) + ## summary(check1) + + ## no strata but data are series with no mirroring, so 132 permutations + CONTROL <- how(within = Within(type = "series", mirror = FALSE)) + check(pyrifos, CONTROL) + + ## no strata but data are series with mirroring, so 264 permutations + CONTROL <- how(within = Within(type = "series", mirror = TRUE)) + check(pyrifos, control = CONTROL) + + ## unrestricted within strata + check(pyrifos, control = how(plots = Plots(strata = ditch), + within = Within(type = "free"))) + + ## time series within strata, no mirroring + check(pyrifos, + control = how(plots = Plots(strata = ditch), + within = Within(type = "series", mirror = FALSE))) + + ## time series within strata, with mirroring + check(pyrifos, + control = how(plots = Plots(strata = ditch), + within = Within(type = "series", mirror = TRUE))) + + ## time series within strata, no mirroring, same permutation + ## within strata + check(pyrifos, + control = how(plots = Plots(strata = ditch), + within = Within(type = "series", constant = TRUE))) + + ## time series within strata, with mirroring, same permutation + ## within strata + check(pyrifos, + control = how(plots = Plots(strata = ditch), + within = Within(type = "series", mirror = TRUE, + constant = TRUE))) + ## permute strata + check(pyrifos, how(plots = Plots(strata = ditch, type = "free"), + within = Within(type = "none"))) + } pyrifs> data(pyrifos) pyrifs> ditch <- gl(12, 1, length=132) pyrifs> week <- gl(11, 12, labels=c(-4, -1, 0.1, 1, 2, 4, 8, 12, 15, 19, 24)) pyrifs> dose <- factor(rep(c(0.1, 0, 0, 0.9, 0, 44, 6, 0.1, 44, 0.9, 0, 6), 11)) 'nperm' >= set of all permutations: complete enumeration. Set of permutations < 'minperm'. Generating entire set. Set of permutations < 'minperm'. Generating entire set. 'nperm' >= set of all permutations: complete enumeration. Set of permutations < 'minperm'. Generating entire set. 'nperm' >= set of all permutations: complete enumeration. Set of permutations < 'minperm'. Generating entire set. [1] 479001600 > > ## this should also also for arbitrary vectors > vec1 <- check(1:100) > vec2 <- check(1:100, how()) > all.equal(vec1, vec2) [1] TRUE > vec3 <- check(1:100, how(within = Within(type = "series"))) 'nperm' >= set of all permutations: complete enumeration. Set of permutations < 'minperm'. Generating entire set. > all.equal(100, vec3$n) [1] TRUE > vec4 <- check(1:100, how(within = Within(type= "series", mirror = TRUE))) 'nperm' >= set of all permutations: complete enumeration. Set of permutations < 'minperm'. Generating entire set. > all.equal(vec4$n, 200) [1] TRUE > > ## enumerate all possible permutations > fac <- gl(2,6) > ctrl <- how(plots = Plots(strata = fac), + within = Within(type = "grid", mirror = FALSE, + constant = TRUE, nrow = 3, ncol = 2)) > check(1:12, ctrl) 'nperm' >= set of all permutations: complete enumeration. Set of permutations < 'minperm'. Generating entire set. [1] 6 > > numPerms(1:12, control = ctrl) [1] 6 > (tmp <- allPerms(12, control = update(ctrl, observed = TRUE))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 [6,] 1 2 3 4 5 6 7 8 9 10 11 12 > (tmp2 <- allPerms(12, control = ctrl)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 > > ## turn on mirroring > ctrl <- update(ctrl, within = update(getWithin(ctrl), mirror = TRUE)) > numPerms(1:12, control = ctrl) [1] 12 > (tmp3 <- allPerms(12, control = update(ctrl, observed = TRUE))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 [6,] 1 2 3 4 5 6 7 8 9 10 11 12 [7,] 1 6 5 4 3 2 7 12 11 10 9 8 [8,] 2 1 6 5 4 3 8 7 12 11 10 9 [9,] 3 2 1 6 5 4 9 8 7 12 11 10 [10,] 4 3 2 1 6 5 10 9 8 7 12 11 [11,] 5 4 3 2 1 6 11 10 9 8 7 12 [12,] 6 5 4 3 2 1 12 11 10 9 8 7 > (tmp4 <- allPerms(12, control = ctrl)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 [6,] 1 6 5 4 3 2 7 12 11 10 9 8 [7,] 2 1 6 5 4 3 8 7 12 11 10 9 [8,] 3 2 1 6 5 4 9 8 7 12 11 10 [9,] 4 3 2 1 6 5 10 9 8 7 12 11 [10,] 5 4 3 2 1 6 11 10 9 8 7 12 [11,] 6 5 4 3 2 1 12 11 10 9 8 7 > ## prints out details of the permutation scheme as > ## well as the matrix of permutations > summary(tmp) Complete enumeration of permutations Permutation Design: Blocks: Defined by: none Plots: Plots: fac Permutation type: none Mirrored?: No Within Plots: Permutation type: grid Mirrored?: No Different permutation within each Plot?: No Grid dimensions: 3 rows 2 cols Permutation details: Number of permutations: 199 Max. number of permutations allowed: 9999 Evaluate all permutations?: No. Activation limit: 5040 All permutations: Contains observed ordering?: Yes [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 [6,] 1 2 3 4 5 6 7 8 9 10 11 12 > summary(tmp2) Complete enumeration of permutations Permutation Design: Blocks: Defined by: none Plots: Plots: fac Permutation type: none Mirrored?: No Within Plots: Permutation type: grid Mirrored?: No Different permutation within each Plot?: No Grid dimensions: 3 rows 2 cols Permutation details: Number of permutations: 198 Max. number of permutations allowed: 9999 Evaluate all permutations?: No. Activation limit: 5040 All permutations: Contains observed ordering?: No [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 2 3 4 5 6 1 8 9 10 11 12 7 [2,] 3 4 5 6 1 2 9 10 11 12 7 8 [3,] 4 5 6 1 2 3 10 11 12 7 8 9 [4,] 5 6 1 2 3 4 11 12 7 8 9 10 [5,] 6 1 2 3 4 5 12 7 8 9 10 11 > > ## different numbers of observations per level of strata > fac <- factor(rep(1:3, times = c(3,2,2))) > ## free permutations in levels of strata > numPerms(7, how(within = Within(type = "free"), + plots = Plots(strata = fac, type = "none"))) [1] 24 > allPerms(7, how(within = Within(type = "free"), + plots = Plots(strata = fac))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 2 3 4 5 7 6 [2,] 1 2 3 5 4 6 7 [3,] 1 2 3 5 4 7 6 [4,] 1 3 2 4 5 6 7 [5,] 1 3 2 4 5 7 6 [6,] 1 3 2 5 4 6 7 [7,] 1 3 2 5 4 7 6 [8,] 2 1 3 4 5 6 7 [9,] 2 1 3 4 5 7 6 [10,] 2 1 3 5 4 6 7 [11,] 2 1 3 5 4 7 6 [12,] 2 3 1 4 5 6 7 [13,] 2 3 1 4 5 7 6 [14,] 2 3 1 5 4 6 7 [15,] 2 3 1 5 4 7 6 [16,] 3 1 2 4 5 6 7 [17,] 3 1 2 4 5 7 6 [18,] 3 1 2 5 4 6 7 [19,] 3 1 2 5 4 7 6 [20,] 3 2 1 4 5 6 7 [21,] 3 2 1 4 5 7 6 [22,] 3 2 1 5 4 6 7 [23,] 3 2 1 5 4 7 6 > ## series permutations in levels of strata > ctrl <- how(within = Within(type = "series"), plots = Plots(strata = fac)) > numPerms(7, control = ctrl) [1] 12 > allPerms(7, control = ctrl) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 2 3 1 5 4 7 6 [2,] 2 3 1 5 4 6 7 [3,] 2 3 1 4 5 7 6 [4,] 2 3 1 4 5 6 7 [5,] 3 1 2 5 4 7 6 [6,] 3 1 2 5 4 6 7 [7,] 3 1 2 4 5 7 6 [8,] 3 1 2 4 5 6 7 [9,] 1 2 3 5 4 7 6 [10,] 1 2 3 5 4 6 7 [11,] 1 2 3 4 5 7 6 > > > > cleanEx() detaching ‘package:vegan’, ‘package:lattice’ > nameEx("get-methods") > ### * get-methods > > flush(stderr()); flush(stdout()) > > ### Name: get-methods > ### Title: Extractor functions to access components of a permutation design > ### Aliases: get-methods getBlocks getBlocks.default getBlocks.how > ### getBlocks.permControl getWithin getWithin.default getWithin.how > ### getWithin.permControl getStrata getStrata.default getStrata.how > ### getStrata.permControl getStrata.Plots getType getType.default > ### getType.how getType.permControl getType.Plots getType.Within > ### getMirror getMirror.default getMirror.how getMirror.permControl > ### getMirror.Plots getMirror.Within getConstant getConstant.default > ### getConstant.how getConstant.permControl getConstant.Within getPlots > ### getPlots.default getPlots.how getPlots.permControl getRow > ### getRow.default getRow.how getRow.permControl getRow.Plots > ### getRow.Within getCol getCol.default getCol.how getCol.permControl > ### getCol.Plots getCol.Within getDim getDim.default getDim.how > ### getDim.permControl getDim.Plots getDim.Within getNperm > ### getNperm.default getNperm.how getNperm.permControl getMaxperm > ### getMaxperm.default getMaxperm.how getMaxperm.permControl getMinperm > ### getMinperm.default getMinperm.how getMinperm.permControl getComplete > ### getComplete.default getComplete.how getComplete.permControl getMake > ### getMake.default getMake.how getObserved getObserved.default > ### getObserved.how getAllperms getAllperms.default getAllperms.how > ### getControl getHow getControl.default getControl.allPerms > ### Keywords: methods utils > > ### ** Examples > > ## extract components from a "how" object > hh <- how() > getWithin(hh) $type [1] "free" $constant [1] FALSE $mirror [1] FALSE $ncol NULL $nrow NULL $call Within() attr(,"class") [1] "Within" > getNperm(hh) [1] 199 > > > > cleanEx() > nameEx("how") > ### * how > > flush(stderr()); flush(stdout()) > > ### Name: how > ### Title: How to define a permutation design? > ### Aliases: how print.how Blocks Within Plots > ### Keywords: utils > > ### ** Examples > > ## Set up factors for the Plots and Blocks > plts <- gl(4, 10) ## 4 Plots of 10 samples each > blks <- gl(2, 20) ## 2 Blocks of 20 samples each > > ## permutation design > h1 <- how(within = Within(type = "series", mirror = TRUE), + plots = Plots(strata = plts, type = "series"), + blocks = blks) > > ## The design can be updated... > ## ... remove the blocking: > update(h1, blocks = NULL) Permutation Design: Blocks: Defined by: none Plots: Plots: plts Permutation type: series Mirrored?: No Within Plots: Permutation type: series Mirrored?: Yes Different permutation within each Plot?: Yes Permutation details: Number of permutations: 199 Max. number of permutations allowed: 9999 Evaluate all permutations?: No. Activation limit: 5040 > > ## ... or switch the type of shuffling at a level: > #update(h1, plots = update(getPlots(h1), type = "none")) > plots2 <- update(getPlots(h1), type = "none") > update(h1, plots = plots2) Permutation Design: Blocks: Blocks: blks Plots: Plots: plts Permutation type: none Mirrored?: No Within Plots: Permutation type: series Mirrored?: Yes Different permutation within each Plot?: Yes Permutation details: Number of permutations: 199 Max. number of permutations allowed: 9999 Evaluate all permutations?: No. Activation limit: 5040 > > > > cleanEx() > nameEx("jackal") > ### * jackal > > flush(stderr()); flush(stdout()) > > ### Name: jackal > ### Title: Mandible lengths of male and female golden jackals > ### Aliases: jackal > ### Keywords: datasets > > ### ** Examples > > data(jackal) > str(jackal) 'data.frame': 20 obs. of 2 variables: $ Length: num 120 107 110 116 114 111 113 117 114 112 ... $ Sex : Factor w/ 2 levels "Male","Female": 1 1 1 1 1 1 1 1 1 1 ... > > ## boxplot of mandible length vs sex > plot(Length ~ Sex, data = jackal) > > > > cleanEx() > nameEx("nobs") > ### * nobs > > flush(stderr()); flush(stdout()) > > ### Name: nobs-methods > ### Title: Number of observations in a given object > ### Aliases: nobs-methods nobs.numeric nobs.integer nobs.matrix > ### nobs.data.frame nobs.factor nobs.character > > ### ** Examples > > ## Don't show: > suppressWarnings(RNGversion("3.5.0")) > ## End(Don't show) > set.seed(1) > ## numeric vector > len <- sample(1:10, 1) > v <- as.numeric(sample(1:100, len)) > len [1] 3 > obs <- nobs(v) > isTRUE(all.equal(len, obs)) [1] TRUE > > ## integer > len <- sample(1L:10L, 1) > obs <- nobs(len) > isTRUE(all.equal(len, obs)) [1] FALSE > > > > > cleanEx() > nameEx("numPerms") > ### * numPerms > > flush(stderr()); flush(stdout()) > > ### Name: numPerms > ### Title: Number of possible permutations for a given object > ### Aliases: numPerms > > ### ** Examples > > ## permutation design --- see ?how > ctrl <- how() ## defaults to freely exchangeable > > ## vector input > v <- 1:10 > (obs <- nobs(v)) [1] 10 > numPerms(v, control = ctrl) [1] 3628800 > > ## integer input > len <- length(v) > (obs <- nobs(len)) [1] 1 > numPerms(len, control = ctrl) [1] 3628800 > > ## new design, objects are a time series > ctrl <- how(within = Within(type = "series")) > numPerms(v, control = ctrl) [1] 10 > ## number of permutations possible drastically reduced... > ## ...turn on mirroring > ctrl <- how(within = Within(type = "series", mirror = TRUE)) > numPerms(v, control = ctrl) [1] 20 > > ## Try blocking --- 2 groups of 5 > bl <- numPerms(v, control = how(blocks = gl(2,5))) > bl [1] 14400 > > ## should be same as > pl <- numPerms(v, control = how(plots = Plots(strata = gl(2,5)))) > pl [1] 14400 > stopifnot(all.equal(bl, pl)) > > > > cleanEx() > nameEx("set-methods") > ### * set-methods > > flush(stderr()); flush(stdout()) > > ### Name: set-methods > ### Title: Replacement functions to set components of a permutation design > ### Aliases: set-methods setBlocks<- setBlocks<-.default setBlocks<-.how > ### setBlocks<-.permControl setWithin<- setWithin<-.default > ### setWithin<-.how setStrata<- setStrata<-.default setStrata<-.how > ### setStrata<-.Plots setType<- setType<-.default setType<-.how > ### setType<-.Plots setType<-.Within setMirror<- setMirror<-.default > ### setMirror<-.how setMirror<-.Plots setMirror<-.Within setConstant<- > ### setConstant<-.default setConstant<-.how setConstant<-.Plots > ### setConstant<-.Within setPlots<- setPlots<-.default setPlots<-.how > ### setRow<- setRow<-.default setRow<-.how setRow<-.Plots setRow<-.Within > ### setCol<- setCol<-.default setCol<-.how setCol<-.Plots setCol<-.Within > ### setDim<- setDim<-.default setDim<-.how setDim<-.Plots setDim<-.Within > ### setNperm<- setNperm<-.default setNperm<-.how setNperm<-.permControl > ### setAllperms<- setAllperms<-.default setAllperms<-.how > ### setAllperms<-.permControl setMaxperm<- setMaxperm<-.default > ### setMaxperm<-.how setMaxperm<-.permControl setMinperm<- > ### setMinperm<-.default setMinperm<-.how setMinperm<-.permControl > ### setComplete<- setComplete<-.default setComplete<-.how > ### setComplete<-.permControl setMake<- setMake<-.default setMake<-.how > ### setObserved<- setObserved<-.default setObserved<-.how > ### Keywords: methods utils > > ### ** Examples > > ## extract components from a "how" object > hh <- how() > getNperm(hh) [1] 199 > setNperm(hh) <- 999 > getNperm(hh) [1] 999 > > > > cleanEx() > nameEx("shuffle-utils") > ### * shuffle-utils > > flush(stderr()); flush(stdout()) > > ### Name: shuffle-utils > ### Title: Utility functions for unrestricted and restricted permutations > ### Aliases: shuffle-utils shuffleFree shuffleGrid shuffleSeries > ### shuffleStrata > ### Keywords: htest design > > ### ** Examples > > ## Don't show: > suppressWarnings(RNGversion("3.5.0")) > ## End(Don't show) > set.seed(3) > > ## draw 1 value at random from the set 1:10 > shuffleFree(1:10, 1) [1] 2 > > ## permute the series 1:10 > x <- 1:10 > shuffleSeries(x) ## with random starting point [1] 10 1 2 3 4 5 6 7 8 9 > shuffleSeries(x, start = 5L) ## known starting point [1] 6 7 8 9 10 1 2 3 4 5 > shuffleSeries(x, flip = TRUE) ## random start, forced mirror [1] 5 6 7 8 9 10 1 2 3 4 > shuffleSeries(x, mirror = TRUE) ## random start, possibly mirror [1] 5 6 7 8 9 10 1 2 3 4 > > ## permute a grid of size 3x3 > shuffleGrid(3, 3) ## random starting row/col [1] 6 4 5 9 7 8 3 1 2 > shuffleGrid(3, 3, start.row = 2, + start.col = 3) ## with known row/col [1] 3 1 2 6 4 5 9 7 8 > shuffleGrid(3, 3, flip = rep(TRUE, 2)) ## random start, forced mirror [1] 8 9 7 2 3 1 5 6 4 > > > > cleanEx() > nameEx("shuffle") > ### * shuffle > > flush(stderr()); flush(stdout()) > > ### Name: shuffle > ### Title: Unrestricted and restricted permutations > ### Aliases: shuffle permute > ### Keywords: htest design > > ### ** Examples > > ## Don't show: > suppressWarnings(RNGversion("3.5.0")) > ## End(Don't show) > set.seed(1234) > > ## unrestricted permutations > shuffle(20) [1] 3 12 11 18 14 10 1 4 8 6 7 5 20 15 2 9 17 16 19 13 > > ## observations represent a time series of line transect > CTRL <- how(within = Within(type = "series")) > shuffle(20, control = CTRL) [1] 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 > > ## observations represent a time series of line transect > ## but with mirroring allowed > CTRL <- how(within = Within(type = "series", mirror = TRUE)) > shuffle(20, control = CTRL) [1] 7 6 5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 > > ## observations represent a spatial grid, 5rx4c > nr <- 5 > nc <- 4 > CTRL <- how(within = Within(type = "grid", ncol = nc, nrow = nr)) > perms <- shuffle(20, control = CTRL) > ## view the permutation as a grid > matrix(matrix(1:20, nrow = nr, ncol = nc)[perms], + ncol = nc, nrow = nr) [,1] [,2] [,3] [,4] [1,] 7 12 17 2 [2,] 8 13 18 3 [3,] 9 14 19 4 [4,] 10 15 20 5 [5,] 6 11 16 1 > > ## random permutations in presence of strata > plots <- Plots(strata = gl(4, 5)) > CTRL <- how(plots = plots, within = Within(type = "free")) > shuffle(20, CTRL) [1] 5 3 4 2 1 8 7 6 9 10 14 11 15 12 13 18 20 16 17 19 > ## as above but same random permutation within strata > CTRL <- how(plots = plots, within = Within(type = "free", + constant = TRUE)) > shuffle(20, CTRL) [1] 3 5 2 1 4 8 10 7 6 9 13 15 12 11 14 18 20 17 16 19 > > ## time series within each level of block > CTRL <- how(plots = plots, within = Within(type = "series")) > shuffle(20, CTRL) [1] 2 3 4 5 1 8 9 10 6 7 15 11 12 13 14 19 20 16 17 18 > ## as above, but with same permutation for each level > CTRL <- how(plots = plots, within = Within(type = "series", + constant = TRUE)) > shuffle(20, CTRL) [1] 2 3 4 5 1 7 8 9 10 6 12 13 14 15 11 17 18 19 20 16 > > ## spatial grids within each level of block, 4 x (5r x 5c) > nr <- 5 > nc <- 5 > nb <- 4 ## number of blocks > plots <- Plots(gl(nb, 25)) > CTRL <- how(plots = plots, + within = Within(type = "grid", ncol = nc, nrow = nr)) > shuffle(100, CTRL) [1] 24 25 21 22 23 4 5 1 2 3 9 10 6 7 8 14 15 11 [19] 12 13 19 20 16 17 18 27 28 29 30 26 32 33 34 35 31 37 [37] 38 39 40 36 42 43 44 45 41 47 48 49 50 46 56 57 58 59 [55] 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 51 52 [73] 53 54 55 83 84 85 81 82 88 89 90 86 87 93 94 95 91 92 [91] 98 99 100 96 97 78 79 80 76 77 > ## as above, but with same permutation for each level > CTRL <- how(plots = plots, + within = Within(type = "grid", ncol = nc, nrow = nr, + constant = TRUE)) > shuffle(100, CTRL) [1] 23 24 25 21 22 3 4 5 1 2 8 9 10 6 7 13 14 15 [19] 11 12 18 19 20 16 17 48 49 50 46 47 28 29 30 26 27 33 [37] 34 35 31 32 38 39 40 36 37 43 44 45 41 42 73 74 75 71 [55] 72 53 54 55 51 52 58 59 60 56 57 63 64 65 61 62 68 69 [73] 70 66 67 98 99 100 96 97 78 79 80 76 77 83 84 85 81 82 [91] 88 89 90 86 87 93 94 95 91 92 > > ## permuting levels of plots instead of observations > CTRL <- how(plots = Plots(gl(4, 5), type = "free"), + within = Within(type = "none")) > shuffle(20, CTRL) [1] 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 16 17 18 19 20 > ## permuting levels of plots instead of observations > ## but plots represent a time series > CTRL <- how(plots = Plots(gl(4, 5), type = "series"), + within = Within(type = "none")) > shuffle(20, CTRL) [1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 > > ## permuting levels of plots but plots represent a time series > ## free permutation within plots > CTRL <- how(plots = Plots(gl(4, 5), type = "series"), + within = Within(type = "free")) > shuffle(20, CTRL) [1] 12 11 13 14 15 16 20 19 17 18 1 2 3 4 5 6 8 10 7 9 > > ## permuting within blocks > grp <- gl(2, 10) # 2 groups of 10 samples each > CTRL <- how(blocks = grp) > shuffle(length(grp), control = CTRL) [1] 1 3 6 10 4 2 7 8 9 5 12 14 11 15 18 17 16 19 20 13 > > ## 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 <- check(x, control)$control ## FIXME + ## number of observations + Nobs <- nobs(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 <- how(nperm = 999, within = Within(type = "free")) > ## perform permutation t test > perm.val <- pt.test(dat, grp, control) > perm.val $t.stat [1] -2.342064 $pval [1] 0.024 > > ## compare perm.val with the p-value from t.test() > t.test(dat ~ grp, var.equal = TRUE) Two Sample t-test data: dat by grp t = -2.3421, df = 38, p-value = 0.02452 alternative hypothesis: true difference in means between group Group 1 and group Group 2 is not equal to 0 95 percent confidence interval: -1.25582408 -0.09136416 sample estimates: mean in group Group 1 mean in group Group 2 8.749336 9.422930 > > > > cleanEx() > nameEx("shuffleSet") > ### * shuffleSet > > flush(stderr()); flush(stdout()) > > ### Name: shuffleSet > ### Title: Generate a set of permutations from the specified design. > ### Aliases: shuffleSet as.matrix.permutationMatrix > ### Keywords: htest design > > ### ** Examples > > ## Don't show: > suppressWarnings(RNGversion("3.5.0")) > ## End(Don't show) > set.seed(1) > ## simple random permutations, 5 permutations in set > shuffleSet(n = 10, nset = 5) No. of Permutations: 5 No. of Samples: 10 (Randomised) 1 2 3 4 5 6 7 8 9 10 p1 3 4 5 7 2 8 9 6 10 1 p2 3 2 6 10 5 7 8 4 1 9 p3 10 2 6 1 9 8 7 5 3 4 p4 5 6 4 2 10 8 9 1 7 3 p5 9 6 7 4 8 10 1 2 3 5 > > ## series random permutations, 5 permutations in set > shuffleSet(10, 5, how(within = Within(type = "series"))) Set of permutations < 'minperm'. Generating entire set. No. of Permutations: 5 No. of Samples: 10 (Sequence) 1 2 3 4 5 6 7 8 9 10 p1 6 7 8 9 10 1 2 3 4 5 p2 8 9 10 1 2 3 4 5 6 7 p3 5 6 7 8 9 10 1 2 3 4 p4 3 4 5 6 7 8 9 10 1 2 p5 2 3 4 5 6 7 8 9 10 1 > > ## series random permutations, 10 permutations in set, > ## with possible mirroring > CTRL <- how(within = Within(type = "series", mirror = TRUE)) > shuffleSet(10, 10, CTRL) Set of permutations < 'minperm'. Generating entire set. No. of Permutations: 10 No. of Samples: 10 (Sequence; mirrored) 1 2 3 4 5 6 7 8 9 10 p1 3 4 5 6 7 8 9 10 1 2 p2 7 8 9 10 1 2 3 4 5 6 p3 10 1 2 3 4 5 6 7 8 9 p4 2 1 10 9 8 7 6 5 4 3 p5 8 9 10 1 2 3 4 5 6 7 p6 4 3 2 1 10 9 8 7 6 5 p7 5 6 7 8 9 10 1 2 3 4 p8 9 8 7 6 5 4 3 2 1 10 p9 5 4 3 2 1 10 9 8 7 6 p10 6 5 4 3 2 1 10 9 8 7 > > ## Permuting strata > ## 4 groups of 5 observations > CTRL <- how(within = Within(type = "none"), + plots = Plots(strata = gl(4,5), type = "free")) > shuffleSet(20, 10, control = CTRL) Set of permutations < 'minperm'. Generating entire set. No. of Permutations: 10 No. of Samples: 20 (Nested in: plots; ) Restricted by Plots: gl(4, 5) (4 plots; Randomised) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 p1 6 7 8 9 10 1 2 3 4 5 11 12 13 14 15 16 17 18 19 20 p2 6 7 8 9 10 16 17 18 19 20 11 12 13 14 15 1 2 3 4 5 p3 11 12 13 14 15 16 17 18 19 20 6 7 8 9 10 1 2 3 4 5 p4 1 2 3 4 5 11 12 13 14 15 6 7 8 9 10 16 17 18 19 20 p5 16 17 18 19 20 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 p6 6 7 8 9 10 1 2 3 4 5 16 17 18 19 20 11 12 13 14 15 p7 11 12 13 14 15 6 7 8 9 10 16 17 18 19 20 1 2 3 4 5 p8 16 17 18 19 20 11 12 13 14 15 6 7 8 9 10 1 2 3 4 5 p9 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 p10 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 > > ## 10 random permutations in presence of Plot-level strata > plotStrata <- Plots(strata = gl(4,5)) > CTRL <- how(plots = plotStrata, + within = Within(type = "free")) > numPerms(20, control = CTRL) [1] 207360000 > shuffleSet(20, 10, control = CTRL) No. of Permutations: 10 No. of Samples: 20 (Nested in: plots; Randomised) Restricted by Plots: gl(4, 5) (4 plots) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 p1 5 4 2 3 1 8 10 7 6 9 12 13 11 14 15 17 16 20 18 19 p2 4 2 5 3 1 9 7 6 10 8 12 11 15 13 14 20 18 17 16 19 p3 1 3 5 4 2 10 7 9 6 8 13 15 11 14 12 18 16 19 17 20 p4 3 5 2 4 1 9 8 6 10 7 13 11 15 14 12 19 18 16 20 17 p5 1 3 5 4 2 7 9 10 8 6 13 11 12 15 14 19 20 18 16 17 p6 5 3 4 2 1 7 6 8 10 9 11 14 13 12 15 19 17 16 20 18 p7 3 1 5 4 2 7 6 10 9 8 13 15 11 14 12 20 16 18 17 19 p8 2 3 4 5 1 7 6 9 10 8 13 12 11 15 14 17 16 20 18 19 p9 4 3 2 1 5 7 8 9 6 10 12 15 11 13 14 19 20 17 18 16 p10 1 5 2 4 3 8 7 6 9 10 11 15 13 12 14 16 20 17 19 18 > ## as above but same random permutation within Plot-level strata > CTRL <- how(plots = plotStrata, + within = Within(type = "free", constant = TRUE)) > numPerms(20, control = CTRL) [1] 120 > shuffleSet(20, 10, CTRL) ## check this. Set of permutations < 'minperm'. Generating entire set. No. of Permutations: 10 No. of Samples: 20 (Nested in: plots; Randomised; same permutation in each plot) Restricted by Plots: gl(4, 5) (4 plots) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 p1 1 5 4 3 2 6 10 9 8 7 11 15 14 13 12 16 20 19 18 17 p2 2 3 1 5 4 7 8 6 10 9 12 13 11 15 14 17 18 16 20 19 p3 1 5 4 2 3 6 10 9 7 8 11 15 14 12 13 16 20 19 17 18 p4 3 2 4 1 5 8 7 9 6 10 13 12 14 11 15 18 17 19 16 20 p5 4 3 5 2 1 9 8 10 7 6 14 13 15 12 11 19 18 20 17 16 p6 1 2 5 3 4 6 7 10 8 9 11 12 15 13 14 16 17 20 18 19 p7 3 4 1 2 5 8 9 6 7 10 13 14 11 12 15 18 19 16 17 20 p8 5 1 3 4 2 10 6 8 9 7 15 11 13 14 12 20 16 18 19 17 p9 2 5 1 3 4 7 10 6 8 9 12 15 11 13 14 17 20 16 18 19 p10 1 3 2 4 5 6 8 7 9 10 11 13 12 14 15 16 18 17 19 20 > > ## time series within each level of Plot strata > CTRL <- how(plots = plotStrata, + within = Within(type = "series")) > shuffleSet(20, 10, CTRL) Set of permutations < 'minperm'. Generating entire set. No. of Permutations: 10 No. of Samples: 20 (Nested in: plots; Sequence) Restricted by Plots: gl(4, 5) (4 plots) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 p1 2 3 4 5 1 10 6 7 8 9 14 15 11 12 13 18 19 20 16 17 p2 3 4 5 1 2 10 6 7 8 9 12 13 14 15 11 17 18 19 20 16 p3 2 3 4 5 1 10 6 7 8 9 11 12 13 14 15 18 19 20 16 17 p4 2 3 4 5 1 10 6 7 8 9 13 14 15 11 12 19 20 16 17 18 p5 3 4 5 1 2 7 8 9 10 6 14 15 11 12 13 19 20 16 17 18 p6 3 4 5 1 2 7 8 9 10 6 15 11 12 13 14 17 18 19 20 16 p7 2 3 4 5 1 10 6 7 8 9 13 14 15 11 12 18 19 20 16 17 p8 1 2 3 4 5 6 7 8 9 10 13 14 15 11 12 17 18 19 20 16 p9 3 4 5 1 2 10 6 7 8 9 12 13 14 15 11 18 19 20 16 17 p10 4 5 1 2 3 9 10 6 7 8 14 15 11 12 13 18 19 20 16 17 > ## as above, but with same permutation for each Plot-level stratum > CTRL <- how(plots = plotStrata, + within = Within(type = "series", constant = TRUE)) > shuffleSet(20, 10, CTRL) 'nperm' >= set of all permutations: complete enumeration. Set of permutations < 'minperm'. Generating entire set. No. of Permutations: 4 No. of Samples: 20 (Nested in: plots; Sequence; same permutation in each plot) Restricted by Plots: gl(4, 5) (4 plots) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 p1 2 3 4 5 1 7 8 9 10 6 12 13 14 15 11 17 18 19 20 16 p2 3 4 5 1 2 8 9 10 6 7 13 14 15 11 12 18 19 20 16 17 p3 4 5 1 2 3 9 10 6 7 8 14 15 11 12 13 19 20 16 17 18 p4 5 1 2 3 4 10 6 7 8 9 15 11 12 13 14 20 16 17 18 19 > > > > ### *