File Coverage

blib/lib/Statistics/Distributions/Bartlett.pm
Criterion Covered Total %
statement 18 60 30.0
branch 0 4 0.0
condition n/a
subroutine 6 8 75.0
pod 0 2 0.0
total 24 74 32.4


line stmt bran cond sub pod time code
1             package Statistics::Distributions::Bartlett;
2              
3 1     1   54130 use warnings;
  1         3  
  1         33  
4 1     1   6 use strict;
  1         1  
  1         35  
5 1     1   5 use Carp;
  1         8  
  1         98  
6 1     1   882 use Statistics::Distributions qw/chisqrprob/;
  1         3806  
  1         94  
7 1     1   11 use List::Util qw/sum/;
  1         3  
  1         170  
8              
9             require Exporter;
10             our @ISA = qw(Exporter);
11             our @EXPORT = qw(bartlett);
12              
13             =head1 NAME
14              
15             Statistics::Distributions::Bartlett - Bartlett's test for equal sample variances.
16              
17             =cut
18              
19             =head1 VERSION
20              
21             This document describes Statistics::Distributions::Bartlett version 0.0.2
22              
23             =cut
24              
25             =head1 SYNOPSIS
26              
27             use Statistics::Distributions::Bartlett;
28            
29             # Create data to pass as ARRAY/ARRAY references
30             my @a = [qw/4534543 543 453 5 543 534 5 4 543 543 6 534 54 3 534 534 54 5/];
31             my @b = [qw/ 546 565 65 64 5 434 65 457 67 78 6 34 2 6 57 43 2 556 7 4563/];
32             my @c = [qw/ 565 44 535 6678 787 5 5/];
33            
34             # Call exported sub routine on ARRAY references of data to print result to STDOUT.
35             &bartlett(\@a,\@b,\@c);
36              
37             # Call in LIST-context to access results directly:
38             my ($K, $p_val, $df, $k, $n_total ) = &bartlett(\@a,\@b,\@c);
39              
40             =cut
41              
42             =head1 DESCRIPTION
43              
44             Bartlett test is used to test if k samples have equal variances. Such homogeneity is often assumed by other statistical
45             tests and consequently the Bartlett test should be used to verify that assumption. See http://www.itl.nist.gov/div898/handbook/eda/section3/eda357.htm.
46              
47             =cut
48              
49 1     1   1006 use version; our $VERSION = qv('0.0.2');
  1         2494  
  1         7  
50              
51             sub bartlett {
52 0     0 0   my @groups = @_;
53 0           my $k = scalar @groups;
54 0 0         croak qq{\nThere must be more than one group} if ($k < 2);
55 0           my $vars = &var(\@groups);
56 0           my $n_total = sum map { scalar @{$_} } @groups;
  0            
  0            
57             #my $SS_p = sum map { print qq{\nss $_->[2] and n $_->[0] and }, ($_->[2]-1) * $_->[0] ;($_->[2]-1) * $_->[0] } @{$SSs};
58 0           my $var_p = sum map { ($_->[1]-1) * $_->[0] } @{$vars};
  0            
  0            
59 0           $var_p /= ($n_total-$k);
60 0           $var_p = log($var_p);
61             #my $SS_sum = sum map { print qq{\nss $_->[2] and n $_->[0] and }, log($_->[0]);($_->[2]-1) * log($_->[0]) } @{$SSs};
62 0           my $var_sum = sum map { ($_->[1]-1) * log($_->[0]) } @{$vars};
  0            
  0            
63 0           my $n_under = sum map { 1 / ($_->[1] - 1) } @{$vars};
  0            
  0            
64 0           my $bar_k = ( ( ($n_total-$k) * $var_p ) - $var_sum ) / (1 + ( 1 / ( 3 * ($k-1))) * ( $n_under - ( 1 / ($n_total-$k)) ) ) ;
65 0           my $df = $k -1;
66 0           my $pval = &chisqrprob($df,$bar_k);
67 0 0         if ( !wantarray ) { print qq{\nK = $bar_k\np_val = $pval\ndf = $df\nk = $k\ntotal n = $n_total}; return; }
  0            
  0            
68 0           else { return ($bar_k, $pval, $df, $k, $n_total) }
69 0           return;
70             }
71              
72             sub var {
73 0     0 0   my $groups = shift;
74 0           my $result = [];
75 0           for my $a_ref (@{$groups}) {
  0            
76             # $stat->count()
77 0           my $n = scalar(@{$a_ref});
  0            
78             # $stat->sum();
79 0           my $sum = sum @{$a_ref};
  0            
80             # $stat->mean()
81 0           my $mean = ( $sum / $n ) ;
82 0           my $var = sum map { ($_-$mean)**2 } @{$a_ref};
  0            
  0            
83 0           $var /= ($n-1);
84 0           push @{$result}, [$var, $n];
  0            
85             }
86 0           return $result;
87             }
88              
89             1; # Magic true value required at end of module
90              
91             __END__