blib/lib/Statistics/ANOVA/Page.pm

filter: Statementstmt 85.5% 77 / 90 Branchbran 50.0% 5 / 10 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 86.3% 19 / 22 Podpod 100% 7 / 7 totaltotal 80.2% 110 / 137 CC 8 SCAR 7.1
CC
8
Cov
80%
_set_tails
5.2
chiprob_test
3.0
_get_data
2.1
CRAP 2.0
↑ Summary
1 package Statistics::ANOVA::Page;
2
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw(Statistics::Data);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::AllUtils qw(sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Math::Cephes qw(:dists);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Statistics::Data::Rank;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Statistics::Zed;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 $Statistics::ANOVA::Page::VERSION = '0.02';
13
14 =head1 NAME
15
16 Statistics::ANOVA::Page - Nonparametric analysis of variance by ranks for trend across repeatedly measured variables (Page and sign tests).
17
18 =head1 VERSION
19
20 This is documentation for B<Version 0.02> of Statistics::ANOVA::Page.
21
22 =head1 SYNOPSIS
23
24 use Statistics::ANOVA::Page;
25 my $page = Statistics::ANOVA::Page->new();
26 $page->load({1 => [2, 4, 6], 2 => [3, 3, 12], 3 => [5, 7, 11, 16]}); # note ordinal datanames
27 my $l_value = $page->observed(); # or expected(), variance()
28 my ($z_value, $p_value) = $page->zprob_test(ccorr => 2, tails => 1);
29 # or without pre-loading:
30 $l_value = $page->observed(data => {1 => [2, 4, 6], 2 => [5, 3, 12]});
31 # or for subset of loaded data:
32 $l_value = $page->observed(lab => [1, 3]);
33
34 =head1 DESCRIPTION
35
36 Calculates Page statistics for nonparametric analysis of variance across given orders of repeatedly measured variables. Ranks are computed exactly as for the L<Friedman|Statistics::ANOVA::Friedman> test, but the ranks are weighted according to the ordinal position of the group/level to which they pertain. Also, the test of significance is based on a standardized value, with the I<p>-value read off the normal distribution. Similarly to the relationship between the Kruskal-Wallis and L<Jonckheere-Terpstra|Statistics::ANOVA::JT> tests for non-dependent observations, the Friedman test returns the same value regardless of the ordinality of the variables as levels, but the Page test is of ranks in association with the ordinality of the variables (as levels rather than groups). These are weighted according to their Perl sort { $a <=> $b} order, so they should have sort-able names that reflect the ordering of the variables.
37
38 With only two groups, the test statistic is equivalent to that provided by a B<sign test>.
39
40 Build tests include comparison of return values with published data, viz. from Hollander and Wolfe (1999, p. 286ff); passing these tests means the results agree.
41
42 =head1 SUBROUTINES/METHODS
43
44 =head2 new
45
46 $page = Statistics::ANOVA::Page->new();
47
48 New object for accessing methods and storing results. This "isa" Statistics::Data object.
49
50 =head2 load, add, unload
51
52 $page->load(1 => [1, 4], 2 => [3, 7]);
53
54 The given data can now be used by any of the following methods. This is inherited from L<Statistics::Data|Statistics::Data>, and all its other methods are available here via the class object. Only passing of data as a hash of arrays (HOA) is supported for now. Alternatively, give each of the following methods the HOA for the optional named argument B<data>.
55
56 =head2 observed
57
58 $val = $page->observed(); # data pre-loaded
59 $val = $page->observed(data => $hashref_of_arefs);
60
61 Returns the observed statistic I<L> based on within-group rankings of the data weighted according to the ordinal position of the variable (by its numerical name) to which they pertain.
62
63 Optionally, if the data have not been pre-loaded, send as named argument B<data>.
64
65 =cut
66
67 1 ▶sub observed {
Subroutine observed: called CC=1 SCAR=0.0
Pod documented
68 1 my ( $self, %args ) = @_;
69 1 return _calc_l_value( _get_data( $self, %args ) );
70 }
71
72 =head2 observed_r
73
74 $val = $page->observed_r(); # data pre-loaded
75 $val = $page->observed_r(data => $hashref_of_arefs);
76
77 This implements a "l2r" transformation: Hollander and Wolfe (1999) describe how Page's I<L>-statistic is directly related to Spearman's rank-order correlation coefficient (see L<Statistics::RankCorrelation|Statistics::RankCorrelation>), based on the observed and predicted order of each associated group/level per observation.
78
79 =cut
80
81 0 ▶sub observed_r {
Subroutine observed_r: not called CC=1 SCAR=6.9
Pod documented
82 0 my ( $self, %args ) = @_;
83 0 return _calc_l2r_value( _get_data( $self, %args ) );
84 }
85
86 =head2 expected
87
88 $val = $page->expected(); # data pre-loaded
89 $val = $page->expected(data => $hashref_of_arefs);
90
91 Returns the expected value of the I<L> statistic for the given data.
92
93 =cut
94
95 1 ▶sub expected {
Subroutine expected: called CC=1 SCAR=0.0
Pod documented
96 1 my ( $self, %args ) = @_;
97 1 return _calc_l_exp( _get_data( $self, %args ) );
98 }
99
100 =head2 variance
101
102 $val = $page->variance(); # data pre-loaded
103 $val = $page->variance(data => $hashref_of_arefs);
104
105 Return the variance expected to occur in the I<L> values for the given data.
106
107 =cut
108
109 1 ▶sub variance {
Subroutine variance: called CC=1 SCAR=0.0
Pod documented
110 1 my ( $self, %args ) = @_;
111 1 return _calc_l_var( _get_data( $self, %args ) );
112 }
113
114 =head2 zprob_test
115
116 $p_val = $page->zprob_test(); # data pre-loaded
117 $p_val = $page->zprob_test(data => $hashref_of_arefs);
118 ($z_val, $p_val) = $page->zprob_test(); # get z-score too
119
120 Calculates an expected I<L> value and variance, to provide a normalized I<L> for which the I<p>-value is read off the normal distribution. This is appropriate for "large" samples. Optional arguments are B<tails> and B<ccorr> as in L<Statistics::Zed|Statistics::Zed>.
121
122 =cut
123
124 1 ▶sub zprob_test {
Subroutine zprob_test: called CC=2 SCAR=7.0
Pod documented
125 1 my ( $self, %args ) = @_;
126 1 my $href = _get_data( $self, %args );
127 1 my $zed = Statistics::Zed->new();
128 1 my ( $z_value, $p_value ) = $zed->z_value(
129 observed => _calc_l_value($href),
130 expected => _calc_l_exp($href),
131 variance => _calc_l_var($href),
132 %args
133 );
134 1 ▶ return wantarray ? ( $z_value, $p_value ) : $p_value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
135 }
136
137 =head2 chiprob_test
138
139 $p_val = $page->chiprob_test(); # data pre-loaded
140 $p_val = $page->chiprob_test(data => $hashref_of_arefs);
141 ($chi_val, $df, $num, $p_val) = $page->chiprob_test();
142
143 Calculates a chi-square statistic based on the observed value of I<L>, the number of ranked variables, and the number of replications; as per Page(1963, Eq. 4). This is a two-tailed test; if the optional argument B<tails> => 1, the returned probability, read off the chi-square distribution, is halved. Called in scalar context, returns the I<p>-value alone. Called in list context, returns the chi-square value, the degrees-of-freedom, number of observations, and then the I<p>-value.
144
145 =cut
146
147 1 ▶sub chiprob_test {
Subroutine chiprob_test: called CC=3 SCAR=11.0
Pod documented
148 1 my ( $self, %args ) = @_;
149 1 my $data_href = _get_data( $self, %args );
150 1 my $l = _calc_l_value($data_href);
151 1 my $n_bt = scalar keys %{$data_href};
152 1 my $n_wt = __PACKAGE__->equal_n( data => $data_href );
153 1 my $num = ( ( 12 * $l ) - ( 3 * $n_wt * $n_bt ) * ( $n_bt + 1 )**2 )**2;
154 1 my $den = ( ( $n_wt * $n_bt**2 ) * ( $n_bt**2 - 1 ) * ( $n_bt + 1 ) );
155 1 ▶ croak 'Chi-square probability test not available given limited number of observations' if ! $den;
Branch: 1 branch
BranchTrueFalseTotal
unless $den 0 1 1
156 1 my $chi = $num / $den;
157 1 my $p_value = _set_tails( chdtrc( 1, $chi ), $args{'tails'} ); # Math::Cephes fn
158 1 ▶ return wantarray ? ( $chi, 1, ( $n_bt * $n_wt ), $p_value ) : $p_value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
159 }
160
161 =head2 chiprob_str
162
163 $str = $page->chiprob_str(data => HOA, correct_ties => 1);
164
165 Performs L<chiprob_test|Statistics::ANOVA::Friedman/chiprob_test> and returns a string of the conventional reporting form, e.g., chi^2(df, N = total observations) = chi_value, p = p_value.
166
167 =cut
168
169 0 ▶sub chiprob_str {
Subroutine chiprob_str: not called CC=1 SCAR=6.9
Pod documented
170 0 my ( $self, %args ) = @_;
171 0 my ( $chi_value, $df, $count, $p_value ) = $self->chiprob_test(%args);
172 0 return "chi^2($df, N = $count) = $chi_value, p = $p_value";
173 }
174
175 3 ▶sub _calc_l_value {
Subroutine _calc_l_value: called CC=1 SCAR=0.0
176 3 my $data = shift;
177 3 my $ranks = Statistics::Data::Rank->sum_of_ranks_within( data => $data );
178 3 my $c = 0;
179 return sum0(
180 15 map { ++$c * $ranks->{$_} }
181 3 sort { $a <=> $b } keys %{$ranks}
182 );
183 }
184
185 0 ▶sub _calc_l2r_value {
Subroutine _calc_l2r_value: not called CC=1 SCAR=6.9
186 0 my $data = shift;
187 0 my $l = _calc_l_value($data);
188 0 my $n_bt = scalar keys %{$data};
189 0 my $n_wt = __PACKAGE__->equal_n( data => $data );
190 0 return ( ( ( 12 * $l ) / ( $n_wt * $n_bt * ( $n_bt**2 - 1 ) ) ) -
191 ( ( 3 * ( $n_bt + 1 ) ) / ( $n_bt - 1 ) ) );
192 }
193
194 2 ▶sub _calc_l_exp {
Subroutine _calc_l_exp: called CC=1 SCAR=0.0
195 2 my $data = shift;
196 2 my $n_bt = scalar keys %{$data};
197 2 my $n_wt = __PACKAGE__->equal_n( data => $data );
198 2 return ( $n_wt * $n_bt * ( $n_bt + 1 )**2 ) / 4;
199 }
200
201 2 ▶sub _calc_l_var {
Subroutine _calc_l_var: called CC=1 SCAR=0.0
202 2 my $data = shift;
203 2 my $n_bt = scalar keys %{$data};
204 2 my $n_wt = __PACKAGE__->equal_n( data => $data );
205 2 return ( $n_wt * $n_bt**2 * ( $n_bt + 1 ) * ( $n_bt**2 - 1 ) ) / 144;
206 }
207
208 1 ▶sub _set_tails {
Subroutine _set_tails: called CC=4 SCAR=16.4
209 1 my ($p_value, $tails) = @_;
210 1 ▶ $tails ||= 2;
Condition50%1/2
$tails ||= 2
l!l
01
MC/DC0%0/1
$tails ||= 2
$tails
Truth table50%1/2
A
A= $tails
Aresult
11
00
211 1 ▶ if (defined $tails and $tails == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tails and $tails == 1) 0 1 1
Condition33%1/3
defined $tails and $tails == 1
!ll&&!rl&&r
010
MC/DC0%0/2
defined $tails and $tails == 1
defined $tails $tails == 1
Truth table33%1/3
A and B
A= defined $tailsB= $tails == 1
ABresult
0X0
100
111
212 0 $p_value /= 2;
213 }
214 1 return $p_value;
215 }
216
217 5 ▶sub _get_data {
Subroutine _get_data: called CC=2 SCAR=7.2
218 5 my ( $self, %args ) = @_;
219 5 my $hoa;
220 5 ▶ if ( ref $args{'data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'data'}) { } 0 5 5
221 0 $hoa = delete $args{'data'};
222 }
223 else {
224 5 $hoa = $self->get_hoa_by_lab(%args);
225 }
226 5 return $hoa;
227 }
228
229 =head1 DIAGNOSTICS
230
231 =over 4
232
233 =item Chi-square probability test not available given limited number of observations
234
235 C<croak>ed if the denominator value to calculate the chi-square statistic is not defined or zero, which would arise if there is only one between-group level, or zero observations within the levels.
236
237 =item Equal number of observations required for calculating ranks within groups
238
239 C<croak>ed via Statistics::Data::Rank *_ranks_within methods given that they need to have the same number of observations per group; as when the different factor levels are repeatedly measured on the same replicants. Most methods require this to be the case.
240
241 =back
242
243 =head1 REFERENCES
244
245 Hollander, M., & Wolfe, D. A. (1999). I<Nonparametric statistical methods>. New York, NY, US: Wiley.
246
247 Page, E. B. (1963). Ordered hypotheses for multiple treatments: A significance test for linear ranks. I<Journal of the American Statistical Association>, I<58>, 216-230. doi: L<10.1080/01621459.1963.10500843|http://dx.doi.org/10.1080/01621459.1963.10500843>. [L<JSTOR|http://www.jstor.org/stable/2282965>]
248
249 =head1 DEPENDENCIES
250
251 L<List::AllUtils|List::AllUtils> : provides the handy sum0() function
252
253 L<Math::Cephes|Math::Cephes> : used for probability functions.
254
255 L<Statistics::Data|Statistics::Data> : used as a C<base> for caching and retrieving data.
256
257 L<Statistics::Data::Rank|Statistics::Data::Rank> : used to implement cross-case ranking.
258
259 L<Statistics::Zed|Statistics::Zed> : for z-testing with optional continuity correction and tailing.
260
261 =head1 AUTHOR
262
263 Roderick Garton, C<< <rgarton at cpan.org> >>
264
265 =head1 BUGS AND LIMITATIONS
266
267 Please report any bugs or feature requests to C<bug-statistics-anova-page-0.02 at rt.cpan.org>, or through
268 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Statistics-ANOVA-Page-0.02>. I will be notified, and then you'll
269 automatically be notified of progress on your bug as I make changes.
270
271 =head1 SUPPORT
272
273 You can find documentation for this module with the perldoc command.
274
275 perldoc Statistics::ANOVA::Page
276
277
278 You can also look for information at:
279
280 =over 4
281
282 =item * RT: CPAN's request tracker (report bugs here)
283
284 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Statistics-ANOVA-Page-0.02>
285
286 =item * AnnoCPAN: Annotated CPAN documentation
287
288 L<http://annocpan.org/dist/Statistics-ANOVA-Page-0.02>
289
290 =item * CPAN Ratings
291
292 L<http://cpanratings.perl.org/d/Statistics-ANOVA-Page-0.02>
293
294 =item * Search CPAN
295
296 L<http://search.cpan.org/dist/Statistics-ANOVA-Page-0.02/>
297
298 =back
299
300
301 =head1 ACKNOWLEDGEMENTS
302
303
304 =head1 LICENSE AND COPYRIGHT
305
306 Copyright 2015-2017 Roderick Garton.
307
308 This program is free software; you can redistribute it and/or modify it
309 under the terms of either: the GNU General Public License as published
310 by the Free Software Foundation; or the Artistic License.
311
312 See L<http://dev.perl.org/licenses/> for more information.
313
314
315 =cut
316
317 3 1; # End of Statistics::ANOVA::Page
↑ Summary