blib/lib/Statistics/KruskalWallis.pm

filter: Statementstmt 96.7% 89 / 92 Branchbran 50.0% 6 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 4 totaltotal 86.1% 106 / 123 CC 14 SCAR 13.1
CC
14
Cov
90%
post_hoc
6.2
_rank_data
5.0
_group_data
4.0
CRAP 3.7
↑ Summary
1 package Statistics::KruskalWallis;
2  
3 1 $VERSION = '0.01';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Statistics::Distributions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 ##############
10 sub new
11 2 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
12 2 my $proto = shift;
13 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
14 2 my $self= {};
15  
16 2 $self->{sample_data} = undef;
17 2 $self->{rank_data} = undef;
18 2 $self->{no_of_sets} = 0;
19 2 $self->{no_of_samples} = 0;
20  
21  
22 2 bless($self,$class);
23 2 return $self;
24 }
25 ##############
26  
27 ##############
28 6 ▶sub load_data {
Subroutine load_data: called CC=1 SCAR=0.0
Pod undocumented
29 6 my $self = shift;
30 6 my $sample_name = shift;
31  
32 6 my (@sample_data)=@_;
33  
34 6 $self->{no_of_samples}+=@sample_data;;
35 6 $self->{no_of_sets} = $self->{no_of_sets} + 1;
36 6 $self->{sample_data}->{$sample_name}=\@sample_data;
37 6 $self->{rank_data}->{$sample_name}->{sum} = 0;
38 6 $self->{rank_data}->{$sample_name}->{n}=0;
39  
40 6 return 1;
41 } # end sub load_data
42 ##############
43  
44 ##############
45 1 ▶sub perform_kruskal_wallis_test {
Subroutine perform_kruskal_wallis_test: called CC=1 SCAR=0.0
Pod undocumented
46 1 my $self=shift;
47  
48 1 my ($sample_name,$sample_data_element,$sample_data_value);
49 1 my ($grouped_data_ref) = $self->_group_data();
50 1 $self->_rank_data($grouped_data_ref);
51 1 my ($H) = $self->_calculate_H();
52 1 my ($chi_prob)=Statistics::Distributions::chisqrprob (($self->{no_of_sets}-1),$H);
53  
54 # chi_prob only valid when no_of_sets > 3
55 1 return ($H,$chi_prob);
56 } # end sub
57 ##############
58  
59 ##############
60 1 ▶sub _group_data {
Subroutine _group_data: called CC=4 SCAR=13.9
61  
62 1 my $self = shift;
63 1 my (%grouped_data,$sample_name,$sample_data_element);
64  
65 1 foreach $sample_name (keys(%{$self->{sample_data}})) {
66 3 foreach $sample_data_element (@{$self->{sample_data}->{$sample_name}}){
67 21 ▶ if (exists($grouped_data{$sample_data_element}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $grouped_data{$sample_data_element}) { } 2 19 21
68 {
69 2 push @{$grouped_data{$sample_data_element}}, $sample_name;
70 } # end if
71 else
72 {
73 19 $grouped_data{$sample_data_element} = [$sample_name];
74 } # end else
75 } # end foreach sample_data_element
76 } # end foreach sample name
77  
78 1 return (\%grouped_data);
79 } # end sub _group_data
80 ##############
81  
82 ##############
83 1 ▶sub _rank_data {
Subroutine _rank_data: called CC=5 SCAR=16.1
84  
85 1 my $self = shift;
86 1 my $grouped_data_ref = shift;
87  
88 1 my $rank = 1;
89 1 my ($sample_name,$sample_data_value);
90  
91 1 foreach $sample_name (keys(%{$self->{sample_data}})) {
92 3 $self->{rank_data}->{$sample_name}->{sum} = 0;
93 3 $self->{rank_data}->{$sample_name}->{n} = 0;
94 } # end foreach
95  
96  
97 1 foreach $sample_data_value (sort { $a <=> $b } (keys(%$grouped_data_ref))) {
98  
99 19 ▶ if (@{$$grouped_data_ref{$sample_data_value}} > 1) {$rank+=0.5;}
Branch: 1 branch
BranchTrueFalseTotal
if (@{$grouped_data_ref->{$sample_data_value};} > 1) 2 17 19
100  
101 19 foreach $sample_name (@{$$grouped_data_ref{$sample_data_value}}) {
102 21 $self->{rank_data}->{$sample_name}->{sum}+= $rank;
103 21 $self->{rank_data}->{$sample_name}->{n}++;
104 } # end foreach
105
106 19 $rank=int($rank+1.5);
107 } # end foreach
108 } # end sub
109 ##############
110  
111 ##############
112 1 ▶sub _calculate_H {
Subroutine _calculate_H: called CC=2 SCAR=6.9
113  
114 1 my $self = shift;
115 # calculate mean sum
116  
117 1 my $sample_name;
118 1 my $mean_sq_sum = 0;
119  
120 1 foreach $sample_name (keys(%{$self->{sample_data}})) {
121 3 $mean_sq_sum += ($self->{rank_data}->{$sample_name}->{sum}**2) / $self->{rank_data}->{$sample_name}->{n};
122 } # end foreach samplename
123  
124 # calculate kw statistic
125  
126 1 my $H = 12 / ( $self->{no_of_samples} * ($self->{no_of_samples} + 1) );
127 1 $H = $H * $mean_sq_sum;
128 1 $H = $H - 3 * ($self->{no_of_samples} + 1);
129  
130 1 return ($H);
131 } # end sub _calculate_H
132 ##############
133  
134 ##############
135 1 ▶sub post_hoc {
Subroutine post_hoc: called CC=5 SCAR=18.2
Pod undocumented
136  
137 1 my $self = shift;
138 1 my $test_name = shift;
139 1 my ($control_group_name,$test_group_name)=@_;
140  
141 1 my ($p_value,$q);
142  
143 # one day may add further post-hoc tests
144 1 ▶ if ($test_name eq 'Newman-Keuls') {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_name eq "Newman-Keuls") 1 0 1
145 1 my $SE = ( $self->{no_of_samples} * ($self->{no_of_samples} + 1) ) / 12;
146 1 $SE = $SE * ( 1/$self->{rank_data}->{$control_group_name}->{n} + 1/$self->{rank_data}->{$test_group_name}->{n});
147 1 $SE = $SE**0.5;
148  
149 1 my $r1 = $self->{rank_data}->{$control_group_name}->{sum} / $self->{rank_data}->{$control_group_name}->{n};
150 1 my $r2 = $self->{rank_data}->{$test_group_name}->{sum} / $self->{rank_data}->{$test_group_name}->{n};
151  
152 1 $q = ( $r1 - $r2 ) / $SE;
153  
154 1 ▶ if ($q>2.576) {$p_value='>0.01';}
Branch: 3 branches
BranchTrueFalseTotal
if ($q > 2.576) { } 1 0 1
elsif ($q > 1.96) { } 0 0 0
elsif ($q > 1.645) { } 0 0 0
155 0 elsif ($q>1.960) {$p_value='>0.05';}
156 0 elsif ($q>1.645) {$p_value='>0.1';}
157 0 else {$p_value='<0.1';}
158  
159 } # end test
160  
161 1 return ($q,$p_value);
162 } # end sub post_hoc
163 ##############
164 1 1;
165  
166 __END__
↑ Summary