Chart/GGPlot/Stat/Smooth.pm

filter: Statementstmt 38.2% 57 / 149 Branchbran 2.6% 2 / 76 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 66.6% 14 / 21 Podpod 0.0% 0 / 4 totaltotal 25.8% 73 / 282 CC 48 SCAR 48.4
CC
48
Cov
24%
compute_group
267.8
__ANON__
240.0
setup_params
30.0
CRAP 126.4
« Chart/GGPlot/Guides.pm ↑ Summary Chart/GGPlot/Backend/Plotly/Geom/Boxplot.pm »
1 package Chart::GGPlot::Stat::Smooth;
2  
3 # ABSTRACT: Statistic method that does smoothing
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 Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use PDL::Core ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use PDL::MatrixOps ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶use Chart::GGPlot::Layer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Chart::GGPlot::Util qw(seq_n);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Chart::GGPlot::Util::Pod qw(layer_func_pod);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Chart::GGPlot::Util::Stats;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 7 with qw(
22 Chart::GGPlot::Stat
23 );
24  
25 0 ▶classmethod required_aes () { [qw(x y)] }
Subroutine required_aes: not called CC=3 SCAR=22.6
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod required_aes (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod required_aes (expected 1, got ' . @_ . ')') 0 0 0
26  
27 0 ▶classmethod _parameters () {
Subroutine _parameters: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod _parameters (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod _parameters (expected 1, got ' . @_ . ')') 0 0 0
28 [
29 0 qw(
30 na_rm
31 method se n span fullrange level method_args
32 )
33 ]
34 7 }
35  
36 7 my $stat_smooth_pod = layer_func_pod(<<'EOT');
37  
38 stat_smooth(:$maping=undef, :$data=undef,
39 :$geom='smooth', :$position='identity',
40 :$method='auto', :$se=true,
41 :$n=80, :$span=0.75, :$fullrange=false, :$level=0.95,
42 :$method_args={},
43 :$na_rm=false, :$show_legend='auto', :$inherit_aes=true,
44 %rest)
45  
46 Arguments:
47
48 =over 4
49  
50 %TMPL_COMMON_ARGS%
51  
52 =item * method
53  
54 Smoothing method (function) to use.
55
56 The available methods are,
57  
58 =over 8
59
60 =item * 'auto'
61  
62 'loess' is used for less than 1000 observations, 'glm' otherwise
63  
64 =item * 'loess'
65  
66 Locally Weighted Regression
67  
68 Requires L<Math::LOESS>.
69  
70 Supported C<$method_args>, (see L<Math::LOESS::Model> for details)
71  
72 =over 12
73  
74 =item * $degree
75  
76 =item * $parametric
77
78 =item * $drop_square
79  
80 =item * $normalize
81  
82 =item * $family
83  
84 =back
85  
86 =item * 'glm' : Generalized Linear Model
87  
88 Requires L<PDL::Stats::GLM> and L<PDL::GSL::CDF>.
89  
90 At this moment we can do only simple linear modeling. Still to support
91 logistic and polynomial in future.
92  
93 Supported C<$method_args>,
94  
95 =over 12
96  
97 =item * $family
98  
99 =back
100  
101 =back
102  
103 =item * method_args
104  
105 Additional optional arguments passed on to the modeling function
106 defined by C<$method>.
107  
108 =item * se
109  
110 Display confidence interval around smooth? (true by default, see
111 C<$level> to control.)
112  
113 =item * fullrange
114  
115 Should the fit span the full range of the plot, or just the data?
116  
117 =item * level
118  
119 Level of confidence interval to use (0.95 by default). Effective when
120 C<$se> is true.
121  
122 =item * span
123  
124 Controls the amount of smoothing for the default loess smoother.
125 Larger number means more smoothing. It should be in the C<(0, 1)> range.
126  
127 =item * n
128
129 Number of points at which to evaluate smoother.
130  
131 =back
132  
133 EOT
134  
135 0 ▶my $stat_smooth_code = fun (
Subroutine __ANON__: not called CC=15 SCAR=54.8
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and @_ % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun (anon)") 0 0 0
Condition0%0/3
@_ > 0 and @_ % 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 0 and @_ % 2
@_ > 0 @_ % 2
Truth table0%0/3
A and B
A= @_ > 0B= @_ % 2
ABresult
0X0
100
111
136 :$maping=undef, :$data=undef,
137 :$geom='smooth', :$position='identity',
138 :$method='auto', :$se=true,
139 :$n=80, :$span=0.75, :$fullrange=false, :$level=0.95,
140 :$method_args={},
141 :$na_rm=false, :$show_legend='auto', :$inherit_aes=true,
142 %rest )
143 0 ▶{
Branch: 12 branches
BranchTrueFalseTotal
exists $rest{'geom'} ? : 0 0 0
exists $rest{'position'} ? : 0 0 0
exists $rest{'method'} ? : 0 0 0
exists $rest{'se'} ? : 0 0 0
exists $rest{'n'} ? : 0 0 0
exists $rest{'span'} ? : 0 0 0
exists $rest{'fullrange'} ? : 0 0 0
exists $rest{'level'} ? : 0 0 0
exists $rest{'method_args'} ? : 0 0 0
exists $rest{'na_rm'} ? : 0 0 0
exists $rest{'show_legend'} ? : 0 0 0
exists $rest{'inherit_aes'} ? : 0 0 0
144 0 return Chart::GGPlot::Layer->new(
145 data => $data,
146 mapping => $maping,
147 stat => 'smooth',
148 geom => $geom,
149 position => $position,
150 show_legend => $show_legend,
151 inherit_aes => $inherit_aes,
152 parmas => {
153 method => $method,
154 se => $se,
155 n => $n,
156 fullrange => $fullrange,
157 level => $level,
158 na_rm => $na_rm,
159 method_args => $method_args,
160 span => $span,
161 %rest
162 },
163 );
164 7 };
165  
166 7 ▶classmethod ggplot_functions() {
Subroutine ggplot_functions: called CC=3 SCAR=11.3
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod ggplot_functions (expected 1, got ' . @_ . ')') 0 7 7
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod ggplot_functions (expected 1, got ' . @_ . ')') 0 7 7
167 return [
168 {
169 7 name => 'stat_smooth',
170 code => $stat_smooth_code,
171 pod => $stat_smooth_pod,
172 },
173 ];
174 7 }
175  
176 0 ▶classmethod _predictdf($method) {
Subroutine _predictdf: not called CC=4 SCAR=26.1
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for classmethod _predictdf (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for classmethod _predictdf (expected 2, got ' . @_ . ')') 0 0 0
177 0 my $func = "Chart::GGPlot::Util::Stats::${method}";
178 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
179 0 ▶ unless (exists &{$func}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists &{$func;}) 0 0 0
180 0 die "Unsupported smooth method: $method";
181 }
182 0 return $func;
183 7 }
184  
185 0 ▶method setup_params($data, $params) {
Subroutine setup_params: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method setup_params (expected 3, got ' . @_ . ')') 0 0 0
@_ > 3 and Function::Parameters::_croak('Too many arguments for method setup_params (expected 3, got ' . @_ . ')') 0 0 0
186 0 my $method = $params->at('method');
187 0 ▶ if ($method eq 'auto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "auto") 0 0 0
188  
189 # Use loess for small datasets
190 # Based on size of the "largest" group to avoid bad memory
191 # behaviour of loess
192 0 my $data1 = $data->select_columns( [qw(group PANEL)] );
193 0 my $splitted = $data1->split( $data1->id );
194 my $max_group =
195 0 List::AllUtils::max( map { $_->nrow } ( values %$splitted ) );
196  
197 0 ▶ if ($max_group < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_group < 1000) 0 0 0
198 0 $method = 'loess';
199 0 $params->set('method', $method);
200 }
201  
202 0 $log->debugf("geom_smooth() using method = %s", $method);
203 }
204  
205 # check if method is supported or not
206 0 $self->_predictdf($method);
207 0 return $params;
208 7 }
209
210 0 ▶around compute_layer( $data, $params, $layout ) {
Subroutine :around: not called CC=5 SCAR=34.0
Branch: 2 branches
BranchTrueFalseTotal
@_ < 5 and Function::Parameters::_croak('Too few arguments for around compute_layer (expected 5, got ' . @_ . ')') 0 0 0
@_ > 5 and Function::Parameters::_croak('Too many arguments for around compute_layer (expected 5, got ' . @_ . ')') 0 0 0
211  
212 # remove all "*_raw" columns, to avoid backends like plotly from
213 # generating wrong hovertext.
214 0 my $data1 = $data->copy;
215 0 for my $colname ( $data1->names->flatten ) {
216 0 ▶ if ( $colname =~ /_raw$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colname =~ /_raw$/u) 0 0 0
217 0 $data1->delete($colname);
218 }
219 }
220 0 $self->$orig( $data1, $params, $layout );
221 7 }
222  
223 0 ▶method compute_group($data, $scales, $params) {
Subroutine compute_group: not called CC=17 SCAR=55.9
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 4 and Function::Parameters::_croak('Too few arguments for method compute_group (expected 4, got ' . @_ . ')') 0 0 0
@_ > 4 and Function::Parameters::_croak('Too many arguments for method compute_group (expected 4, got ' . @_ . ')') 0 0 0
224 0 my $method = $params->at('method');
225 0 ▶ my $se = $params->at('se') // true;
Condition0%0/3
$params->at("se") // &true()
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->at("se") // &true()
$params->at("se") &true()
Truth table0%0/3
A // B
A= $params->at("se")B= &true()
ABresult
1X1
011
000
226 0 ▶ my $n = $params->at('n') // 80;
Condition0%0/2
$params->at("n") // 80
l!l
00
MC/DC0%0/1
$params->at("n") // 80
$params->at("n")
Truth table0%0/2
A
A= $params->at("n")
Aresult
11
00
227 0 ▶ my $span = $params->at('span') // 0.75;
Condition0%0/2
$params->at("span") // "0.75"
l!l
00
MC/DC0%0/1
$params->at("span") // "0.75"
$params->at("span")
Truth table0%0/2
A
A= $params->at("span")
Aresult
11
00
228 0 ▶ my $fullrange = $params->at('fullrange') // false;
Condition0%0/3
$params->at("fullrange") // &false()
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->at("fullrange") // &false()
$params->at("fullrange") &false()
Truth table0%0/3
A // B
A= $params->at("fullrange")B= &false()
ABresult
1X1
011
000
229 0 my $xseq = $params->at('xseq');
230 0 ▶ my $level = $params->at('level') // 0.95;
Condition0%0/2
$params->at("level") // "0.95"
l!l
00
MC/DC0%0/1
$params->at("level") // "0.95"
$params->at("level")
Truth table0%0/2
A
A= $params->at("level")
Aresult
11
00
231 0 ▶ my $method_args = $params->at('method_args') // {};
Condition0%0/2
$params->at("method_args") // {}
l!l
00
MC/DC0%0/1
$params->at("method_args") // {}
$params->at("method_args")
Truth table0%0/2
A
A= $params->at("method_args")
Aresult
11
00
232 0 ▶ my $na_rm = $params->at('na_rm') // false;
Condition0%0/3
$params->at("na_rm") // &false()
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->at("na_rm") // &false()
$params->at("na_rm") &false()
Truth table0%0/3
A // B
A= $params->at("na_rm")B= &false()
ABresult
1X1
011
000
233  
234 0 ▶ if ($data->at('x')->uniq->length < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->at("x")->uniq->length < 2) 0 0 0
235 # Not enough data to perform fit
236 0 return Data::Frame->new();
237 }
238  
239 0 ▶ unless (defined $xseq) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $xseq) 0 0 0
240 0 my $x = $data->at('x');
241 0 ▶ if ($x->type < PDL::float) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->type < PDL::float()) { } 0 0 0
242 0 ▶ $xseq =
Branch: 1 branch
BranchTrueFalseTotal
$fullrange ? : 0 0 0
243 $fullrange
244 ? $scales->at('x')->dimension
245 : $x->uniq->qsort;
246 }
247 else {
248 0 ▶ my $range =
Branch: 1 branch
BranchTrueFalseTotal
$fullrange ? : 0 0 0
249 $fullrange
250 ? $scales->at('x')->dimension
251 : pdl($x->minmax);
252 0 $xseq = seq_n($range->at(0), $range->at(1), $n);
253 }
254 }
255  
256 0 my $predictdf = $self->_predictdf($method);
257  
258 0 ▶ my %base_args = (
Branch: 1 branch
BranchTrueFalseTotal
$data->exists('weight') ? : 0 0 0
259 x => $data->at('x'),
260 y => $data->at('y'),
261 weights => (
262 $data->exists('weight')
263 ? $data->at('weight')
264 : PDL::Core::ones( $data->nrow )
265 ),
266 xseq => $xseq,
267 se => $se,
268 level => $level,
269 );
270 0 ▶ if ($method eq 'loess') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "loess") 0 0 0
271 0 $method_args->{span} = $span;
272 }
273  
274 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
275 0 return $predictdf->( %base_args, %$method_args );
276 7 }
277  
278 7 __PACKAGE__->meta->make_immutable;
279  
280 7 1;
281  
282 __END__
« Chart/GGPlot/Guides.pm ↑ Summary Chart/GGPlot/Backend/Plotly/Geom/Boxplot.pm »