DescTools (version 0.99.8.1)

RunsTest: Runs Test for Randomness

Description

Performs a one sample runs test or a two sample Wald-Wolfowitz-Test on vectors of data.

Usage

RunsTest(x, ...)

## S3 method for class 'default':
RunsTest(x, y = NULL, alternative = c("two.sided", "less", "greater"), 
         exact = NULL, na.rm = FALSE, ...)

## S3 method for class 'formula':
RunsTest(formula, data, subset, na.action, \dots)

Arguments

x
a dichotomous vector of data values or a (non-empty) numeric vector of data values.
y
an optional (non-empty) numeric vector of data values.
formula
a formula of the form lhs ~ rhs where lhs gives the data values and rhs the corresponding groups.
data
an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula)
subset
an optional vector specifying a subset of observations to be used.
na.action
a function which indicates what should happen when the data contain NAs. Defaults to getOption("na.action").
alternative
a character string specifying the alternative hypothesis, must be one of "two.sided" (default), "less" or "greater".
exact
a logical indicating whether an exact p-value should be computed. By default exact values will be calculated for small vectors with a total length
na.rm
defines if NAs should be omitted. Default is FALSE.
...
further arguments to be passed to or from methods.

Value

  • A list with the following components.
  • statisticz, the value of the standardized runs statistic, if not exact p-values are computed.
  • parameterthe number of runs, the total number of zeros (m) and ones (n)
  • p.valuethe p-value for the test.
  • data.namea character string giving the names of the data.
  • alternativea character string describing the alternative hypothesis.

Details

The runs test for randomness is used to test the hypothesis that a series of numbers is random. The 2-sample test is known as the Wald-Wolfowitz test. For a categorical variable, the number of runs correspond to the number of times the category changes, that is, where $x_{i}$ belongs to one category and $x_{i+1}$ belongs to the other. The number of runs, is the number of sign changes plus one. For a numeric variable x containing more than two values, a run is a set of sequential values that are either all above or below a specified cutpoint, typically the median.

References

Wackerly, D., Mendenhall, W. Scheaffer, R. L. (1986): Mathematical Statistics with Applications, 3rd Ed., Duxbury Press, CA. Wald, A. and Wolfowitz, J. (1940): On a test whether two samples are from the same population, Ann. Math Statist. 11, 147-162.

See Also

Run Length Encoding rle

Examples

Run this code
# x will be coerced to a dichotomous variable
x <- c("S","S", "T", "S", "T","T","T", "S", "T")
RunsTest(x)


x <- c(13, 3, 14, 14, 1, 14, 3, 8, 14, 17, 9, 14, 13, 2, 16, 1, 3, 12, 13, 14)
RunsTest(x)
# this will be treated as
RunsTest(x < median(x))

plot( (x < median(x)) - 0.5, type="s", ylim=c(-1,1) )
abline(h=0)

set.seed(123)
x <- sample(0:1, size=100, replace=TRUE)
RunsTest(x)
# As you would expect of values from a random number generator, the test fails to reject 
# the null hypothesis that the data are random. 


# SPSS example
x <- c(31,23,36,43,51,44,12,26,43,75,2,3,15,18,78,24,13,27,86,61,13,7,6,8)
RunsTest(x)
RunsTest(x, exact=TRUE)

# SPSS example small dataset
x <- c(1, 1, 1, 1, 0, 0, 0, 0, 1, 1)
RunsTest(x)
RunsTest(x, exact=FALSE)

# if y is not NULL, the Wald-Wolfowitz-Test will be performed
A <- c(35,44,39,50,48,29,60,75,49,66)
B <- c(17,23,13,24,33,21,18,16,32)

RunsTest(A, B, exact=TRUE)
RunsTest(A, B, exact=FALSE)

Run the code above in your browser using DataCamp Workspace