blib/lib/Statistics/FisherPitman.pm

filter: Statementstmt 50.0% 65 / 130 Branchbran 18.4% 7 / 38 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 72.2% 13 / 18 Podpod 100% 8 / 8 totaltotal 46.5% 94 / 202 CC 29 SCAR 34.2
CC
29
Cov
42%
p_value
110.0
string
30.0
_fy_shuffle
12.0
CRAP 30.6
↑ Summary
1 package Statistics::FisherPitman;
2
3 3 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Statistics::Descriptive;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 $VERSION = 0.034;
11
12 =pod
13
14 =head1 NAME
15
16 Statistics::FisherPitman - Randomization-based alternative to one-way independent groups ANOVA; unequal variances okay
17
18 =head1 SYNOPSIS
19
20 use Statistics::FisherPitman 0.034;
21
22 my @dat1 = (qw/12 12 14 15 12 11 15/);
23 my @dat2 = (qw/13 14 18 19 22 21 26/);
24
25 my $fishpit = Statistics::FisherPitman->new();
26 $fishpit->load({d1 => \@dat1, d2 => \@dat2});
27
28 # Oh, more data just came in:
29 my @dat3 = (qw/11 7 7 2 19 19/);
30 $fishpit->add({d3 => \@dat3});
31
32 my $T = $fishpit->t_value();
33 # now go to monte carlo to get a p for your T
34
35 # or get a t_value and p_value in one by randomization test:
36 $fishpit->p_value(resamplings => 1000)->dump(title => "A test");
37
38 =head1 DESCRIPTION
39
40 Tests for a difference between independent samples. It is commonly recommended as an alternative to the oneway independent groups ANOVA when variances are unequal, as its test-statistic, I<T>, is not dependent on an estimate of variance. As a randomization test, it is "distribution-free", with the probability of obtaining the observed value of I<T> being derived from the data themselves.
41
42 =head1 METHODS
43
44 =head2 new
45
46 $fishpit = Statistics::FisherPitman->new()
47
48 Class constructor; expects nothing.
49
50 =cut
51
52 #-----------------------------------------------------------------------
53 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
54 #-----------------------------------------------------------------------
55 1 my $proto = shift;
56 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
57 1 my $self= {};
58 ##$self->{$_} = '' foreach qw/df_t df_e f_value chi_value p_value ss_t ss_e title/;
59 1 bless($self, $class);
60 1 return $self;
61 }
62
63 =head2 load
64
65 $fishpit->load('aname', @data1)
66 $fishpit->load('aname', \@data1)
67 $fishpit->load({'aname' => \@data1, 'another_name' => \@data2})
68
69 I<Alias>: C<load_data>
70
71 Accepts either (1) a single C<name =E<gt> value> pair of a sample name, and a list (referenced or not) of data; or (2) a hash reference of named array references of data. The data are loaded into the class object by name, within a hash named C<data>, as L<Statistics::Descriptive::Full|Statistics::Descriptive> objects. So you can easily get at any descriptives for the groups you've loaded - e.g., $fishpit->{'data'}->{'aname'}->mean() - or you could get at the data again by going $fishpit->{'data'}->{'aname'}->get_data(); and so on. The names of the data are up to you.
72
73 Each call L<unload|unload>s any previous loads.
74
75 Returns the Statistics::FisherPitman object.
76
77 =cut
78
79 #-----------------------------------------------------------------------
80 1 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
81 #-----------------------------------------------------------------------
82 1 my $self = shift;
83 1 $self->unload();
84 1 $self->add(@_);
85 }
86 3 *load_data = \&load; # Alias
87
88
89 =head2 add
90
91 $fishpit->add('another_name', @data2)
92 $fishpit->add('another_name', \@data2)
93 $fishpit->add({'another_name' => \@data2})
94
95 I<Alias>: C<add_data>
96
97 Same as L<load|load> except that any previous loads are not L<unload|unload>ed.
98
99 =cut
100
101 #-----------------------------------------------------------------------
102 1 ▶sub add {
Subroutine add: called CC=6 SCAR=23.0
Pod documented
103 #-----------------------------------------------------------------------
104 1 my $self = shift;
105
106 1 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 1 0 1
107 1 ▶ while (my ($sample_name, $sample_data) = each %{$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$_[0];}) 2 1 3
108 2 ▶ if (ref $sample_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sample_data) 2 0 2
109 2 $self->{'data'}->{$sample_name} = Statistics::Descriptive::Full->new();
110 2 $self->{'data'}->{$sample_name}->add_data(@{$sample_data});
111 }
112 }
113 }
114 else {
115 0 my $sample_name = shift;
116 0 ▶ my $sample_data = ref $_[0] eq 'ARRAY' ? $_[0] : scalar (@_) ? \@_ : croak 'No list of data';
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
scalar @_ ? : 0 0 0
117 0 $self->{'data'}->{$sample_name} = Statistics::Descriptive::Full->new();
118 0 $self->{'data'}->{$sample_name}->add_data(@{$sample_data});
119 }
120 }
121 3 *add_data = \&add; # Alias
122
123 =head2 unload
124
125 $fishpit->unload();
126
127 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.
128
129 =cut
130
131 #-----------------------------------------------------------------------
132 1 ▶sub unload {
Subroutine unload: called CC=2 SCAR=6.9
Pod documented
133 #-----------------------------------------------------------------------
134 1 my $self = shift;
135 1 $self->{'data'} = {};
136 1 $self->{$_} = undef foreach qw/df_t df_e f_value chi_value p_value ss_t ss_e ms_t ms_e conf_int/;
137 }
138
139 =head2 t_value
140
141 $fishpit->t_value()
142
143 Returns a Fisher-Pitman T-value for the loaded data, and lumps the value into the class object for the key I<t_value>.
144
145 I<T> is calculated as follows:
146
147 =for html <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>g</i><br>&nbsp;<i>T</i> = &nbsp;SUM&nbsp;&nbsp;<i>n</i><sub><i>i</i></sub>&nbsp;<i>x<sub><i>i</i></sub>&sup2;</i></sup><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>i</i> = 1</p>
148
149 which pertains to the I<n>umber of observations in each I<i> of I<g> samples, and
150
151 =for html <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>n</i><sub><i>i</i></sub><br>&nbsp;<i>x</i><sub><i>i</i></sub> = &nbsp;1/<i>n</i><sub><i>i</i></sub>&nbsp;SUM&nbsp;&nbsp;<i>x</i><sub><i>ij</i></sub><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>j</i> = 1</p>
152
153 (for each I<j> observation in the I<i> sample).
154
155 =cut
156
157 #-----------------------------------------------------------------------
158 1 ▶sub t_value {
Subroutine t_value: called CC=5 SCAR=16.6
Pod documented
159 #-----------------------------------------------------------------------
160 1 my ($self, %args) = @_;
161 0 ▶ my %data = ref $args{'data'} eq 'HASH' ? %{$args{'data'}} : ref $self->{'data'} eq 'HASH' ? %{$self->{'data'}} : croak 'No reference to a hash of data for performing ANOVA';
Branch: 2 branches
BranchTrueFalseTotal
ref $args{'data'} eq 'HASH' ? : 0 1 1
ref $self->{'data'} eq 'HASH' ? : 1 0 1
162 1 my (%lens, @dat, %orig) = ();
163
164 1 foreach (keys %data) {
165 2 ▶ $lens{$_} = $data{$_}->count or croak 'Empty data sent to Fisher-Pitman test';
Branch: 1 branch
BranchTrueFalseTotal
$lens{$_} = $data{$_}->count or croak("Empty data sent to Fisher-Pitman test") 2 0 2
166 2 $orig{$_} = [$data{$_}->get_data];
167 2 push @dat, $data{$_}->get_data;
168 }
169
170 1 my $T = _get_T(\%orig);
171 1 $self->{'t_value'} = $T;
172
173 1 return $T;
174 }
175
176 =head2 p_value
177
178 $fishpit->p_value(resamplings => 'non-negative number')
179
180 I<Alias>: test
181
182 With a positive value for I<resamplings>, the loaded data will be shuffled so many times, and the T-value calculated for each resampling. The proportion of T-values in these resamplings that are greater than I<or equal to> the T-value of the original data, as loaded, is the I<p_value> for basing significance considerations upon.
183
184 Randomization test is simply based on pooling all the data and, for each resampling, giving them a Fisher-Yates shuffle, and distributing them to so many groups, of so many sample-sizes, as in the original dataset.
185
186 The class object is fed the values for C<t_value> and C<p_value>. Confidence interval (95%) of the true proportion (p-value) is also calculated and stored as a two-element array named C<conf_int>. The method returns only itself. So you can get at these values thus:
187
188 print "T = $fishpit->{'t_value'}, p = $fishpit->{'p_value'}\n";
189 print '95% confidence interval for the proportion of Ts greater than or equal to the observed value ranges from ';
190 print "$fishpit->{'conf_int'}->[0] to $fishpit->{'conf_int'}->[1].\n";
191
192 =cut
193
194 #-----------------------------------------------------------------------
195 0 ▶sub p_value {
Subroutine p_value: not called CC=10 SCAR=47.0
Pod documented
196 #-----------------------------------------------------------------------
197 0 my ($self, %args) = @_;
198 0 ▶ my %data = ref $args{'data'} eq 'HASH' ? %{$args{'data'}} : ref $self->{'data'} eq 'HASH' ? %{$self->{'data'}} : croak 'No reference to a hash of data for performing ANOVA';
Branch: 2 branches
BranchTrueFalseTotal
ref $args{'data'} eq 'HASH' ? : 0 0 0
ref $self->{'data'} eq 'HASH' ? : 0 0 0
199
200 0 my (%lens, @dat, %orig) = ();
201 0 foreach (keys %data) {
202 0 $orig{$_} = [$data{$_}->get_data];
203 0 push @dat, $data{$_}->get_data;
204 0 ▶ $lens{$_} = $data{$_}->count or croak 'Empty data sent to Fisher-Pitman test';
Branch: 1 branch
BranchTrueFalseTotal
$lens{$_} = $data{$_}->count or croak("Empty data sent to Fisher-Pitman test") 0 0 0
205 }
206
207 0 my $T = _get_T(\%orig);
208 0 $self->{'t_value'} = $T;
209
210 0 ▶ my $resamplings = $args{'resamplings'} || return $self;
Condition0%0/2
$args{'resamplings'} || (return $self)
l!l
00
MC/DC0%0/1
$args{'resamplings'} || (return $self)
$args{'resamplings'}
Truth table0%0/2
A
A= $args{'resamplings'}
Aresult
11
00
211
212 0 my ($n_gteq, $name, @ari, @perm, %rands) = (0);
213
214 0 foreach (1 .. $resamplings) {
215 0 _fy_shuffle(\@dat);
216 0 @perm = @dat;
217 0 foreach $name(keys %data) {
218 0 @ari = ();
219 0 for (1 .. $lens{$name}) {
220 0 push @ari, shift @perm;
221 }
222 0 $rands{$name} = [@ari];
223 }
224 0 ▶ $n_gteq++ if _get_T(\%rands) >= $T;
Branch: 1 branch
BranchTrueFalseTotal
if _get_T(\%rands) >= $T 0 0 0
225 0 %rands = ();
226 }
227 0 my $p = $n_gteq / $resamplings;
228 0 $self->{'p_value'} = $p;
229 0 $self->{'conf_int'} = _conf_int($n_gteq, $resamplings);
230 0 return $self;
231 }
232 3 *test = \&p_value; # Alias
233
234 =head2 dump
235
236 $fishpit->dump(title => 'A test of something', conf_int => 1|0, precision_p => integer)
237
238 Prints a line to STDOUT of the form I<T = t_value, p = p_value>. Above this string, a title can also be printed, by giving a value to the optional I<title> argument. The 95% confidence interval, and the precision of the p-value(s), can also be optionally dumped, as above. Ends with a line-break, i.e., "\n".
239
240 =cut
241
242 #-----------------------------------------------------------------------
243 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
244 #-----------------------------------------------------------------------
245 0 my ($self, %args) = @_;
246 0 ▶ print "$args{'title'}\n" if $args{'title'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"title"} 0 0 0
247 0 print $self->string(%args);
248 0 print "\n";
249 }
250
251 =head2 string
252
253 $fishpit->string(conf_int => 1|0, precision_p => integer)
254
255 Returns a line of the form I<T = t_value, p = p_value>, to the precision specified (if any), and, optionally, with the confidence-interval for the p-value appended.
256
257 =cut
258
259 #-----------------------------------------------------------------------
260 0 ▶sub string {
Subroutine string: not called CC=5 SCAR=34.0
Pod documented
261 #-----------------------------------------------------------------------
262 0 my ($self, %args) = @_;
263 0 my $str = '';
264 0 $str .= "T = $self->{'t_value'}";
265 0 ▶ if (defined $self->{'p_value'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"p_value"}) 0 0 0
266 0 $str .= ', p = ';
267 0 ▶ $str .= $args{'precision_p'} ? sprintf('%.' . $args{'precision_p'} . 'f', $self->{'p_value'}) : $self->{'p_value'};
Branch: 1 branch
BranchTrueFalseTotal
$args{'precision_p'} ? : 0 0 0
268 0 ▶ if ($args{'conf_int'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"conf_int"}) 0 0 0
269 0 $str .= " (95% CI: ";
270 0 ▶ if ($args{'precision_p'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'precision_p'}) { } 0 0 0
271 0 $str .= sprintf('%.' . $args{'precision_p'} . 'f', $self->{'conf_int'}->[0]);
272 0 $str .= ', ';
273 0 $str .= sprintf('%.' . $args{'precision_p'} . 'f', $self->{'conf_int'}->[1]);
274 }
275 else {
276 0 $str .= join(', ', @{$self->{'conf_int'}});
277 }
278 0 $str .= ')';
279 }
280 }
281 0 return $str;
282 }
283
284 1 ▶sub _get_T {
Subroutine _get_T: called CC=2 SCAR=6.9
285 1 my ($data) = @_;
286 1 my ($T, $xij, $count) = (0, 0);
287 1 foreach (keys %{$data}) {
288 2 $count = scalar(@{$data->{$_}});
289 2 $xij = 1 / $count * sum(@{$data->{$_}});
290 2 $T += $count * $xij**2
291 }
292 1 return $T;
293 }
294
295 0 ▶sub _conf_int {
Subroutine _conf_int: not called CC=1 SCAR=6.9
296 0 my ($ng, $ns) = @_;
297 0 my $p = $ng / $ns;
298 0 my $i = 1.96 * sqrt($p * (1 - $p) / $ns);
299 0 my $lo = $p - $i;
300 0 my $hi = $p + $i;
301 0 return [$lo, $hi];
302 }
303
304 0 ▶sub _fy_shuffle { # fisher-yates shuffle, via Perl FAQ 4
Subroutine _fy_shuffle: not called CC=3 SCAR=24.8
305 0 my ($list) = @_;
306 0 my $i;
307 0 ▶ for ($i = @$list; --$i; ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$i) 0 0 0
308 0 my $j = int rand ($i+1);
309 0 ▶ next if $i == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 0 0 0
310 0 @$list[ $i, $j ] = @$list[ $j, $i ];
311 }
312 }
313
314 3 1;
315 __END__
↑ Summary