Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
qddyy committed Jul 10, 2024
1 parent 2d76309 commit eab4042
Show file tree
Hide file tree
Showing 21 changed files with 246 additions and 61 deletions.
43 changes: 29 additions & 14 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ output:
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(
Expand All @@ -36,7 +37,7 @@ options(

## Overview

This R package implements some of the non-parametric tests in chapters 1-5 of [@Higgins2003](#references).
This R package implements several non-parametric tests in chapters 1-5 of [@Higgins2003](#references).

It depends on [R6](https://CRAN.R-project.org/package=R6) for object oriented design and [Rcpp](https://CRAN.R-project.org/package=Rcpp) for integration of R and C++.

Expand Down Expand Up @@ -66,8 +67,9 @@ options(LearnNonparam.pmt_progress = TRUE)
t <- Wilcoxon$new(n_permu = 1e6)
```
- using the `pmt` (**p**er**m**utation **t**est) function (*recommended*)
- using the `pmt` (**p**er**m**u**t**ation test) wrapper
```{r, create_pmt, eval = FALSE}
# recommended for a unified API
t <- pmt("twosample.wilcoxon", n_permu = 1e6)
```
Expand All @@ -76,8 +78,14 @@ options(LearnNonparam.pmt_progress = TRUE)
```
- Provide it with samples
```{asciicast, set_seed, include = FALSE}
set.seed(3)
```
```{asciicast, test}
t$test(rnorm(20, 1), rnorm(20, 0))
set.seed(-1)
t$test(rnorm(10, 1), rnorm(10, 0))
```
- Check the results
Expand All @@ -90,22 +98,29 @@ options(LearnNonparam.pmt_progress = TRUE)
```
```{asciicast, print}
options(digits = 3)
t$print()
```
```{asciicast, plot}
ggplot2::theme_set(ggplot2::theme_minimal())
t$plot(style = "ggplot2", binwidth = 1)
```
```{asciicast, save_plot, include = FALSE}
ggplot2::ggsave("./man/figures/README/ggplot.svg", device = "svg")
ggplot2::ggsave(
"./man/figures/README/histogram.svg",
width = 12, height = 9, device = "svg"
)
```
```{r, include_plot, echo = FALSE, out.width = "100%", out.height = "75%"}
knitr::include_graphics("./man/figures/README/ggplot.svg")
```{r, include_plot, echo = FALSE}
knitr::include_graphics("./man/figures/README/histogram.svg")
```
- Modify some active bindings and see how the results change
- Modify some settings and observe the change
```{asciicast, modify}
t$type <- "asymp"
t$p_value
Expand All @@ -118,22 +133,22 @@ LearnNonparam::pmts()
```
</details>

The `define_pmt` function allows users to define new permutation tests. Take Cramér-von Mises test as an example:
`define_pmt` allows users to define new permutation tests. Take the two-sample Cramér-Von Mises test as an example:

```{asciicast, define}
t <- define_pmt(
# this is a two-sample permutation test
inherit = "twosample",
statistic = function(x, y) {
# pre-calculate certain constants that remain invariant during permutation
# (optional) pre-calculate certain constants that remain invariant during permutation
n_x <- length(x)
n_y <- length(y)
F_x <- seq_len(n_x) / n_x
G_y <- seq_len(n_y) / n_y
# return a closure to calculate the test statistic
function(x, y) {
x <- sort(x)
y <- sort(y)
x <- sort.int(x)
y <- sort.int(y)
F <- approxfun(x, F_x, "constant", 0, 1, ties = "ordered")
G <- approxfun(y, G_y, "constant", 0, 1, ties = "ordered")
sum(c(F_x - G(x), G_y - F(y))^2)
Expand All @@ -142,11 +157,11 @@ t <- define_pmt(
# reject the null hypothesis when the test statistic is large
rejection = "r",
scoring = "none", n_permu = 1e4,
name = "Cramér-von Mises Test",
alternative = "samples are from different distributions"
name = "Two-Sample Cramér-Von Mises Test",
alternative = "samples are from different continuous distributions"
)
t$test(rnorm(20, 1), rnorm(20, 0))$print()
t$test(rnorm(10), runif(10))$print()
```

## References
57 changes: 35 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ check](https://github.com/qddyy/LearnNonparam/actions/workflows/R-CMD-check.yaml

## Overview

This R package implements some of the non-parametric tests in chapters
1-5 of [Higgins (2003)](#references).
This R package implements several non-parametric tests in chapters 1-5
of [Higgins (2003)](#references).

It depends on [R6](https://CRAN.R-project.org/package=R6) for object
oriented design and [Rcpp](https://CRAN.R-project.org/package=Rcpp) for
Expand Down Expand Up @@ -52,22 +52,25 @@ options(LearnNonparam.pmt_progress = TRUE)
t <- Wilcoxon$new(n_permu = 1e6)
```

- using the `pmt` (**p**er**m**utation **t**est) function
(*recommended*)
- using the `pmt` (**p**er**m**u**t**ation test) wrapper

``` r
# recommended for a unified API
t <- pmt("twosample.wilcoxon", n_permu = 1e6)
```

- Provide it with samples

``` r
t$test(rnorm(20, 1), rnorm(20, 0))
set.seed(-1)

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

<picture>
<source media="(prefers-color-scheme: dark)" srcset="man/figures/README/test-dark.svg">
<img src="man/figures/README/test.svg" /> </picture>
<img src="man/figures/README/test.svg" width="100%" style="display: block; margin: auto;" />
</picture>

- Check the results

Expand All @@ -77,35 +80,43 @@ options(LearnNonparam.pmt_progress = TRUE)

<picture>
<source media="(prefers-color-scheme: dark)" srcset="man/figures/README/statistic-dark.svg">
<img src="man/figures/README/statistic.svg" /> </picture>
<img src="man/figures/README/statistic.svg" width="100%" style="display: block; margin: auto;" />
</picture>

``` r
t$p_value
```

<picture>
<source media="(prefers-color-scheme: dark)" srcset="man/figures/README/p_value-dark.svg">
<img src="man/figures/README/p_value.svg" /> </picture>
<img src="man/figures/README/p_value.svg" width="100%" style="display: block; margin: auto;" />
</picture>

``` r
options(digits = 3)

t$print()
```

<picture>
<source media="(prefers-color-scheme: dark)" srcset="man/figures/README/print-dark.svg">
<img src="man/figures/README/print.svg" /> </picture>
<img src="man/figures/README/print.svg" width="100%" style="display: block; margin: auto;" />
</picture>

``` r
ggplot2::theme_set(ggplot2::theme_minimal())

t$plot(style = "ggplot2", binwidth = 1)
```

<picture>
<source media="(prefers-color-scheme: dark)" srcset="man/figures/README/plot-dark.svg">
<img src="man/figures/README/plot.svg" /> </picture>
<img src="man/figures/README/plot.svg" width="100%" style="display: block; margin: auto;" />
</picture>

<img src="./man/figures/README/ggplot.svg" width="100%" height="75%" />
<img src="./man/figures/README/histogram.svg" width="100%" style="display: block; margin: auto;" />

- Modify some active bindings and see how the results change
- Modify some settings and observe the change

``` r
t$type <- "asymp"
Expand All @@ -114,7 +125,8 @@ options(LearnNonparam.pmt_progress = TRUE)

<picture>
<source media="(prefers-color-scheme: dark)" srcset="man/figures/README/modify-dark.svg">
<img src="man/figures/README/modify.svg" /> </picture>
<img src="man/figures/README/modify.svg" width="100%" style="display: block; margin: auto;" />
</picture>

<details>
<summary>
Expand Down Expand Up @@ -150,23 +162,23 @@ See <code>pmts()</code> for tests implemented in this package.

</details>

The `define_pmt` function allows users to define new permutation tests.
Take Cramér-von Mises test as an example:
`define_pmt` allows users to define new permutation tests. Take the
two-sample Cramér-Von Mises test as an example:

``` r
t <- define_pmt(
# this is a two-sample permutation test
inherit = "twosample",
statistic = function(x, y) {
# pre-calculate certain constants that remain invariant during permutation
# (optional) pre-calculate certain constants that remain invariant during permutation
n_x <- length(x)
n_y <- length(y)
F_x <- seq_len(n_x) / n_x
G_y <- seq_len(n_y) / n_y
# return a closure to calculate the test statistic
function(x, y) {
x <- sort(x)
y <- sort(y)
x <- sort.int(x)
y <- sort.int(y)
F <- approxfun(x, F_x, "constant", 0, 1, ties = "ordered")
G <- approxfun(y, G_y, "constant", 0, 1, ties = "ordered")
sum(c(F_x - G(x), G_y - F(y))^2)
Expand All @@ -175,16 +187,17 @@ t <- define_pmt(
# reject the null hypothesis when the test statistic is large
rejection = "r",
scoring = "none", n_permu = 1e4,
name = "Cramér-von Mises Test",
alternative = "samples are from different distributions"
name = "Two-Sample Cramér-Von Mises Test",
alternative = "samples are from different continuous distributions"
)

t$test(rnorm(20, 1), rnorm(20, 0))$print()
t$test(rnorm(10), runif(10))$print()
```

<picture>
<source media="(prefers-color-scheme: dark)" srcset="man/figures/README/define-dark.svg">
<img src="man/figures/README/define.svg" /> </picture>
<img src="man/figures/README/define.svg" width="100%" style="display: block; margin: auto;" />
</picture>

## References

Expand Down
9 changes: 1 addition & 8 deletions man/PermuTest.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/SiegelTukey.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/Sign.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/Wilcoxon.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/figures/README/define-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion man/figures/README/define.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit eab4042

Please sign in to comment.