blib/lib/Statistics/Gtest.pm

filter: Statementstmt 77.3% 201 / 260 Branchbran 65.1% 43 / 66 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 78.1% 25 / 32 Podpod 83.3% 15 / 18 totaltotal 75.1% 290 / 386 CC 49 SCAR 19.7
CC
49
Cov
75%
_initialize
17.3
getRow
12.0
getCol
12.0
CRAP 7.2
↑ Summary
1 #!/usr/bin/perl -w
2  
3 package Statistics::Gtest;
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 $VERSION = '0.07';
11  
12 4 my $self;
13  
14 10 ▶sub new {
Subroutine new: called CC=4 SCAR=15.2
Pod undocumented
15 10 my ($type, $data) = @_;
16 10 my $datahandle;
17 10 ▶ if (! $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 10 10
18 0 _error(1);
19 }
20 10 ▶ if (@_ eq 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ eq 3) 0 10 10
21 0 _error(2);
22 }
23 10 ▶ if ($datahandle = _getHandle($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($datahandle = _getHandle($data)) { } 10 0 10
24 10 $self = {};
25 10 _initialize($datahandle);
26 10 bless($self, $type);
27 10 $self;
28 }
29 else {
30 0 _error(3);
31 }
32 }
33  
34 10 ▶sub getG {
Subroutine getG: called CC=1 SCAR=0.0
Pod documented
35 10 my $self = shift;
36 10 $self->{'G'} = $self->getRawG() / $self->getQ();
37 10 $self->{'G'};
38 }
39  
40 20 ▶sub getRawG {
Subroutine getRawG: called CC=1 SCAR=0.0
Pod documented
41 20 my $self = shift;
42 20 $self->{'logsum'} = _sumCellOp();
43 20 $self->{'G'} = 2 * $self->{'logsum'};
44 20 2 * $self->{'logsum'};
45 }
46  
47 20 ▶sub getQ {
Subroutine getQ: called CC=1 SCAR=0.0
Pod documented
48 20 my $self = shift;
49 20 $self->{'q'} = _williamsC();
50 20 $self->{'q'};
51 }
52  
53 3 ▶sub setExpected {
Subroutine setExpected: called CC=7 SCAR=20.8
Pod documented
54 3 my ($self, $expData) = @_;
55 3 my $datahandle;
56 my @expect;
57 3 my $expectTotal = 0;
58 3 ▶ if (@_ eq 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ eq 3) 0 3 3
59 0 _error(2);
60 }
61 3 ▶ if ($datahandle = _getHandle($expData)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($datahandle = _getHandle($expData)) { } 3 0 3
62 3 foreach my $row_ref (@{$datahandle}) {
63 3 ▶ if (!ref $row_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $row_ref) 0 3 3
64 0 _error(4);
65 }
66 3 my @row = @{$row_ref};
67 3 foreach my $cell (@row) {
68 12 _checkNumValidity($cell);
69 12 push(@expect, $cell);
70 12 $expectTotal += $cell;
71 }
72 3 $self->{'expected'} = \@expect;
73 # Expected values no longer intrinsic to observed data, so set
74 # flag to 0.
75 3 $self->{'intrinsic'} = 0;
76 }
77 }
78 else {
79 0 _error(3);
80 }
81 # Data sanity checks
82 3 ▶ if ($expectTotal != $self->{'sumtotal'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expectTotal != $self->{"sumtotal"}) 0 3 3
83 warn "Warning: Total of expected values ($expectTotal) does not ",
84 0 "equal total of observed values (",$self->{'sumtotal'},").\nThis ",
85 "will invalidate the test unless the discrepancy is very minor ",
86 "\n(e.g., the result of rounding error).\n";
87 }
88 }
89  
90 0 ▶sub getObserved {
Subroutine getObserved: not called CC=1 SCAR=6.9
Pod documented
91 0 my $self = shift;
92 0 my $exp = _formatData($self->{'observed'});
93 0 $exp;
94 }
95  
96 6 ▶sub getExpected {
Subroutine getExpected: called CC=1 SCAR=0.0
Pod documented
97 6 my $self = shift;
98 6 my $exp = _formatData($self->{'expected'});
99 6 $exp;
100 }
101  
102 10 ▶sub getDF {
Subroutine getDF: called CC=1 SCAR=0.0
Pod documented
103 10 my $self = shift;
104 10 $self->{'df'};
105 }
106  
107 0 ▶sub setDF {
Subroutine setDF: not called CC=1 SCAR=6.9
Pod documented
108 0 my $self = shift;
109 0 $self->{'df'} = shift;
110 # Degrees of freedom set externally.
111 0 $self->{'df_extern'} = 1;
112 }
113  
114 0 ▶sub getDFstate {
Subroutine getDFstate: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my $self = shift;
116 0 $self->{'df_extern'};
117 }
118  
119 0 ▶sub getHypothesis {
Subroutine getHypothesis: not called CC=1 SCAR=6.9
Pod undocumented
120 0 my $self = shift;
121 0 $self->{'intrinsic'};
122 }
123  
124 0 ▶sub getRow {
Subroutine getRow: not called CC=3 SCAR=24.8
Pod documented
125 0 my $self = shift;
126 0 my $idx = shift;
127 0 ▶ if ($idx > ($self->{'nrows'} - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx > $self->{"nrows"} - 1) 0 0 0
128 0 return;
129 }
130 0 my @row = ();
131 0 my $skip = $self->{'ncols'};
132 0 my $offset = $idx * $skip;
133 0 my $final = $offset + $skip;
134 0 for my $cell ($offset .. $final - 1) {
135 0 push(@row, $self->{'observed'}->[$cell]);
136 }
137 0 \@row;
138 }
139  
140 0 ▶sub getCol {
Subroutine getCol: not called CC=3 SCAR=24.8
Pod documented
141 0 my $self = shift;
142 0 my $idx = shift;
143 0 ▶ if ($idx > ($self->{'ncols'} - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx > $self->{"ncols"} - 1) 0 0 0
144 0 return;
145 }
146 0 my @col = ();
147 0 my $skip = $self->{'ncols'};
148 0 my $max = $skip * $self->{'nrows'};
149 0 ▶ for (my $cell = $idx; $cell < $max; $cell += $skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell < $max) 0 0 0
150 0 push(@col,$self->{'observed'}->[$cell]);
151 }
152 0 \@col;
153 }
154  
155 20 ▶sub rowSum {
Subroutine rowSum: called CC=1 SCAR=0.0
Pod documented
156 20 my $self = shift;
157 20 my $idx = shift;
158 20 $self->{'rowsums'}->[$idx];
159 }
160  
161 20 ▶sub colSum {
Subroutine colSum: called CC=1 SCAR=0.0
Pod documented
162 20 my $self = shift;
163 20 my $idx = shift;
164 20 $self->{'colsums'}->[$idx];
165 }
166
167 10 ▶sub getRowNum {
Subroutine getRowNum: called CC=1 SCAR=0.0
Pod documented
168 10 my $self = shift;
169 10 $self->{'nrows'};
170 }
171  
172 10 ▶sub getColNum {
Subroutine getColNum: called CC=1 SCAR=0.0
Pod documented
173 10 my $self = shift;
174 10 $self->{'ncols'};
175 }
176  
177 10 ▶sub getSumTotal {
Subroutine getSumTotal: called CC=1 SCAR=0.0
Pod documented
178 10 my $self = shift;
179 10 $self->{'sumtotal'};
180 }
181  
182 # private methods
183  
184 # Do all the initialization work: verify the data and write it into the
185 # object; calculate the row and column sums and grand total; determine
186 # the format of the data table (rows x columns); calculate the likely
187 # degrees of freedom and generate default expected values. Set state
188 # flags indicating that the expected values are 'intrinsic' (generated
189 # from the observed data), and that the degrees of freedom are also
190 # based on table characteristics.
191 10 ▶sub _initialize {
Subroutine _initialize: called CC=16 SCAR=28.5
192 10 my $datahandle = shift;
193 10 my @datastruct = ();
194 10 my @rowsums = ();
195 10 my @sumcols = ();
196 10 my ($ncols, $nrows, $sumtotal);
197 10 my $totalN = 0;
198 10 $self->{'colsums'} = ();
199  
200 10 foreach my $row_ref (@{$datahandle}) {
201 17 ▶ if (!ref $row_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $row_ref) 0 17 17
202 0 _error(4);
203 }
204 17 my @row = @{$row_ref};
205 17 $ncols = scalar @row;
206 17 my $i = 0;
207 17 foreach my $cell (@row) {
208 46 _checkNumValidity($cell);
209 46 push(@datastruct, $cell);
210 46 $sumtotal += $cell;
211 46 $self->{'colsums'}->[$i] += $cell;
212 46 $i++;
213 46 $totalN++;
214 }
215 17 $nrows++;
216 17 push(@rowsums, _rowsum(\@row));
217 }
218  
219 # Data sanity checks; make sure data exist and are internally
220 # consistent.
221 10 ▶ if ($totalN == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($totalN == 0) 0 10 10
222 0 _error(5);
223 }
224  
225 10 ▶ if ($totalN != ($nrows * $ncols)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($totalN != $nrows * $ncols) 0 10 10
226 0 _error(6);
227 }
228
229 10 ▶ if (($ncols == 0) || ($nrows == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ncols == 0 or $nrows == 0) 0 10 10
Condition33%1/3
$ncols == 0 or $nrows == 0
l!l&&r!l&&!r
0010
MC/DC0%0/2
$ncols == 0 or $nrows == 0
$ncols == 0 $nrows == 0
Truth table33%1/3
A or B
A= $ncols == 0B= $nrows == 0
ABresult
1X1
011
000
230 0 _error(7);
231 }
232
233 10 my $tSum = 0;
234 10 foreach my $r (@rowsums) {
235 17 $tSum += $r;
236 }
237 10 ▶ if ($sumtotal != $tSum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sumtotal != $tSum) 0 10 10
238 0 _error(6);
239 }
240
241 10 $tSum = 0;
242 10 foreach my $r (@{$self->{'colsums'}}) {
243 32 $tSum += $r;
244 }
245 10 ▶ if ($sumtotal != $tSum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sumtotal != $tSum) 0 10 10
246 0 _error(6);
247 }
248  
249 10 $self->{'observed'} = \@datastruct;
250 10 $self->{'rowsums'} = \@rowsums;
251 10 $self->{'nrows'} = $nrows;
252 10 $self->{'ncols'} = $ncols;
253 10 $self->{'sumtotal'} = $sumtotal;
254  
255 # tabletype: 0 = 2-way, 1 = single column, 2 = single row
256 10 ▶ if (($ncols == 1) || ($nrows == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ncols == 1 or $nrows == 1) { } 6 4 10
Condition100%3/3
$ncols == 1 or $nrows == 1
l!l&&r!l&&!r
154
MC/DC100%2/2
$ncols == 1 or $nrows == 1
$ncols == 1 $nrows == 1
Truth table100%3/3
A or B
A= $ncols == 1B= $nrows == 1
ABresult
1X1
011
000
257 6 ▶ if ($ncols == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ncols == 1) 1 5 6
258 1 $self->{'df'} = $nrows - 1;
259 1 $self->{'tabletype'} = 1;
260 }
261 6 ▶ if ($nrows == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nrows == 1) 5 1 6
262 5 $self->{'df'} = $ncols - 1;
263 5 $self->{'tabletype'} = 2;
264 }
265 }
266 else {
267 4 $self->{'df'} = ($nrows - 1) * ($ncols - 1);
268 4 $self->{'tabletype'} = 0;
269 }
270 10 $self->{'expected'}= _expected(\@datastruct);
271 10 $self->{'intrinsic'} = 1;
272 10 $self->{'df_extern'} = 0;
273 }
274  
275 # Calculate and return William's correction, q. Calculation varies
276 # depending on table format (1-way vs. 2-way).
277 20 ▶sub _williamsC {
Subroutine _williamsC: called CC=5 SCAR=16.1
278 20 my $sumtotal = $self->{'sumtotal'};
279 20 my $rowsums = $self->{'rowsums'};
280 20 my $colsums = $self->{'colsums'};
281 20 ▶ if ($self->{'tabletype'} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'tabletype'} == 1) { } 2 18 20
elsif ($self->{'tabletype'} == 2) { } 10 8 18
282 $self->{'q'} =
283 2 1 + ($self->{'nrows'} + 1) / (6 * $sumtotal);
284 }
285 elsif ($self->{'tabletype'} == 2) {
286 $self->{'q'} =
287 10 1 + ($self->{'ncols'} + 1) / (6 * $sumtotal);
288 }
289 else {
290 8 my $recipRows = 0;
291 8 my $recipCols = 0;
292 8 foreach my $rval (@{$rowsums}) {
293 16 $recipRows += (1 / $rval);
294 }
295 8 foreach my $cval (@{$colsums}) {
296 22 $recipCols += (1 / $cval);
297 }
298 8 my $denom = 6 * $sumtotal * $self->{'df'};
299 8 my $num = ($sumtotal * $recipRows - 1) * ($sumtotal * $recipCols - 1);
300 8 $self->{'q'} = 1 + $num / $denom;
301 }
302 }
303  
304 # Generate the default expected values, based on a hypothesis of
305 # independence (no association) between factors.
306 # Return ref to expected array.
307 10 ▶sub _expected {
Subroutine _expected: called CC=3 SCAR=11.0
308 10 my $data = shift;
309 10 my @expected = ();
310 10 my $rows = $self->{'nrows'};
311 10 my $cols = $self->{'ncols'};
312 10 my $sumtotal = $self->{'sumtotal'};
313 10 for my $row (0 .. $rows-1) {
314 17 my $marginalRow = $self->{'rowsums'}->[$row] / $sumtotal;
315 17 my @a;
316 17 for my $col (0 .. $cols-1) {
317 46 my $marginalCol = $self->{'colsums'}->[$col] / $sumtotal;
318 46 push(@expected, ($marginalRow * $marginalCol * $sumtotal));
319 }
320 }
321 10 \@expected;
322 }
323  
324 # Calculate the log-likelihood statistic.
325 # Return sum of Obs * ln(Obs/Exp) for all cells.
326 20 ▶sub _sumCellOp {
Subroutine _sumCellOp: called CC=2 SCAR=6.9
327 20 my $logsum = 0;
328 20 my $ncells = $self->{'nrows'} * $self->{'ncols'};
329 20 my $o = $self->{'observed'};
330 20 my $e = $self->{'expected'};
331 20 for my $cell (0 .. $ncells-1) {
332 92 $logsum += $o->[$cell] * log($o->[$cell] / $e->[$cell]);
333 }
334 20 $logsum;
335 }
336  
337 # Return sum of the cells in this row.
338 17 ▶sub _rowsum {
Subroutine _rowsum: called CC=2 SCAR=6.9
339 17 my $row = shift;
340 17 my $total = 0;
341 17 foreach my $val (@{$row}) {
342 46 $total += $val;
343 }
344 17 $total;
345 }
346  
347 # Order the data array back into the format originally input.
348 6 ▶sub _formatData {
Subroutine _formatData: called CC=3 SCAR=11.0
349 6 my $ref = shift;
350 6 my @data;
351 6 my $len = scalar @{$ref};
352 6 my $rlen = $len / $self->{'nrows'};
353 6 my $idx = $rlen - 1;
354 6 ▶ for (my $r=0; $r<$len; $r+=$rlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $len) 9 3 12
355 9 my $row = [ @{$ref}[$r ... $idx] ];
356 9 ▶ return $row if ($rlen eq $len);
Branch: 1 branch
BranchTrueFalseTotal
if $rlen eq $len 3 6 9
357 6 push(@data, $row);
358 6 $idx += $rlen;
359 }
360 3 \@data;
361 }
362  
363 # Check to make sure the values are numbers, greater than 0.
364 # Throw error if any problems are found.
365 58 ▶sub _checkNumValidity {
Subroutine _checkNumValidity: called CC=4 SCAR=16.6
366 58 my $value = shift;
367 58 my $eString = "Invalid data.";
368 58 ▶ if ($value !~ /^\d+\.?\d*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\d+\.?\d*$/) 0 58 58
369 0 _error(8);
370 }
371  
372 # Very low cell counts (< 5) aren't great, either. Maybe a
373 # warning is in order.
374 58 ▶ if ($value < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 5) 0 58 58
375 0 warn "Warning: Very small cell frequency (freq. < 5) found.\n",
376 "This will reduce the reliability of the test.\n";
377 }
378 58 ▶ if ($value == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == 0) 0 58 58
379 0 _error(9);
380 }
381 }
382  
383 # Take a filehandle, return an array reference.
384 6 ▶sub _fileread {
Subroutine _fileread: called CC=2 SCAR=6.9
385 6 my $data = shift;
386 6 my @a;
387 6 ▶ while (<$data>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $data)) 9 6 15
388 9 my @row = split(/\s+/,$_);
389 9 push(@a, \@row);
390 }
391 6 \@a;
392 }
393  
394 # Try to take whatever input format is thrown at us, and turn it into
395 # an array reference, or just bail out completely if we can't figure
396 # it out.
397 13 ▶sub _getHandle {
Subroutine _getHandle: called CC=7 SCAR=19.6
398 13 my $data = shift;
399 13 ▶ if (ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) { } 7 6 13
400 7 ▶ if ($data =~ /\AARRAY/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\AARRAY/) 3 4 7
401 3 ▶ if (ref $data->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->[0]) { } 2 1 3
402 2 return $data;
403 }
404 else {
405 1 return [ $data ];
406 }
407 }
408 4 ▶ if ($data =~ /\AGLOB/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\AGLOB/) 2 2 4
409 2 return _fileread($data);
410 }
411 2 ▶ if ($data =~ /\AIO::File=GLOB/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\AIO::File=GLOB/) { } 2 0 2
412 2 return _fileread($data);
413 }
414 else {
415 0 _error(10);
416 }
417 }
418 else {
419 6 my $fh = new IO::File;
420 6 ▶ if ($fh->open("< $data")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->open("< $data")) { } 2 4 6
421 2 return _fileread($fh);
422 }
423 else {
424 4 my @a = split(/\s+/,$data);
425 4 return [ \@a ];
426  
427 }
428 0 undef $fh;
429 }
430 0 0;
431 }
432  
433 # Keep all the error strings in one place.
434 0 ▶sub _error {
Subroutine _error: not called CC=1 SCAR=6.9
435 0 my ($code) = shift;
436 0 my $errorbar = "\n!-----------------!\n";
437 0 my @e = (
438 "", # Normal execution
439 "Input error: Must pass in a filename for a file containing the data.\n\tEx: \$g = new Statistics::Gtest(\$datafile);",
440 "Input error: Can't use array or hash as data; pass array ref.",
441 "Input error: Invalid data format.",
442 "Input error: Data must be structured as array of array refs, with each array ref
443 pointing to one row of data.",
444 "Data inconsistency: No data found in table.",
445 "Data inconsistency: sum of table values does not match total.",
446 "Data inconsistency: could not read row or column.",
447 "Data invalid: All data must be positive integers.",
448 "Data invalid: Found a zero frequency value. Can't continue.",
449 "Input error: Reference to invalid data type; must be reference to array.",
450 );
451  
452 0 print $errorbar;
453 0 print "\n Error",Carp::shortmess();
454 0 print " ", $e[$code], "\n";
455 0 print Carp::longmess(" Stack trace:\n");
456 0 print $errorbar;
457 0 exit $code;
458 }
459  
460 4 1;
461  
462 __END__
↑ Summary