blib/lib/Statistics/ANOVA/Friedman.pm

filter: Statementstmt 73.9% 51 / 69 Branchbran 44.4% 8 / 18 Conditioncond 26.6% 4 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 81.2% 13 / 16 Podpod 100% 4 / 4 totaltotal 62.3% 81 / 130 CC 15 SCAR 27.5
CC
15
Cov
62%
fprob_test
56.0
chiprob_test
7.8
_definitely_no
3.0
CRAP 15.7
↑ Summary
1 package Statistics::ANOVA::Friedman;
2
3 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use base qw(Statistics::Data);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use List::AllUtils qw(sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Math::Cephes qw(:dists);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Statistics::Data::Rank;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 $Statistics::ANOVA::Friedman::VERSION = '0.02';
12
13 =head1 NAME
14
15 Statistics::ANOVA::Friedman - Nonparametric repeated measures analysis of variance for dependent factorial measures (Friedman Test)
16
17 =head1 VERSION
18
19 This is documentation for version 0.02, released February 2017.
20
21 =head1 SYNOPSIS
22
23 use Statistics::ANOVA::Friedman;
24 my $fri = Statistics::ANOVA::Friedman->new();
25 my ($chi_value, $df, $count, $p_value) = $fri->chiprob_test(data => HOA);
26 $fri->load({1 => [2, 4, 6], 2 => [3, 3, 12], 3 => [5, 7, 11]}); # or pre-load with HOA
27 ($chi_value, $df, $count, $p_value) = $fri->chiprob_test();
28 my ($f_value, $df_b, $df_w, $p_value2) = $fri->fprob_test();
29
30 =head1 DESCRIPTION
31
32 Performs the B<Friedman> nonparametric analysis of variance - for dependent (correlated, matched) measures of two or more discrete (nominal) variables, such as when the measures are taken from the same source (e.g., person, plot) but under different conditions. A ranking procedure is used, but, unlike the case for independent measures, the ranks are taken at each common index of each measure, i.e., within-groups.
33
34 By default, the method accounts for and corrects for ties, but if B<correct_ties> => 0, the test-statistic is uncorrected. The correction involves accounting for the number of tied variables at each index, as per Hollander & Wolfe (1995), Eq. 7.8, p. 274.
35
36 Correctness of output is tested on installation using example data from Hollander & Wolfe (1999, p. 274ff), Rice (1995, p. 470), Sarantakos (1993, p. 404-405), and Siegal (1956, p. 167ff); tests fail if the published chi-values and degrees-of-freedom are not returned by the module.
37
38 The module uses L<Statistics::Data|Statistics::Data> as a base so that data can be pre-loaded and added to per that module's methods.
39
40 =head1 SUBROUTINES/METHODS
41
42 =head2 new
43
44 $fri = Statistics::ANOVA::Friedman->new();
45
46 New object for accessing methods and storing results. This "isa" Statistics::Data object.
47
48 =head2 load, add, unload
49
50 $fri->load('a' => [1, 4], 'b' => [3, 7]);
51
52 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>.
53
54 =head2 chiprob_test
55
56 ($chi_value, $df, $count, $p_value) = $fri->chiprob_test(data => HOA, correct_ties => 1);
57
58 Performs the ANOVA and returns the chi-square value, its degrees-of-freedom, the total number of observations, and associated probability value (or only the latter if called in scalar context). Default value of optional argument B<correct_ties> is 1.
59
60 =cut
61
62 5 ▶sub chiprob_test {
Subroutine chiprob_test: called CC=7 SCAR=20.5
Pod documented
63 5 my ( $self, %args ) = @_;
64 5 ▶ my $data = $args{'data'} ? delete $args{'data'} : $self->get_hoa(%args);
Branch: 1 branch
BranchTrueFalseTotal
$args{'data'} ? : 2 3 5
65 5 my $n_bt = scalar keys %{$data};
66 5 my $n_wt = $self->equal_n( data => $data );
67 5 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if not $n_wt or $n_wt == 1 or $n_bt < 2 0 5 5
Condition33%2/6
not $n_wt or $n_wt == 1 or $n_bt < 2
l!l&&r!l&&!r
005
not $n_wt or $n_wt == 1
l!l&&r!l&&!r
005
MC/DC0%0/3
not $n_wt or $n_wt == 1 or $n_bt < 2
not $n_wt $n_wt == 1 $n_bt < 2
Truth table25%1/4
not(A) or B or C
A= not $n_wtB= $n_wt == 1C= $n_bt < 2
ABCresult
1XX1
01X1
0011
0000
68 'Need to have equal numbers of observations greater than 1 per two or more variables for chiprob_test'
69 if not $n_wt
70 or $n_wt == 1
71 or $n_bt < 2;
72 my $chi =
73 5 ▶ _definitely_no( $args{'correct_ties'} )
Branch: 1 branch
BranchTrueFalseTotal
_definitely_no($args{'correct_ties'}) ? : 1 4 5
74 ? _chi_ig_ties( $n_bt, $n_wt,
75 scalar Statistics::Data::Rank->sumsq_ranks_within( data => $data ) )
76 : _chi_by_ties( $n_bt, $n_wt,
77 Statistics::Data::Rank->sumsq_ranks_within( data => $data ) );
78 5 my $df = $n_bt - 1;
79 5 my $p_value = chdtrc( $df, $chi ); # Math::Cephes fn
80 5 ▶ return wantarray ? ( $chi, $df, ( $n_bt * $n_wt ), $p_value ) : $p_value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 0 5
81 }
82
83 =head2 chiprob_str
84
85 $str = $fri->chiprob_str(data => HOA, correct_ties => 1);
86
87 Performs the same test as for L<chiprob_test|Statistics::ANOVA::Friedman/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.
88
89 =cut
90
91 0 ▶sub chiprob_str {
Subroutine chiprob_str: not called CC=1 SCAR=6.9
Pod documented
92 0 my ( $self, %args ) = @_;
93 0 my ( $chi_value, $df, $count, $p_value ) = $self->chiprob_test(%args);
94 0 return "chi^2($df, N = $count) = $chi_value, p = $p_value";
95 }
96
97 =head2 fprob_test
98
99 ($f_value, $df_b, $df_w, $p_value) = $fri->fprob_test(data => HOA);
100 $p_value = $fri->fprob_test(data => HOA);
101
102 Performs the same test as above but transforms the chi-value into an I<F>-distributed value, returning this I<F>-equivalent value, between and within groups degrees-of-freedom, and then the associated probability off the I<F>-distribution (or only the latter if called in scalar context). Default value of optional argument B<correct_ties> is 1. This method has not been tested against sample data as yet.
103
104 =cut
105
106 0 ▶sub fprob_test {
Subroutine fprob_test: not called CC=7 SCAR=40.3
Pod documented
107 0 my ( $self, %args ) = @_;
108 0 ▶ my $data = $args{'data'} ? delete $args{'data'} : $self->get_hoa(%args);
Branch: 1 branch
BranchTrueFalseTotal
$args{'data'} ? : 0 0 0
109 0 my $n_bt = scalar keys %{$data};
110 0 my $n_wt = $self->equal_n( data => $data );
111 0 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if not $n_wt or $n_wt == 1 or $n_bt < 2 0 0 0
Condition0%0/6
not $n_wt or $n_wt == 1 or $n_bt < 2
l!l&&r!l&&!r
000
not $n_wt or $n_wt == 1
l!l&&r!l&&!r
000
MC/DC0%0/3
not $n_wt or $n_wt == 1 or $n_bt < 2
not $n_wt $n_wt == 1 $n_bt < 2
Truth table0%0/4
not(A) or B or C
A= not $n_wtB= $n_wt == 1C= $n_bt < 2
ABCresult
1XX1
01X1
0011
0000
112 'Need to have equal numbers of observations greater than 1 per two or more variables for fprob_test'
113 if not $n_wt
114 or $n_wt == 1
115 or $n_bt < 2;
116 my $chi =
117 0 ▶ _definitely_no( $args{'correct_ties'} )
Branch: 1 branch
BranchTrueFalseTotal
_definitely_no($args{'correct_ties'}) ? : 0 0 0
118 ? _chi_ig_ties( $n_bt, $n_wt,
119 scalar Statistics::Data::Rank->sumsq_ranks_within( data => $data ) )
120 : _chi_by_ties( $n_bt, $n_wt,
121 Statistics::Data::Rank->sumsq_ranks_within( data => $data ) );
122 0 my $f_value = ( ( $n_wt - 1 ) * $chi ) / ( $n_wt * ( $n_bt - 1 ) - $chi );
123 0 my $df_b = $n_bt - 1;
124 0 my $df_w = ( $n_wt - 1 ) * ($df_b);
125 0 my $p_value = fdtrc( $df_b, $df_w, $f_value ); # Math::Cephes fn
126 0 ▶ return wantarray ? ( $f_value, $df_b, $df_w, $p_value ) : $p_value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
127 }
128
129 =head2 fprob_str
130
131 $str = $fri->chiprob_str(data => HOA, correct_ties => 1);
132
133 Performs the same test as for L<fprob_test|Statistics::ANOVA::Friedman/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.
134
135 =cut
136
137 0 ▶sub fprob_str {
Subroutine fprob_str: not called CC=1 SCAR=6.9
Pod documented
138 0 my ( $self, %args ) = @_;
139 0 my ( $f_value, $df_b, $df_w, $p_value ) = $self->fprob_test(%args);
140 0 return "F($df_b, $df_w) = $f_value, p = $p_value";
141 }
142
143 1 ▶sub _chi_ig_ties {
Subroutine _chi_ig_ties: called CC=1 SCAR=0.0
144 1 my ( $c, $n, $sumsq ) = @_;
145 1 return ( 12 / ( $n * $c * ( $c + 1 ) ) ) * $sumsq - 3 * $n * ( $c + 1 );
146 }
147
148 4 ▶sub _chi_by_ties {
Subroutine _chi_by_ties: called CC=1 SCAR=0.0
149 4 my ( $c, $n, $sumsq, $xtied ) = @_;
150 4 my $num = 12 * $sumsq - 3 * $n**2 * $c * ( $c + 1 )**2;
151 4 my $sum = sum0( map { _sumcubes($_) - $c } values %{$xtied} );
152 4 my $den = $n * $c * ( $c + 1 ) - ( 1 / ( $c - 1 ) ) * $sum;
153 4 my $chi = $num / $den;
154 4 return $chi;
155 }
156
157 40 ▶sub _sumcubes {
Subroutine _sumcubes: called CC=1 SCAR=0.0
158 40 my @v = @_;
159 40 return sum0( map { $_**3 } @{ shift @v } );
160 }
161
162 5 ▶sub _definitely_no {
Subroutine _definitely_no: called CC=3 SCAR=11.1
163 5 my @v = @_;
164 5 ▶ return ( defined $v[0] and $v[0] == 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $v[0] && $v[0] == 0 ? : 1 4 5
Condition66%2/3
defined $v[0] and $v[0] == 0
!ll&&!rl&&r
401
MC/DC50%1/2
defined $v[0] and $v[0] == 0
defined $v[0] $v[0] == 0
Truth table66%2/3
A and B
A= defined $v[0]B= $v[0] == 0
ABresult
0X0
100
111
165 }
166
167 =head1 DEPENDENCIES
168
169 L<List::AllUtils|List::AllUtils> : used for summing.
170
171 L<Math::Cephes|Math::Cephes> : used for probability functions.
172
173 L<Statistics::Data|Statistics::Data> : used as base.
174
175 L<Statistics::Data::Rank|Statistics::Data::Rank> : used to calculate the dependent sum-square of ranks. See this module for retrieving the actual arrays of ranks and sum-squares.
176
177 =head1 DIAGNOSTICS
178
179 =over 4
180
181 =item Need to have equal numbers of observations greater than 1 per two or variables for chiprob_test
182
183 C<croak>ed if there are not equal numbers of numerical values in each given variable, and if there are not at least two variables. Similarly for fprob_test.
184
185 =back
186
187 =head1 REFERENCES
188
189 Hollander, M., & Wolfe, D. A. (1999). I<Nonparametric statistical methods>. New York, NY, US: Wiley.
190
191 Rice, J. A. (1995). I<Mathematical statistics and data analysis>. Belmont, CA, US: Duxbury.
192
193 Sarantakos, S. (1993). I<Social research>. Melbourne, Australia: MacMillan.
194
195 Siegal, S. (1956). I<Nonparametric statistics for the behavioral sciences>. New York, NY, US: McGraw-Hill
196
197 =head1 AUTHOR
198
199 Roderick Garton, C<< <rgarton at cpan.org> >>
200
201 =head1 BUGS AND LIMITATIONS
202
203 Please report any bugs or feature requests to C<bug-statistics-anova-friedman-0.02 at rt.cpan.org>, or through
204 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Statistics-ANOVA-Friedman-0.02>. I will be notified, and then you'll
205 automatically be notified of progress on your bug as I make changes.
206
207 =head1 SUPPORT
208
209 You can find documentation for this module with the perldoc command.
210
211 perldoc Statistics::ANOVA::Friedman
212
213 You can also look for information at:
214
215 =over 4
216
217 =item * RT: CPAN's request tracker (report bugs here)
218
219 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Statistics-ANOVA-Friedman-0.02>
220
221 =item * AnnoCPAN: Annotated CPAN documentation
222
223 L<http://annocpan.org/dist/Statistics-ANOVA-Friedman-0.02>
224
225 =item * CPAN Ratings
226
227 L<http://cpanratings.perl.org/d/Statistics-ANOVA-Friedman-0.02>
228
229 =item * Search CPAN
230
231 L<http://search.cpan.org/dist/Statistics-ANOVA-Friedman-0.02/>
232
233 =back
234
235 =head1 LICENSE AND COPYRIGHT
236
237 Copyright 2015-2017 Roderick Garton.
238
239 This program is free software; you can redistribute it and/or modify it
240 under the terms of either: the GNU General Public License as published
241 by the Free Software Foundation; or the Artistic License.
242
243 See L<http://dev.perl.org/licenses/> for more information.
244
245 =cut
246
247 6 1; # End of Statistics::ANOVA::Friedman
↑ Summary