
Last chance! 50% off unlimited learning
Sale ends in
Performs a test whether the elements of x
are serially independent - say, whether
they occur in a random order - by counting how many runs there are above and below a threshold.
If y
is supplied a two sample Wald-Wolfowitz-Test testing the equality of two distributions against general alternatives will be computed.
RunsTest(x, ...)# S3 method for default
RunsTest(x, y = NULL, alternative = c("two.sided", "less", "greater"),
exact = NULL, correct = TRUE, na.rm = FALSE, ...)
# S3 method for formula
RunsTest(formula, data, subset, na.action, …)
a dichotomous vector of data values or a (non-empty) numeric vector of data values.
an optional (non-empty) numeric vector of data values.
a formula of the form lhs ~ rhs
where lhs
gives the data values and rhs the corresponding groups.
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)
.
an optional vector specifying a subset of observations to be used.
a function which indicates what should happen when the data contain NAs. Defaults to getOption("na.action")
.
a character string specifying the alternative hypothesis, must be one of "two.sided"
(default), "less"
or "greater"
.
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 <= 30 and the normal approximation for longer ones.
a logical indicating whether to apply continuity correction when computing the test statistic. Default is TRUE
. Ignored if exact
is set to TRUE
. See details.
defines if NA
s should be omitted. Default is FALSE
.
further arguments to be passed to or from methods.
A list with the following components.
z, the value of the standardized runs statistic, if not exact p-values are computed.
the number of runs, the total number of zeros (m) and ones (n)
the p-value for the test.
a character string giving the names of the data.
a character string describing the alternative hypothesis.
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
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. This is not necessarily the best choice. If another threshold should be used use a code like: RunsTest(x > mean(x))
.
The exact distribution of runs and the p-value based on it are described in the manual of SPSS "Exact tests" http://www.sussex.ac.uk/its/pdfs/SPSS_Exact_Tests_21.pdf.
The normal approximation of the runs test is calculated with the expected number of runs under the null
Setting the continuity correction correct = TRUE
will yield the normal approximation as SAS (and SPSS if n < 50) does it, see http://support.sas.com/kb/33/092.html.
The c is set to
Wald-Wolfowitz with Ties. Ideally there should be no ties in the data used for the Wald-Wolfowitz test. In practice there is no problem with ties within a group, but if ties occur between members of the different groups then there is no unique sequence of observations. For example the data sets A: 10,14,17,19,34 and B: 12,13,17,19,22 can give four possible sequences, with two possible values for r (7 or 9). The "solution" to this is to list every possible combination, and calculate the test statistic for each one. If all test statistics are significant at the chosen level, then one can reject the null hypothesis. If only some are significant, then Siegel (1956) suggests that the average of the P-values is taken. Help for finding all permutations of ties can be found at: https://stackoverflow.com/questions/47565066/all-possible-permutations-in-factor-variable-when-ties-exist-in-r
However this solutions seems quite coarse and in general, the test should not be used if there are more than one or two ties. We have better tests to distinguish between two samples!
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.
Siegel, S. (1956) Nonparametric Statistics for the Behavioural Sciences, McGraw-Hill Kogakusha, Tokyo.
Run Length Encoding rle
# NOT RUN {
# 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, exact=TRUE) # exact probability
RunsTest(x, exact=FALSE) # normal approximation
# 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 DataLab