blib/lib/Statistics/ANOVA/KW.pm

filter: Statementstmt 87.5% 70 / 80 Branchbran 53.5% 15 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 14 / 16 Podpod 100% 5 / 5 totaltotal 78.3% 105 / 134 CC 16 SCAR 13.2
CC
16
Cov
77%
fprob_test
6.4
fprob_str
6.0
chiprob_test
5.6
CRAP 3.7
↑ Summary
1 package Statistics::ANOVA::KW;
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::Lite qw(mean);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 $Statistics::ANOVA::KW::VERSION = '0.01';
13
14 =head1 NAME
15
16 Statistics::ANOVA::KW - Kruskall-Wallis statistics and test (nonparametric independent analysis of variance by ranks for nominally grouped data)
17
18 =head1 VERSION
19
20 This is documentation for B<Version 0.01> of Statistics::ANOVA::KW.
21
22 =head1 SYNOPSIS
23
24 use Statistics::ANOVA::KW;
25 my $kw = Statistics::ANOVA::KW->new();
26 $kw->load({1 => [2, 4, 6], 2 => [3, 3, 12], 3 => [5, 7, 11, 16]});
27 my $h_value = $kw->h_value(); # default used to correct for ties
28 my $p_value = $kw->chiprob_test(); # H taken as chi^2 distributed
29 my ($h_value, $df, $count, $p_value_by_chi, $phi) = $kw->chiprob_test(); # same as above, called in array context
30 my ($f_value, $df_b, $df_w, $p_value_by_f, $omega_sq) = $kw->fprob_test(); # F-equivalent value tests
31
32 # or without pre-loading, and specify correct_ties as well:
33 $h_value = $kw->h_value(data => {1 => [2, 4, 6], 2 => [5, 3, 12]}, correct_ties => 1);
34 # or test only a subset of the loaded data:
35 $h_value = $kw->h_value(lab => [1, 3]);
36
37 =head1 DESCRIPTION
38
39 Performs calculations for the Kruskal-Wallis one-way nonparametric analysis of variance by ranks. This is for (at least) ordinal-level measurements of two or more samples of a nominal/categorical variable with equality of variances across the samples. The test is unreliable for small number of observations per sample (conventionally, all samples should have more than five observations). See L<REFERENCES|Statistics::ANOVA::KW/REFERENCES> for more information, and discussions of the assumptions/interpretations, and pros/cons, of the test at L<laerd statistics|https://statistics.laerd.com/spss-tutorials/kruskal-wallis-h-test-using-spss-statistics.php> (pro) and L<biostathandbook|http://www.biostathandbook.com/kruskalwallis.html> (con). Note that the Kruskall-Wallis test is often described as a test for I<three> or more samples, in contrast to the Mann-Whitney test, which is restricted to two samples, but KW can also be used with only two samples: the absolute value of the I<z>-value from a Mann-Whitney test equals the square-root of the KW statistic for two factors.
40
41 Data-loading and retrieval are as provided in L<Statistics::Data|Statistics::Data>, on which this module's class object is C<base>d, so its other methods are available here.
42
43 Return values are tested on installation against published examples and output from other software (e.g., SPSS).
44
45 =head2 new
46
47 $kw = Statistics::ANOVA::KW->new();
48
49 New object for accessing methods and storing results. This "isa" Statistics::Data object.
50
51 =head2 load, add, unload
52
53 $kw->load('a' => [1, 4, 3.2], 'b' => [6.5, 6.5, 9], 'c' => [3, 7, 4.4]);
54
55 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. Once loaded, subsets of the loaded data can be tested by passing their names (or labels) in a referenced array to the argument B<lab> in the following methods (as supported by L<Statistics::Data|Statistics::Data/get_hoa_by_lab_numonly_indep>). Once loaded, any non-numeric values in the samples are culled ahead of running the following methods.
56
57 Alternatively, without pre-loading the data, directly give the following methods the HOA of data as the value for the optional named argument B<data>.
58
59 =cut
60
61 =head2 h_value
62
63 $h_value = $kw->h_value(data => \%data, correct_ties => 1);
64 $h_value = $kw->h_value(); # assuming data have already been loaded, & default of TRUE for correct_ties
65
66 Returns the Kruskall-Wallis I<H> statistic.
67
68 =cut
69
70 4 ▶sub h_value {
Subroutine h_value: called CC=4 SCAR=14.2
Pod documented
71 4 my ( $self, %args ) = ( shift, @_ );
72 my $data =
73 $args{'data'}
74 4 ▶ ? delete $args{'data'}
Branch: 1 branch
BranchTrueFalseTotal
$args{'data'} ? : 0 4 4
75 : $self->get_hoa_by_lab_numonly_indep(%args);
76 my $correct_ties = defined $args{'correct_ties'}
77 4 ▶ and $args{'correct_ties'} == 0 ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
my $correct_ties = defined $args{"correct_ties"} and $args{"correct_ties"} == 0 ? '???' : '???' 4 0 4
$args{'correct_ties'} == 0 ? : 1 3 4
78 4 return ( _kw_stats( $data, $correct_ties ) )[0];
79 }
80
81 =head2 chiprob_test
82
83 ($chi_value, $df, $count, $p_value, $phi) = $kw->chiprob_test(data => HOA, correct_ties => 1); # H as chi-square
84 $p_value = $kw->chiprob_test(data => HOA, correct_ties => 1);
85 $p_value = $kw->chiprob_test(); # assuming data have already been loaded, & default of TRUE for correct_ties
86
87 Performs the ANOVA and, assuming I<chi>-square distribution of the Kruskall-Wallis I<H> value, returns its value, its degrees-of-freedom, the total number of observations (I<N>), its I<chi>-square probability value, and I<phi>-coefficient as an estimate of effect-size ( = square-root of (I<chi>-square divided by I<N>) ). Returns only the I<p>-value if called in scalar context. Default value of optional argument B<correct_ties> is 1.
88
89 =cut
90
91 1 ▶sub chiprob_test {
Subroutine chiprob_test: called CC=5 SCAR=17.2
Pod documented
92 1 my ( $self, %args ) = ( shift, @_ );
93 my $data =
94 $args{'data'}
95 1 ▶ ? delete $args{'data'}
Branch: 1 branch
BranchTrueFalseTotal
$args{'data'} ? : 0 1 1
96 : $self->get_hoa_by_lab_numonly_indep(%args);
97 my $correct_ties = defined $args{'correct_ties'}
98 1 ▶ and $args{'correct_ties'} == 0 ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
my $correct_ties = defined $args{"correct_ties"} and $args{"correct_ties"} == 0 ? '???' : '???' 1 0 1
$args{'correct_ties'} == 0 ? : 1 0 1
99 1 my ( $chi, $df_b, $count ) = _kw_stats( $data, $correct_ties );
100 1 my $p_value = chdtrc( $df_b, $chi ); # Math::Cephes fn
101 return
102 wantarray
103 1 ▶ ? ( $chi, $df_b, $count, $p_value, sqrt( $chi / $count ) )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
104 : $p_value;
105 }
106
107 =head2 chiprob_str
108
109 $str = $kw->chiprob_str(data => HOA, correct_ties => 1);
110 $str = $kw->chiprob_str(); # assuming data have already been loaded, & default of TRUE for correct_ties
111
112 Performs the same test as for L<chiprob_test|Statistics::ANOVA::KW/chiprob_test> but returns not an array but a string of the conventional reporting form, e.g., chi^2(df, N = total observations) = chi_value, p = p_value.
113
114 =cut
115
116 0 ▶sub chiprob_str {
Subroutine chiprob_str: not called CC=1 SCAR=6.9
Pod documented
117 0 my ( $self, %args ) = ( shift, @_ );
118 0 my ( $chi_value, $df, $count, $p_value, $phi ) = $self->chiprob_test(%args);
119 0 return "chi^2($df, N = $count) = $chi_value, p = $p_value, phi = $phi";
120 }
121
122 =head2 fprob_test
123
124 ($f_value, $df_b, $df_w, $p_value, $es_omega) = $kw->fprob_test(data => HOA, correct_ties => BOOL);
125 $p_value = $kw->fprob_test(data => HOA, correct_ties => BOOL);
126 $p_value = $kw->fprob_test(); # assuming data have already been loaded, & default of TRUE for correct_ties
127
128 Performs the same test as above but transforms the I<chi>-square value into an I<F>-distributed value, returning an array comprised of (1) this I<F>-estimate value, its (2) between- and (3) within-groups degrees-of-freedom, (4) the associated probability of the value per the I<F>-distribution, and (5) an estimate of the effect-size statistic, (partial) I<omega>-squared. The latter is returned only if L<Statistics::ANOVA::EffectSize|Statistics::ANOVA::EffectSize> is installed and available. Called in scalar context, only the I<F>-estimated I<p>-value is returned. The default value of the optional argument B<correct_ties> is 1. This method has not been tested against sample/published data (not being provided in the usual software packages).
129
130 =cut
131
132 2 ▶sub fprob_test {
Subroutine fprob_test: called CC=6 SCAR=18.6
Pod documented
133 2 my ( $self, %args ) = ( shift, @_ );
134 my $data =
135 $args{'data'}
136 2 ▶ ? delete $args{'data'}
Branch: 1 branch
BranchTrueFalseTotal
$args{'data'} ? : 0 2 2
137 : $self->get_hoa_by_lab_numonly_indep(%args);
138 my $correct_ties = defined $args{'correct_ties'}
139 2 ▶ and $args{'correct_ties'} == 0 ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
my $correct_ties = defined $args{"correct_ties"} and $args{"correct_ties"} == 0 ? '???' : '???' 2 0 2
$args{'correct_ties'} == 0 ? : 0 2 2
140 2 my ( $f_value, $df_b, $df_w ) = _f_stats( $data, $correct_ties );
141 2 my $p_value = fdtrc( $df_b, $df_w, $f_value ); # Math::Cephes fn
142 2 ▶ return $p_value if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 1 2
143
144 1 my $es_omega;
145 1 eval { require Statistics::ANOVA::EffectSize; };
146 1 ▶ if ( !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 1 1
147 0 $es_omega = Statistics::ANOVA::EffectSize->omega_sq_partial_by_f(
148 f_value => $f_value,
149 df_b => $df_b,
150 df_w => $df_w
151 );
152 }
153 1 return ( $f_value, $df_b, $df_w, $p_value, $es_omega );
154 }
155
156 =head2 fprob_str
157
158 $str = $kw->chiprob_str(data => HOA, correct_ties => BOOL);
159 $str = $kw->chiprob_str(); # assuming data have already been loaded, using default of TRUE for correct_ties
160
161 Performs the same test as for L<fprob_test|Statistics::ANOVA::KW/fprob_test> but returns not an array but a string of the conventional reporting form, e.g., F(df_b, df_w) = f_value, p = p_value (and also, then, an estimate of partial I<omega>-squared, if available, see above).
162
163 =cut
164
165 0 ▶sub fprob_str {
Subroutine fprob_str: not called CC=2 SCAR=17.9
Pod documented
166 0 my ( $self, %args ) = ( shift, @_ );
167 0 my ( $f_value, $df_b, $df_w, $p_value, $es_omega ) =
168 $self->fprob_test(%args);
169 0 my $str = "F($df_b, $df_w) = $f_value, p = $p_value";
170 0 ▶ if ( defined $es_omega ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $es_omega) 0 0 0
171 0 $str .= ', est_omega^2_p = ' . $es_omega;
172 }
173 0 return $str;
174 }
175
176 7 ▶sub _kw_stats {
Subroutine _kw_stats: called CC=3 SCAR=11.2
177 7 my ( $data, $correct_ties ) = @_;
178 7 my ( $ranks_href, $ties_aref, $gn, $ties_var ) =
179 Statistics::Data::Rank->ranks_between( data => $data );
180 my $num = sum0(
181 map {
182 17 scalar @{ $ranks_href->{$_} } *
183 17 ( mean( @{ $ranks_href->{$_} } ) - ( ( $gn + 1 ) / 2 ) )**2
184 7 } keys %{$ranks_href}
185 );
186 7 my $h = 12 / ( $gn * ( $gn + 1 ) ) * $num;
187
188 # correction for ties:
189 7 ▶ $h /= ( 1 - ( $ties_var / ( $gn**3 - $gn ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $correct_ties and not $correct_ties 7 0 7
Condition33%1/3
defined $correct_ties and not $correct_ties
!ll&&!rl&&r
070
MC/DC0%0/2
defined $correct_ties and not $correct_ties
defined $correct_ties not $correct_ties
Truth table33%1/3
A and not(B)
A= defined $correct_tiesB= not $correct_ties
ABresult
0X0
100
111
190 unless defined $correct_ties and not $correct_ties;
191 7 return ( $h, ( scalar keys %{$ranks_href} ) - 1, $gn ); # H, df, and grand N
192 }
193
194 2 ▶sub _f_stats {
Subroutine _f_stats: called CC=1 SCAR=0.0
195 2 my ( $data, $correct_ties ) = @_;
196 2 my ( $h, $df_b, $n ) = _kw_stats( $data, $correct_ties );
197 2 my $df_w = sum0( map { scalar @{ $data->{$_} } - 1 } keys %{$data} );
198 2 my $n_bt = scalar keys( %{$data} );
199 2 my $f_val = ( $h / ( $n_bt - 1 ) ) / ( ( $n - 1 - $h ) / ( $n - $n_bt ) );
200 2 return ( $f_val, $df_b, $df_w );
201 }
202
203 =head1 DEPENDENCIES
204
205 L<List::AllUtils|List::AllUtils> : used for summing.
206
207 L<Math::Cephes|Math::Cephes> : used for probability functions.
208
209 L<Statistics::Data|Statistics::Data> : used as base.
210
211 L<Statistics::Data::Rank|Statistics::Data::Rank> : used to calculate between-group sum-of ranks.
212
213 =head1 REFERENCES
214
215 Hollander, M., & Wolfe, D. A. (1999). I<Nonparametric statistical methods>. New York, NY, US: Wiley.
216
217 Rice, J. A. (1995). I<Mathematical statistics and data analysis>. Belmont, CA, US: Duxbury.
218
219 Sarantakos, S. (1993). I<Social research>. Melbourne, Australia: MacMillan.
220
221 Siegal, S. (1956). I<Nonparametric statistics for the behavioral sciences>. New York, NY, US: McGraw-Hill
222
223 =head1 SEE ALSO
224
225 L<Statistics::ANOVA::JT|Statistics::ANOVA::JT> : Also a nonparametric ANOVA by ranks for independent samples, but where the ordinality of the numerical labels of the sample names (the order of the groups) is taken into account.
226
227 L<Statistics::KruskallWallis|Statistics::KruskallWallis> : Returns the I<H>-value and its I<chi>-square I<p>-value (only), and implements the Newman-Keuls test for pairwise comparison.
228
229 =head1 AUTHOR
230
231 Roderick Garton, C<< <rgarton at cpan.org> >>
232
233 =head1 BUGS
234
235 Please report any bugs or feature requests to C<bug-statistics-anova-kw-0.01 at rt.cpan.org>, or through
236 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Statistics-ANOVA-KW-0.01>. I will be notified, and then you'll
237 automatically be notified of progress on your bug as I make changes.
238
239 =head1 SUPPORT
240
241 You can find documentation for this module with the perldoc command.
242
243 perldoc Statistics::ANOVA::KW
244
245 You can also look for information at:
246
247 =over 4
248
249 =item * RT: CPAN's request tracker (report bugs here)
250
251 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Statistics-ANOVA-KW-0.01>
252
253 =item * AnnoCPAN: Annotated CPAN documentation
254
255 L<http://annocpan.org/dist/Statistics-ANOVA-KW-0.01>
256
257 =item * CPAN Ratings
258
259 L<http://cpanratings.perl.org/d/Statistics-ANOVA-KW-0.01>
260
261 =item * Search CPAN
262
263 L<http://search.cpan.org/dist/Statistics-ANOVA-KW-0.01/>
264
265 =back
266
267
268 =head1 ACKNOWLEDGEMENTS
269
270
271 =head1 LICENSE AND COPYRIGHT
272
273 Copyright 2015 Roderick Garton.
274
275 This program is free software; you can redistribute it and/or modify it
276 under the terms of either: the GNU General Public License as published
277 by the Free Software Foundation; or the Artistic License.
278
279 See L<http://dev.perl.org/licenses/> for more information.
280
281
282 =cut
283
284 3 1; # End of Statistics::ANOVA::KW
↑ Summary