MyNixOS website logo
Description

'R6'-Based Flexible Framework for Permutation Tests.

Implements non-parametric tests from Higgins (2004, ISBN:0534387756), including tests for one sample, two samples, k samples, paired comparisons, blocked designs, trends and association. Built with 'Rcpp' for efficiency and 'R6' for flexible, object-oriented design, the package provides a unified framework for performing or creating custom permutation tests.

LearnNonparam logo

License CRANstatus Dependencies CRAN RStudio mirrordownloads CRAN RStudio mirrordownloads

Overview

This R package implements several non-parametric tests in chapters 1-5 of Higgins (2004), including tests for one sample, two samples, k samples, paired comparisons, blocked designs, trends and association. Built with Rcpp for efficiency and R6 for flexible, object-oriented design, it provides a unified framework for performing or creating custom permutation tests.

Installation

Install the stable version from CRAN:

install.packages("LearnNonparam")

Install the development version from Github:

# install.packages("remotes")
remotes::install_github("qddyy/LearnNonparam")

Usage

library(LearnNonparam)
  • Construct a test object

    • from some R6 class directly
    t <- Wilcoxon$new(n_permu = 1e6)
    
    • using the pmt (permutation test) wrapper
    # recommended for a unified API
    t <- pmt("twosample.wilcoxon", n_permu = 1e6)
    
  • Provide it with samples

    set.seed(-1)
    
    t$test(rnorm(10, 1), rnorm(10, 0))
    
  • Check the results

    t$statistic
    
    t$p_value
    
    options(digits = 3)
    
    t$print()
    
    ggplot2::theme_set(ggplot2::theme_minimal())
    
    t$plot(style = "ggplot2", binwidth = 1)
    
  • Modify some settings and observe the change

    t$type <- "asymp"
    t$p_value
    
See pmts() for tests implemented in this package.
keyclasstest
onesample.quantileQuantileQuantile Test
onesample.cdfCDFInference on Cumulative Distribution Function
twosample.differenceDifferenceTwo-Sample Test Based on Mean or Median
twosample.wilcoxonWilcoxonTwo-Sample Wilcoxon Test
twosample.scoresumScoreSumTwo-Sample Test Based on Sum of Scores
twosample.ansariAnsariBradleyAnsari-Bradley Test
twosample.siegelSiegelTukeySiegel-Tukey Test
twosample.rmdRatioMeanDevianceRatio Mean Deviance Test
twosample.ksKolmogorovSmirnovTwo-Sample Kolmogorov-Smirnov Test
ksample.onewayOneWayOne-Way Test for Equal Means
ksample.kwKruskalWallisKruskal-Wallis Test
ksample.jtJonckheereTerpstraJonckheere-Terpstra Test
multcomp.studentizedStudentizedMultiple Comparison Based on Studentized Statistic
paired.signSignTwo-Sample Sign Test
paired.differencePairedDifferencePaired Comparison Based on Differences
rcbd.onewayRCBDOneWayOne-Way Test for Equal Means in RCBD
rcbd.friedmanFriedmanFriedman Test
rcbd.pagePagePage Test
association.corrCorrelationTest for Association Between Paired Samples
table.chisqChiSquareChi-Square Test on Contingency Table

Extending

define_pmt allows users to define new permutation tests. Take the two-sample Wilcoxon test as an example:

t_custom <- define_pmt(
    # this is a two-sample permutation test
    inherit = "twosample",
    statistic = function(x, y) {
        # (optional) pre-calculate certain constants that remain invariant during permutation
        m <- length(x)
        n <- length(y)
        # return a closure to calculate the test statistic
        function(x, y) sum(x) / m - sum(y) / n
    },
    # reject the null hypothesis when the test statistic is too large or too small
    rejection = "lr", n_permu = 1e5
)

Also, the statistic can be written in C++. Leveraging Rcpp sugars and C++14 features, only minor modifications are needed to make it compatible with C++ syntax.

t_cpp <- define_pmt(
    inherit = "twosample", rejection = "lr", n_permu = 1e5,
    statistic = "[](const auto& x, const auto& y) {
        auto m = x.length();
        auto n = y.length();
        return [=](const auto& x, const auto& y) {
            return sum(x) / m - sum(y) / n;
        };
    }"
)

It’s easy to check that t_custom and t_cpp are equivalent:

x <- rnorm(10, mean = 0)
y <- rnorm(10, mean = 5)
set.seed(0)
t_custom$test(x, y)$print()
set.seed(0)
t_cpp$test(x, y)$print()

Performance

coin is a commonly used R package for performing permutation tests. Below is a benchmark:

library(coin)

data <- c(x, y)
group <- factor(c(rep("x", length(x)), rep("y", length(y))))

options(LearnNonparam.pmt_progress = FALSE)
benchmark <- microbenchmark::microbenchmark(
    R = t_custom$test(x, y),
    Rcpp = t_cpp$test(x, y),
    coin = wilcox_test(data ~ group, distribution = approximate(nresample = 1e5, parallel = "no"))
)
benchmark

It can be seen that C++ brings significantly better performance than pure R, even surpassing the coin package (under sequential execution). However, all tests in this package are currently written in R with no plans for migration to C++ in the future. This is because the primary goal of this package is not to maximize performance but to offer a flexible framework for permutation tests.

References

Higgins, J. J. 2004. An Introduction to Modern Nonparametric Statistics. Duxbury Advanced Series. Brooks/Cole.

Metadata

Version

1.2.9

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows