Runs a suite of nonparametric randomness tests from the randtests package.

run_randtests(x, alter = "two.sided", na_rm = TRUE, alpha = 0.05)

Arguments

x

A numeric vector containing the sequence to test for randomness.

alter

Alternative hypothesis. One of "two.sided", "less", "greater", "left.sided", or "right.sided". The values "less" and "greater" are mapped internally to "left.sided" and "right.sided", respectively.

na_rm

Logical. If TRUE, missing values are removed before testing. If FALSE, the function throws an error when x contains missing values.

alpha

Significance level used to determine the Reject_H0 column. Default is 0.05.

Value

A data.frame with one row per randomness test and the following columns:

Test

Short name of the randomness test.

Method

Method name returned by the underlying randtests function.

Alternative

Alternative hypothesis used in the test.

N

Number of non-missing observations tested.

Statistic

Observed test statistic.

Statistic_Name

Name of the test statistic, when available.

Expected_Mu

Expected value of the statistic under the null hypothesis, when available.

Variance

Variance of the statistic under the null hypothesis, when available.

P_Value

Raw p-value returned by the test.

Error

Error message if the test failed; otherwise NA.

Reject_H0

Logical value indicating whether the null hypothesis is rejected at level alpha using the raw p-value.

Details

The overall null hypothesis is that the input sequence is randomly ordered, independent, and has no systematic trend or non-random structure. Each test evaluates this null hypothesis from a different perspective, such as runs, ranks, signs of differences, turning points, or monotonic trend.

The null hypothesis for the full battery is:

$$H_0: the sequence is randomly ordered, independent, and contains no systematic pattern.$$

The tests included are:

Bartels Rank Test

Tests whether the observations are randomly ordered, with no serial dependence or trend.

Cox Stuart Test

Tests whether there is no monotonic trend in the sequence.

Difference Sign Test

Tests whether successive differences show no systematic upward or downward tendency.

Runs Test

Tests whether the sequence is randomly ordered around a reference point, typically the median.

Turning Point Test

Tests whether the number of local peaks and valleys is consistent with randomness.

Rank Test

Tests whether the ordering of the observations is random, with no trend.

The Reject_H0 column indicates whether each test rejects its null hypothesis at the chosen alpha level using the raw p-value returned by that test.

Examples

library(randngen)
n <- 10000
seed <- 12345
lcg_digits <- lcg(n=n, seed=seed)
run_randtests(lcg_digits)
#>              Test                 Method Alternative     N    Statistic
#> 1    Bartels Rank     Bartels Ratio Test   two.sided 10000   -0.9519354
#> 2      Cox Stuart        Cox Stuart test   two.sided 10000 2491.0000000
#> 3 Difference Sign   Difference Sign Test   two.sided 10000    0.3290732
#> 4       Runs Test              Runs Test   two.sided 10000    1.0600530
#> 5   Turning Point     Turning Point Test   two.sided 10000   -1.9291642
#> 6       Rank Test Mann-Kendall Rank Test   two.sided 10000    0.2980996
#>   Statistic_Name Expected_Mu     Variance    P_Value Error Reject_H0
#> 1      statistic         2.0 3.999440e-04 0.34112974  <NA>     FALSE
#> 2      statistic          NA           NA 0.81001047  <NA>     FALSE
#> 3      statistic      4999.5 8.334167e+02 0.74210036  <NA>     FALSE
#> 4      statistic      5001.0 2.499750e+03 0.28912048  <NA>     FALSE
#> 5      statistic          NA           NA 0.05371048  <NA>     FALSE
#> 6      statistic  24997500.0 2.778194e+10 0.76562711  <NA>     FALSE