Skip to content

Commit

Permalink
added CI of p-value in print()
Browse files Browse the repository at this point in the history
  • Loading branch information
qddyy committed Jul 8, 2024
1 parent 669a852 commit c5afc5f
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 57 deletions.
2 changes: 1 addition & 1 deletion R/CDF.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ CDF <- R6Class(
)
},

.print = function(...) {
.print = function() {
cat(format(self), sep = "\n")
},

Expand Down
11 changes: 6 additions & 5 deletions R/MultipleComparison.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ MultipleComparison <- R6Class(
private$.calculate_extra()
},

.print = function(digits) {
.print = function() {
cat("\n\t", private$.name, "\n\n")

cat(
Expand All @@ -93,7 +93,7 @@ MultipleComparison <- R6Class(
"type:",
if ((type <- private$.type) == "permu") {
n_used <- as.numeric(attr(private$.n_permu, "n_used"))
paste0(type, "(", format(n_used, digits = digits), ")")
paste0(type, "(", format(n_used), ")")
} else type
),
paste("method:", private$.method),
Expand All @@ -103,7 +103,7 @@ MultipleComparison <- R6Class(

cat(
"family-wise confidence level:",
paste0(format(private$.conf_level * 100, digits = digits), "%")
paste0(format(private$.conf_level * 100), "%")
)
cat("\n\n")

Expand All @@ -117,8 +117,9 @@ MultipleComparison <- R6Class(
data_names[private$.group_ij$i],
data_names[private$.group_ij$j],
sep = " ~ "
), check.names = FALSE, fix.empty.names = FALSE
), digits = digits
),
check.names = FALSE, fix.empty.names = FALSE
)
)
},

Expand Down
33 changes: 17 additions & 16 deletions R/PermuTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ PermuTest <- R6Class(

#' @description Print the results of the test.
#'
#' @param digits an integer specifying the minimum number of significant digits to be printed in values.
#'
#' @return The object itself (invisibly).
print = function(digits = getOption("digits")) {
print = function() {
if (is.null(private$.raw_data)) {
cat(format(self), sep = "\n")
} else {
private$.print(digits = digits)
private$.print()
}

invisible(self)
Expand Down Expand Up @@ -201,7 +199,7 @@ PermuTest <- R6Class(
private$.calculate_p_permu()
},

.print = function(digits) {
.print = function() {
cat("\n", "\t", private$.name, "\n\n")

cat(
Expand All @@ -210,7 +208,7 @@ PermuTest <- R6Class(
"type:",
if ((type <- private$.type) == "permu") {
n_used <- as.numeric(attr(private$.n_permu, "n_used"))
paste0(type, "(", format(n_used, digits = digits), ")")
paste0(type, "(", format(n_used), ")")
} else type
),
paste("method:", private$.method),
Expand All @@ -219,15 +217,18 @@ PermuTest <- R6Class(
cat("\n")

cat(
paste(
"statistic", "=",
format(private$.statistic, digits = digits)
),
paste("statistic", "=", format(private$.statistic)),
{
p <- format.pval(private$.p_value, digits = digits)
p <- private$.p_value
eps <- .Machine$double.eps
paste(
"p-value",
if (!startsWith(p, "<")) paste("=", p) else p
if (p < eps) "<" else "=",
if (p < eps) format(eps) else format(p),
if (private$.type == "permu" && private$.n_permu != 0) {
q <- qnorm(0.975) * sqrt(p * (1 - p) / n_used)
paste("(\u00B1", format(q), "at 95% confidence)")
}
)
},
sep = ", "
Expand All @@ -254,17 +255,17 @@ PermuTest <- R6Class(
}

if (!is.null(private$.estimate)) {
cat("estimate:", format(private$.estimate, digits = digits))
cat("estimate:", format(private$.estimate))
cat("\n")
}

if (!is.null(private$.conf_int)) {
cat(
paste0(
format(private$.conf_level * 100, digits = digits), "%",
format(private$.conf_level * 100), "%",
" confidence interval: ",
"(", format(private$.conf_int[1], digits = digits), ",",
" ", format(private$.conf_int[2], digits = digits), ")"
"(", format(private$.conf_int[1]), ",",
" ", format(private$.conf_int[2]), ")"
)
)
cat("\n")
Expand Down
14 changes: 9 additions & 5 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,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 @@ -67,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 @@ -95,11 +96,14 @@ options(LearnNonparam.pmt_progress = TRUE)
```
```{asciicast, print}
options(digits = 2)
t$print()
```
```{asciicast, plot}
ggplot2::theme_set(ggplot2::theme_minimal())
t$plot(style = "ggplot2", binwidth = 1)
```
Expand All @@ -114,7 +118,7 @@ options(LearnNonparam.pmt_progress = TRUE)
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 @@ -127,14 +131,14 @@ 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
Expand Down
19 changes: 11 additions & 8 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,10 +52,10 @@ 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)
```

Expand Down Expand Up @@ -91,6 +91,8 @@ options(LearnNonparam.pmt_progress = TRUE)
</picture>

``` r
options(digits = 2)

t$print()
```

Expand All @@ -101,6 +103,7 @@ options(LearnNonparam.pmt_progress = TRUE)

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

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

Expand All @@ -111,7 +114,7 @@ options(LearnNonparam.pmt_progress = TRUE)

<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 Down Expand Up @@ -157,15 +160,15 @@ 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
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/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.
2 changes: 1 addition & 1 deletion man/figures/README/modify-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/modify.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 c5afc5f

Please sign in to comment.