Streamlining Permutation Tests with Enhanced Set Partitions in R

Oct 11, 2026 339 views

Recent enhancements in the permute package for R focus on simplifying the configuration of permutation tests. The latest features include options for ordered set partitions and a streamlined formula interface for specifying plots and blocks, taking accessibility and efficiency to the next level.

These updates are part of the development version of the permute package. Users interested in exploring these features can install it directly from my R-universe using:

install.packages("permute", repos = c(
"https://gavinsimpson.r-universe.dev",
"https://cloud.r-project.org"
))

Reducing Permutations with Ordered Set Partitions

The jackal dataset, included with permute, presents mandible measurements for male and female golden jackals and serves to illustrate the new capabilities. The objective is to analyze mean mandible lengths across sexes. Instead of simply shuffling all observation indices, which leads to an astronomical number of permutations, we need to focus on distinct assignments that maintain group sizes.

Using the new feature type = "partition" within the Plots() function significantly cuts down the complexity. For example, with the jackal data, the number of permutations drops from over 2 trillion to just 184,756, illustrating how effective this addition is:

ctrl <- how(
plots = Plots(strata = ~ Sex, type = "partition"),
data = jackal
)
numPerms(nrow(jackal), control = ctrl)
[1] 184756

This ordered set partitioning allows for clearer analysis, where 'ordered' signifies distinguishable groups, such as male and female, rather than requiring sorted observations within each group.

Applying Permutation Tests with Linear Models

The flexibility of permute extends beyond ecology-focused analyses; it can integrate with various statistical models. For example, consider fitting a linear model to the jackal dataset:

fit <- lm(Length ~ Sex, data = jackal)
coef(fit)
(Intercept) SexFemale
113.4 -4.8 

Here, the intercept signifies the mean mandible length for males, and the coefficient for females indicates they have mandibles approximately 4.8 mm shorter on average. A permutation test can explore this further by shuffling the sex labels while keeping the mandible lengths intact:

set.seed(42)
perms <- shuffleSet(nrow(jackal), nset = 999, control = ctrl)
observed <- unname(coef(fit)[2])
null <- apply(perms, 1, function(i) {
permuted <- jackal
permuted$Sex <- jackal$Sex[i]
unname(coef(lm(Length ~ Sex, data = permuted))[2])
})

This approach generates a matrix of permuted coefficients, allowing users to easily visualize and calculate p-values from the generated distribution against the observed coefficients.

Enhancing Usability with a Formula Interface

The addition of a formula-based interface enhances usability for specifying plots and blocks, making it more intuitive for users. For instance, when working with the dune meadow dataset from the vegan package, it allows for a clear declaration of conditions under which analyses should occur:

library("vegan")
data(dune, package = "vegan")
data(dune.env, package = "vegan")
formula_ctrl <- how(
plots = Plots(strata = ~ Management / Use),
blocks = ~ Management,
data = dune.env
)

This code snippet defines how to manage within-plot permutations while maintaining a block structure based on management categories. The simplicity and clarity of this formula interface make the design and execution of tests more accessible.

When executing a permutation test, the formula interface respects these defined structures, crucial for deriving accurate conclusions from the data. Moreover, functions like shufflePartition() and shufflePartitionSet() provide straightforward shortcuts for users wanting quick analyses or sampling.

Concluding Thoughts on the New Features

The improvements in the permute package represent vital steps towards easing the complexity involved in designing and executing permutation tests. The ordered set partitions drastically reduce the number of necessary permutations, and the new formula interface streamlines the entire process. These features not only save time but also reduce potential errors, allowing for a thorough analysis that respects the structure of the data. Users are encouraged to explore these updates and provide feedback, further enhancing the utility of the permute package.

Source: Gavin Simpson · www.r-bloggers.com

Comments

Sign in to comment.
No comments yet. Be the first to comment.

Related Articles

Ordered set partitions and formulas in permute