blib/lib/Statistics/ANOVA/JT.pm

filter: Statementstmt 100% 111 / 111 Branchbran 87.5% 7 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 96.6% 145 / 150 CC 12 SCAR 7.7
CC
12
Cov
98%
zprob_test
4.1
variance
3.0
_calc_j_value
3.0
CRAP 2.2
↑ Summary
1 package Statistics::ANOVA::JT;
2
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use base qw(Statistics::Data);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Algorithm::Combinatorics qw(combinations);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use List::AllUtils qw(sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Statistics::Data::Rank;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Statistics::Lite qw(count max);
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 =head1 NAME
13
14 Statistics::ANOVA::JT - Jonckheere-Terpstra statistics and test
15
16 =head1 VERSION
17
18 Version 0.01
19
20 =cut
21
22 5 our $VERSION = '0.01';
23
24 =head1 SYNOPSIS
25
26 use Statistics::ANOVA::JT;
27 my $jt = Statistics::ANOVA::JT->new();
28 $jt->load({1 => [2, 4, 6], 2 => [3, 3, 12], 3 => [5, 7, 11, 16]}); # note ordinal datanames
29 my $j_value = $jt->observed(); # or expected(), variance()
30 my ($z_value, $p_value) = $jt->zprob_test(ccorr => 2, tails => 1, correct_ties => 1);
31 # or without pre-loading:
32 $j_value = $jt->observed(data => {1 => [2, 4, 6], 2 => [5, 3, 12]});
33 # or for subset of loaded data:
34 $j_value = $jt->observed(lab => [1, 3]);
35
36 =head1 DESCRIPTION
37
38 Calculates Jonckheere-Terpstra statistics for sameness (common population) across given orders of independent variables. The statistics are based on a between-groups pooled ranking of the data, like the Kruskal-Wallis test, but, unlike Kruskall-Wallis that returns the same result regardless of order of levels, it takes into account ordinal value of the named data. As ordinal values, numerical intervals between the named values do not matter.
39
40 Data-loading and retrieval are as provided in L<Statistics::Data|Statistics::Data>, on which the JT object is C<base>d, so its other methods are available here.
41
42 Return values are tested on installation against published examples: in Hollander and Wolfe (1999), for sample MStat output on L<mcardle.wisc.edu|http://mcardle.wisc.edu/mstat/help/help/Notes-05.html>, and for the final I<Z>-value in the L<wikipedia|http://en.m.wikipedia.org/wiki/Jonckheere\'s_trend_test> example.
43
44 =head1 SUBROUTINES/METHODS
45
46 =head2 new
47
48 $jt = Statistics::ANOVA::JT->new();
49
50 New object for accessing methods and storing results. This "isa" Statistics::Data object.
51
52 =head2 observed
53
54 $val = $jt->observed(); # data pre-loaded
55 $val = $jt->observed(data => $hashref_of_arefs);
56
57 Returns the statistic I<J>: From between-group rankings of all possible pairwise splits of the data, accumulates I<J> as the sum of I<k>(I<k> - 1)/2 Mann-Whitney I<U> counts.
58
59 Optionally, if the data have not been pre-loaded, send as named argument B<data>.
60
61 =cut
62
63 2 ▶sub observed {
Subroutine observed: called CC=1 SCAR=0.0
Pod documented
64 2 my ( $self, %args ) = @_;
65 2 return _calc_j_value( _get_data($self, %args) );
66 }
67
68 =head2 expected
69
70 $val = $jt->expected(); # data pre-loaded
71 $val = $jt->expected(data => $hashref_of_arefs);
72
73 Returns the expected value of the I<J> statistic for the given data.
74
75 =cut
76
77 3 ▶sub expected {
Subroutine expected: called CC=1 SCAR=0.0
Pod documented
78 3 my ( $self, %args ) = @_;
79 3 return _calc_jexp( _get_data($self, %args) );
80 }
81
82 =head2 variance
83
84 $val = $jt->variance(); # data pre-loaded
85 $val = $jt->variance(data => $hashref_of_arefs);
86
87 Return the variance expected to occur in the I<J> values for the given data.
88
89 By default, the method accounts for and corrects for ties, but if C<correct_ties> = 0, the returned value is the usual "null" distribution variance, otherwise with an elaborate correction accounting for the number of tied variables and each of their sizes, as offered by Hollander & Wolfe (1999) Eq 6.19, p. 204.
90
91 =cut
92
93 2 ▶sub variance {
Subroutine variance: called CC=3 SCAR=11.0
Pod documented
94 2 my ( $self, %args ) = @_;
95 2 ▶ if ( defined $args{'correct_ties'} and $args{'correct_ties'} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'correct_ties'} and $args{'correct_ties'} == 0) { } 1 1 2
Condition66%2/3
defined $args{"correct_ties"} and $args{"correct_ties"} == 0
!ll&&!rl&&r
101
MC/DC50%1/2
defined $args{"correct_ties"} and $args{"correct_ties"} == 0
defined $args{"correct_ties"} $args{"correct_ties"} == 0
Truth table66%2/3
A and B
A= defined $args{"correct_ties"}B= $args{"correct_ties"} == 0
ABresult
0X0
100
111
96 1 return _calc_jvar_ig_ties( _get_data($self, %args) );
97 }
98 else {
99 1 return _calc_jvar_by_ties( _get_data($self, %args) );
100 }
101 }
102
103 =head2 zprob_test
104
105 $p_val = $jt->zprob_test(); # data pre-loaded
106 $p_val = $jt->zprob_test(data => $hashref_of_arefs);
107 ($z_val, $p_val) = $jt->zprob_test(); # get z-score too
108
109 Performs a z-test on the data and returns the associated probability; or, if called in array context, the z-value itself and then the probability value.
110
111 Rather than calculating the exact I<p>-value, calculates an expected I<J> value and variance, to provide a normalized I<J> for which the I<p>-value is read off the normal distribution. This is appropriate for "large" samples, e.g., greater-than 3 levels, with more than eight observations per level. Otherwise, read the value returned from C<$jt-E<gt>observed()> and look it up in a table of I<j>-values, such as in Hollander & Wolfe (1999), p. 649ff.
112
113 Optional arguments include B<correct_ties> (as above), and B<tails> and B<ccorr> as in L<Statistics::Zed|Statistics::Zed>. For example, to continuity correct by reducing the observed I<J>-value by 1 (recommended in some texts), set B<ccorr> => 2 (for half on either side of the expected value; if B<ccorr> => 1, then 0.5 is taken off the observed deviation, and so on). The default is not to continuity correct.
114
115 =cut
116
117 3 ▶sub zprob_test {
Subroutine zprob_test: called CC=4 SCAR=14.0
Pod documented
118 3 my ( $self, %args ) = @_;
119 3 my $href = _get_data($self, %args);
120 3 require Statistics::Zed;
121 3 my $zed = Statistics::Zed->new();
122 my ( $z_value, $p_value ) = $zed->z_value(
123 observed => _calc_j_value($href),
124 expected => _calc_jexp($href),
125 variance =>
126 3 ▶ ( defined $args{'correct_ties'} and $args{'correct_ties'} == 0 )
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'correct_ties'} && $args{'correct_ties'} == 0 ? : 1 2 3
Condition66%2/3
defined $args{"correct_ties"} and $args{"correct_ties"} == 0
!ll&&!rl&&r
201
MC/DC50%1/2
defined $args{"correct_ties"} and $args{"correct_ties"} == 0
defined $args{"correct_ties"} $args{"correct_ties"} == 0
Truth table66%2/3
A and B
A= defined $args{"correct_ties"}B= $args{"correct_ties"} == 0
ABresult
0X0
100
111
127 ? _calc_jvar_ig_ties($href)
128 : _calc_jvar_by_ties($href),
129 %args
130 );
131 3 ▶ return wantarray ? ( $z_value, $p_value ) : $p_value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
132 }
133
134 5 ▶sub _calc_j_value {
Subroutine _calc_j_value: called CC=3 SCAR=11.0
135 5 my $data = shift;
136 5 my $rankd = Statistics::Data::Rank->new();
137 5 my $j_value = 0;
138 5 for my $pairs ( combinations( [ keys %{$data} ], 2 ) ) {
139 15 my ( $p1, $p2 ) = @{$pairs};
140 my $ranks_href = $rankd->ranks_between(
141 15 data => { $p1 => $data->{$p1}, $p2 => $data->{$p2} } );
142 my %counts = (
143 15 $p1 => count( @{ $data->{$p1} } ),
144 15 $p2 => count( @{ $data->{$p2} } )
145 );
146 15 my $nprod = $counts{$p1} * $counts{$p2};
147 15 my @us = ();
148 15 for ( $p1, $p2 ) {
149 30 my $n = $counts{$_};
150 push @us,
151 $nprod +
152 ( ( $n * ( $n + 1 ) ) / 2 ) -
153 30 sum0( @{ $ranks_href->{$_} } );
154 }
155 15 $j_value += max(@us);
156 }
157 5 return $j_value;
158 }
159
160 6 ▶sub _calc_jexp {
Subroutine _calc_jexp: called CC=2 SCAR=6.9
161 6 my ($data) = @_;
162 6 my ( $nj, $sum_n ) = ( 0, 0 );
163 6 for ( keys %{$data} ) {
164 18 my $n = count( @{ $data->{$_} } );
165 18 $sum_n += $n;
166 18 $nj += $n**2;
167 }
168 6 return ( $sum_n**2 - $nj ) / 4;
169 }
170
171 2 ▶sub _calc_jvar_ig_ties {
Subroutine _calc_jvar_ig_ties: called CC=2 SCAR=6.9
172 2 my $data = shift;
173 2 my ( $sum_n, $v ) = ( 0, 0 );
174 2 for ( keys %{$data} ) {
175 6 my $n = count( @{ $data->{$_} } );
176 6 $sum_n += $n;
177 6 $v += $n**2 * ( 2 * $n + 3 );
178 }
179 2 return ( $sum_n**2 * ( 2 * $sum_n + 3 ) - $v ) / 72;
180 }
181
182 3 ▶sub _calc_jvar_by_ties {
Subroutine _calc_jvar_by_ties: called CC=2 SCAR=6.9
183 3 my $data = shift;
184 3 my $sum_n = 0;
185 3 my @ns = ();
186 3 for ( keys %{$data} ) {
187 9 my $n = count( @{ $data->{$_} } );
188 9 $sum_n += $n;
189 9 push @ns, $n;
190 }
191 3 my $ng = scalar( keys( %{$data} ) );
192 3 my $rankd = Statistics::Data::Rank->new();
193 3 my ( $uranks_href, $xtied, $gn ) =
194 $rankd->ranks_between( data => $data );
195 3 my $a2 = sum0( map { $_ * ( $_ - 1 ) * ( 2 * $_ + 5 ) } @ns );
196 3 my $a3 = sum0( map { $_ * ( $_ - 1 ) * ( 2 * $_ + 5 ) } @{$xtied} );
197 3 my $b2 = sum0( map { $_ * ( $_ - 1 ) * ( $_ - 2 ) } @ns );
198 3 my $b3 = sum0( map { $_ * ( $_ - 1 ) * ( $_ - 2 ) } @{$xtied} );
199 3 my $c2 = sum0( map { $_ * ( $_ - 1 ) } @ns );
200 3 my $c3 = sum0( map { $_ * ( $_ - 1 ) } @{$xtied} );
201 3 return ( 1 / 72 * ( $gn * ( $gn - 1 ) * ( 2 * $gn + 5 ) - $a2 - $a3 ) ) +
202 ( 1 / ( 36 * $gn * ( $gn - 1 ) * ( $gn - 2 ) ) * $b2 * $b3 ) +
203 ( 1 / ( 8 * $gn * ( $gn - 1 ) ) * $c2 * $c3 );
204 }
205
206 10 ▶sub _get_data {
Subroutine _get_data: called CC=2 SCAR=6.9
207 10 my ($self, %args) = @_;
208 10 my $hoa;
209 10 ▶ if (ref $args{'data'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'data'}) { } 2 8 10
210 2 $hoa = delete $args{'data'};
211 }
212 else {
213 8 $hoa = $self->get_hoa_by_lab(%args);
214 }
215 10 return $hoa;
216 }
217
218 =head1 REFERENCES
219
220 Hollander, M., & Wolfe, D. A. (1999). I<Nonparametric statistical methods>. New York, NY, US: Wiley.
221
222 =head1 DEPENDENCIES
223
224 L<Statistics::Data|Statistics::Data> : used as a C<base> for caching and retrieving data.
225
226 L<Statistics::Data::Rank|Statistics::Data::Rank> : used to implement between-sample ranking.
227
228 L<Statistics::Zed|Statistics::Zed> : for z-testing with optional continuity correction and tailing.
229
230 L<Algorithm::Combinatorics|Algorithm::Combinatorics> : provides the C<combinations> algorithm to provide all possible pairs of data-names to loop thru in calculating the observed I<J> value.
231
232 L<List::AllUtils|List::AllUtils> : provides the handy sum0() function
233
234 =head1 BUGS
235
236 Please report any bugs or feature requests to C<bug-statistics-anova-jt-0.01 at rt.cpan.org>, or through
237 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Statistics-ANOVA-JT-0.01>. I will be notified, and then you'll
238 automatically be notified of progress on your bug as I make changes.
239
240 =head1 SUPPORT
241
242 You can find documentation for this module with the perldoc command.
243
244 perldoc Statistics::ANOVA::JT
245
246 You can also look for information at:
247
248 =over 4
249
250 =item * RT: CPAN's request tracker (report bugs here)
251
252 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Statistics-ANOVA-JT-0.01>
253
254 =item * AnnoCPAN: Annotated CPAN documentation
255
256 L<http://annocpan.org/dist/Statistics-ANOVA-JT-0.01>
257
258 =item * CPAN Ratings
259
260 L<http://cpanratings.perl.org/d/Statistics-ANOVA-JT-0.01>
261
262 =item * Search CPAN
263
264 L<http://search.cpan.org/dist/Statistics-ANOVA-JT-0.01/>
265
266 =back
267
268 =head1 AUTHOR
269
270 Roderick Garton, C<< <rgarton at cpan.org> >>
271
272 =head1 LICENSE AND COPYRIGHT
273
274 Copyright 2015 Roderick Garton.
275
276 This program is free software; you can redistribute it and/or modify it
277 under the terms of either: the GNU General Public License as published
278 by the Free Software Foundation; or the Artistic License.
279
280 See L<http://dev.perl.org/licenses/> for more information.
281
282 =cut
283
284 5 1; # End of Statistics::ANOVA::JT
↑ Summary