| pmt | R Documentation |
Construct test objects in a unified way.
pmt(key, ...)
pmts(
which = c("all", "onesample", "twosample", "ksample", "multcomp", "paired", "rcbd",
"association", "table")
)
define_pmt(
inherit = c("twosample", "ksample", "paired", "rcbd", "association", "table"),
statistic,
rejection = c("lr", "l", "r"),
scoring = "none",
n_permu = 10000,
name = "User-Defined Permutation Test",
alternative = NULL,
depends = character(),
plugins = character(),
includes = character()
)
key |
a character string specifying the test. Check |
... |
extra parameters passed to the constructor. |
which |
a character string specifying the desired tests. |
inherit |
a character string specifying the type of permutation test. |
statistic |
definition of the test statistic. See details. |
rejection |
a character string specifying where the rejection region is. |
scoring |
one of:
- a character string in |
n_permu |
an integer indicating number of permutations for the permutation distribution. If set to |
name |
a character string specifying the name of the test. |
alternative |
a character string describing the alternative hypothesis. |
depends, plugins, includes |
passed to |
The test statistic can be defined using either R or Rcpp, with the statistic parameter specified as:
R: a function returning a closure that returns a double.
Rcpp: a character string defining a captureless lambda (since C++11) returning another lambda that captures by value, accepts parameters of the same type, and returns a double.
The purpose of this design is to pre-calculate certain constants that remain invariant during permutation.
When using Rcpp, the parameters for different inherit are listed as follows. Note that the names can be customized, and the types can be replaced with auto (thanks to the support for generic lambdas in C++14). See examples.
inherit | Parameter 1 | Parameter 2 |
"twosample" | const NumericVector& sample_1 | const NumericVector& sample_2 |
"ksample" | const NumericVector& combined_sample | const IntegerVector& one_based_group_index |
"paired" | const NumericVector& sample_1 | const NumericVector& sample_2 |
"rcbd" | const NumericMatrix& block_as_column_data | |
"association" | const NumericVector& sample_1 | const NumericVector& sample_2 |
"table" | const IntegerMatrix& contingency_table | |
When using R, the parameters should be the R equivalents of these.
a test object corresponding to the specified key.
a data frame containing keys and corresponding tests implemented in this package.
a test object based on the specified statistic.
statistic should not cause errors or return missing values.
The data is permuted in-place. Therefore, modifications to the data within statistic may lead to incorrect results. Since R has copy-on-modify semantics but C++ does not, it is recommended to pass const references when using Rcpp in define_pmt, as shown in the table above.
pmt("twosample.wilcoxon")
pmts("ksample")
x <- rnorm(5)
y <- rnorm(5, 1)
t <- define_pmt(
inherit = "twosample",
scoring = base::rank, # equivalent to "rank"
statistic = function(...) function(x, y) sum(x)
)$test(x, y)$print()
t$scoring <- function(x) qnorm(rank(x) / (length(x) + 1)) # equivalent to "vw"
t$print()
t$n_permu <- 0
t$print()
r <- define_pmt(
inherit = "twosample", n_permu = 1e5,
statistic = function(x, y) {
m <- length(x)
n <- length(y)
function(x, y) sum(x) / m - sum(y) / n
}
)
rcpp <- define_pmt(
inherit = "twosample", 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;
};
}"
)
# equivalent
# rcpp <- define_pmt(
# inherit = "twosample", n_permu = 1e5,
# statistic = "[](const NumericVector& x, const NumericVector& y) {
# R_xlen_t m = x.length();
# R_xlen_t n = y.length();
# return [m, n](const NumericVector& x, const NumericVector& y) -> double {
# return sum(x) / m - sum(y) / n;
# };
# }"
# )
options(LearnNonparam.pmt_progress = FALSE)
system.time(r$test(x, y))
system.time(rcpp$test(x, y))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.