ANOVA.pm

filter: Statementstmt 79.9% 458 / 573 Branchbran 50.0% 120 / 240 Conditioncond 22.9% 11 / 48 MC/DCmcdc 3.2% 1 / 31 Subroutinesub 87.3% 55 / 63 Podpod 95.2% 20 / 21 totaltotal 68.1% 665 / 976 CC 156 SCAR 31.2
CC
156
Cov
68%
string
156.0
df_b
90.0
dump
42.0
CRAP 22.6
↑ Summary ANOVA/Compare.pm »
1 package Statistics::ANOVA;
2 16 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use base qw(Statistics::Data);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use List::AllUtils qw(any sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Math::Cephes qw(:dists);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Statistics::Data::Rank;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Statistics::Lite qw(count max mean min sum stddev variance);
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 16 $Statistics::ANOVA::VERSION = '0.14';
15 16 Readonly my $ALPHA_DEFAULT => .05;
16
17 =head1 NAME
18
19 Statistics::ANOVA - Parametric and nonparametric 1-way analyses of variance for means-comparison and clustering per differences/trends over independent or repeated measures of variables or levels
20
21 =head1 VERSION
22
23 This is documentation for B<Version 0.14> of Statistics::ANOVA.
24
25 =head1 SYNOPSIS
26
27 use Statistics::ANOVA 0.14;
28 my $aov = Statistics::ANOVA->new();
29
30 # Some data:
31 my @gp1 = (qw/8 7 11 14 9/);
32 my @gp2 = (qw/11 9 8 11 13/);
33 my $res; # each anova method returns hash of F-value, p-value, ss_b, ss_w, etc., where relevant
34
35 # Load the data:
36 $aov->load_data({1 => \@gp1, 2 => \@gp2}); # NB: hashref
37 # or $aov->load_data([ [1, \@gp1], [2, \@gp2] ]);
38 # or $aov->load_data([ [1, @gp1], [2, @gp2] ]);
39 my @gp3 = (qw/7 13 12 8 10/);
40 $aov->add_data(3 => \@gp3);
41
42 # Test equality of variances before omnibus comparison:
43 %res = $aov->obrien()->dump(title => 'O\'Brien\'s test of equality of variances');
44 %res = $aov->levene()->dump(title => 'Levene\'s test of equality of variances');
45
46 # 1.10 Independent nominal variables ANOVA - parametric testing:
47 %res = $aov->anova(independent => 1, parametric => 1)->dump(title => 'Indep. variables parametric ANOVA', eta_squared => 1, omega_squared => 1);
48 # 1.11 Independent nominal variables (groups) ANOVA - NON-parametric:
49 %res = $aov->anova(independent => 1, parametric => 0)->dump(title => 'Kruskal-Wallis test');
50
51 # or if independent AND ordered variables (levels): test linear/non-linear trend:
52 # 1.20 Independent ordinal variables ANOVA - parametric testing:
53 %res = $aov->anova(independent => 1, parametric => 1, ordinal => 1)->dump(title => 'Indep. variables parametric ANOVA: Linear trend');
54 %res = $aov->anova(independent => 1, parametric => 1, ordinal => 2)->dump(title => 'Indep. variables parametric ANOVA: Non-linear trend');
55 # 1.21 Independent ordinal variables ANOVA - NONparametric testing:
56 %res = $aov->anova(independent => 1, parametric => 0, ordinal => 1)->dump(title => 'Jonckheere-Terpstra test');
57
58 # If they are repeated measures:
59 # 2.10 Dependent nominal variables ANOVA - parametric testing:
60 %res = $aov->anova(independent => 0, parametric => 1)->dump(title => 'Dependent variables ANOVA');
61 # 2.11 Dependent nominal variables ANOVA - NONparametric testing:
62 %res = $aov->anova(independent => 0, parametric => 0, f_equiv => 0)->dump(title => 'Friedman test');
63
64 # or if repeated AND ordinal measures:
65 # 2.20 Dependent ordinal variables ANOVA - parametric testing: NOT IMPLEMENTED
66 #$aov->anova(independent => 0, parametric => 1)->dump(title => '');
67 # 2.21 Dependent ordinal variables test - NONparametric testing:
68 %res = $aov->anova(independent => 0, parametric => 0, ordinal => 1, f_equiv => 0)->dump(title => 'Page test');
69
70 # Get pairwise comparisons (nominality of the factor assumed):
71 $aov->compare(independent => 1, parametric => 1, flag => 1, alpha => .05, dump => 1); # Indep. obs. F- (or t-)tests
72 $aov->compare(independent => 0, parametric => 1, flag => 1, alpha => .05, dump => 1); # Paired obs. F (or t-)tests
73 $aov->compare(independent => 1, parametric => 0, flag => 1, alpha => .05, dump => 1); # Wilcoxon (between-variables) sum-of-ranks (Dwass Procedure)
74 $aov->compare(independent => 0, parametric => 0, flag => 1, alpha => .05, dump => 1); # Friedman-type (within-variables) sum-of-ranks
75
76 print $aov->table(precision_p => 3, precision_s => 3);
77
78 $aov->unload('g3'); # back to 2 datasets (g1 and g2)
79
80 =head1 DESCRIPTION
81
82 =for html <blockquote>&quot;If your predictor variables are categorical (ordered or unordered) and your response variables are continuous, your design is called an ANOVA (for <b>AN</b>alysis <b>O</b>f <b>VA</b>riance&quot;&mdash;Gotelli &amp; Ellison (2004, p. 171).</blockquote>
83
84 With that idea in mind, in order to actually perform an ANOVA, you really only need to define an analysis as based on (1) ordered or unordered predictors, (2) independent or repeated measurement of their effects on response variables (i.e., from different or the same data-sources), and then (3) whether parametric or nonparametric assumptions can be made about how the factors impact on the response variables. This module facilitates selecting the right type of ANOVA, by a mere true/false setting of three arguments based on the three latter concepts-- attempting to meet just about every possible combination of these analysis specs. More specifically ...
85
86 By setting the Boolean (0, 1) value of three parameters (B<independent>, B<parametric> and B<ordinal>), this module returns and memorizes results from oneway parametric or non-parametric analyses-of-variance (ANOVAs) for either nominal groups or ordinal levels of an independent factor, and for either independent or dependent (repeated measures) observations within each group/level of that factor.
87
88 Parametric tests are of the traditional Fisher-type. Non-parametric tests comprise the Kruskal-Wallis, Jonckheere-Terpstra, Friedman and Page tests; all rank-based tests (with default accounting for ties in ranks).
89
90 Other, related routines are offered: for parametrically testing equality of variances (O'Brien and Levene tests); for estimating proportion of variance accounted for (I<eta>-squared) and effect-size (I<omega>-squared); and for making some rudimentary I<a priori> pairwise comparisons by independent/dependent I<t>-tests.
91
92 Reliability of the implemented methods has been tested against at least two different published exemplars of the methods; and by comparing output with one or another open-source or commercial statistics package. That this module's stats and tests match these examplars is tested during installation (at least via CPAN, or when making a "manual" installation).
93
94 The API has been stable over all versions, but, ahead of versioning to 1.0, it might well be expected to change. News of method unreliabilities and/or limitations are welcome. Ones from Cathal Seoghie re version 0.01, and Patrick H. Degnan re version 0.07, have already helped this module's development.
95
96 =head1 METHODS
97
98 =head2 INTERFACE
99
100 Object-oriented. No subs are explicitly exported, no arguments are set for cross-method application. The class-object holds the myriad of statistics produced by the last ANOVA run.
101
102 =head3 new
103
104 $aov = Statistics::ANOVA->new()
105
106 Create a new Statistics::ANOVA object for accessing the subs.
107
108 =head2 HANDLING DATA
109
110 =head3 load
111
112 $aov->load('aname', @data1)
113 $aov->load('aname', \@data1)
114 $aov->load(['aname', @data1], ['another_name', @data2])
115 $aov->load(['aname', \@data1], ['another_name', \@data2])
116 $aov->load({'aname' => \@data1, 'another_name' => \@data2})
117
118 I<Alias>: C<load_data>
119
120 Accepts data for analysis in any of the above-shown forms, but always with the requirement that:
121
122 =over
123
124 =item 1.
125
126 a single set of observations (the "group" or "level") is given a unique name, and
127
128 =item 2.
129
130 you do not mix the methods, e.g., a hashref here, an arrayref there.
131
132 =back
133
134 The reason for these options is that there are as many as it is practically and intuitively possible to make in Perl's Statistics modules that it's a cost and pain to traverse them; so multiple structures are permitted.
135
136 =over
137
138 =item 1. sample_name => AREF:
139
140 provide C<name =E<gt> value> pairs of data keyed by a stringy name, each with referenced array of values.
141
142 =item 2. data => AREF
143
144 a reference to an array of referenced arrays, where each of the latter arrays consists of a sample name occupying the first index, and then its sample data, as an array or yet another referenced array; e.g., [ ['group A', 20, 22, 18], ['group B', 18, 20, 16] ]
145
146 =item 3. { sample_name_A => AREF, sample_name_B => AREF}
147
148 a hash reference of named array references of data. This is the preferred method - the one that is first checked in the elongated C<if> clause that parses all this variety.
149
150 =back
151
152 The data are loaded into the class object by name, within a hash named C<data>, as flat arrays. So it's all up to you then what statistics and how follow from using this package.
153
154 The names of the data are up to you, the user; whatever can be set as the key in a hash. But if you intend to do trend analysis, you should, as a rule, give only I<numerical> names to your groups/levels, defining their ordinality (with respect to the limitations on algorithms presently offered for trend analysis).
155
156 Each call L<unload|unload>s any previous loads.
157
158 Returns the Statistics::ANOVA object - nothing but its blessed self.
159
160 =cut
161
162 24 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
163 24 my $self = shift;
164 24 $self->unload();
165 24 $self->add(@_);
166 24 return;
167 }
168 16 *load_data = \&load; # Alias
169
170 =head3 add, add_data
171
172 $aov->add('another_name', \@data2);
173 $aov->add(['another_name', \@data2]);
174 $aov->add({'another_name' => \@data2});
175
176 Same as L<load|load> except that any previous loads are not L<unload|unload>ed. Again, the hash-referenced list is given preferential treatment.
177
178 =cut
179
180 41 ▶sub add {
Subroutine add: called CC=7 SCAR=19.5
Pod documented
181 41 my $self = shift;
182 41 my ( $name, $data ) = ();
183
184 41 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 24 17 41
elsif (ref $_[0] eq 'ARRAY') { } 4 13 17
185 24 ▶ while ( ( $name, $data ) = each %{ $_[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$_[0];}) 81 24 105
186 81 ▶ if ( ref $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 81 0 81
187 81 $self->SUPER::add( $name, $data );
188 }
189 }
190 }
191 elsif ( ref $_[0] eq 'ARRAY' ) {
192 4 $self->add( _aref2href( $_[0] ) );
193 }
194 else {
195 13 $name = shift;
196 13 ▶ $data =
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 11 2 13
scalar @_ ? : 2 0 2
197 ref $_[0] eq 'ARRAY' ? $_[0]
198 : scalar(@_) ? \@_
199 : croak 'No list of data for ANOVA';
200 13 $self->SUPER::add( $name, $data );
201 }
202 41 return;
203 }
204 16 *add_data = \&add; # Alias
205
206 =head3 unload
207
208 $aov->unload() # bye to everything
209 $aov->unload('g1') # so long data named "g1"
210
211 I<Alias>: C<delete_data>
212
213 With nil or no known arguments, empties all cached data and calculations upon them, ensuring these will not be used for testing. This will be automatically called with each new load, but, to take care of any development, it could be good practice to call it yourself whenever switching from one dataset for testing to another.
214
215 Alternatively, supply one or more names of already loaded data to clobber just them out of existence; preserving any other loads.
216
217 =cut
218
219 33 ▶sub unload {
Subroutine unload: called CC=3 SCAR=11.0
Pod documented
220 33 my ($self) = shift;
221 33 ▶ if ( scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 5 28 33
222 5 foreach (@_) {
223 5 $self->SUPER::unload( name => $_ );
224 }
225 }
226 else {
227 28 $self->SUPER::unload();
228 }
229 33 $self->{'_cleared'} = 1;
230 33 return 1;
231 }
232 16 *delete_data = \&unload; # Alias
233
234 =head3 I<Missing/Invalid values>
235
236 Any data-points/observations sent to L<load|load> or L<add|add> that are undefined or not-a-number are marked for purging before being anova-tested or tested pairwise. The data arrays accessed as above, will still show the original values. When, however, you call one of the anova or pairwise methods, the data must and will be purged of these invalid values before testing.
237
238 When the C<independent> parameter equals 1 when sent to L<anova|anova> or L<compare|compare>, each list is simply purged of any undefined or invalid values. This also occurs for the equality of variances tests.
239
240 When C<independent> parameter equals 0 when sent to L<anova|anova> and L<compare|compare>, each list is purged of any value at all indices that, in any list, contain invalid values. So if two lists are (1, 4, 2) and (2, ' ', 3), the lists will have to become (1, 2) and (2, 3) to account for the bung value in the second list, and to keep all the observations appropriately paired.
241
242 The number of indices that were subject to purging is cached thus: $aov->{'purged'}. The L<dump|dump> method can also reveal this value.
243
244 The C<looks_like_number> method in L<Scalar::Util|Scalar::Util/looks_like_number> is used for checking validity of values. (Although Params::Classify::is_number might be stricter, looks_like_number benchmarks at least a few thousand %s faster.)
245
246 =head2 PROBABILITY TESTING
247
248 One generic method L<anova|anova> (a.k.a. aov, test) is used to access the possible combitinations of parametric or nonparametric tests, for independent or dependent/related observations, and for categorical or ordinal analysis. Accessing the different statistical tests depends on setting I<three> parameters on a true/false basis: I<independent>, I<parametric> and I<ordinal>.
249
250 The attribute C<independent> refers to whether or not each level of the variable was yielded by independent or related sources of data; e.g., If the same people provided you with responses under the various factors, or if the factors were tested by different participants apiece; when respectively C<independent> => 0 or 1.
251
252 The following describes the particular tests you get upon each possible combination of these alternatives.
253
254 =head3 1. INDEPENDENT groups/levels
255
256 =head4 1.10 PARAMETRIC test for NOMINAL groups
257
258 %res = $aov->anova(independent => 1, parametric => 1, ordinal => 0)
259
260 Offers the standard Fisher-type ANOVA for independent measures of the different levels of a factor.
261
262 =head4 1.11 PARAMETRIC test for ORDINAL levels
263
264 $aov->anova(independent => 1, parametric => 1, ordinal => 1) # test linear trend
265 $aov->anova(independent => 1, parametric => 1, ordinal => -1) # test non-linear trend
266
267 If the independent/treatment/between groups variable is actually measured on a continuous scale/is a quantitative factor, assess their B<linear trend>: Instead of asking "How sure can we be that the means-per-group are equal?", ask "How sure can we be that there is a departure from flatness of the means-per-level?".
268
269 The essential difference is that in place of the the between (treatment) mean sum-of-squares in the numerator is the linear sum of squares in which each "group" mean is weighted by the deviation of the level-value (the name of the "group") from the mean of the levels (and divided by the sum of the squares of these deviations).
270
271 If the number of observations per level is unequal, the module applies the simple I<unweighted> approach. This is recommended as a general rule by Maxwell and Delaney (1990), given that the I<weighted> approach might erroneously suggest a linear trend (unequal means) when, in fact, the trend is curvilinear (and by which the means balance out to equality); unless "there are strong theoretical reasons to believe that the only true population trend is linear" (p. 234). (But then you might be theoretically open to either. While remaining as the default, a future option might access the I<hierarchical, weighted> approach.)
272
273 To test if there is the possibility of a B<non-linear trend>, give the value of -1 to the C<ordinal> argument.
274
275 Note that the contrast coefficients are calculated directly from the values of the independent variable, rather than using a look-up table. This respects the actual distance between values, but requires that the names of the sample data, of the groups (or levels), are I<numerical> names when L<load|load>ed - i.e., such that the data-keys can be summed and averaged.
276
277 =head4 1.20 NONPARAMETRIC test for NOMINAL groups (Kruskal-Wallis test)
278
279 %res = $aov->anova(independent => 1, parametric => 0, ordinal => 0)
280
281 Performs a one-way independent groups ANOVA using the non-parametric B<Kruskal-Wallis> sum-of-ranks method for a single factor with 2 or more levels. By default, instead of an I<F>-value, there is an I<H>-value. The I<p>-value is read off the chi-square distribution. The test is generally considered to be unreliable if there are no more than 3 groups and all groups comprise 5 or fewer observations. An estimate of I<F> can, alternatively be returned, if the optional argument B<f_equiv> => 1.
282
283 By default, this method accounts for and corrects for ties in ranks across the levels, but if C<correct_ties> = 0, I<H> is uncorrected. The correction involves giving each tied score the mean of the ranks for which it is tied (see Siegal, 1956, p. 188ff).
284
285 =head4 1.21 NONPARAMETRIC test for ORDINAL levels (Jonckheere-Terpstra test)
286
287 $aov->anova(independent => 1, parametric => 0, ordinal => 1)
288
289 Performs the B<Jonckheere-Terpstra> nonparametric test for independent but ordered levels. The method returns:
290
291 $res{'j_value'} : the observed value of J
292 $res{'j_exp'} : the expected value of J
293 $res{'j_var'} : the variance of J
294 $res{'z_value'} : the normalized value of J
295 $res{'p_value'} : the one-tailed probability of observing a value as great as or greater than z_value.
296
297 =head3 2. DEPENDENT groups/levels (REPEATED MEASURES)
298
299 =head4 2.10 PARAMETRIC test for NOMINAL groups
300
301 %res = $aov->anova(independent => 0, parametric => 1, ordinal => 0, multivariate => 0|1)
302
303 Performs parametric repeated measures analysis of variance. This uses the traditional univariate, or "mixed-model," approach, with sphericity assumed (i.e., equal variances of all factor differences, within each factor and all possible pairs of factors). The assumption is met when there are only two levels of the repeated measures factor; but unequal variances might be a problem when there are more than two levels. No methods are presently applied to account for the possibility of non-sphericity.
304
305 =head4 2.11 PARAMETRIC test for ORDINAL levels
306
307 [Not implemented.]
308
309 =head4 2.20 NONPARAMETRIC test for NOMINAL groups (Friedman test)
310
311 %res = $aov->anova(independent => 0, parametric => 0, ordinal => 0)
312
313 Performs the B<Friedman> nonparametric analysis of variance - for two or more dependent (matched, related) groups. The statistical attributes now within the class object (see L<anova|anova>) pertain to this test, e.g., $aov->{'chi_value'} gives the chi-square statistic from the Friedman test; and $aov->{'p_value'} gives the associated I<p>-value (area under the right-side, upper tail of the distribution). If B<f_equiv> => 1, then, instead of the I<chi>-value, and I<p>-value read off the I<chi>-square distribution, you get the I<F>-value equivalent, with the I<p>-value read off the I<F>-distribution.
314
315 =cut
316
317 =head4 2.21 NONPARAMETRIC test for ORDINAL levels (Page test)
318
319 %res = $aov->anova(independent => 0, parametric => 0, ordinal => 1, tails => 1|2)
320
321 This implements the B<Page> (1963) analysis of variance by ranks for repeated measures of ordinally scaled variables; so requires - numerically named variables. The statistical attributes now within the class object (see L<anova|anova>) pertain to this test, and are chiefly:
322
323 $res{'l_value'} : the observed test statistic (sum of ordered and weighted ranks)
324 $res{'l_exp'} : expected value of the test statistic
325 $res{'l_var'} : variance of the test statistic (given so many groups and observations)
326 $res{'z_value'} : the standardized l_value
327 $res{'p_value'} : the 2-tailed probability associated with the z_value (or 1-tailed if tails => 1).
328 $res{'r_value'} : estimate of the Spearman rank-order correlation coefficient
329 based on the observed and predicted order of each associated variable per observation.
330
331 =head3 anova
332
333 $aov->anova(independent => 1|0, parametric => 1|0, ordinal => 0|1)
334
335 I<Aliases>: aov, test
336
337 Generic method to access all anova functions by specifying TRUE/FALSE values for C<independent>, C<parametric> and C<ordinal>.
338
339 Independent Parametric Ordinal What you get
340 1 1 0 Fisher-type independent groups ANOVA
341 1 1 1 Fisher-type independent groups ANOVA with trend analysis
342 1 0 0 Kruskal-Wallis independent groups ANOVA
343 1 0 1 Jonckheere-Terpstra independent groups trend analysis
344 0 1 0 Fisher-type dependent groups ANOVA (univariate or multivariate)
345 0 1 1 (Fisher-type dependent groups ANOVA with trend analysis; not implemented)
346 0 0 0 Friedman's dependent groups ANOVA
347 0 0 1 Page's dependent groups trend analysis
348
349 All methods return nothing but the class object after feeding it with the relevant statistics, which you can access by name, as follows:
350
351 $res{'f_value'} (or $res{'chi_value'}, $res{'h_value'}, $res{'j_value'}, $res{'l_value'} and/or $res{'z_value'})
352 $res{'p_value'} : associated with the test statistic
353 $res{'df_b'} : between-groups/treatment/numerator degree(s) of freedom
354 $res{'df_w'} : within-groups/error/denominator degree(s) of freedom (also given with F-equivalent Friedman test)
355 $res{'ss_b'} : between-groups/treatment sum of squares
356 $res{'ss_w'} : within-groups/error sum of squares
357 $res{'ms_b'} : between-groups/treatment mean squares
358 $res{'ms_w'} : within-groups/error mean squares
359
360 =cut
361
362 41 ▶sub anova {
Subroutine anova: called CC=8 SCAR=20.8
Pod documented
363 41 my ( $self, %args ) = @_;
364 41 foreach (qw/independent parametric/) {
365 82 ▶ $args{$_} = 1 if !defined $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$_} 0 82 82
366 }
367 41 ▶ $args{'ordinal'} = 0 if !defined $args{'ordinal'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"ordinal"} 8 33 41
368
369 41 ▶ if ( !$self->{'_cleared'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_cleared"}) 2 39 41
370 2 $self->{$_} = undef foreach
371 qw/df_b df_w f_value chi_value h_value j_value j_exp j_var l_value l_exp l_var z_value p_value ss_b ss_w ms_b ms_w eta_sq omega_sq purged/;
372 2 $self->{'_cleared'} = 1;
373 }
374
375 41 ▶ if ( $args{'independent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'independent'}) { } 23 18 41
376 23 _aov_indep( $self, %args );
377 }
378 else {
379 18 _aov_rmdep( $self, %args );
380 }
381 41 ▶ return wantarray ? %{ $self->{'_stat'} } : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 32 41
382 }
383 16 *aov = \&anova;
384 16 *test = \&anova;
385
386 23 ▶sub _aov_indep {
Subroutine _aov_indep: called CC=10 SCAR=23.2
387 23 my ( $self, %args ) = @_;
388 23 my $data = $self->get_hoa_numonly_indep(%args);
389 croak 'Not enough variables for performing ANOVA'
390 23 ▶ if scalar keys %{$data} < 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$data;} < 2 0 23 23
391 23 ▶ if ( any { !scalar @{ $data->{$_} } } keys %{$data} ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not scalar @{$data->{$_};}; } , keys %{$data;})) 0 23 23
392 0 croak 'Empty data following purge of invalid value(s)';
393 }
394 23 ▶ if ( $args{'parametric'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'parametric'}) { } 16 7 23
395 16 ▶ if ( !$args{'ordinal'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'ordinal'}) { } 11 5 16
396 (
397 $self->{'_stat'}->{'f_value'},
398 $self->{'_stat'}->{'df_b'}, $self->{'_stat'}->{'df_w'},
399 $self->{'_stat'}->{'ss_b'}, $self->{'_stat'}->{'ss_w'},
400 $self->{'_stat'}->{'ms_b'}, $self->{'_stat'}->{'ms_w'},
401 11 $self->{'_stat'}->{'p_value'},
402
403 ) = _aov_indep_param_cat($data);
404 11 $self->{'_dfree'} = 0;
405 }
406 else {
407 5 ▶ if ( $args{'ordinal'} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'ordinal'} == 1) { } 3 2 5
408 3 $self->_aov_indep_param_ord_linear($data);
409 }
410 else {
411 2 $self->_aov_indep_param_ord_nonlinear($data);
412 }
413 }
414 }
415 else {
416 7 ▶ $args{'correct_ties'} = 1 if !defined $args{'correct_ties'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"correct_ties"} 7 0 7
417 7 ▶ if ( !$args{'ordinal'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'ordinal'}) { } 4 3 7
418 4 ▶ $args{'f_equiv'} = 0 if !defined $args{'f_equiv'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"f_equiv"} 2 2 4
419 $self->_aov_indep_dfree_cat( $data, $args{'correct_ties'},
420 4 $args{'f_equiv'} );
421 }
422 else {
423 3 ▶ $args{'tails'} = 2 if !defined $args{'tails'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"tails"} 3 0 3
424 (
425 $self->{'_stat'}->{'j_value'}, $self->{'_stat'}->{'j_exp'},
426 $self->{'_stat'}->{'j_var'}, $self->{'_stat'}->{'z_value'},
427 $self->{'_stat'}->{'p_value'}
428 )
429 = _aov_indep_dfree_ord( $data, $args{'correct_ties'},
430 3 $args{'tails'} );
431 }
432 }
433 23 return;
434 }
435
436 18 ▶sub _aov_rmdep {
Subroutine _aov_rmdep: called CC=11 SCAR=24.2
437 18 my ( $self, %args ) = @_;
438 18 my $data = $self->get_hoa_numonly_across(%args);
439 18 my $n_bt = scalar keys %{$data};
440 18 ▶ croak 'Not enough variables for performing ANOVA'
Branch: 1 branch
BranchTrueFalseTotal
if $n_bt < 2 0 18 18
441 if $n_bt < 2;
442 18 my $n_wt = $self->equal_n( data => $data );
443 18 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if not $n_wt or $n_wt == 1 0 18 18
Condition33%1/3
not $n_wt or $n_wt == 1
l!l&&r!l&&!r
0018
MC/DC0%0/2
not $n_wt or $n_wt == 1
not $n_wt $n_wt == 1
Truth table33%1/3
not(A) or B
A= not $n_wtB= $n_wt == 1
ABresult
1X1
011
000
444 'Number of observations per variable need to be equal and greater than 1 for repeated measures ANOVA'
445 if !$n_wt or $n_wt == 1;
446 18 ▶ if ( $args{'parametric'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'parametric'}) { } 10 8 18
447 10 ▶ if ( !$args{'ordinal'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'ordinal'}) { } 9 1 10
448 (
449 $self->{'_stat'}->{'f_value'}, $self->{'_stat'}->{'df_b'},
450 $self->{'_stat'}->{'df_w'}, $self->{'_stat'}->{'ss_b'},
451 $self->{'_stat'}->{'ss_w'}, $self->{'_stat'}->{'ms_b'},
452 9 $self->{'_stat'}->{'ms_w'}, $self->{'_stat'}->{'p_value'}
453 ) = _aov_rmdep_cat_param( $data, $n_bt, $n_wt );
454 9 $self->{'_dfree'} = 0;
455 }
456 else {
457 1 _aov_rmdep_ord_param( $data, $n_bt, $n_wt );
458 }
459 }
460 else {
461 8 ▶ if ( !$args{'ordinal'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'ordinal'}) { } 6 2 8
462 6 ▶ $args{'correct_ties'} = 1 if !defined $args{'correct_ties'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"correct_ties"} 5 1 6
463 6 ▶ $args{'f_equiv'} = 0 if !defined $args{'f_equiv'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"f_equiv"} 0 6 6
464 6 ▶ if ( $args{'f_equiv'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'f_equiv'}) { } 1 5 6
465 (
466 $self->{'_stat'}->{'f_value'},
467 $self->{'_stat'}->{'df_b'},
468 $self->{'_stat'}->{'df_w'},
469 $self->{'_stat'}->{'p_value'}
470 )
471 1 = _aov_rmdep_cat_dfree_fequiv( $data, $args{'correct_ties'} );
472 1 $self->{'_dfree'} = 1;
473 }
474 else {
475 (
476 $self->{'_stat'}->{'chi_value'},
477 $self->{'_stat'}->{'df_b'}, $self->{'_stat'}->{'count'},
478 $self->{'_stat'}->{'p_value'},
479
480 5 ) = _aov_rmdep_cat_dfree( $data, $args{'correct_ties'} );
481 5 $self->{'_dfree'} = 1;
482 }
483 }
484 else {
485 2 ▶ $args{'tails'} = 2 if !defined $args{'tails'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"tails"} 2 0 2
486 (
487 $self->{'_stat'}->{'l_value'}, $self->{'_stat'}->{'l_exp'},
488 $self->{'_stat'}->{'l_var'}, $self->{'_stat'}->{'z_value'},
489 $self->{'_stat'}->{'p_value'}, $self->{'_stat'}->{'r_value'}
490 2 ) = _aov_rmdep_ord_dfree( $data, $args{'tails'} );
491 2 $self->{'_dfree'} = 1;
492 }
493 }
494 18 return;
495 }
496
497 16 ▶sub _aov_indep_param_cat {
Subroutine _aov_indep_param_cat: called CC=3 SCAR=11.2
498 16 my ($data) = @_;
499 16 my ( $ss_w, $df_w ) = _sumsq_w_indep_param($data);
500 16 ▶ croak 'No within-groups data for performing ANOVA' if !$ss_w || !$df_w;
Branch: 1 branch
BranchTrueFalseTotal
unless $ss_w and $df_w 0 16 16
Condition33%1/3
$ss_w and $df_w
!ll&&!rl&&r
0016
MC/DC0%0/2
$ss_w and $df_w
$ss_w $df_w
Truth table33%1/3
A and B
A= $ss_wB= $df_w
ABresult
0X0
100
111
501 16 my $ss_b = _sumsq_b_indep_param_cat($data);
502 16 my $df_b = _df_b_indep_param_cat($data); # a - 1
503 16 my $ms_b = $ss_b / $df_b;
504 16 my $ms_w = $ss_w / $df_w;
505 16 my $f_value = $ms_b / $ms_w;
506 16 my $p_value = fdtrc( $df_b, $df_w, $f_value );
507 16 return ( $f_value, $df_b, $df_w, $ss_b, $ss_w, $ms_b, $ms_w, $p_value );
508 }
509
510 3 ▶sub _aov_indep_param_ord_linear {
Subroutine _aov_indep_param_ord_linear: called CC=3 SCAR=11.2
511 3 my ( $self, $data ) = @_;
512 3 my ( $ss_w, $df_w ) = _sumsq_w_indep_param($data);
513 3 ▶ croak 'No within-groups data for performing ANOVA' if !$ss_w || !$df_w;
Branch: 1 branch
BranchTrueFalseTotal
unless $ss_w and $df_w 0 3 3
Condition33%1/3
$ss_w and $df_w
!ll&&!rl&&r
003
MC/DC0%0/2
$ss_w and $df_w
$ss_w $df_w
Truth table33%1/3
A and B
A= $ss_wB= $df_w
ABresult
0X0
100
111
514 3 my $ss_l = _sumsq_b_indep_param_ord($data);
515 3 my $df_b = _df_b_indep_param_ord_linear($data); # a - 1
516 3 my $ms_w = $ss_w / $df_w;
517 3 my $f_value = $ss_l / $ms_w;
518 3 my $p_value = fdtrc( $df_b, $df_w, $f_value ); # Math::Cephes function
519 (
520 $self->{'_stat'}->{'f_value'}, $self->{'_stat'}->{'df_b'},
521 $self->{'_stat'}->{'df_w'}, $self->{'_stat'}->{'ss_b'},
522 $self->{'_stat'}->{'ss_w'}, $self->{'_stat'}->{'ms_w'},
523 3 $self->{'_stat'}->{'p_value'}, $self->{'_dfree'}
524 ) = ( $f_value, $df_b, $df_w, $ss_l, $ss_w, $ms_w, $p_value, 0 );
525 3 return;
526 }
527
528 2 ▶sub _aov_indep_param_ord_nonlinear {
Subroutine _aov_indep_param_ord_nonlinear: called CC=3 SCAR=11.2
529 2 my ( $self, $data ) = @_;
530 2 my ( $ss_w, $df_w ) = _sumsq_w_indep_param($data);
531 2 ▶ croak 'No within-groups data for performing ANOVA' if !$ss_w || !$df_w;
Branch: 1 branch
BranchTrueFalseTotal
unless $ss_w and $df_w 0 2 2
Condition33%1/3
$ss_w and $df_w
!ll&&!rl&&r
002
MC/DC0%0/2
$ss_w and $df_w
$ss_w $df_w
Truth table33%1/3
A and B
A= $ss_wB= $df_w
ABresult
0X0
100
111
532 2 my $df_b = _df_b_indep_param_ord_nonlinear($data);
533 2 my $ss_b = _sumsq_b_indep_param_ord_nonlinear($data); # a - 2
534 2 my $ms_b = $ss_b / $df_b;
535 2 my $ms_w = $ss_w / $df_w;
536 2 my $f_value = $ms_b / $ms_w;
537 2 my $p_value = fdtrc( $df_b, $df_w, $f_value ); # Math::Cephes function
538 (
539 $self->{'_stat'}->{'f_value'}, $self->{'_stat'}->{'df_b'},
540 $self->{'_stat'}->{'df_w'}, $self->{'_stat'}->{'ss_b'},
541 $self->{'_stat'}->{'ss_w'}, $self->{'_stat'}->{'ms_b'},
542 $self->{'_stat'}->{'ms_w'}, $self->{'_stat'}->{'p_value'},
543 2 $self->{'_dfree'}
544 ) = ( $f_value, $df_b, $df_w, $ss_b, $ss_w, $ms_b, $ms_w, $p_value, 0 );
545 2 return;
546 }
547
548 4 ▶sub _aov_indep_dfree_cat {
Subroutine _aov_indep_dfree_cat: called CC=3 SCAR=11.0
549 4 my ( $self, $data, $correct_ties, $f_equiv ) = @_;
550 4 eval { require Statistics::ANOVA::KW; };
551 4 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
552 'Don\'t know how to run Kruskall-Wallis test. Maybe you need to install Statistics::ANOVA::KW.'
553 if $@;
554 4 my $kw = Statistics::ANOVA::KW->new();
555 4 $kw->load_data($data);
556 4 ▶ if ($f_equiv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_equiv) { } 1 3 4
557 1 my ( $f_value, $df_b, $df_w, $p_value ) =
558 $kw->fprob_test( correct_ties => $correct_ties );
559 (
560 $self->{'_stat'}->{'f_value'}, $self->{'_stat'}->{'df_b'},
561 $self->{'_stat'}->{'df_w'}, $self->{'_stat'}->{'p_value'},
562 1 $self->{'_dfree'}
563 ) = ( $f_value, $df_b, $df_w, $p_value, 0 );
564 }
565 else {
566 3 my ( $chi_value, $df, $count, $p_value ) =
567 $kw->chiprob_test( correct_ties => $correct_ties );
568 (
569 $self->{'_stat'}->{'h_value'}, $self->{'_stat'}->{'df_b'},
570 $self->{'_stat'}->{'count'}, $self->{'_stat'}->{'p_value'},
571 3 $self->{'_dfree'}
572 ) = ( $chi_value, $df, $count, $p_value, 1 );
573 }
574 4 return;
575 }
576
577 3 ▶sub _aov_indep_dfree_ord {
Subroutine _aov_indep_dfree_ord: called CC=2 SCAR=6.9
578 3 my ( $data, $correct_ties, $tails ) = @_;
579 3 eval { require Statistics::ANOVA::JT; };
580 3 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
581 'Don\'t know how to run Jonckheere-Terpstra test. Maybe you need to install Statistics::ANOVA::JT.'
582 if $@;
583 3 my $jt = Statistics::ANOVA::JT->new();
584 3 $jt->load_data($data);
585 3 my $j_obs = $jt->observed();
586 3 my $j_exp = $jt->expected();
587 3 my $j_var = $jt->variance( correct_ties => $correct_ties );
588 3 my ( $z_value, $p_value ) = $jt->zprob_test(
589 correct_ties => $correct_ties,
590 tails => $tails
591 );
592 3 return ( $j_obs, $j_exp, $j_var, $z_value, $p_value );
593 }
594
595 9 ▶sub _aov_rmdep_cat_param {
Subroutine _aov_rmdep_cat_param: called CC=1 SCAR=0.0
596 9 my ( $data, $n_bt, $n_wt, ) = @_;
597 9 my ( $ss_b, $ss_w, $df_b, $df_w ) =
598 _sumsq_bw_rmdep_param_uni( $data, $n_bt, $n_wt );
599 9 my $ms_b = $ss_b / $df_b;
600 9 my $ms_w = $ss_w / $df_w;
601 9 my $f_value = $ms_b / $ms_w;
602 9 my $p_value = fdtrc( $df_b, $df_w, $f_value ); # Math::Cephes
603 9 return ( $f_value, $df_b, $df_w, $ss_b, $ss_w, $ms_b, $ms_w, $p_value );
604 }
605
606 1 ▶sub _aov_rmdep_ord_param {
Subroutine _aov_rmdep_ord_param: called CC=1 SCAR=0.0
607 1 carp
608 ':-( Parametric trend analysis for dependent/repeated measures is not implemented';
609 1 return;
610 }
611
612 5 ▶sub _aov_rmdep_cat_dfree {
Subroutine _aov_rmdep_cat_dfree: called CC=2 SCAR=7.0
613 5 my ( $data, $correct_ties ) = @_;
614 5 eval { require Statistics::ANOVA::Friedman; };
615 5 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
616 'Don\'t know how to do Friedman ANOVA. Perhaps you need to install Statistics::ANOVA::Friedman.'
617 if $@;
618 5 my ( $chi, $df, $count, $p_value ) =
619 Statistics::ANOVA::Friedman->chiprob_test(
620 data => $data,
621 correct_ties => $correct_ties
622 );
623 5 return ( $chi, $df, $count, $p_value );
624 }
625
626 1 ▶sub _aov_rmdep_cat_dfree_fequiv {
Subroutine _aov_rmdep_cat_dfree_fequiv: called CC=2 SCAR=7.0
627 1 my ( $data, $correct_ties ) = @_;
628 1 eval { require Statistics::ANOVA::Friedman; };
629 1 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
630 'Don\'t know how to do Friedman ANOVA. Perhaps you need to install Statistics::ANOVA::Friedman.'
631 if $@;
632 1 my ( $f_value, $df_b, $df_w, $p_value ) =
633 Statistics::ANOVA::Friedman->fprob_test(
634 data => $data,
635 correct_ties => $correct_ties
636 );
637 1 return ( $f_value, $df_b, $df_w, $p_value );
638 }
639
640 2 ▶sub _aov_rmdep_ord_dfree {
Subroutine _aov_rmdep_ord_dfree: called CC=2 SCAR=6.9
641 2 my ( $data, $tails ) = @_;
642 2 eval { require Statistics::ANOVA::Page; };
643 2 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
644 'Don\'t know how to do Page ANOVA. Perhaps you need to install Statistics::ANOVA::Page.'
645 if $@;
646 2 my $page = Statistics::ANOVA::Page->new();
647 2 $page->load_data($data);
648 2 my $l_obs = $page->observed();
649 2 my $l_exp = $page->expected();
650 2 my $l_var = $page->variance();
651 2 my ( $z_value, $p_value ) = $page->zprob_test( tails => $tails );
652 2 my $r_value = $page->observed_r();
653 2 return ( $l_obs, $l_exp, $l_var, $z_value, $p_value, $r_value, 1 );
654 }
655
656 =head3 Tests for equality of variances
657
658 =head4 obrien
659
660 $aov->obrien()
661
662 I<Alias>: obrien_test
663
664 Performs B<O'Brien's> (1981) test for equality of variances within each variable: based on transforming each observation in relation to its variance and its deviation from its mean; and performing an ANOVA on these values (for which the mean is equal to the variance of the original observations). The procedure is recognised to be robust against violations of normality (unlike I<F>-max) (Maxwell & Delaney, 1990).
665
666 The statistical attributes now within the class object (see L<anova|anova>) pertain to this test, e.g., $aov->{'f_value'} gives the I<F>-statistic for O'Brien's Test; and $aov->{'p_value'} gives the I<p>-value associated with the I<F>-statistic for O'Brien's Test.
667
668 =cut
669
670 3 ▶sub obrien {
Subroutine obrien: called CC=7 SCAR=19.6
Pod documented
671 3 my ( $self, %args ) = @_;
672
673 #ref $self->{'data'} eq 'HASH' ? %{$self->{'data'}} : croak 'No reference to a hash of data for performing ANOVA';
674 3 my $tdata = $self->get_hoa_numonly_indep(%args); # List-wise clean-up
675 croak 'Not enough variables for performing ANOVA'
676 3 ▶ if scalar( keys( %{$tdata} ) ) <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$tdata;} <= 1 0 3 3
677 3 ▶ if ( any { !scalar @{ $tdata->{$_} } } keys %{$tdata} ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not scalar @{$tdata->{$_};}; } , keys %{$tdata;})) 0 3 3
678 0 croak 'Empty data following purge of invalid value(s)';
679 }
680 3 my ( $m, $v, $n, $sname, $sdata, @r, @data ) = ();
681 3 $self->{'obrien'} = {};
682
683 # Traverse each sample of data:
684 3 ▶ while ( ( $sname, $sdata ) = each %{$tdata} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$tdata;}) 11 3 14
685
686 # For each var, compute the sample mean and the unbiased sample variance:
687 ( $m, $v, $n ) =
688 11 ( mean( @{$sdata} ), variance( @{$sdata} ), count( @{$sdata} ) );
689
690 # Transform each observation:
691 11 foreach ( @{$sdata} ) {
692 55 push @r,
693 (
694 (
695 ( ( $n - 1.5 ) * $n * ( ( $_ - $m )**2 ) ) -
696 ( .5 * $v * ( $n - 1 ) )
697 ) / ( ( $n - 1 ) * ( $n - 2 ) )
698 );
699 }
700 11 $self->{'obrien'}->{$sname} = [@r];
701 11 @r = ();
702
703 # Check that each variable mean of the O'Briens are equal to the variance of the original data:
704 11 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (sprintf("%.2f", mean(@{$$self{"obrien"}{$sname};})) != sprintf("%.2f", $v)) 0 11 11
705 11 sprintf( '%.2f', mean( @{ $self->{'obrien'}->{$sname} } ) ) !=
706 sprintf( '%.2f', $v ) )
707 {
708 0 croak "Mean for sample $sname does not equal variance";
709 }
710 }
711
712 # Perform an ANOVA using the O'Brien values as the DV:
713 (
714 $self->{'_stat'}->{'f_value'},
715 $self->{'_stat'}->{'df_b'}, $self->{'_stat'}->{'df_w'},
716 $self->{'_stat'}->{'ss_b'}, $self->{'_stat'}->{'ss_w'},
717 $self->{'_stat'}->{'ms_b'}, $self->{'_stat'}->{'ms_w'},
718 $self->{'_stat'}->{'p_value'},
719
720 3 ) = _aov_indep_param_cat( $self->{'obrien'} );
721 3 $self->{'_dfree'} = 0;
722 3 ▶ return wantarray ? %{ $self->{'_stat'} } : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
723 }
724 16 *obrien_test = \&obrien; # Alias
725
726 =head4 levene
727
728 $aov->levene()
729
730 I<Alias>: levene_test
731
732 Performs B<Levene's> (1960) test for equality of variances within each variable: an ANOVA of the absolute deviations, i.e., absolute value of each observation less its mean.
733
734 The statistical attributes now within the class object (see L<anova|anova>) pertain to this test, e.g., $aov->{'f_value'} gives the I<F>-statistic for Levene's Test; and $aov->{'p_value'} gives the I<p>-value associated with the I<F>-statistic for Levene's Test.
735
736 =cut
737
738 2 ▶sub levene {
Subroutine levene: called CC=6 SCAR=18.1
Pod documented
739 2 my ( $self, %args ) = @_;
740
741 #ref $self->{'data'} eq 'HASH' ? %{$self->{'data'}} : croak 'No reference to an associative array for performing ANOVA';
742 2 my $tdata = $self->get_hoa_numonly_indep(%args); # List-wise clean-up
743 croak 'Not enough variables for performing ANOVA'
744 2 ▶ if scalar( keys( %{$tdata} ) ) <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$tdata;} <= 1 0 2 2
745 2 ▶ if ( any { !scalar @{ $tdata->{$_} } } keys %{$tdata} ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not scalar @{$tdata->{$_};}; } , keys %{$tdata;})) 0 2 2
746 0 croak 'Empty data following purge of invalid value(s)';
747 }
748 2 my ( $m, $v, $n, @d ) = ();
749 2 $self->{'levene'} = {};
750
751 # Traverse each sample of data:
752 2 ▶ while ( my ( $sname, $sdata ) = each %{$tdata} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$tdata;}) 7 2 9
753
754 # For each variable, compute the sample mean and the unbiased sample variance:
755 7 $m = mean( @{$sdata} );
756 7 $v = variance( @{$sdata} );
757 7 $n = count( @{$sdata} );
758
759 # For each observation, compute the absolute deviation:
760 7 my $m = mean( @{$sdata} );
761 7 push @d, abs( $_ - $m ) foreach @{$sdata};
762 7 $self->{'levene'}->{$sname} = [@d];
763 7 @d = ();
764 }
765
766 # Perform an ANOVA using the abs. deviations as the DV:
767 (
768 $self->{'_stat'}->{'f_value'},
769 $self->{'_stat'}->{'df_b'}, $self->{'_stat'}->{'df_w'},
770 $self->{'_stat'}->{'ss_b'}, $self->{'_stat'}->{'ss_w'},
771 $self->{'_stat'}->{'ms_b'}, $self->{'_stat'}->{'ms_w'},
772 $self->{'_stat'}->{'p_value'},
773
774 2 ) = _aov_indep_param_cat( $self->{'levene'} );
775 2 $self->{'_dfree'} = 0;
776 0 ▶ return wantarray ? %{ $self->{'_stat'} } : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
777 }
778 16 *levene_test = \&levene; # Alias
779
780 =head2 MEASURING EFFECT
781
782 Follow-up parametric ANOVAs. Note that for the one-way ANOVAs here tested, eta-squared is the same as partial eta-squared.
783
784 =head3 eta_squared
785
786 $etasq = $aov->eta_squared(independent => BOOL, parametric => BOOL, ordinal => BOOL);
787
788 Returns the effect size estimate (partial) eta-squared, calculated using sums-of-squares via L<Statistics::ANOVA::EffectSize|Statistics::ANOVA::EffectSize/eta_sq_partial_by_ss>. Also feeds $aov with the value, named 'eta_sq'.
789
790 =cut
791
792 2 ▶sub eta_squared {
Subroutine eta_squared: called CC=2 SCAR=7.0
Pod documented
793 2 my ( $self, @args ) = @_;
794 2 eval { require Statistics::ANOVA::EffectSize; };
795 2 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
796 'Don\'t know how to do ANOVA effect-sizes. Perhaps you need to install Statistics::ANOVA::EffectSize.'
797 if $@;
798 2 my $etasq = Statistics::ANOVA::EffectSize->eta_sq_partial_by_ss(
799 $self->anova(@args) );
800 2 $self->{'_stat'}->{'eta_sq'} = $etasq;
801 2 return $etasq;
802 }
803
804 =head3 omega_squared
805
806 Returns the effect size estimate (partial) omega-squared, calculated using mean sums-of-squares via L<Statistics::ANOVA::EffectSize|Statistics::ANOVA::EffectSize/omega_sq_partial_by_ss>. Also feeds $aov with the value, named 'omega_sq'.
807
808 =cut
809
810 1 ▶sub omega_squared {
Subroutine omega_squared: called CC=2 SCAR=7.0
Pod documented
811 1 my ( $self, %args ) = @_;
812 1 eval { require Statistics::ANOVA::EffectSize; };
813 1 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
814 'Don\'t know how to do ANOVA effect-sizes. Perhaps you need to install Statistics::ANOVA::EffectSize.'
815 if $@;
816 1 my $omg_sq = Statistics::ANOVA::EffectSize->omega_sq_partial_by_ss(
817 $self->anova(%args), count => $self->grand_n() );
818 1 $self->{'_stat'}->{'omega_sq'} = $omg_sq;
819 1 return $omg_sq;
820 }
821
822 =head2 IDENTIFYING RELATIONSHIPS/DIFFERENCES
823
824 =head3 compare
825
826 $aov->compare(independent => 1|0, parametric => 1|0, tails => 2|1, flag => 0|1, alpha => .05,
827 adjust_p => 0|1, adjust_e => 1|0|2, use_t => 0|1, dump => 0|1, str => 0|1)
828
829 Performs all possible pairwise comparisons, with the Bonferroni approach to control experiment-wise error-rate. The particular tests depend on whether or not you want parametric (default) or nonparametric tests, and if the observations have been made independently (between groups, the default) or by repeated measures. See L<Statistics::ANOVA::Compare|Statistics::ANOVA::Compare>.
830
831 =cut
832
833 5 ▶sub compare {
Subroutine compare: called CC=2 SCAR=7.0
Pod documented
834 5 my ( $self, %args ) = @_;
835 5 eval { require Statistics::ANOVA::Compare; };
836 5 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
837 'Don\'t know how to do ANOVA comparisons. Perhaps you need to install Statistics::ANOVA::Compare.'
838 if $@;
839 5 my $cmp = Statistics::ANOVA::Compare->new();
840 5 $cmp->share($self);
841 5 return $cmp->run(%args);
842 }
843
844 =head3 confidence
845
846 $itv_str = $aov->(independent => 1|0, alpha => .05, name => 'aname', limits => 0) # get interval for single variable as string
847 $lim_aref = $aov->(independent => 1|0, alpha => .05, name => 'aname', limits => 1) # get upper & lower limits for single variable as aref
848 $itv_href = $aov->(independent => 1|0, alpha => .05, name => ['aname', 'bname'], limits => 0) # get interval for 2 variables as hashref keyed by variable names
849 $lim_href = $aov->(independent => 1|0, alpha => .05, name => ['aname','bname'], limits => 1) # get upper & lower limits for 2 variables as hashref of variable-named arefs
850 $itv_href = $aov->(independent => 1|0, alpha => .05, name => undef, limits => 0) # get intervals for all variables as hashref keyed by variable names
851 $lim_href = $aov->(independent => 1|0, alpha => .05, name => undef, limits => 1) # upper & lower limits for all variables as hashref
852
853 Computes confidence intervals using (by default) the pooled estimate of variability over groups/levels, rather than the standard error within each group/level, as described by Masson and Loftus (2003). For a between groups design, the confidence interval (as usual) indicates that, at a certain level of probability, the true population mean is likely to be within the interval returned. For a within-subjects design, as any effect of the variability between subjects is eliminated, the confidence interval (alternatively) indicates the reliability of the how the sample means are distributed as an estimate of the how the population means are distributed.
854
855 In either case, there is an assumption that the variances within each condition are the same between the conditions (homogeneity of variances assumption).
856
857 Actual algorithm depends on whether the measures are obtained from indepedently (between-groups) (independent => 1) or by repeated measures (independent => 0) (i.e., whether between-groups or within-groups design). Default is between-groups.
858
859 The option C<use_mse> can be set to equal 0 so that the (typical) standard error of the mean is used in place of the mean-square error. This is one option to use when the variances are unequal.
860
861 The option C<conditions> can, optionally, include a referenced array naming the particular conditions that should be included when calculating I<MSe>. By default, this is all the conditions, using I<MSe> from the omnibus ANOVA. This is one option to handle the case of unequal variances between conditions.
862
863 =cut
864
865 2 ▶sub confidence {
Subroutine confidence: called CC=15 SCAR=32.9
Pod documented
866 2 my ( $self, %args ) = @_;
867
868 croak 'Need to run ANOVA to obtain requested statistic'
869 if !defined $self->{'_stat'}->{'df_w'}
870 2 ▶ || !defined $self->{'_stat'}->{'ms_w'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_stat"}{"df_w"} and defined $self->{"_stat"}{"ms_w"} 0 2 2
Condition33%1/3
defined $self->{"_stat"}{"df_w"} and defined $self->{"_stat"}{"ms_w"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->{"_stat"}{"df_w"} and defined $self->{"_stat"}{"ms_w"}
defined $self->{"_stat"}{"df_w"} defined $self->{"_stat"}{"ms_w"}
Truth table33%1/3
A and B
A= defined $self->{"_stat"}{"df_w"}B= defined $self->{"_stat"}{"ms_w"}
ABresult
0X0
100
111
871
872 2 my $data = $self->get_hoa_numonly_indep(); # List-wise clean-up of all data
873 croak 'Not enough variables for performing ANOVA'
874 2 ▶ if scalar( keys( %{$data} ) ) <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$data;} <= 1 0 2 2
875 2 ▶ if ( any { !scalar @{ $data->{$_} } } keys %{$data} ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not scalar @{$data->{$_};}; } , keys %{$data;})) 0 2 2
876 0 croak 'Empty data following purge of invalid value(s)';
877 }
878
879 # Init key params:
880 2 ▶ my $indep = defined $args{'independent'} ? $args{'independent'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'independent'} ? : 2 0 2
881 2 my $alpha = _init_alpha( $args{'alpha'} ); # default = .05
882 2 my $tcrit = abs( stdtri( $self->{'_stat'}->{'df_w'}, $alpha / 2 ) );
883 2 ▶ my $limits = delete $args{'limits'} or 0;
Branch: 1 branch
BranchTrueFalseTotal
my $limits = delete $args{"limits"} or '???' 0 2 2
884 2 ▶ my $use_mse = defined $args{'use_mse'} ? $args{'use_mse'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'use_mse'} ? : 0 2 2
885 my @names =
886 defined $args{'name'}
887 ? ref $args{'name'}
888 0 ? @{ $args{'name'} }
889 : ( $args{'name'} )
890 0 ▶ : keys( %{$data} );
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'name'} ? : 2 0 2
ref $args{'name'} ? : 0 2 2
891 my @conditions =
892 0 ▶ ref $args{'conditions'} ? @{ $args{'conditions'} } : @names;
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'conditions'} ? : 0 2 2
893 2 my ( $erv, $itv, %confints ) = ();
894
895 2 foreach (@names) {
896 2 ▶ if ($use_mse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_mse) { } 2 0 2
897 2 my $mse;
898 2 $mse = $self->{'_stat'}->{'ms_w'};
899 2 $erv = sqrt( $mse / count( @{ $data->{$_} } ) );
900 }
901 else {
902 $erv =
903 0 stddev( @{ $data->{$_} } ) / sqrt( count( @{ $data->{$_} } ) );
904 }
905 2 $itv = $erv * $tcrit;
906 2 ▶ if ($limits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limits) { } 0 2 2
907 $confints{$_} = [
908 0 mean( @{ $data->{$_} } ) - $itv,
909 0 mean( @{ $data->{$_} } ) + $itv
910 ];
911 }
912 else {
913 2 $confints{$_} = $itv;
914 }
915 }
916 2 ▶ return scalar( keys(%confints) ) > 1 ? \%confints : $confints{ $names[0] };
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %confints > 1 ? : 0 2 2
917 }
918
919 =head2 ACCESSING RESULTS
920
921 =head3 string
922
923 $str = $aov->string(mse => 1, eta_squared => 1, omega_squared => 1, precision_p => integer, precision_s => integer)
924
925 Returns a statement of result, in the form of C<F(df_b, df_w) = f_value, p = p_value>; or, for Friedman test C<chi^2(df_b) = chi_value, p = p_value> (to the value of I<precision_p>, if any); and so on for other test statistics. Optionally also get MSe, eta_squared and omega_squared values appended to the string, where relevant. These and the test statistic are "sprintf"'d to the I<precision_s> specified (or, by default, not at all).
926
927 =cut
928
929 0 ▶sub string {
Subroutine string: not called CC=12 SCAR=50.5
Pod documented
930 0 my ( $self, %args ) = @_;
931 0 my $str;
932 my $p_value =
933 $args{'precision_p'}
934 ? sprintf(
935 '%.' . $args{'precision_p'} . 'f',
936 $self->{'_stat'}->{'p_value'}
937 )
938 0 ▶ : $self->{'_stat'}->{'p_value'};
Branch: 1 branch
BranchTrueFalseTotal
$args{'precision_p'} ? : 0 0 0
939 0 ▶ my $precision_s = $args{'precision_s'} || 0;
Condition0%0/2
$args{'precision_s'} || 0
l!l
00
MC/DC0%0/1
$args{'precision_s'} || 0
$args{'precision_s'}
Truth table0%0/2
A
A= $args{'precision_s'}
Aresult
11
00
940 0 ▶ if ( defined $self->{'_stat'}->{'f_value'} && !$self->{'_dfree'} ) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $self->{'_stat'}{'f_value'} and not $self->{'_dfree'}) { } 0 0 0
elsif (defined $self->{'_stat'}{'h_value'}) { } 0 0 0
elsif (defined $self->{'_stat'}{'j_value'}) { } 0 0 0
elsif (defined $self->{'_stat'}{'l_value'}) { } 0 0 0
elsif (defined $self->{'_stat'}{'chi_value'}) { } 0 0 0
Condition0%0/3
defined $self->{"_stat"}{"f_value"} and not $self->{"_dfree"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"_stat"}{"f_value"} and not $self->{"_dfree"}
defined $self->{"_stat"}{"f_value"} not $self->{"_dfree"}
Truth table0%0/3
A and not(B)
A= defined $self->{"_stat"}{"f_value"}B= not $self->{"_dfree"}
ABresult
0X0
100
111
941 0 $str .= "F($self->{'_stat'}->{'df_b'}, $self->{'_stat'}->{'df_w'}) = ";
942 0 $str .= _precisioned( $precision_s, $self->{'_stat'}->{'f_value'} );
943 0 $str .= ", p = $p_value,";
944 $str .=
945 ' MSe = '
946 . _precisioned( $precision_s, $self->{'_stat'}->{'ms_w'} ) . ','
947 0 ▶ if $args{'mse'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"mse"} 0 0 0
948 $str .=
949 ' eta^2_p = '
950 . _precisioned( $precision_s, $self->eta_squared() ) . ','
951 0 ▶ if $args{'eta_squared'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"eta_squared"} 0 0 0
952 $str .=
953 ' omega^2_p = '
954 . _precisioned( $precision_s, $self->omega_squared() ) . ','
955 0 ▶ if $args{'omega_squared'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"omega_squared"} 0 0 0
956 0 chop($str);
957 }
958 elsif ( defined $self->{'_stat'}->{'h_value'} ) { # Kruskal-Wallis statistic
959 0 $str .= "H($self->{'_stat'}->{'df_b'}) = ";
960 0 $str .= _precisioned( $precision_s, $self->{'_stat'}->{'h_value'} );
961 0 $str .= ", p = $p_value";
962 }
963 elsif ( defined $self->{'_stat'}->{'j_value'} )
964 { # Jonckheere-Terpstra statistic
965 0 $str .= "J = ";
966 0 $str .= _precisioned( $precision_s, $self->{'_stat'}->{'j_value'} );
967 0 $str .= ", p = $p_value";
968 }
969 elsif ( defined $self->{'_stat'}->{'l_value'} ) { # Page statistic
970 0 $str .= "L = ";
971 0 $str .= _precisioned( $precision_s, $self->{'_stat'}->{'l_value'} );
972 0 $str .= ", p = $p_value";
973 }
974 elsif ( defined $self->{'_stat'}->{'chi_value'} ) { # Friedman statistic
975 0 $str .=
976 "chi^2($self->{'_stat'}->{'df_b'}, N = $self->{'_stat'}->{'count'}) = ";
977 0 $str .= _precisioned( $precision_s, $self->{'_stat'}->{'chi_value'} );
978 0 $str .= ", p = $p_value";
979 }
980 else {
981 0 croak 'Need to run omnibus test (anova) to obtain results string';
982 }
983 0 return $str;
984 }
985
986 =head3 table
987
988 $table = $aov->table(precision_p => integer, precision_s => integer);
989
990 Returns a table listing the degrees of freedom, sums of squares, and mean squares for the tested "factor" and "error" (between/within variables), and the I<F>- and I<p>-values. The test statistics are "sprintf"'d to the I<precision_s> specified (or, by default, not at all); the p value's precision can be specified by I<precision_p>.
991
992 Up to this version, if calculating any of these values was not essential to calculation of the test statistic, the value will simply appear as a blank in the table. If the omnibus test last made was non-parametric, and no I<F>-value was calculated, then the table returned is entirely an empty string.
993
994 Formatting with right-justification where appropriate is left for user-joy.
995
996 =cut
997
998 8 ▶sub table {
Subroutine table: called CC=14 SCAR=27.1
Pod documented
999 8 my ( $self, %args ) = @_;
1000 8 my $tbl = q{};
1001 8 ▶ my $precision_p = $args{'precision_p'} || 0;
Condition50%1/2
$args{'precision_p'} || 0
l!l
08
MC/DC0%0/1
$args{'precision_p'} || 0
$args{'precision_p'}
Truth table50%1/2
A
A= $args{'precision_p'}
Aresult
11
00
1002 8 ▶ my $precision_s = $args{'precision_s'} || 0;
Condition50%1/2
$args{'precision_s'} || 0
l!l
08
MC/DC0%0/1
$args{'precision_s'} || 0
$args{'precision_s'}
Truth table50%1/2
A
A= $args{'precision_s'}
Aresult
11
00
1003
1004 # F-table:
1005 8 ▶ if ( defined $self->{'_stat'}->{'f_value'} && !$self->{'_dfree'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_stat"}{"f_value"} and not $self->{"_dfree"}) 4 4 8
Condition66%2/3
defined $self->{"_stat"}{"f_value"} and not $self->{"_dfree"}
!ll&&!rl&&r
044
MC/DC50%1/2
defined $self->{"_stat"}{"f_value"} and not $self->{"_dfree"}
defined $self->{"_stat"}{"f_value"} not $self->{"_dfree"}
Truth table66%2/3
A and not(B)
A= defined $self->{"_stat"}{"f_value"}B= not $self->{"_dfree"}
ABresult
0X0
100
111
1006 4 $tbl .= "\t$_" foreach ( 'df', 'SumSq', 'MeanSq', 'F', 'Pr(>F)' );
1007 4 ▶ $tbl .= "\teta^2_p" if defined $self->{'_stat'}->{'eta_sq'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_stat"}{"eta_sq"} 0 4 4
1008 4 ▶ $tbl .= "\tomega^2_p" if defined $self->{'_stat'}->{'omega_sq'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_stat"}{"omega_sq"} 0 4 4
1009 4 $tbl .= "\n";
1010 4 $tbl .= "$_\t" foreach ( 'Factor', $self->{'_stat'}->{'df_b'} );
1011 $tbl .= _precisioned( $precision_s, $_ )
1012 4 . "\t" foreach (
1013 $self->{'_stat'}->{'ss_b'},
1014 $self->{'_stat'}->{'ms_b'},
1015 $self->{'_stat'}->{'f_value'}
1016 );
1017 4 for my $es (qw/eta_sq omega_sq/) {
1018 $tbl .= "\t" . _precisioned( $precision_s, $self->{'_stat'}->{$es} )
1019 8 ▶ if defined $self->{'_stat'}->{$es};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_stat"}{$es} 0 8 8
1020 }
1021 4 $tbl .= _precisioned( $precision_p, $self->{'_stat'}->{'p_value'} );
1022 4 $tbl .= "\n";
1023 4 $tbl .= "$_\t" foreach ( 'Error', $self->{'_stat'}->{'df_w'} );
1024 $tbl .= _precisioned( $precision_s, $_ ) . "\t"
1025 4 foreach ( $self->{'_stat'}->{'ss_w'}, $self->{'_stat'}->{'ms_w'} );
1026 4 $tbl .= "\n";
1027 }
1028 8 return $tbl;
1029 }
1030
1031 =head3 dump
1032
1033 $aov->dump(title => 'ANOVA test', precision_p => integer, precision_s => integer, mse => 1, eta_squared => 1, omega_squared => 1, verbose => 1)
1034
1035 Prints the string returned by L<string|string>, or, if specified with the attribute I<table> => 1, the table returned by L<table|table>; and the string as well if I<string> => 1. A newline - "\n" - is appended at the end of the print of the string. Above this string or table, a title can also be printed, by giving a value to the optional C<title> attribute.
1036
1037 If I<verbose> => 1, then any curiosities arising in the calculations are noted at the end of other dumps. At the moment, this is only the number of observations that might have been purged were they identified as undefined or not-a-number upon loading/adding.
1038
1039 =cut
1040
1041 0 ▶sub dump {
Subroutine dump: not called CC=6 SCAR=37.4
Pod documented
1042 0 my ( $self, %args ) = @_;
1043 0 ▶ print "$args{'title'}\n" if $args{'title'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"title"} 0 0 0
1044 0 ▶ if ( $args{'table'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'table'}) { } 0 0 0
1045 0 print $self->table(%args);
1046 0 ▶ print $self->string(%args), "\n" if $args{'string'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"string"} 0 0 0
1047 }
1048 else {
1049 0 print $self->string(%args), "\n";
1050 }
1051 print "Observations purged as undefined or not-a-number: "
1052 . $self->{'purged'} . "\n"
1053 0 ▶ if $self->{'purged'} && $args{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"purged"} and $args{"verbose"} 0 0 0
Condition0%0/3
$self->{"purged"} and $args{"verbose"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"purged"} and $args{"verbose"}
$self->{"purged"} $args{"verbose"}
Truth table0%0/3
A and B
A= $self->{"purged"}B= $args{"verbose"}
ABresult
0X0
100
111
1054 0 return;
1055 }
1056
1057 =head2 STATISTICS
1058
1059 =head3 ss_total
1060
1061 $ss_tot = $aov(independent => BOOL, ordinal => BOOL);
1062 ($ss_tot, $s_b, $ss_w) = $aov(independent => BOOL, ordinal => BOOL);
1063
1064 Returns the total sum-of-squares, being the sum of the between- and within-groups sums-of-squares, and so definable as the "corrected" total sum-of-squares. Called in array context, also returns the between- and within-groups sums-of-squares themselves.
1065
1066 =cut
1067
1068 1 ▶sub ss_total {
Subroutine ss_total: called CC=10 SCAR=34.7
Pod documented
1069 1 my ( $self, %args ) = @_;
1070 1 ▶ $args{'independent'} = 1 if !defined $args{'independent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"independent"} 0 1 1
1071 1 ▶ $args{'ordinal'} = 0 if !defined $args{'ordinal'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"ordinal"} 0 1 1
1072 1 my $data = _get_data( $self, %args );
1073
1074 1 my ( $ss_b, $ss_w ) = ();
1075 1 ▶ if ( $args{'independent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'independent'}) { } 1 0 1
1076 1 $ss_w = _sumsq_w_indep_param($data);
1077 1 ▶ if ( !$args{'ordinal'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'ordinal'}) { } 1 0 1
1078 1 $ss_b = _sumsq_b_indep_param_cat($data);
1079 }
1080 else {
1081 0 ▶ if ( $args{'ordinal'} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'ordinal'} == 1) { } 0 0 0
1082 0 $ss_b = _sumsq_b_indep_param_ord($data);
1083 }
1084 else {
1085 0 $ss_b = _sumsq_b_indep_param_ord_nonlinear($data);
1086 }
1087 }
1088 }
1089 else {
1090 0 my $n_bt = scalar keys %{$data};
1091 0 ▶ croak 'Not enough variables for performing ANOVA'
Branch: 1 branch
BranchTrueFalseTotal
if $n_bt < 2 0 0 0
1092 if $n_bt < 2;
1093 0 my $n_wt = $self->equal_n( data => $data );
1094 0 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if not $n_wt or $n_wt == 1 0 0 0
Condition0%0/3
not $n_wt or $n_wt == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $n_wt or $n_wt == 1
not $n_wt $n_wt == 1
Truth table0%0/3
not(A) or B
A= not $n_wtB= $n_wt == 1
ABresult
1X1
011
000
1095 'Number of observations per variable need to be equal and greater than 1 for repeated measures ANOVA'
1096 if !$n_wt or $n_wt == 1;
1097 0 ( $ss_b, $ss_w ) = _sumsq_bw_rmdep_param_uni( $data, $n_bt, $n_wt );
1098 }
1099 1 ▶ return wantarray ? ( ( $ss_b + $ss_w ), $ss_b, $ss_w ) : $ss_b + $ss_w;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
1100 }
1101
1102 =head3 ss_b
1103
1104 $ss_b = $anova->ss_b(independent => BOOL, ordinal => BOOL);
1105
1106 Returns the between-groups (aka treatment, effect, factor) sum-of-squares for the given data and the independence of the groups, and whether or not they have an ordinal relationship.
1107
1108 =cut
1109
1110 2 ▶sub ss_b {
Subroutine ss_b: called CC=9 SCAR=33.7
Pod documented
1111 2 my ( $self, %args ) = @_;
1112 2 ▶ $args{'independent'} = 1 if !defined $args{'independent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"independent"} 0 2 2
1113 2 ▶ $args{'ordinal'} = 0 if !defined $args{'ordinal'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"ordinal"} 0 2 2
1114 2 my $data = _get_data( $self, %args );
1115 2 my $ss;
1116 2 ▶ if ( $args{'independent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'independent'}) { } 2 0 2
1117 2 ▶ if ( !$args{'ordinal'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'ordinal'}) { } 2 0 2
1118 2 $ss = _sumsq_b_indep_param_cat($data);
1119 }
1120 else {
1121 0 ▶ if ( $args{'ordinal'} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'ordinal'} == 1) { } 0 0 0
1122 0 $ss = _sumsq_b_indep_param_ord($data);
1123 }
1124 else {
1125 0 $ss = _sumsq_b_indep_param_ord_nonlinear($data);
1126 }
1127 }
1128 }
1129 else {
1130 0 my $n_bt = scalar keys %{$data};
1131 0 ▶ croak 'Not enough variables for performing ANOVA'
Branch: 1 branch
BranchTrueFalseTotal
if $n_bt < 2 0 0 0
1132 if $n_bt < 2;
1133 0 my $n_wt = $self->equal_n( data => $data );
1134 0 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if not $n_wt or $n_wt == 1 0 0 0
Condition0%0/3
not $n_wt or $n_wt == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $n_wt or $n_wt == 1
not $n_wt $n_wt == 1
Truth table0%0/3
not(A) or B
A= not $n_wtB= $n_wt == 1
ABresult
1X1
011
000
1135 'Number of observations per variable need to be equal and greater than 1 for repeated measures ANOVA'
1136 if !$n_wt or $n_wt == 1;
1137 0 ($ss) = _sumsq_bw_rmdep_param_uni( $data, $n_bt, $n_wt );
1138 }
1139 2 return $ss;
1140 }
1141
1142 =head3 ss_w
1143
1144 $ss_w = $anova->ss_w(independent => BOOL);
1145
1146 Returns the within-groups (aka error) sum-of-squares for the given data and according to whether the data per group are independent or dependent.
1147
1148 =cut
1149
1150 2 ▶sub ss_w {
Subroutine ss_w: called CC=6 SCAR=26.9
Pod documented
1151 2 my ( $self, %args ) = @_;
1152 2 ▶ $args{'independent'} = 1 if !defined $args{'independent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"independent"} 0 2 2
1153 2 my $data = _get_data( $self, %args );
1154 2 my $ss;
1155 2 ▶ if ( $args{'independent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'independent'}) { } 2 0 2
1156 2 $ss = _sumsq_w_indep_param($data);
1157 }
1158 else {
1159 0 my $n_bt = scalar keys %{$data};
1160 0 ▶ croak 'Not enough variables for performing ANOVA' if $n_bt < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $n_bt < 2 0 0 0
1161 0 my $n_wt = $self->equal_n( data => $data );
1162 0 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if not $n_wt or $n_wt == 1 0 0 0
Condition0%0/3
not $n_wt or $n_wt == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $n_wt or $n_wt == 1
not $n_wt $n_wt == 1
Truth table0%0/3
not(A) or B
A= not $n_wtB= $n_wt == 1
ABresult
1X1
011
000
1163 'Number of observations per variable need to be equal and greater than 1 for repeated measures ANOVA'
1164 if !$n_wt or $n_wt == 1;
1165 0 ( $_, $ss ) = _sumsq_bw_rmdep_param_uni( $data, $n_bt, $n_wt );
1166 }
1167 2 return $ss;
1168 }
1169
1170 =head3 df_b
1171
1172 =cut
1173
1174 0 ▶sub df_b {
Subroutine df_b: not called CC=9 SCAR=45.0
Pod documented
1175 0 my ( $self, %args ) = @_;
1176 0 ▶ $args{'independent'} = 1 if !defined $args{'independent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"independent"} 0 0 0
1177 0 ▶ $args{'ordinal'} = 0 if !defined $args{'ordinal'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"ordinal"} 0 0 0
1178 0 my $data = _get_data( $self, %args );
1179 0 my $df;
1180 0 ▶ if ( $args{'independent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'independent'}) { } 0 0 0
1181 0 ▶ if ( !$args{'ordinal'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'ordinal'}) { } 0 0 0
1182 0 $df = _df_b_indep_param_cat($data);
1183 }
1184 else {
1185 0 ▶ if ( $args{'ordinal'} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'ordinal'} == 1) { } 0 0 0
1186 0 $df = _df_b_indep_param_ord_linear($data);
1187 }
1188 else {
1189 0 $df = _df_b_indep_param_ord_nonlinear($data);
1190 }
1191 }
1192
1193 #_aov_indep_dfree_ord
1194 }
1195 else {
1196 0 my $n_bt = scalar keys %{$data};
1197 0 ▶ croak 'Not enough variables for performing ANOVA'
Branch: 1 branch
BranchTrueFalseTotal
if $n_bt < 2 0 0 0
1198 if $n_bt < 2;
1199 0 my $n_wt = $self->equal_n( data => $data );
1200 0 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if not $n_wt or $n_wt == 1 0 0 0
Condition0%0/3
not $n_wt or $n_wt == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $n_wt or $n_wt == 1
not $n_wt $n_wt == 1
Truth table0%0/3
not(A) or B
A= not $n_wtB= $n_wt == 1
ABresult
1X1
011
000
1201 'Number of observations per variable need to be equal and greater than 1 for repeated measures ANOVA'
1202 if !$n_wt or $n_wt == 1;
1203 0 ($df) = _df_b_indep_dfree_cat($data);
1204 }
1205 0 return $df;
1206 }
1207
1208 16 ▶sub _df_b_indep_param_cat {
Subroutine _df_b_indep_param_cat: called CC=1 SCAR=0.0
1209 16 my $data = shift;
1210 16 return ( scalar keys %{$data} ) - 1;
1211 }
1212
1213 3 ▶sub _df_b_indep_param_ord_linear {
Subroutine _df_b_indep_param_ord_linear: called CC=1 SCAR=0.0
1214 3 my $data = shift;
1215 3 return ( scalar keys %{$data} ) - 1;
1216 }
1217
1218 2 ▶sub _df_b_indep_param_ord_nonlinear {
Subroutine _df_b_indep_param_ord_nonlinear: called CC=1 SCAR=0.0
1219 2 my $data = shift;
1220 2 return ( scalar keys %{$data} ) - 2;
1221 }
1222
1223 0 ▶sub _df_b_indep_dfree_cat {
Subroutine _df_b_indep_dfree_cat: not called CC=1 SCAR=6.9
1224 0 my $data = shift;
1225 0 return ( scalar keys %{$data} ) - 1;
1226 }
1227
1228 =head3 grand_mean
1229
1230 $mean = $anova->grand_mean();
1231
1232 Returns the mean of all observations.
1233
1234 =cut
1235
1236 0 ▶sub grand_mean {
Subroutine grand_mean: not called CC=1 SCAR=6.9
Pod documented
1237 0 my ( $self, %args ) = @_;
1238 0 my $data = _get_data( $self, %args );
1239 0 return mean( map { @{ $data->{$_} } } keys %{$data} );
1240 }
1241
1242 =head3 grand_sum
1243
1244 $sum = $anova->grand_sum($data);
1245
1246 Returns the sum of all observations.
1247
1248 =cut
1249
1250 0 ▶sub grand_sum {
Subroutine grand_sum: not called CC=1 SCAR=6.9
Pod documented
1251 0 my ( $self, %args ) = @_;
1252 0 my $data = _get_data( $self, %args );
1253 0 return sum0( map { @{ $data->{$_} } } keys %{$data} );
1254 }
1255
1256 =head3 grand_n
1257
1258 $count = $anova->grand_n();
1259
1260 Returns the number of all observations.
1261
1262 =cut
1263
1264 1 ▶sub grand_n {
Subroutine grand_n: called CC=1 SCAR=0.0
Pod documented
1265 1 my ( $self, %args ) = @_;
1266 1 my $data = _get_data( $self, %args );
1267 1 return count( map { @{ $data->{$_} } } keys %{$data} );
1268 }
1269
1270 # Private methods
1271
1272 6 ▶sub _get_data {
Subroutine _get_data: called CC=5 SCAR=17.8
1273 6 my ( $self, %args ) = @_;
1274 6 my ($data) = ();
1275 6 ▶ if ( ref $args{'data'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args{'data'}) { } 0 6 6
elsif (not defined $args{'independent'} or $args{'independent'} == 1) { } 6 0 6
1276 0 $data = delete $args{'data'};
1277 }
1278 ▶ elsif ( not defined $args{'independent'} or $args{'independent'} == 1 ) {
Condition66%2/3
not defined $args{"independent"} or $args{"independent"} == 1
l!l&&r!l&&!r
150
MC/DC0%0/2
not defined $args{"independent"} or $args{"independent"} == 1
not defined $args{"independent"} $args{"independent"} == 1
Truth table66%2/3
not(A) or B
A= not defined $args{"independent"}B= $args{"independent"} == 1
ABresult
1X1
011
000
1279 6 $data = $self->get_hoa_numonly_indep(%args);
1280 }
1281 else {
1282 0 $data = $self->get_hoa_numonly_across(%args);
1283 }
1284
1285 6 ▶ if ( any { !scalar @{ $data->{$_} } } keys %{$data} ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not scalar @{$data->{$_};}; } , keys %{$data;})) 0 6 6
1286 0 croak 'Empty data following purge of invalid value(s)';
1287 }
1288 6 return $data;
1289 }
1290
1291 21 ▶sub _sumsq_b_indep_param_cat {
Subroutine _sumsq_b_indep_param_cat: called CC=1 SCAR=0.0
1292 21 my $data = shift;
1293 my @group_ns_and_means =
1294 71 map { [ count( @{ $data->{$_} } ), mean( @{ $data->{$_} } ) ] }
1295 21 keys %{$data};
1296 21 my $grand_mean = mean( map { @{ $data->{$_} } } keys %{$data} );
1297 21 return sum0( map { $_->[0] * ( $_->[1] - $grand_mean )**2 }
1298 @group_ns_and_means );
1299 }
1300
1301 5 ▶sub _sumsq_b_indep_param_ord { # linear between-group sum-of-squares
Subroutine _sumsq_b_indep_param_ord: called CC=2 SCAR=6.9
1302 5 my $data = shift;
1303 5 my @names = keys( %{$data} );
1304 croak
1305 "Check names for variables: All need to be numerical for trend analysis"
1306 5 ▶ if grep { !looks_like_number($_) } @names;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not &looks_like_number($_);} @names 0 5 5
1307 5 my $mean_t = mean(@names); # mean of the ordinal values
1308 my $sum_sample_contrasts =
1309 5 sum( map { mean( @{ $data->{$_} } ) * ( $_ - $mean_t ) } @names );
1310 my $sumsquared_coeffs =
1311 5 sum( map { ( $_ - $mean_t )**2 / count( @{ $data->{$_} } ) } @names )
1312 ; # unweighted
1313 5 return $sum_sample_contrasts**2 / $sumsquared_coeffs;
1314 }
1315
1316 2 ▶sub _sumsq_b_indep_param_ord_nonlinear {
Subroutine _sumsq_b_indep_param_ord_nonlinear: called CC=1 SCAR=0.0
1317 2 my $data = shift;
1318 2 return _sumsq_b_indep_param_cat($data) - _sumsq_b_indep_param_ord($data);
1319 }
1320
1321 24 ▶sub _sumsq_w_indep_param { # within-group SS (and DF):
Subroutine _sumsq_w_indep_param: called CC=4 SCAR=13.9
1322 24 my $data = shift;
1323 24 my ( $ss_w, $df_w ) = ( 0, 0 );
1324 24 foreach ( keys %{$data} ) {
1325 80 my $mean = mean( @{ $data->{$_} } );
1326 80 $ss_w += ( $_ - $mean )**2 foreach @{ $data->{$_} };
1327 80 $df_w += ( count( @{ $data->{$_} } ) - 1 );
1328 }
1329 24 ▶ return wantarray ? return ( $ss_w, $df_w ) : $ss_w;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 21 3 24
1330 }
1331
1332 sub _sumsq_bw_rmdep_param_uni
1333 9 ▶{ # error and treatment sums-of-squares for rm anovas (univariate method)
Subroutine _sumsq_bw_rmdep_param_uni: called CC=7 SCAR=19.5
1334 9 my ( $data, $n_bt, $n_wt ) = @_;
1335 9 my ( $ss_b, $ss_w, $df_b, $df_w, $i, @i_means, %i_data, %j_means ) = ();
1336
1337 # Mean over each index:
1338 9 ▶ for ( $i = 0 ; $i < $n_wt ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n_wt) 72 9 81
1339 72 push @{ $i_data{$i} }, $data->{$_}->[$i] foreach keys %{$data};
1340 72 $i_means[$i] = mean( @{ $i_data{$i} } );
1341 }
1342 9 ▶ croak 'No means to divide by' if !scalar @i_means;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @i_means 0 9 9
1343 9 my $grand_mean = sum0(@i_means) / scalar @i_means;
1344
1345 9 foreach ( keys %{$data} ) {
1346 32 $j_means{$_} = mean( @{ $data->{$_} } );
1347 32 $ss_b += ( $j_means{$_} - $grand_mean )**2;
1348 }
1349 9 $ss_b *= $n_wt;
1350
1351 9 foreach ( keys %{$data} ) {
1352 32 ▶ for ( $i = 0 ; $i < $n_wt ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n_wt) 273 32 305
1353 $ss_w +=
1354 273 ( $data->{$_}->[$i] - $i_means[$i] - $j_means{$_} + $grand_mean )
1355 **2;
1356 }
1357 }
1358 9 $df_b = $n_bt - 1;
1359 9 $df_w = $df_b * ( $n_wt - 1 );
1360 9 return ( $ss_b, $ss_w, $df_b, $df_w );
1361 }
1362
1363 4 ▶sub _aref2href {
Subroutine _aref2href: called CC=4 SCAR=13.9
1364 4 my $aref = shift;
1365 4 my %hash = ();
1366 4 ▶ $aref = [$aref] if !ref $aref->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aref->[0] 1 3 4
1367 4 foreach ( @{$aref} ) {
1368 6 ▶ if ( ref $_->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_->[1]) { } 4 2 6
1369 4 $hash{ $_->[0] } = $_->[1];
1370 }
1371 else {
1372 2 my $name = shift( @{$_} );
1373 2 $hash{$name} = [ @{$_} ];
1374 }
1375 }
1376 4 return \%hash;
1377 }
1378
1379 0 ▶sub _pcorrect { # (1 - ( 1 - p)^N )
Subroutine _pcorrect: not called CC=1 SCAR=6.9
1380 0 return 1 - ( 1 - $_[0] )**$_[1];
1381 }
1382
1383 24 ▶sub _precisioned {
Subroutine _precisioned: called CC=3 SCAR=12.7
1384 24 ▶ return $_[0]
Branch: 2 branches
BranchTrueFalseTotal
$_[0] ? : 0 24 24
defined $_[1] ? : 24 0 24
1385 ? sprintf( '%.' . $_[0] . 'f', $_[1] )
1386 : ( defined $_[1] ? $_[1] : q{} ); # don't lose any zero
1387 }
1388
1389 2 ▶sub _init_alpha {
Subroutine _init_alpha: called CC=4 SCAR=22.3
1390 2 my $val = shift;
1391 2 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 2 2
1392 0 ▶ if ( $val > 0 && $val < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val > 0 and $val < 1) { } 0 0 0
Condition0%0/3
$val > 0 and $val < 1
!ll&&!rl&&r
000
MC/DC0%0/2
$val > 0 and $val < 1
$val > 0 $val < 1
Truth table0%0/3
A and B
A= $val > 0B= $val < 1
ABresult
0X0
100
111
1393 0 return $val;
1394 }
1395 else {
1396 0 croak "Alpha value should be between 0 and 1, not '$val'.";
1397 }
1398 }
1399 else {
1400 2 return $ALPHA_DEFAULT;
1401 }
1402 }
1403
1404 0 ▶sub cluster {
Subroutine cluster: not called CC=1 SCAR=6.9
Pod undocumented
1405 0 croak 'cluster() method is deprecated. See Statistics::ANOVA::Cluster';
1406 0 return;
1407 }
1408
1409 16 1;
1410
1411 __END__
↑ Summary ANOVA/Compare.pm »