blib/lib/Statistics/Sequences/Joins.pm

filter: Statementstmt 84.6% 83 / 98 Branchbran 71.4% 30 / 42 Conditioncond 42.8% 6 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 84.2% 16 / 19 Podpod 100% 9 / 9 totaltotal 76.4% 146 / 191 CC 28 SCAR 17.3
CC
28
Cov
77%
_count_pfrq
20.0
observed
7.1
_get_N_and_p
6.1
CRAP 5.6
↑ Summary
1 package Statistics::Sequences::Joins;
2 4 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use base qw(Statistics::Sequences);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use List::AllUtils qw(true uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Statistics::Zed 0.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 $Statistics::Sequences::Joins::VERSION = '0.20';
10
11 =pod
12
13 =head1 NAME
14
15 Statistics::Sequences::Joins - The Joins Test: Wishart-Hirschfeld statistics for frequency of alternations in a dichotomous sequence
16
17 =head1 VERSION
18
19 This is documentation for B<Version 0.20> of Statistics::Sequences::Joins.
20
21 =head1 SYNOPSIS
22
23 use Statistics::Sequences::Joins 0.20;
24 my $joins = Statistics::Sequences::Joins->new();
25 $joins->load([1, 0, 0, 0, 1, 1, 0, 1, 1, 1]); # bi-valued sequence
26 my $val = $joins->observed(); # or give "data => AREF" to stat methods
27 $val = $joins->expected(trials => 10, prob => .5); # sufficient, independent of data
28 $val = $joins->variance(trials => 10, prob => .5); # same
29 $val = $joins->z_value(tails => 1, ccorr => 1); # use loaded data
30 my ($z, $p) = $joins->z_value(tails => 1, ccorr => 1); # as above, but wantarray for z- and p-value
31 $p = $joins->p_value(tails => 1); # using loaded data
32 $val = $joins->z_value(trials => 10, observed => 4, tails => 1, ccorr => 1); # sufficicent
33 my $href = $joins->stats_hash(values => {observed => 1, p_value => 1}); # or other methods as attribs in the hashref
34 # print values to STDOUT:
35 $joins->dump(values => {observed => 1, expected => 1, p_value => 1}, format => 'line', flag => 1, precision_s => 3, precision_p => 7);
36
37 =head1 DESCRIPTION
38
39 A sequence of dichotomous, binary-valued, two-element events consists of zero or more alternations (or "joins") of those events. For example, joins are marked out with asterisks for the following sequence:
40
41 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 1 1 1 1 0 0
42 * * * * * * * *
43
44 So there's a join (of 0 and 1) at indices 1 and 2 (from zero), then immediately another join (of 1 and 0) at indices 2 and 3, and then another join at 5 and 6 ... for a total joincount of eight.
45
46 This module provides methods to calculate and return this observed joincount, and also the expected joincount and its variance for the number of trials and probability of each event, following the limiting form of the probability distribution of the number of joins in a binary-valued sequence given by Wishart and Hirschfeld (1936). This assumes that the probability that an event can take one or another value at each trial is constant over all trials. The concept might seem similar to L<runs|Statistics::Sequences::Runs> but runs are counted for each continuous segment between alternations, while it is blind to the length of these repetitions and even to event-probabilities.
47
48 =head1 METHODS
49
50 Methods include those described in L<Statistics::Sequences>, and have the same form as those in its other sub-modules, but naturally have specific operations as follows.
51
52 =head2 new
53
54 $joins = Statistics::Sequences::Joins->new();
55
56 Returns a new Joins object. Expects/accepts no arguments but the classname.
57
58 =head2 load
59
60 $joins->load(@data); # anonymously
61 $joins->load(\@data);
62 $joins->load('sample1' => \@data); # labelled whatever
63
64 Loads data anonymously or by name - see L<load|Statistics::Data/load, load_data> in the Statistics::Data manpage for details on the various ways data can be loaded and then retrieved (more than shown here). Here, the data are checked to ensure that they contain no more than two unique elements--if not, a C<carp> and return of 0 occurs. Every load unloads all previous loads and any additions to them.
65
66 Alternatively, skip this action; data don't have to be pre-loaded to use the stats methods here (see below).
67
68 =cut
69
70 2 ▶sub load {
Subroutine load: called CC=2 SCAR=7.3
Pod documented
71 2 my $self = shift;
72 2 $self->SUPER::load(@_);
73 2 my $data = $self->access( index => -1 );
74 2 my @uniq = uniq( @{$data} );
75 2 ▶ if ( scalar @uniq > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @uniq > 2) { } 0 2 2
76 0 carp __PACKAGE__, ' More than two elements were found in the data: '
77 . join( ' ', @uniq );
78 0 return 0;
79 }
80 else {
81 2 return 1;
82 }
83 }
84
85 =head2 add, access, unload
86
87 See L<Statistics::Data> for these additional operations on data that have been loaded.
88
89 =head2 observed
90
91 $count = $joins->observed(); # assumes data have already been loaded
92 $count = $joins->observed(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1]);
93
94 Returns the number of joins (or alternations) in a sequence - i.e., when, from the second trial onwards, the event on trial I<i> doesn't equal the event on trial I<i> - 1. For example, the following sequence adds up to 7 joins:
95
96 Sequence: 1 0 0 0 1 0 0 1 0 1 1 0
97 JoinCount: 0 1 1 1 2 3 3 4 5 6 6 7
98
99 Formally, for a sequence I<A> = {I<a>_I<i>} indexed from zero,
100
101 =for html <table cellpadding="0" cellspacing="0"><tr><td>&nbsp;&nbsp;</td><td valign="middle"><i>J</i> =&nbsp;</td><td valign="middle"><table cellpadding="0" cellspacing="0"><tr><td align="center"><sub><i>N</i>&ndash;1</sub></td></tr><tr><td align="center">&Sigma;</td></tr><tr><td align="center"><sup><i>i</i>=1</sup></td></tr></table></td><td valign="middle" style="font-size:large;">&nbsp;}&nbsp;</td><td valign="middle"><table cellpadding="0" cellspacing="0"><tr><td align="left">0,&nbsp;</td><td><i>a</i><sub><i>i</i></sub> =&nbsp;<i>a</i><sub><i>i</i>&ndash;1</sub></td></tr><tr><td align="left">1,&nbsp;</td><td>otherwise</td></tr></table></td></tr></table>
102
103 The sequence to test can have been already L<load|Statistics::Sequences::Joins/load>ed, or it can be sent directly to this method, keyed as B<data>. If no data are found by either of these ways, a C<croak> is heard.
104
105 =cut
106
107 8 ▶sub observed {
Subroutine observed: called CC=7 SCAR=19.7
Pod documented
108 8 my $self = shift;
109 8 ▶ my $args = ref $_[0] ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 3 8
110 8 ▶ my $data = ref $args->{'data'} ? $args->{'data'} : $self->access($args);
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'data'} ? : 5 3 8
111 croak 'No sequence of data to calculate joincout'
112 if not ref $data
113 8 ▶ or not scalar @{$data};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data and scalar @{$data;} 0 8 8
Condition33%1/3
ref $data and scalar @{$data;}
!ll&&!rl&&r
008
MC/DC0%0/2
ref $data and scalar @{$data;}
ref $data scalar @{$data;}
Truth table33%1/3
A and B
A= ref $dataB= scalar @{$data;}
ABresult
0X0
100
111
114 8 my $sum = 0;
115 8 for my $i ( 1 .. scalar @{$data} - 1 ) {
116 56 ▶ $sum++
Branch: 1 branch
BranchTrueFalseTotal
if $data->[$i] ne $$data[$i - 1] 27 29 56
117 if $data->[$i] ne $data->[ $i - 1 ]
118 ; # increment count if event is not same as last
119 }
120 8 return $sum;
121 }
122 4 *jco = \&observed;
123
124 =head2 expected
125
126 $val = $joins->expected(); # assumes data already loaded, uses default prob value (.5)
127 $val = $joins->expected(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1]); # count these data, use default prob value (.5)
128 $val = $joins->expected(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1], prob => .2); # count these data, use given prob value
129 $val = $joins->expected(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1], state => 1); # count off trial numbers and prob. of event
130 $val = $joins->expected(prob => .2, trials => 10); # use this trial number and probability of one of the 2 events
131
132 Returns the expected number of joins between the two possible elements of the given data, or for data of the given attributes, from Wishart and Hirschfeld (1936, p. 228):
133
134 =for html <p>&nbsp;&nbsp;<i>E[J]</i> = 2(<i>N</i> &ndash; 1)<i>p</i><i>q</i>
135
136 where I<N> is the number of observations/trials, I<p> is the expected probability of the joined event taking on its observed value, and I<q> is (1 - I<p>), the expected probability of the joined event I<not> taking on its observed value.
137
138 The data to test can already have been L<load|Statistics::Sequences::Joins/load>ed, or you send it directly keyed as B<data>. The data are only needed to count off the number of trials, and the proportion of 1s (or other given state of the two), if the B<trials> and B<prob> attributes are not defined. If B<state> is defined, then B<prob> is worked out from the actual data (as long as there are some, or 1/2 is assumed). If B<state> is not defined, B<prob> takes the value you give to it, or, if it too is not defined, then 1/2 (assuming equiprobability of the two events).
139
140 Counting up the observed number of joins needs some data to count through, but getting the expectation and variance for the joincount can just be fed with the number of B<trials>, and the B<prob>ability of one of the two events.
141
142 =cut
143
144 11 ▶sub expected {
Subroutine expected: called CC=2 SCAR=6.9
Pod documented
145 11 my $self = shift;
146 11 ▶ my $args = ref $_[0] ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 7 4 11
147 11 my ( $n, $p ) = _get_N_and_p( $self, $args );
148 11 return 2 * ( $n - 1 ) * $p * ( 1 - $p );
149 }
150 4 *jce = \&expected;
151
152 =head2 variance
153
154 $val = $joins->variance(); # assume data already "loaded" for counting
155 $val = $joins->variance(data => $aref); # use inplace array reference, will use default prob of 1/2
156 $val = $joins->variance(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1]); # count off trial numbers and prob. of event
157 $val = $joins->variance(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1], prob => prob); # specify the event prob (recommended)
158 $val = $joins->variance(trials => number, prob => prob); # sufficient statistics
159
160 Returns the expected variance in the number of joins for the given data, as estimated in Wishart and Hirschfeld (1936, p. 232), with a correction for small I<N> (the second term) given by Burdick and Kelly (1977, p. 106, Eq. 20) that is trivial for very large I<N>:
161
162 =for html <p>&nbsp;&nbsp;<i>V[J]</i> = 4<i>N</i><i>p</i><i>q</i>(1 &ndash; 3<i>p</i><i>q</i>) &ndash; 2<i>p</i><i>q</i>(3 &ndash; 10<i>p</i><i>q</i>)
163
164 with variables defined as above for L<expected|Statistics::Sequences::Joins/expected>. The default operation applies the Burdick-Kelly correction; this can be dodged by specifying B<ncorr> => 0.
165
166 The data to test can already have been L<load|Statistics::Sequences::Joins/load>ed, or it is given directly, keyed as B<data>. The data are only needed to count off the number of trials, and estimate the expected probability of the joined event, if the B<trials> and B<prob> attributes aren't defined. If B<state> is defined, then B<prob> is worked out from the actual data (as long as there are some, or expect a C<croak>). If B<state> is not defined, B<prob> takes the given value or, if it too is not defined, then 1/2 (assuming equiprobability of the two events).
167
168 =cut
169
170 14 ▶sub variance {
Subroutine variance: called CC=4 SCAR=13.9
Pod documented
171 14 my $self = shift;
172 14 ▶ my $args = ref $_[0] ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 6 8 14
173 14 my ( $n, $p ) = _get_N_and_p( $self, $args );
174 14 my $pq = $p * ( 1 - $p );
175 14 my $var = 4 * $n * $pq * ( 1 - 3 * $pq );
176 $var -= 2 * $pq * ( 3 - 10 * $pq )
177 if not defined $args->{'ncorr'}
178 14 ▶ or $args->{'ncorr'} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args->{"ncorr"} or $args->{"ncorr"} == 1 11 3 14
Condition100%3/3
not defined $args->{"ncorr"} or $args->{"ncorr"} == 1
l!l&&r!l&&!r
923
MC/DC100%2/2
not defined $args->{"ncorr"} or $args->{"ncorr"} == 1
not defined $args->{"ncorr"} $args->{"ncorr"} == 1
Truth table100%3/3
not(A) or B
A= not defined $args->{"ncorr"}B= $args->{"ncorr"} == 1
ABresult
1X1
011
000
179 14 return $var;
180 }
181 4 *jcv = \&variance;
182
183 =head2 obsdev
184
185 $v = $joins->obsdev(); # use data already loaded - anonymously; or specify its "label" or "index" - see observed()
186 $v = $joins->obsdev(data => [qw/blah bing blah blah blah/]); # use these data
187 $v = $joing->obsdev(observed => NUM, trials => NUM, prop => PROB); # sufficient
188
189 Returns the observed deviation: the observed I<less> expected joincount for the loaded/given sequence (I<O> - I<E>). Alias: C<observed_deviation>. Alternatively, the observed value might be given (as B<observed> => NUM), and so the method only has to get the expected value (as specified in L<expected|Statistics::Sequences::Joins/expected>).
190
191 =cut
192
193 1 ▶sub obsdev {
Subroutine obsdev: called CC=3 SCAR=11.4
Pod documented
194 1 my $self = shift;
195 1 ▶ my $args = ref $_[0] ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
196 my $obs =
197 defined $args->{'observed'}
198 1 ▶ ? $args->{'observed'}
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'observed'} ? : 0 1 1
199 : $self->observed($args);
200 1 return $obs - $self->expected($args);
201 }
202 4 *observed_deviation = \&obsdev;
203
204 =head2 stdev
205
206 $v = $joins->stdev(); # use data already loaded - anonymously; or specify its "label" or "index" - see observed()
207 $v = $joins->stdev(data => [qw/blah bing blah blah blah/]);
208
209 Returns the standard deviation (square-root of the variance). Alias: C<stantard_deviation>.
210
211 =cut
212
213 2 ▶sub stdev {
Subroutine stdev: called CC=1 SCAR=0.0
Pod documented
214 2 return sqrt( variance(@_) );
215 }
216 4 *standard_deviation = \&stdev;
217
218 =head2 z_value
219
220 $val = $joins->z_value(); # data already loaded, use default windows and prob
221 $val = $joins->z_value(data => $aref, prob => .5, ccorr => 1, ncorr => 1);
222 ($zvalue, $pvalue) = $joins->z_value(data => $aref, prob => .5, ccorr => 1, tails => 2); # same but wanting an array, get the p-value too
223
224 Returns the I<Z>-score from a test of joincount deviation, taking the joincount L<expected|Statistics::Sequences::Joins/expected> away from that L<observed|Statistics::Sequences::Joins/observed> and dividing by the root expected joincount L<variance|Statistics::Sequences::Joins/variance>, by default with a continuity correction (B<ccorr>) to expectation. Called in list context, returns the I<Z>-score with its I<p>-value for the B<tails> (1 or 2) specified (2 by default).
225
226 The data to test can already have been L<load|Statistics::Sequences::Joins/load>ed, or it is given directly, keyed as B<data>.
227
228 Other options are B<precision_s> (for the z_value) and B<precision_p> (for the p_value), and B<ncorr> for the (default) correction for small I<N>.
229
230 =cut
231
232 6 ▶sub z_value {
Subroutine z_value: called CC=6 SCAR=18.0
Pod documented
233 6 my $self = shift;
234 6 ▶ my $args = ref $_[0] ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 6 6
235 my $obs =
236 defined $args->{'observed'}
237 6 ▶ ? $args->{'observed'}
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'observed'} ? : 2 4 6
238 : $self->observed($args);
239 6 ▶ my $ccorr = defined $args->{'ccorr'} ? $args->{'ccorr'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'ccorr'} ? : 2 4 6
240 6 ▶ my $tails = $args->{'tails'} || 2;
Condition50%1/2
$args->{'tails'} || 2
l!l
06
MC/DC0%0/1
$args->{'tails'} || 2
$args->{'tails'}
Truth table50%1/2
A
A= $args->{'tails'}
Aresult
11
00
241 6 my $zed = Statistics::Zed->new();
242 my ( $zval, $pval ) = $zed->zscore(
243 observed => $obs,
244 expected => $self->expected($args),
245 variance => $self->variance($args),
246 ccorr => $ccorr,
247 tails => $tails,
248 precision_s => $args->{'precision_s'},
249 6 precision_p => $args->{'precision_p'},
250 );
251 6 ▶ return wantarray ? ( $zval, $pval ) : $zval;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 5 6
252 }
253 4 *jzs = \&z_value;
254 4 *joincount_zscore = \&z_value;
255 4 *zscore = \&z_value;
256
257 =head2 p_value
258
259 $p = $joins->p_value(); # using loaded data and default args
260 $p = $joins->p_value(ccorr => 0|1, tails => 1|2); # as above, with options
261 $p = $joins->p_value(data => [1, 0, 1, 1, 0]); # directly giving data (by-passing load and read)
262 $p = $joins->p_value(trials => NUM, observed => NUM, prob => PROB); # without using data
263
264 Returns the normal probability value for I<Z>-value given by taking the joincount L<expected|Statistics::Sequences::Joins/expected> away from that L<observed|Statistics::Sequences::Joins/observed> and dividing by the root expected joincount L<variance|Statistics::Sequences::Joins/variance>, by default with a continuity correction (B<ccorr>) to expectation and with B<tails> => 2. Data are those already L<load|Statistics::Sequences::Joins/load>ed, or as directly keyed as B<data>. In the absence of "data", the sufficient statistics of B<trials> and B<prob> are required (or, by default, B<prob> => 1/2 is used).
265
266 =cut
267
268 1 ▶sub p_value {
Subroutine p_value: called CC=1 SCAR=0.0
Pod documented
269 1 return ( z_value(@_) )[1];
270 }
271 4 *test = \&p_value;
272 4 *joins_test = \&p_value;
273 4 *jct = \&p_value;
274
275 =head2 stats_hash
276
277 $href = $joins->stats_hash(values => {observed => 1, expected => 1, variance => 1, z_value => 1, p_value => 1}, prob => .5, ccorr => 1);
278
279 Returns a hashref for the counts and stats as specified in its "values" argument, and with any options for calculating them (e.g., exact for p_value). See L<Statistics::Sequences/stats_hash> for details. If calling via a "joins" object, the option "stat => 'joins'" is not needed (unlike when using the parent "sequences" object).
280
281 =head2 dump
282
283 $joins->dump(values => { observed => 1, variance => 1, p_value => 1}, exact => 1, flag => 1, precision_s => 3); # among other options
284
285 Print Joins-test results to STDOUT. See L<dump|Statistics::Sequences/dump> in the Statistics::Sequences manpage for details.
286
287 =cut
288
289 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
290 0 my $self = shift;
291 0 ▶ my $args = ref $_[0] ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
292 0 $args->{'stat'} = 'joins';
293 0 $self->SUPER::dump($args);
294 0 return;
295 }
296
297 # returns two vars: (1) number of elements in the given "data" (sequence), and (2) relative frequency of a given state in the data
298 25 ▶sub _get_N_and_p {
Subroutine _get_N_and_p: called CC=6 SCAR=18.2
299 25 my ( $self, $args, $n, $data ) = @_;
300 25 ▶ if ( defined $args->{'trials'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'trials'}) { } 9 16 25
301 9 $n = $args->{'trials'};
302 }
303 else {
304 16 ▶ $data = ref $args->{'data'} ? $args->{'data'} : $self->access($args);
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'data'} ? : 10 6 16
305 16 $n = scalar @{$data};
306 }
307 my $p =
308 defined $args->{'prob'} ? $args->{'prob'}
309 : ( defined $args->{'state'} and defined $data )
310 25 ▶ ? _count_pfrq( $data, $args->{'state'} )
Branch: 2 branches
BranchTrueFalseTotal
defined $args->{'prob'} ? : 21 4 25
defined $args->{'state'} && defined $data ? : 0 4 4
Condition33%1/3
defined $args->{"state"} and defined $data
!ll&&!rl&&r
400
MC/DC0%0/2
defined $args->{"state"} and defined $data
defined $args->{"state"} defined $data
Truth table33%1/3
A and B
A= defined $args->{"state"}B= defined $data
ABresult
0X0
100
111
311 : .5;
312 25 return ( $n, $p );
313 }
314
315 0 ▶sub _count_pfrq {
Subroutine _count_pfrq: not called CC=4 SCAR=30.0
316 0 my ( $aref, $state, $count ) = @_;
317 0 ▶ return .5 if not ref $aref or not scalar @{$aref};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aref and scalar @{$aref;} 0 0 0
Condition0%0/3
ref $aref and scalar @{$aref;}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $aref and scalar @{$aref;}
ref $aref scalar @{$aref;}
Truth table0%0/3
A and B
A= ref $arefB= scalar @{$aref;}
ABresult
0X0
100
111
318 0 ▶ $count++ if true { $_ eq $state } @{$aref};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &true(sub { $_ eq $state; } , @{$aref;}) 0 0 0
319 0 return $count / scalar @{$aref};
320 }
321
322 4 1;
323
324 __END__
↑ Summary