| 1 | package Chart::GGPlot::Stat::Boxplot; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Statistic method that gets the statistics data for boxplot | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Chart::GGPlot::Class qw(:pdl); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use MooseX::Singleton; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | our $VERSION = '0.002003'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Data::Frame; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use List::AllUtils qw(pairwise); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use PDL::Primitive qw(which); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use POSIX qw(floor); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use Chart::GGPlot::Aes::Functions qw(aes); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | ▶ | use Chart::GGPlot::Layer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | ▶ | use Chart::GGPlot::Util qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | is_discrete range_ has_groups resolution remove_missing | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | ▶ | use Chart::GGPlot::Util::Pod qw(layer_func_pod); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 7 | with qw( | |||||||||||||||||||||||||||||||||||||||||
| 24 | Chart::GGPlot::Stat | ||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | ▶ | classmethod required_aes () { ['y'] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
required_aes: called CC=3 SCAR=11.2
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | classmethod non_missing_aes() { ['weight'] } | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | classmethod _parameters () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parameters: called CC=3 SCAR=11.3
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | qw( | |||||||||||||||||||||||||||||||||||||||||
| 33 | na_rm | ||||||||||||||||||||||||||||||||||||||||||
| 34 | bins binwidth boundary breaks center pad | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 36 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | } | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | my $stat_boxplot_pod = layer_func_pod(<<'EOT'); | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | stat_boxplot(:$mapping=undef, :$data=undef, | ||||||||||||||||||||||||||||||||||||||||||
| 42 | :$geom='boxplot', :$position='dodge2', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | :$coef=1.5, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | :$na_rm=false, :$show_legend='auto', :$inherit_aes=true, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | %rest) | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | %TMPL_COMMON_ARGS% | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =item * $coef | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | Length of the whiskers as multiple of IQR. Defaults to 1.5. | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $stat_boxplot_code = fun ( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
Branch: 1 branch
Condition0%0/3
@_ > 0 and @_ % 2
MC/DC0%0/2
@_ > 0 and @_ % 2
@_ > 0
@_ % 2
Truth table0%0/3
A and B
A= @_ > 0B= @_ % 2
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | :$mapping=undef, :$data=undef, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | :$geom='boxplot', :$position='dodge2', | ||||||||||||||||||||||||||||||||||||||||||
| 64 | :$coef=1.5, :$na_rm=false, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | :$show_legend='auto', :$inherit_aes=true, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | %rest ) | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return Chart::GGPlot::Layer->new( | |||||||||||||||||||||||||||||||||||||||||
| 69 | data => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | mapping => $mapping, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | stat => 'boxplot', | ||||||||||||||||||||||||||||||||||||||||||
| 72 | geom => $geom, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | position => $position, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | show_legend => $show_legend, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | inherit_aes => $inherit_aes, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | params => { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | na_rm => $na_rm, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | coef => $coef, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | %rest, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 7 | ▶ | classmethod ggplot_functions () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ggplot_functions: called CC=3 SCAR=11.3
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 7 | name => 'stat_boxplot', | |||||||||||||||||||||||||||||||||||||||||
| 88 | code => $stat_boxplot_code, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | pod => $stat_boxplot_pod, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 7 | } | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | method setup_data($data, $params) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setup_data: called CC=4 SCAR=14.8
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | unless ($data->exists('x')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $data->set('x', pdl(0)); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | return remove_missing( | |||||||||||||||||||||||||||||||||||||||||
| 99 | $data, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | na_rm => false, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | vars => ['x'], | ||||||||||||||||||||||||||||||||||||||||||
| 102 | name => 'stat_boxplot' | ||||||||||||||||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 7 | } | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | method setup_params ($data, $params) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setup_params: called CC=9 SCAR=25.6
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | unless ( $params->exists('width') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | $params->set( 'width', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | resolution( $data->exists('x') ? $data->at('x') : 0 ) * 0.75 ); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | if ( $data->exists('x') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | my $x = $data->at('x'); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | if ( !is_discrete($x) and !has_groups($data) and $x->uniq->length > 1 ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not is_discrete($x) || has_groups($data) and $x->uniq->length > 1
is_discrete($x) || has_groups($data)
MC/DC0%0/3
not is_discrete($x) || has_groups($data) and $x->uniq->length > 1
is_discrete($x)
has_groups($data)
$x->uniq->length > 1
Truth table25%1/4
not(A || B) and C
A= is_discrete($x)B= has_groups($data)C= $x->uniq->length > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | warn("Continuous x aesthetic -- did you forget aes(group=>...)?"); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | return $params; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 7 | } | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 7 | ▶ | method compute_group ($data, $scales, $params) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compute_group: called CC=10 SCAR=24.1
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 7 | my $width = $params->at('width'); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 7 | ▶ | my $na_rm = $params->at('na_rm') // false; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$params->at("na_rm") // &false()
MC/DC0%0/2
$params->at("na_rm") // &false()
$params->at("na_rm")
&false()
Truth table33%1/3
A // B
A= $params->at("na_rm")B= &false()
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 7 | ▶ | my $coef = $params->at('coef') // 1.5; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->at("coef") // "1.5"
MC/DC0%0/1
$params->at("coef") // "1.5"
$params->at("coef")
Truth table50%1/2
A
A= $params->at("coef")
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 7 | my @qs = ( 0, 0.25, 0.5, 0.75, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 7 | my @stats; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 7 | ▶ | if ( $data->exists('weight') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ...; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 7 | @stats = map { $data->at('y')->pct($_) } @qs; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 7 | my $iqr = $stats[3] - $stats[1]; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 7 | my $y = $data->at('y'); | |||||||||||||||||||||||||||||||||||||||||
| 139 | 7 | my $outliers = | |||||||||||||||||||||||||||||||||||||||||
| 140 | ( ( $y < $stats[1] - $coef * $iqr ) | ( $y > $stats[3] + $coef * $iqr ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 7 | ▶ | if ( $outliers->any ) { # ajust min and max for outliers | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 5 | ( $stats[0], $stats[4] ) = $y->where( !$outliers )->minmax; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 7 | ▶ | if ( $data->at('x')->uniq->length > 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $width = range_( $data->at('x') )->diff->at(0) * 0.9; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 7 | my @names = qw(ymin lower middle upper ymax); | |||||||||||||||||||||||||||||||||||||||||
| 150 | my $df = Data::Frame->new( | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 7 | ▶ | columns => [ pairwise { $a => pdl( [$b] ) } @names, @stats ] ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 7 | $df->set( 'outliers', PDL::SV->new( [ $y->where($outliers) ] ) ); | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 7 | my $n; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 7 | ▶ | if ( $data->exists('weight') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | # Sum up weights for non-NA positions of y and weight | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my $weight = $data->at('weight'); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $n = int( $weight->where( $y->isgood & $weight->isgood )->sum ); | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 7 | $n = int( $y->isgood->sum ); | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 7 | my $middle = $df->at('middle'); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 7 | my $sqrt_n = sqrt($n); | |||||||||||||||||||||||||||||||||||||||||
| 167 | 7 | $df->set( 'notchupper', $middle + 1.58 * $iqr / $sqrt_n ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 7 | $df->set( 'notchlower', $middle - 1.58 * $iqr / $sqrt_n ); | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 7 | my $x = $data->at('x'); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 7 | ▶ | $df->set( 'x', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | pdl( [ $x->$_DOES('PDL::Factor') ? $x->at(0) : range_($x)->average ] ) | ||||||||||||||||||||||||||||||||||||||||||
| 173 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 7 | $df->set( 'width', pdl( [$width] ) ); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 7 | $df->set( 'relvarwidth', pdl( [$sqrt_n] ) ); | |||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 7 | return $df; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 7 | } | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||||||||||||||||