| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | $loess->model->degree($parametric) if defined $parametric; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | $loess->model->degree($drop_square) if defined $drop_square; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $loess->model->degree($normalize) if defined $normalize; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $loess->fit(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my $predict = $loess->predict( $xseq, $se ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $ci = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||