qddyy/LearnNonparam 是一个托管在 GitHub 上的开源项目,主要使用 R 编写,采用 GNU General Public License v2.0 许可证,目前获得 6 个 Star、0 次 Fork。
仓库全名
qddyy/LearnNonparam
README 摘要
LearnNonparam 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…
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.
Multiple Comparison Based on Studentized Statistic
rcbd.oneway
RCBDOneWay
One-Way Test for Equal Means in RCBD
rcbd.friedman
Friedman
Friedman Test
rcbd.page
Page
Page Test
table.chisq
ChiSquare
Chi-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
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
)
For R >= 4.4.0, the quickr
package can be used to accelerate statistic. However, this results in
repeated crossings of the R-Fortran boundary and makes pre-calculation
of constants impossible.
In cases where both pre-calculation and computational efficiency are
required, 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(
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, t_quickr 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_quickr$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:
It can be seen that C++ brings significantly better performance than
pure R, which enables it to even surpass the coin package in its
no-parallelization setting. However, all tests in this package are
currently written in pure 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.
评论