# [R] Code find exact distribution for runs test?

Dale Steele dale.w.steele at gmail.com
Thu Feb 11 02:15:33 CET 2010

```I've been attempting to understand the one-sample run test for
randomness.  I've found run.test{tseries} and run.test{lawstat}.  Both
use a large sample approximation for distribution of the total number
of runs in a sample of n1 observations of one type and n2 observations
of another type.

I've been unable to find R code to generate the exact distribution and
would like to see how this could be done (not homework).

For example, given the data:

dtemp <- c(12, 13, 12, 11, 5, 2, -1, 2, -1, 3, 2, -6, -7, -7, -12, -9,
6, 7, 10, 6, 1, 1, 3, 7, -2, -6, -6, -5, -2, -1)

The Monte Carlo permutation approach seems to get me part way.

# calculate the number of runs in the data vector
nruns <- function(x) {
signs <- sign(x)
runs <- rle(signs)
r <- length(runs\$lengths)
return(r)
}

MC.runs <- function(x, nperm) {
RUNS <- numeric(nperm)
for (i in  1:nperm) {
RUNS[i] <- nruns(sample(x))
}
cdf <- cumsum(table(RUNS))/nperm
return(list(RUNS=RUNS, cdf=cdf, nperm=nperm))
}

MC.runs(dtemp, 100000)

Thanks.  --Dale

```