Chart/GGPlot/Util/Stats.pm

filter: Statementstmt 26.6% 20 / 75 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 71.4% 5 / 7 Podpod 100% 2 / 2 totaltotal 18.7% 27 / 144 CC 28 SCAR 53.8
CC
28
Cov
15%
loess
342.0
glm
111.1
BEGIN
1.0
CRAP 217.2
« Chart/GGPlot.pm ↑ Summary Chart/GGPlot/Guides.pm »
1 package Chart::GGPlot::Util::Stats;
2  
3 # ABSTRACT: Utility statistics functions
4  
5 7 ▶use Chart::GGPlot::Setup qw(:base :pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = '0.002003'; # VERSION
8  
9 7 ▶use parent qw(Exporter::Tiny);
Subroutine BEGIN: called CC=1 SCAR=0.0
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  
14 7 our @EXPORT_OK = qw(loess glm);
15  
16  
17 0 ▶fun loess (:$x, :$y, :$weights, :$xseq,
Subroutine loess: not called CC=18 SCAR=58.3
Pod documented
18 :$span=0.75,
19 :$se=false, :$level=0.95,
20 :$degree=undef, :$parametric=undef,
21 :$drop_square=undef, :$normalize=undef,
22 0 ▶ :$family=undef) {
Branch: 10 branches
BranchTrueFalseTotal
@_ < 8 and Function::Parameters::_croak('Too few arguments for fun loess (expected 8, got ' . @_ . ')') 0 0 0
@_ > 0 and @_ % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun loess") 0 0 0
exists ${__rest}{'x'} ? : 0 0 0
exists ${__rest}{'y'} ? : 0 0 0
exists ${__rest}{'weights'} ? : 0 0 0
exists ${__rest}{'xseq'} ? : 0 0 0
exists ${__rest}{'span'} ? : 0 0 0
exists ${__rest}{'se'} ? : 0 0 0
exists ${__rest}{'level'} ? : 0 0 0
keys %{__rest} and Function::Parameters::_croak('In fun loess: no such named parameter: ' . join(", ", sort(keys %{__rest}))) 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
23 0 load Math::LOESS;
24  
25 0 my $loess = Math::LOESS->new(
26 x => $x,
27 y => $y,
28 weights => $weights,
29 span => $span,
30 maybe family => $family,
31 );
32 0 ▶ $loess->model->degree($degree) if defined $degree;
Branch: 1 branch
BranchTrueFalseTotal
if defined $degree 0 0 0
33 0 ▶ $loess->model->degree($parametric) if defined $parametric;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parametric 0 0 0
34 0 ▶ $loess->model->degree($drop_square) if defined $drop_square;
Branch: 1 branch
BranchTrueFalseTotal
if defined $drop_square 0 0 0
35 0 ▶ $loess->model->degree($normalize) if defined $normalize;
Branch: 1 branch
BranchTrueFalseTotal
if defined $normalize 0 0 0
36  
37 0 $loess->fit();
38 0 my $predict = $loess->predict( $xseq, $se );
39  
40 0 ▶ my $ci =
Branch: 1 branch
BranchTrueFalseTotal
$se ? : 0 0 0
41 $se ? $predict->confidence( 1 - $level ) : { fit => $predict->values };
42 0 my @columns = ( x => $xseq, y => $ci->{fit} );
43 0 ▶ if ($se) {
Branch: 1 branch
BranchTrueFalseTotal
if ($se) 0 0 0
44 0 push @columns, ( ymin => $ci->{lower}, ymax => $ci->{upper} );
45 }
46 0 return Data::Frame->new( columns => \@columns );
47 7 }
48  
49  
50 0 ▶fun glm (:$x, :$y, :$xseq,
Subroutine glm: not called CC=11 SCAR=47.1
Pod documented
51 :$se=false, :$level=0.95,
52 0 ▶ :$family='gaussian', %rest) {
Branch: 8 branches
BranchTrueFalseTotal
@_ < 6 and Function::Parameters::_croak('Too few arguments for fun glm (expected 6, got ' . @_ . ')') 0 0 0
@_ > 0 and @_ % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun glm") 0 0 0
exists $rest{'x'} ? : 0 0 0
exists $rest{'y'} ? : 0 0 0
exists $rest{'xseq'} ? : 0 0 0
exists $rest{'se'} ? : 0 0 0
exists $rest{'level'} ? : 0 0 0
exists $rest{'family'} ? : 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
53 0 load PDL::Stats::GLM;
54 0 load PDL::GSL::CDF;
55  
56 0 my $n = $x->length;
57 0 my %m = PDL::Stats::GLM::ols( $y, $x, { plot => 0 } );
58  
59 # fit_t = Xb = pdl($x, [1...])->t x $m{b}->t = $m{b} x pdl($x, [1...])
60 0 my $fit = ( $m{b} x pdl( $xseq, [ (1) x $xseq->length ] ) )->flat;
61 0 my @columns = ( x => $xseq, y => $fit );
62  
63 0 ▶ if ($se) {
Branch: 1 branch
BranchTrueFalseTotal
if ($se) 0 0 0
64 0 my $res = $y - $m{y_pred};
65 0 my $mse = ( $res**2 )->sum / ( $y->length - 2 );
66 0 my $residual_scale = sqrt($mse);
67  
68 0 my $se_fit =
69 $residual_scale *
70 sqrt( 1 / $n +
71 $n *
72 ( $xseq - $x->average )**2 /
73 ( $n * ( $x**2 )->sum - ( $x->sum )**2 ) );
74  
75 0 my $cdf_func = "PDL::GSL::CDF::gsl_cdf_${family}_Pinv";
76 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 0 my $t = $cdf_func->( 1 - ( 1 - $level ) / 2, 1 );
78  
79 0 push @columns,
80 ( ymin => $fit - $t * $se_fit, ymax => $fit + $t * $se_fit );
81 }
82  
83 0 return Data::Frame->new( columns => \@columns );
84 7 }
85  
86 7 1;
87  
88 __END__
« Chart/GGPlot.pm ↑ Summary Chart/GGPlot/Guides.pm »