Interpreting the results of two tests of randomness

I have a series of more than 1000 numbers which are not supposed to exhibit any particular trend, regularity or non randomness. The numbers are the results of tests for the presence of substances in various samples of liquid kept in various conditions. The numbers are known to form a Poisson distribution, as most samples and treatments give low test values. I have run Bartels Rank Test and Mann Kendall's Test on the data using R, and I have these results:

Bartels Ratio Test

data:  c(t[, 3], t[, 4])
statistic = -0.27283, n = 1314, p-value = 0.785
alternative hypothesis: nonrandomness

Mann-Kendall Rank Test

data:  c(t[, 3], t[, 4])
statistic = -5.9172, n = 1314, p-value = 3.275e-09
alternative hypothesis: trend

Am I right in believing that these test results have opposite meanings, Bartel's saying the series is random and Mann-Kendall saying it isn't? Can anyone cast light on what the results are trying to tell me?

• Interpreting the results of statistical tests is not on-topic for Stack Overflow, a site dedicated to programming questions. If you have questions about statistical tests, you should ask them on Cross Validated. Dec 8 '16 at 15:54
• The assertion that Poisson distribution is known to describe this process because "most samples and treatments give low test values" is not in general true. The CV.com site will definitely be a better place to start, but I think advice to review the statistical errors implicit in the question will be necessary.
– DWin
Dec 8 '16 at 15:59
• Much insight can be had by plotting the data as a time series, along with a smoothed version of it. Consider using the square roots of the data for the plotting positions: any trends will become more evident.
– whuber
Dec 8 '16 at 17:01

The name for that test seems to me to be far too encompassing. It harkens back to a relatively early (1941) publication from von Neumann and so should not be taken as a comprehensive definition of "randomness". Furthermore the results are not testing for the same sort of departure from random ordering, so they do not have "opposite" meanings. The JSTOR site with academic access to the Bartel article exposes the first page which emphasizes the sequential aspect of the sought-for non-randomness. (I found that Bartel has made teh paper available at ResearchGate: https://www.researchgate.net/publication/230639951_The_Rank_Version_of_von_Neumann's_Ratio_Test_for_Randomness.)

The first one appears to be a general test for serial ordering which would be a particular type of departure from "randomness". This would be violated in cases where there is an oscillatory pattern as well as in cases where there were an upward or downward trend. (You don't say what function in R delivers these results and a search of the CRAN package library shows there are multiple choices for someone looking for a "Bartels Ratio Test". I'm guessing the one in DescTools.)

The second one is a test for a more specific sort of departure from random ordering, a "trend" test. Both an oscillatory and a "drift" upward or downward would also be expected to show with a positive test for auto-correlation. When testing for statistical departures from a state of "randomness" the ability to construct a powerful test will depend on the specific "alternative". A narrowly defined alternative will generally allow a more powerful test. The second test, I would have imagined, should narrow down the type of departure from a random serial ordering to that of a trend (of unspecified direction).

I found that the p-values of the implementation in DescTools::BartelsRankTest gave p-value results opposite to what I expected. I was expecting that an alterative of "oscillation" would give me low values for a contaminated sign-wave value and that it would give me high values for an alternative of "trend", so be careful if this is the function you are using. Make sure that you understand how the function behaves with distributions of known characteristics and maybe check against the results of similar testing in other packages such as: lawstat::bartels.test, randtests::bartels.rank.test, climtrends::Von.Neumann.ratio.rank, EnvStats::serialCorrelationTest, and SciencesPo:: Bartels

x <- sin(1:10000) + rnorm(10000)

BartelsRankTest(x, alternative="trend", method="beta")
#---------
Bartels Ratio Test

data:  x
statistic = -18.339, n = 10000, p-value < 2.2e-16
alternative hypothesis: trend
#--------
BartelsRankTest(x, alternative="oscillation", method="beta")
#--------
Bartels Ratio Test

data:  x
statistic = -18.339, n = 10000, p-value = 1
alternative hypothesis: systematic oscillation
#---------

You also might want to look up runs tests and tests of auto-correlation.

In R, the 'cartels.test' option 'positive.correlated' is for too few runs alternative hypotheses, and 'negative.correlation' for too many runs. Thus for a linear time series 'positive.correlated' will be significant:

> require(lawstat)
> plot(1:100+rnorm(100), type="b",xlim=c(0,100), xlab="Run", ylab="Data", pch=20)
> bartels.test(1:100, alternative = "positive")

Bartels Test - Positive Correlated

data:  1:100
Standardized Bartels Statistic = -9.9941,
RVN Ratio = 0.0011881, p-value < 2.2e-16

> bartels.test(1:100, alternative = "negative")

Bartels Test - Negative Correlated

data:  1:100
Standardized Bartels Statistic = -9.9941,
RVN Ratio = 0.0011881, p-value = 1

For oscillatory time series, 'negative.correlation' will be significant:

> plot(sin(pi/2*(seq(1,100,2)))+rnorm(100), type="b",xlim=c(0,100), xlab="Run", ylab="Data", pch=20)
> bartels.test((sin(pi/2*(seq(1,100,2)))+rnorm(100)), alternative = "positive")

Bartels Test - Positive Correlated

data:  (sin(pi/2 * (seq(1, 100, 2))) + rnorm(100))
Standardized Bartels Statistic = 5.2674, RVN
Ratio = 3.0535, p-value = 1

> bartels.test((sin(pi/2*(seq(1,100,2)))+rnorm(100)), alternative = "negative")

Bartels Test - Negative Correlated

data:  (sin(pi/2 * (seq(1, 100, 2))) + rnorm(100))
Standardized Bartels Statistic = 4.7944, RVN
Ratio = 2.9589, p-value = 8.16e-07

But oscillatory for the Bartels test means that 'diff(X)' changes sign for every run. If the oscillation takes more than one data point, it will yield 'less' runs than expected. This is the case with your example above  where the from a minimum to a maximum takes 3 runs (I used only 100 points to avoid a cluttered plot):

> plot(sin(1:100), type="b",xlim=c(0,100), xlab="Run", ylab="Data", pch=20)
> bartels.test((sin(1:100)), alternative = "positive")

Bartels Test - Positive Correlated

data:  (sin(1:100))
Standardized Bartels Statistic = -5.285, RVN
Ratio = 0.94299, p-value = 6.284e-08

> bartels.test((sin(1:100)), alternative = "negative")

Bartels Test - Negative Correlated

data:  (sin(1:100))
Standardized Bartels Statistic = -5.285, RVN
Ratio = 0.94299, p-value = 1