learnnonparam

'R6'-Based Flexible Framework for Permutation Tests

https://github.com/qddyy/learnnonparam

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.8%) to scientific vocabulary

Keywords

hypothesis-test nonparametric-statistics permutation-test r r-package
Last synced: 6 months ago · JSON representation

Repository

'R6'-Based Flexible Framework for Permutation Tests

Basic Info
Statistics
  • Stars: 7
  • Watchers: 2
  • Forks: 0
  • Open Issues: 0
  • Releases: 23
Topics
hypothesis-test nonparametric-statistics permutation-test r r-package
Created over 2 years ago · Last pushed 6 months ago
Metadata Files
Readme Changelog License

README.Rmd

---
bibliography: man/references/ref.bib
always_allow_html: yes
output:
  github_document:
    df_print: kable
---

```{r, setup, include = FALSE}
knitr::opts_chunk$set(
    collapse = TRUE, comment = "#>",
    fig.path = "man/figures/README",
    fig.align = "center", out.width = "100%",
    asciicast_theme = if (Sys.getenv("IN_PKGDOWN") == "true") "pkgdown" else "readme"
)
asciicast::init_knitr_engine(
    echo = TRUE, echo_input = FALSE,
    startup = quote(library(LearnNonparam))
)
options(
    asciicast_at = "all",
    asciicast_cursor = FALSE,
    asciicast_knitr_svg = TRUE,
    asciicast_padding_y = 0,
    asciicast_start_wait = 0,
    asciicast_end_wait = 1,
    asciicast_timeout = Inf
)
```

# LearnNonparam logo

[![License](https://img.shields.io/cran/l/LearnNonparam?color=orange)](https://cran.r-project.org/web/licenses/GPL-2)
[![CRAN status](https://www.r-pkg.org/badges/version/LearnNonparam)](https://cran.r-project.org/package=LearnNonparam)
[![Dependencies](https://tinyverse.netlify.app/badge/LearnNonparam)](https://cran.r-project.org/package=LearnNonparam)
[![CRAN RStudio mirror downloads](https://cranlogs.r-pkg.org/badges/LearnNonparam)](https://r-pkg.org/pkg/LearnNonparam)
[![CRAN RStudio mirror downloads](https://cranlogs.r-pkg.org/badges/grand-total/LearnNonparam)](https://r-pkg.org/pkg/LearnNonparam)

## Overview

This R package implements several non-parametric tests in chapters 1-5 of [@higgins2004](#references), including tests for one sample, two samples, k samples, paired comparisons, blocked designs, trends and association. Built with [Rcpp](https://CRAN.R-project.org/package=Rcpp) for efficiency and [R6](https://CRAN.R-project.org/package=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](https://CRAN.R-project.org/package=LearnNonparam):

```{r, install_cran, eval = FALSE}
install.packages("LearnNonparam")
```

Install the development version from [Github](https://github.com/qddyy/LearnNonparam):

```{r, install_github, eval = FALSE}
# install.packages("remotes")
remotes::install_github("qddyy/LearnNonparam")
```

## Usage

```{r, library}
library(LearnNonparam)
```

- Construct a test object
    - from some R6 class directly
    ```{r, create_R6, eval = FALSE}
    t <- Wilcoxon$new(n_permu = 1e6)
    ```

    - using the `pmt` (**p**er**m**utation **t**est) wrapper
    ```{r, create_pmt, eval = FALSE}
    # recommended for a unified API
    t <- pmt("twosample.wilcoxon", n_permu = 1e6)
    ```

    ```{asciicast, create, include = FALSE}
    t <- pmt("twosample.wilcoxon", n_permu = 1e6)
    ```

- Provide it with samples

    ```{asciicast, test}
    set.seed(-1)

    t$test(rnorm(10, 1), rnorm(10, 0))
    ```

- Check the results
    ```{asciicast, statistic}
    t$statistic
    ```

    ```{asciicast, p_value}
    t$p_value
    ```

    ```{asciicast, print}
    options(digits = 3)

    t$print()
    ```

    ```{asciicast, plot}
    ggplot2::theme_set(ggplot2::theme_minimal())

    t$plot(style = "ggplot2", binwidth = 1) # or ggplot2::autoplot(t, binwidth = 1)
    ```

    ```{asciicast, save_plot, include = FALSE}
    ggplot2::ggsave(
        "./man/figures/README/histogram.svg",
        width = 12, height = 9, device = "svg"
    )
    ```

    ```{r, include_plot, echo = FALSE}
    knitr::include_graphics("./man/figures/README/histogram.svg")
    ```

- Modify some settings and observe the change
    ```{asciicast, modify}
    t$type <- "asymp"
    t$p_value
    ```

See pmts() for tests implemented in this package.

```{r, pmts} pmts() ```

## Extending `define_pmt` allows users to define new permutation tests. Take the two-sample Wilcoxon test as an example: ```{asciicast, define_r} t_custom <- define_pmt( # this is a two-sample permutation test method = "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 = "<>", 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. ```{asciicast, define_cpp} t_cpp <- define_pmt( method = "twosample", rejection = "<>", 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: ```{asciicast, prepare_data} x <- rnorm(10, mean = 0) y <- rnorm(10, mean = 5) ``` ```{asciicast, t_custom_res} set.seed(0) t_custom$test(x, y)$print() ``` ```{asciicast, t_cpp_res} set.seed(0) t_cpp$test(x, y)$print() ``` ## Performance [coin](https://CRAN.R-project.org/package=coin) is a commonly used R package for performing permutation tests. Below is a benchmark: ```{asciicast, 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")) ) ``` ```{asciicast, benchmark_res} 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

Owner

  • Login: qddyy
  • Kind: user

GitHub Events

Total
  • Release event: 6
  • Push event: 248
  • Create event: 7
Last Year
  • Release event: 6
  • Push event: 248
  • Create event: 7

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 1
  • Total pull requests: 2
  • Average time to close issues: N/A
  • Average time to close pull requests: about 2 hours
  • Total issue authors: 1
  • Total pull request authors: 1
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 2
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • qddyy (1)
Pull Request Authors
  • qddyy (2)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 211 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 7
  • Total maintainers: 1
cran.r-project.org: LearnNonparam

'R6'-Based Flexible Framework for Permutation Tests

  • Versions: 7
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 211 Last month
Rankings
Dependent packages count: 28.0%
Dependent repos count: 34.5%
Average: 49.7%
Downloads: 86.7%
Maintainers (1)
Last synced: 6 months ago