set.seed(1234)
## unrestricted permutations
permuted.index2(20)
## observations represent a time series of line transect
permuted.index2(20, control = permControl(type = "series"))
## observations represent a time series of line transect
## but with mirroring allowed
permuted.index2(20, control = permControl(type = "series", mirror = TRUE))
## observations represent a spatial grid
perms <- permuted.index2(20, permControl(type = "grid",
ncol = 4, nrow = 5))
## view the permutation as a grid
matrix(matrix(1:20, nrow = 5, ncol = 4)[perms], ncol = 4, nrow = 5)
## random permutations in presence of strata
block <- gl(4, 5)
permuted.index2(20, permControl(strata = block, type = "free"))
## as above but same random permutation within strata
permuted.index2(20, permControl(strata = block, type = "free",
constant = TRUE))
## time series within each level of block
permuted.index2(20, permControl(strata = block, type = "series"))
## as above, but with same permutation for each level
permuted.index2(20, permControl(strata = block, type = "series",
constant = TRUE))
## spatial grids within each level of block
permuted.index2(100, permControl(strata = block, type = "grid",
ncol = 5, nrow = 5))
## as above, but with same permutation for each level
permuted.index2(100, permControl(strata = block, type = "grid",
ncol = 5, nrow = 5, constant = TRUE))
## permuting levels of block instead of observations
permuted.index2(20, permControl(strata = block, type = "free",
permute.strata = TRUE))
## 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 <- .Internal(mean(x))
ybar <- .Internal(mean(y))
xvar <- .Internal(cov(x, NULL, 1, FALSE))
yvar <- .Internal(cov(y, NULL, 1, FALSE))
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(type = "free", nperm = 999)
## 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)
Run the code above in your browser using DataLab