NAME

Statistics::Sequences::Turns - Kendall's test for turning-points - peaks or troughs - in a numerical sequence

SYNOPSIS

use Statistics::Sequences::Turns 0.031;
$turns = Statistics::Sequences::Turns->new();
$turns->load(0, 3, 9, 2 , 1, 1, 3, 4, 0, 3, 5, 5, 5, 8, 4, 7, 3, 2, 4, 3, 6); # or send with each stat call
$val = $turns->observed(); # also expected() and variance(), with optional arg trials => n in place of real data 
($val, $sig) = $turns->zscore(tails => 2, ccorr => 1); # Z = -0.0982471864864821, 2p = 0.92174
$turns->test()->dump(text => 1); # print of all the descriptives and zscore, lumping each into object as well

DESCRIPTION

For data of the continuous numerical type, a count of turns is incremented if the value on trial i, for i is greater than zero and less than n, is, with respect to its neighbours, a peak (greater than both neighbours) or a trough (less than both neighbours). Comparing this count with the expected number of turns, and the expected variance of this count, for a randomly generated sequence completes the test.

METHODS

new

$turns = Statistics::Sequences::Turns->new();

Returns a new Turns object. Expects/accepts no arguments but the classname.

load

$turns->load(@data);
$turns->load(\@data);
$turns->load('dist1' => \@data1, 'dist2' => \@data2)
$turns->load({'dist1' => \@data1, 'dist2' => \@data2})

Loads data anonymously or by name. See load in the Statistics::Sequences manpage.

add

See "add" in Statistics::Sequences

read

See "read" in Statistics::Sequences

unload

See "unload" in Statistics::Sequences

observed, turncount_observed, tco

$count = $turns->observed(); # assumes testdata have already been loaded
$count = $turns->observed(data => [qw/0 0 1 1 0 1 1 1 0 1/]);

Returns observed number of turns. This is the number of peaks and troughs, starting the count from index 1 of a flat array, checking if both its left/right (or past/future) neighbours are lesser than it (a peak) or greater than it (a trough). Wherever the values in successive indices of the list are equal, they are treated as a single observation/datum - so the following:

0 0 1 1 0 1 1 1 0 1

is counted up for turns as

0 1 0 1 0 1
  * * * *

So there are four turns in this example - two peaks (0 1 0) and two troughs (1 0 1). (If repeated, this sequence would significantly deviate from expectation, p = .035.)

The data to test can already have been loaded, or you send it here as a flat referenced array keyed as data.

expected, turncount_expected, tce

$val = $turns->expected(); # assumes testdata have already been loaded
$val = $turns->expected(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1]); # count these data
$val = $turns->expected(trials => 10); # use this trial number, assume no data

Returns the expected number of turns, which is simply set by N the number of trials/observations/sample-size ...:

  E[T] = 2 / 3 (N – 2)

variance, turncount_variance, tcv

$val = $turns->variance(); # assume the data are already "loaded" for counting
$val = $turns->variance(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1]); # count for these data
$val = $turns->variance(trials => number); # use this trial number 

Returns the expected variance in the number of turns for the given length of data N.

  V[T] = (16N – 29 ) / 90

zscore, turncount_zscore, tzs, z_value

$val = $turns->zscore(); # data already loaded, use default windows and prob
$val = $turns->zscore(data => $aref, ccorr => 1);
($zvalue, $pvalue) =  $turns->zscore(data => $aref, ccorr => 1, tails => 2); # same but wanting an array, get the p-value too

Returns the zscore from a test of turncount deviation, taking the turncount expected away from that observed and dividing by the root expected turncount variance, by default with a continuity correction in the numerator. Called wanting an array, returns the z-value with its p-value for the tails (1 or 2) given.

The data to test can already have been loaded, or you send it directly as a flat referenced array keyed as data.

test, turns_test, tnt

$joins->test();

Test the currently loaded data for significance of the number of turning-points. Returns the Turns object, lumped with a z_value, p_value, and the descriptives observed, expected and variance. Note: for turns there is "a fairly rapid tendency of the distribution to normality" (Kendall 1973, p. 24).

dump

$turns->dump(flag => '1|0', text => '0|1|2');

Print test results to STDOUT. See dump in the Statistics::Sequences manpage for details.

EXAMPLE

Seating at the diner

These are the data from Swed and Eisenhart (1943) also given as an example for the Runs test and Vnomes test. It lists the occupied (O) and empty (E) seats in a row at a lunch counter. Have people taken up their seats on a random basis? The Runs test suggested some non-random basis for people to take their seats, ouputting (as per dump):

Runs: observed = 11.00, expected = 7.88, Z = 1.60, 1p = 0.054834

That means there was more serial discontinuity than expected. What does the test of Turns tell us?

use Statistics::Sequences::Turns;
my $turns = Statistics::Sequences::Turns->new();
my @seating = (qw/E O E E O E E E O E E E O E O E/);
$turns->load(\@data);
$turns->binate(); # transform Es and Os into 1s and 0s
$turns->test(tails => 1)->dump();

This outputs, as returned by string:

Z = 1.95615199108988, 1p = 0.025224

So each seated person is neighboured by empty seats, and/or each empty seat is neighboured by seated persons, more so than would be expected if people were taking their seats randomly.

REFERENCES

Kendall, M. G. (1973). Time-series. London, UK: Griffin. [The test is described on pages 22-24; in the Example 2.1 for this test, the expected number of turns should be calculated with the value 52 (i.e., n - 2), not 54.]

SEE ALSO

Statistics::Sequences for other tests of sequences, and for sharing data between these tests.

TO DO/BUGS

Implementation of the serial test for non-overlapping v-nomes.

REVISION HISTORY

See CHANGES in installation dist for revisions.

AUTHOR/LICENSE

rgarton AT cpan DOT org

This program is free software. It may be used, redistributed and/or modified under the same terms as Perl-5.6.1 (or later) (see http://www.perl.com/perl/misc/Artistic.html).

DISCLAIMER

To the maximum extent permitted by applicable law, the author of this module disclaims all warranties, either express or implied, including but not limited to implied warranties of merchantability and fitness for a particular purpose, with regard to the software and the accompanying documentation.