blib/lib/Statistics/Zed.pm

filter: Statementstmt 91.9% 149 / 162 Branchbran 68.1% 60 / 88 Conditioncond 57.1% 12 / 21 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 95.8% 23 / 24 Podpod 100% 11 / 11 totaltotal 81.1% 259 / 319 CC 56 SCAR 16.8
CC
56
Cov
82%
p2z
8.2
tails
7.5
p_value
7.0
CRAP 5.4
↑ Summary
1 package Statistics::Zed;
2 8 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use base qw(Statistics::Data);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Math::Cephes qw(:dists);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Statistics::Lite qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use String::Util qw(hascontent nocontent);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 $Statistics::Zed::VERSION = '0.10';
12
13 =head1 NAME
14
15 Statistics::Zed - Data-handling and calculations for ratio of observed to standard deviation (zscore)
16
17 =head1 VERSION
18
19 Version 0.10
20
21 =head1 SYNOPSIS
22
23 use Statistics::Zed 0.10;
24
25 # new() with optional args:
26 $zed = Statistics::Zed->new(
27 ccorr => 1,
28 tails => 2,
29 precision_s => 3,
30 precision_p => 7,
31 );
32
33 # optionally pre-load one or more values with these names:
34 $zed->load(observed => [5, 6, 3], expected => [2.5, 3, 3], variance => [8, 8, 9]);
35 $zed->add(observed => [3, 6], expected => [2.7, 2.5], variance => [7, 8]); # update loaded arrays
36 $z_value = $zed->score(); # calc z_value from pre-loaded data
37
38 # alternatively, call zscore() - alias score() - with the required args (with arefs or single values):
39 $z_value = $zed->zscore(
40 observed => 5,
41 expected => 2.5,
42 variance => 8,
43 );
44
45 # as either of above, but call in array context for more results:
46 ($z_value, $p_value, $observed_deviation, $standard_deviation) = $zed->zscore();
47
48 # as either of above but with optional args:
49 $z_value = $zed->zscore(ccorr => 1, precision_s => 3);
50
51 # get the normal distribution p_value only - alias z2p():
52 $p_value = $zed->p_value(); # using pre-loaded data
53 $p_value = $zed->p_value(observed => 5, expected => 2.5, variance => 8); # from given data
54 $p_value = $zed->p_value(tails => 2, ccorr => 1, precision_p => 5); # same as either with optional args
55
56 # "inverse phi" (wraps to Math::Cephes::ndtri):
57 $z_value = $zed->p2z(value => $p_value, tails => 1|2);
58
59 =head1 DESCRIPTION
60
61 Methods are provided to:
62
63 + L<calculate a z-score|Statistics::Zed/zscore>: ratio of an observed deviation to a standard deviation, with optional continuity correction
64
65 + L<convert z-value to normal p-value|Statistics::Zed/p_value>, and L<convert p-value to normal-equiv z-value|Statistics::Zed/p2z>
66
67 + L<load|Statistics::Zed/load>, L<add|Statistics::Zed/add>, save & retrieve observed, expected and variance values to compute z_score across samples
68
69 + support z-testing in L<Statistics::Sequences|Statistics::Sequences> and other modules.
70
71 Optionally, load/add B<observed>, B<expected> and B<variance> values (named as such) and compute a z-score between/after updates. The module uses L<Statistics::Data|Statistics::Data> to cache each observed, expected and variance values, and to provide for the load/add methods, as well as to save/retrieve these values between class calls (not documented here, see L<Statistics::Data|Statistics::Data>). Alternatively, simply call L<zscore|Statistics::Zed/zscore> and L<pvalue|Statistics::Zed/pvalue>, passing them the values by these labels in a hash (or hashref), with either single numerical values or referenced arrays of the same. Optionally, specify tails, where relevant, and precision the returned z-values and p-values as required.
72
73 =head1 SUBROUTINES/METHODS
74
75 =head2 new
76
77 $zed = Statistics::Zed->new();
78 $zed = Statistics::Zed->new(ccorr => NUM, tails => 1|2, precision_s => INT, precision_p => INT);
79
80 Returns a Statistics::Zed object. Accepts setting of any of the L<OPTIONS|Statistics::Zed/OPTIONS>.
81
82 =cut
83
84 8 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
85 8 my ( $class, @args ) = @_;
86 8 ▶ my $args = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 8 8
87 8 my $self = $class->SUPER::new();
88
89 # Set default values:
90 8 $self->{'tails'} = 2;
91 8 $self->{'ccorr'} = 0;
92
93 8 ▶ if ( scalar keys %{$args} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$args;}) 4 4 8
94 4 foreach ( keys %{$args} ) {
95 7 $self->{$_} = $args->{$_};
96 }
97 }
98 8 return $self;
99 }
100
101 =head2 load
102
103 $zed->load(observed => [NUMs], expected => [NUMs], variance => [NUMs]); # labelled list of each required series
104 $zed->load({ observed => [NUMs], expected => [NUMs], variance => [NUMs] }); # same but as referenced hash
105
106 Optionally load data for each of B<observed>, B<expected> and B<variance> series as arefs (reference to list of numbers), using C<load> in L<Statistics::Data|Statistics::Data/load>. Returns 1 if successful but croaks if data cannot be loaded; see L<DIAGNOSTICS|Statistics::Zed/DIAGNOSTICS>.
107
108 =cut
109
110 3 ▶sub load {
Subroutine load: called CC=5 SCAR=17.2
Pod documented
111 3 my ( $self, @args ) = @_;
112 3 $self->SUPER::load(@args);
113
114 # ensure there are named data for each of 'observed', 'expected' and 'variance':
115 3 my $data = 0;
116 3 foreach (qw/observed expected variance/) {
117 9 ▶ $data++ if $self->access( label => $_ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->access("label", $_) 9 0 9
118 }
119 croak
120 3 ▶'Data for deviation ratio are incomplete: Need arefs of data labelled \'observed\', \'expected\' and \'variance\''
Branch: 1 branch
BranchTrueFalseTotal
if $data > 0 and $data != 3 0 3 3
Condition33%1/3
$data > 0 and $data != 3
!ll&&!rl&&r
030
MC/DC0%0/2
$data > 0 and $data != 3
$data > 0 $data != 3
Truth table33%1/3
A and B
A= $data > 0B= $data != 3
ABresult
0X0
100
111
121 if $data > 0 and $data != 3;
122 3 return 1;
123 }
124
125 =head2 add
126
127 $zed->add(observed => [NUMs], expected => [NUMs], variance => [NUMs]); # labelled list of each required series
128 $zed->add({ observed => [NUMs], expected => [NUMs], variance => [NUMs] }); # same but as referenced hash
129
130 Update any existing, previously loaded data, via C<add> in L<Statistics::Data|Statistics::Data/add>. Returns 1 if successful but croaks if data cannot be added; see L<DIAGNOSTICS|Statistics::Zed/DIAGNOSTICS>.
131
132 =cut
133
134 7 ▶sub add {
Subroutine add: called CC=4 SCAR=14.2
Pod documented
135 7 my ( $self, @args ) = @_;
136 7 ▶ my $args = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 7 7
137 7 $self->SUPER::add($args);
138 7 foreach (qw/observed expected variance/) {
139 21 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $self->access("label", $_) 0 21 21
140 'Data for deviation ratio are incomplete: Need arefs of data labelled \'observed\', \'expected\' and \'variance\''
141 if !$self->access( label => $_ );
142 }
143 7 return 1;
144 }
145
146 =head2 zscore
147
148 $zval = $zed->zscore(); # assuming observed, expected and variance values already loaded/added, as above
149 $zval = $zed->zscore(observed => NUM, expected => NUM, variance => NUM);
150 $zval = $zed->zscore(observed => [NUMs], expected => [NUMs], variance => [NUMs]);
151 ($zval, $pval, $obs_dev, $stdev) = $zed->zscore(); # same but array context call for more info
152 $zscore = $zed->zscore(observed => [12], expected => [5], variance => [16], ccorr => 1); # same but with continuity correction
153
154 Returns the I<z>-value for the values of B<observed>, B<expected> and B<variance> sent to L<load|Statistics::Zed/load> and/or L<add|Statistics::Zed/add>, or as sent in a call to this method itself as a hash (or hashref). If called wanting an array, then the I<z>-value, its probability, the observed deviation and the standard deviation are returned.
155
156 I<Alias>: score, z_value
157
158 As described in L<OPTIONS|Statistics::Zed/OPTIONS>, optionally specify a numerical value for L<ccorr|Statistics::Zed/ccorr> for performing the continuity-correction to the observed deviation, and a value of either 1 or 2 to specify the L<tails|Statistics::Zed/tails> for reading off the normal distribution.
159
160 The basic formula is the basic:
161
162 =for html <p>&nbsp;&nbsp;&nbsp;<i>Z</i> = ( <i>&times;</i> &ndash; <i><o>X</o></i> ) / SD</p>
163
164 where I<X> is the expected value (mean, etc.). If supplying an array of values for each of the required arguments, then the z-score is based on summing their values, i.e., (sum of observeds less sum of expecteds) divided by square-root of the sum of the variances.
165
166 =cut
167
168 10 ▶sub zscore {
Subroutine zscore: called CC=6 SCAR=18.1
Pod documented
169 10 my ( $self, @args ) = @_;
170 10 ▶ my $args = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 4 6 10
171 10 my $desc = _get_descriptives( $self, $args );
172
173 # Calc z-value - leave undefined if no expected deviation:
174 10 my ( $z_value, $obs_dev, $exp_dev ) = ();
175 10 $obs_dev = $desc->{'observed'} - $desc->{'expected'};
176 10 $obs_dev = _ccorr( $self, $args, $obs_dev );
177 10 $exp_dev = sqrt $desc->{'variance'};
178 10 ▶ $z_value = $obs_dev / $exp_dev if $exp_dev;
Branch: 1 branch
BranchTrueFalseTotal
if $exp_dev 10 0 10
179
180 # Return array of z-value and p-value etc if wanted, precisioned as wanted, but empty-string if undefined:
181 10 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 8 2 10
182 8 ▶ if ( hascontent($z_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (hascontent($z_value)) { } 8 0 8
183 8 $args->{'value'} = $z_value;
184 8 my $p_value = _precision( $self, $args, 'p', $self->z2p($args) );
185 8 $z_value = _precision( $self, $args, 's', $z_value );
186 8 return ( $z_value, $p_value, $obs_dev, $exp_dev );
187 }
188 else {
189 0 return ( q{}, q{}, $obs_dev, $exp_dev );
190 }
191 }
192 else {
193 2 ▶ return hascontent($z_value)
Branch: 1 branch
BranchTrueFalseTotal
hascontent($z_value) ? : 2 0 2
194 ? _precision( $self, $args, 's', $z_value )
195 : q{};
196 }
197 }
198
199 # aliases:
200 8 *score = \&zscore;
201 8 *z_value = \&zscore;
202 8 *test = \&zscore; # legacy
203
204 =head2 p_value
205
206 $p_value = $zed->p_value($z); # assumes 2-tailed
207 $p_value = $zed->p_value(value => $z); # assumes 2-tailed
208 $p_value = $zed->p_value(value => $z, tails => 1);
209 $p_value = $zed->p_value(); # assuming observed, expected and variance values already loaded/added, as above
210 $p_value = $zed->p_value(observed => NUM, expected => NUM, variance => NUM);
211 $p_value = $zed->p_value(observed => [NUMs], expected => [NUMs], variance => [NUMs]);
212
213 I<Alias>: C<pvalue>, C<z2p>
214
215 Send a I<z>-value, get its associated I<p>-value, 2-tailed by default, or depending on the value of the optional argument B<tails>. If you pass in just one value (unkeyed), it is taken as the z-value. Alternatively, it can be passed the same arguments as for L<zscore|Statistics::Zed/zscore> so that it will calculate the zscore itself but return only the p-value.
216
217 Uses L<Math::Cephes|Math::Cephes> C<ndtr> normal probability function, which returns 0 if the z-value is greater than or equal to 38.
218
219 The optional argument B<precision_p> renders the returned p-value to so many decimal places (simply by sprintf).
220
221 =cut
222
223 17 ▶sub p_value {
Subroutine p_value: called CC=7 SCAR=19.5
Pod documented
224 17 my ( $self, @args ) = @_;
225 17 ▶ my $args =
Branch: 2 branches
BranchTrueFalseTotal
ref $args[0] ? : 8 9 17
scalar(@args) % 2 == 0 ? : 8 1 9
226 ref $args[0] ? $args[0]
227 : ( scalar(@args) % 2 == 0 ) ? {@args}
228 : { value => $args[0] };
229 17 my $z_value;
230 17 ▶ if ( hascontent( $args->{'value'} )
Branch: 1 branch
BranchTrueFalseTotal
if (hascontent($args->{'value'}) and &looks_like_number($args->{'value'})) { } 15 2 17
Condition66%2/3
hascontent($args->{"value"}) and &looks_like_number($args->{"value"})
!ll&&!rl&&r
2015
MC/DC50%1/2
hascontent($args->{"value"}) and &looks_like_number($args->{"value"})
hascontent($args->{"value"}) &looks_like_number($args->{"value"})
Truth table66%2/3
A and B
A= hascontent($args->{"value"})B= &looks_like_number($args->{"value"})
ABresult
0X0
100
111
231 and looks_like_number( $args->{'value'} ) )
232 {
233 15 $z_value = $args->{'value'};
234 }
235 else {
236 2 $z_value = $self->zscore($args);
237 }
238 17 ▶ return q{} if nocontent($z_value);
Branch: 1 branch
BranchTrueFalseTotal
if nocontent($z_value) 0 17 17
239 17 my $p_value = ndtr($z_value);
240 17 ▶ $p_value = 1 - $p_value if $p_value > .5;
Branch: 1 branch
BranchTrueFalseTotal
if $p_value > "0.5" 14 3 17
241 17 $p_value *= _set_tails( $self, $args );
242 17 return _precision( $self, $args, 'p', $p_value );
243 }
244 8 *pvalue = \&p_value;
245 8 *z2p = \&p_value;
246
247 =head2 p2z
248
249 $z_value = $zed->p2z($p_value) # the p-value is assumed to be 2-tailed
250 $z_value = $zed->p2z(value => $p_value) # the p-value is assumed to be 2-tailed
251 $z_value = $zed->p2z(value => $p_value, tails => 1) # specify 1-tailed probability
252
253 Returns the I<z>-value associated with a I<p>-value using the inverse phi function C<ndtri> in L<Math::Cephes|Math::Cephes>. I<The p-value is assumed to be two-tailed>, and so is firstly (before conversion) divided by 2, e.g., .05 becomes .025 so you get I<z> = 1.96. As a one-tailed probability, it is then assumed to be a probability of being I<greater> than a certain amount, i.e., of getting a I<z>-value I<greater> than or equal to that observed. So the inverse phi function is actually given (1 - I<p>-value) to work on. So .055 comes back as 1.598 (speaking of the top-end of the distribution), and .991 comes back as -2.349 (now going from right to left across the distribution). This is not the same as found in inversion methods in common spreadsheet packages but seems to be expected by humans.
254
255 =cut
256
257 7 ▶sub p2z {
Subroutine p2z: called CC=8 SCAR=21.0
Pod documented
258 7 my ( $self, @args ) = @_;
259 7 ▶ my $args =
Branch: 2 branches
BranchTrueFalseTotal
ref $args[0] ? : 0 7 7
scalar(@args) % 2 == 0 ? : 7 0 7
260 ref $args[0] ? $args[0]
261 : ( scalar(@args) % 2 == 0 ) ? {@args}
262 : { value => $args[0] };
263 7 my $p_value;
264 7 ▶ if ( hascontent( $args->{'value'} )
Branch: 1 branch
BranchTrueFalseTotal
if (hascontent($args->{'value'}) and $self->all_proportions([$args->{'value'}])) { } 4 2 6
Condition100%3/3
hascontent($args->{"value"}) and $self->all_proportions([$args->{"value"}])
!ll&&!rl&&r
124
MC/DC100%2/2
hascontent($args->{"value"}) and $self->all_proportions([$args->{"value"}])
hascontent($args->{"value"}) $self->all_proportions([$args->{"value"}])
Truth table100%3/3
A and B
A= hascontent($args->{"value"})B= $self->all_proportions([$args->{"value"}])
ABresult
0X0
100
111
265 and $self->all_proportions( [ $args->{'value'} ] ) )
266 {
267 4 $p_value = $args->{'value'};
268 }
269 else {
270 2 croak 'Cannot compute z-value from p-value: ' . $args->{'value'};
271 }
272
273 # Avoid ndtri errors by first accounting for 0 and 1 ...
274 4 my $z_value;
275 4 ▶ if ( $p_value == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p_value == 0) { } 0 4 4
elsif ($p_value == 1) { } 1 3 4
276 0 $z_value = undef;
277 }
278 elsif ( $p_value == 1 ) {
279 1 $z_value = 0;
280 }
281 else {
282 3 ▶ $p_value /= 2
Branch: 1 branch
BranchTrueFalseTotal
if _set_tails($self, $args) == 2 1 2 3
283 if _set_tails( $self, $args ) ==
284 2; # p-value has been given as two-tailed - only use 1 side
285 3 $z_value = ndtri( 1 - $p_value );
286 }
287 4 return $z_value;
288 }
289
290 =head2 obsdev
291
292 $obsdev = $zed->obsdev(); # assuming observed and expected values already loaded/added, as above
293 $obsdev = $zed->obsdev(observed => NUM, expected => NUM);
294 $obsdev = $zed->obsdev(observed => [NUMs], expected => [NUMs]);
295
296 Returns the observed deviation (only), as would be returned as the third value if calling L<zscore|Statistics::Zed/zscore> in array context. This is simply the (sum of) the observed value(s) less the (sum of) the expected value(s), with the (sum of) the latter given the continuity correction if this is (optionally) also given as an argument, named B<ccorr>; see L<OPTIONS|Statistics::Zed/OPTIONS>.
297
298 =cut
299
300 3 ▶sub obsdev {
Subroutine obsdev: called CC=2 SCAR=7.0
Pod documented
301 3 my ( $self, @args ) = @_;
302 3 ▶ my $args = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 3 3
303 3 my $desc = _get_descriptives( $self, $args );
304 return _ccorr( $self, $args,
305 3 ( $desc->{'observed'} - $desc->{'expected'} ) );
306 }
307
308 =head2 ccorr
309
310 $zed->ccorr(value => 1); # will be used in all methods, unless they are given a ccorr value to use
311 $val = $zed->ccorr(); # returns any value set in new() or previously here
312
313 Set the value of the optional B<ccorr> argument to be used for all statistics methods, or, without a B<value>, return the current value. This might be undef if it has not previously been explicitly set in L<new|Statistics::Zed/new> or via this method. To quash any set value, specify B<value> => 0. When sending a value for B<ccorr> to any other method, this value takes precedence over any previously set, but it does not "re-set" the cached value that is set here or in L<new|Statistics::Zed/new>. See L<OPTIONS|Statistics::Zed/OPTIONS> for how this value is used. It is assumed that the value sent is a valid numerical value.
314
315 =cut
316
317 3 ▶sub ccorr {
Subroutine ccorr: called CC=3 SCAR=11.0
Pod documented
318 3 my ( $self, @args ) = @_;
319 3 ▶ my $args = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 3 3
320 3 ▶ if ( defined $args->{'value'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'value'}) { } 1 2 3
321 1 $self->{'ccorr'} = $args->{'value'};
322 }
323 else {
324 2 return $self->{'ccorr'};
325 }
326 1 return;
327 }
328
329 =head2 tails
330
331 $zed->tails(value => 1); # will be used in all methods, unless they are given a tails value to use
332 $val = $zed->tails(); # returns any value set in new() or previously here
333
334 Set the value of the optional B<tails> argument to be used for all statistics methods, or, without a B<value>, return the current value. The default is 2; and this can be overriden by setting its value in L<new|Statistics::Zed/new>, by this method, or as an explicit argument in any method. When sending a value for B<tails> to any other method, this value takes precedence over any previously set, but it does not "re-set" the cached value that is set here or in L<new|Statistics::Zed/new>. See L<p_value|Statistics::Zed/p_value>, L<p2z|Statistics::Zed/p2z> and L<OPTIONS|Statistics::Zed/OPTIONS> for how this value is used. The value set must be either 1 or 2; a croak is heard otherwise.
335
336 =cut
337
338 3 ▶sub tails {
Subroutine tails: called CC=6 SCAR=20.2
Pod documented
339 3 my ( $self, @args ) = @_;
340 3 ▶ my $args = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 3 3
341 3 ▶ if ( defined $args->{'value'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'value'}) { } 1 2 3
342 1 ▶ if ( looks_like_number( $args->{'value'} )
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($args->{'value'}) and $args->{'value'} == 1 || $args->{'value'} == 2) { } 1 0 1
Condition33%2/6
&looks_like_number($args->{"value"}) and $args->{"value"} == 1 || $args->{"value"} == 2
!ll&&!rl&&r
001
$args->{"value"} == 1 or $args->{"value"} == 2
l!l&&r!l&&!r
100
MC/DC0%0/3
&looks_like_number($args->{"value"}) and $args->{"value"} == 1 || $args->{"value"} == 2
&looks_like_number($args->{"value"}) $args->{"value"} == 1 $args->{"value"} == 2
Truth table25%1/4
A and B or C
A= &looks_like_number($args->{"value"})B= $args->{"value"} == 1C= $args->{"value"} == 2
ABCresult
0XX0
1000
11X1
1011
343 and ( $args->{'value'} == 1 or $args->{'value'} == 2 ) )
344 {
345 1 $self->{'tails'} = $args->{'value'};
346 }
347 else {
348 0 croak
349 "Cannot set tails() option: value must be numeric and equal either 1 or 2, not '$args->{'value'}'";
350 }
351 }
352 else {
353 2 return $self->{'tails'};
354 }
355 1 return;
356 }
357
358 =head2 string
359
360 $str = $zed->string(); # assuming observed, expected and variance values already loaded/added, as above
361 $str = $zed->string(observed => NUM, expected => NUM, variance => NUM);
362 $str = $zed->string(observed => [NUMs], expected => [NUMs], variance => [NUMs]);
363
364 Returns a string giving the zscore and p-value. Takes the same arguments as for L<zscore|Statistics::Zed/zscore>, which it calls itself, taking its returned values to make up a string in the form B<Z = 0.141, 1p = 0.44377>. Accepts the optional arguments B<tails>, B<ccorr>, B<precsion_s> and B<precision_p>; see L<OPTIONS|Statistics::Zed/OPTIONS>. In the example, B<precision_s> has been specified as 3, B<precision_p> has been set to 5, and B<tails> has been set to 1.
365
366 =cut
367
368 2 ▶sub string {
Subroutine string: called CC=2 SCAR=7.0
Pod documented
369 2 my ( $self, @args ) = @_;
370 2 ▶ my $args = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 2 2
371 2 my ( $zval, $pval ) = zscore( $self, $args );
372 2 my $tails = _set_tails( $self, $args );
373 2 return "Z = $zval, " . $tails . "p = $pval";
374 }
375
376 =head2 dump
377
378 $zed->dump(); # assuming observed, expected and variance values already loaded/added, as above
379 $zed->dump(observed => NUM, expected => NUM, variance => NUM);
380 $zed->dump(observed => [NUMs], expected => [NUMs], variance => [NUMs]);
381
382 Prints to STDOUT a line giving the zscore and p-value, being what would be returned by L<string|Statistics::Zed/string> but with a new-line "\n" character appended.
383
384 =cut
385
386 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
387 0 my ( $self, @args ) = @_;
388 0 ▶ print string( $self, @args ), "\n"
Branch: 1 branch
BranchTrueFalseTotal
print string($self, @args), "\n" or croak("Could not print statistical values") 0 0 0
389 or croak 'Could not print statistical values';
390 0 return 1;
391 }
392
393 ## Private methods:
394
395 # Obtain required values by given args or, if not given, by pre-loaded data, or croak:
396 13 ▶sub _get_descriptives {
Subroutine _get_descriptives: called CC=6 SCAR=19.4
397 13 my ( $self, $args ) = @_;
398 13 my %desc = ();
399 13 for (qw/observed expected variance/) {
400 39 ▶ if ( nocontent( $args->{$_} )
Branch: 3 branches
BranchTrueFalseTotal
if (nocontent($args->{$_}) and my $data = $self->access('label', $_)) { } 30 9 39
elsif (ref $args->{$_}) { } 0 9 9
elsif (&looks_like_number($args->{$_})) { } 9 0 9
Condition66%2/3
nocontent($args->{$_}) and my $data = $self->access("label", $_)
!ll&&!rl&&r
9030
MC/DC50%1/2
nocontent($args->{$_}) and my $data = $self->access("label", $_)
nocontent($args->{$_}) my $data = $self->access("label", $_)
Truth table66%2/3
A and B
A= nocontent($args->{$_})B= my $data = $self->access("label", $_)
ABresult
0X0
100
111
401 and my $data = $self->access( label => $_ ) )
402 { # try pre-loaded data
403 30 $desc{$_} = sum( @{$data} );
404 }
405 elsif ( ref $args->{$_} ) { # assume isa aref
406 0 $desc{$_} = sum( @{$data} );
407 }
408 elsif ( looks_like_number( $args->{$_} ) ) { # assume single value
409 9 $desc{$_} = $args->{$_};
410 }
411 else {
412 0 croak
413 "Cannot compute z-value: No defined or numerical '$_' value(s)";
414 }
415 }
416 13 return \%desc;
417 }
418
419 sub _precision
420 35 ▶{ # any $args value takes precedence; try $self value; otherwise, return as is
Subroutine _precision: called CC=3 SCAR=11.2
421 35 my ( $self, $args, $type, $value ) = @_;
422 35 my $precision;
423 35 ▶ if ( $args->{ 'precision_' . $type } ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$args{'precision_' . $type}) { } 5 30 35
elsif ($$self{'precision_' . $type}) { } 0 30 30
424 5 $precision = $args->{ 'precision_' . $type };
425 }
426 elsif ( $self->{ 'precision_' . $type } ) {
427 0 $precision = $self->{ 'precision_' . $type };
428 }
429 else {
430 30 return $value;
431 }
432 5 return sprintf q{%.} . $precision . 'f',
433 $value; # tried Number::Format but overflows too easily
434 }
435
436 sub _set_tails
437 22 ▶{ # any $args value takes precedence; try $self value; otherwise, return as is
Subroutine _set_tails: called CC=6 SCAR=19.4
438 22 my ( $self, $args, $value ) = @_;
439 22 my $tails;
440 22 ▶ if ( $args->{'tails'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'tails'}) { } 13 9 22
elsif ($self->{'tails'}) { } 9 0 9
441 13 $tails = $args->{'tails'};
442 }
443 elsif ( $self->{'tails'} ) {
444 9 $tails = $self->{'tails'};
445 }
446 22 ▶ if ( !defined $tails )
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $tails) { } 0 22 22
elsif ($tails != 1 and $tails != 2) { } 0 27 27
447 { # what might have been in $self was clobbered by user, perhaps
448 0 $tails = 2;
449 }
450 ▶ elsif ( $tails != 1 && $tails != 2 ) {
Condition66%2/3
$tails != 1 and $tails != 2
!ll&&!rl&&r
5170
MC/DC0%0/2
$tails != 1 and $tails != 2
$tails != 1 $tails != 2
Truth table66%2/3
A and B
A= $tails != 1B= $tails != 2
ABresult
0X0
100
111
451 0 croak
452 "Cannot compute p-value: Argument \'tails\' should have value of either 1 or 2, not '$tails'";
453 }
454 22 return $tails;
455 }
456
457 # Apply continuity correction to deviation:
458 13 ▶sub _ccorr {
Subroutine _ccorr: called CC=6 SCAR=18.3
459 13 my ( $self, $args, $dev ) = @_;
460 13 ▶ if ($dev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dev) { } 13 0 13
461 13 my $d;
462 13 ▶ if ( defined $args->{'ccorr'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args->{'ccorr'}) { } 7 6 13
elsif (defined $self->{'ccorr'}) { } 6 0 6
463 7 $d = $args->{'ccorr'};
464 }
465 elsif ( defined $self->{'ccorr'} ) {
466 6 $d = $self->{'ccorr'};
467 }
468 13 ▶ if ( !$d ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $d) { } 6 7 13
469 6 return $dev;
470 }
471 else {
472 7 my $cdev = abs($dev) - .5 * $d;
473 7 ▶ $cdev *= -1 if $dev < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dev < 0 0 7 7
474 7 return $cdev;
475 }
476 }
477 else {
478 0 return $dev;
479 }
480 }
481
482 8 1;
483
484 __END__
↑ Summary