| 1 | package Statistics::LineFit; | ||||||||||||||||||||||||||||
| 2 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 14 | ▶ | use Carp qw(carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 14 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use Exporter (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 14 | ▶ | use vars qw ($AUTHOR $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 14 | $AUTHOR = 'Richard Anderson <cpan(AT)richardanderson(DOT)org>'; | |||||||||||||||||||||||||||
| 8 | 14 | @EXPORT = @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 9 | 14 | %EXPORT_TAGS = (); | |||||||||||||||||||||||||||
| 10 | 14 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 11 | 14 | $VERSION = 0.06; | |||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 15 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # Purpose: Create a new Statistics::LineFit object | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | 15 | my ($caller, $validate, $hush) = @_; | |||||||||||||||||||||||||||
| 19 | 15 | ▶ | my $self = { doneRegress => 0, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 20 | gotData => 0, | ||||||||||||||||||||||||||||
| 21 | hush => defined $hush ? $hush : 0, | ||||||||||||||||||||||||||||
| 22 | validate => defined $validate ? $validate : 0, | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | 15 | ▶ | bless $self, ref($caller) || $caller; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $caller || $caller
MC/DC0%0/2
ref $caller || $caller
ref $caller
$caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
| |||||||||||||||||||||||||||||
| 25 | 15 | return $self; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 17 | ▶ | sub coefficients { | ||||||||||||||||||||||||||
|
Subroutine
coefficients: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||
| 30 | # Purpose: Return the slope and intercept from least squares line fit | ||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||
| 32 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 17 | ▶ | unless (defined $self->{intercept} and defined $self->{slope}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"intercept"} and defined $self->{"slope"}
MC/DC50%1/2
defined $self->{"intercept"} and defined $self->{"slope"}
defined $self->{"intercept"}
defined $self->{"slope"}
Truth table66%2/3
A and B
A= defined $self->{"intercept"}B= defined $self->{"slope"}
| |||||||||||||||||||||||||||||
| 34 | 12 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 15 | return ($self->{intercept}, $self->{slope}); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 14 | ▶ | sub computeSums { | ||||||||||||||||||||||||||
|
Subroutine
computeSums: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||
| 41 | # Purpose: Compute sum of x, y, x**2, y**2 and x*y (private method) | ||||||||||||||||||||||||||||
| 42 | # | ||||||||||||||||||||||||||||
| 43 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 14 | my ($sumX, $sumY, $sumXX, $sumYY, $sumXY) = (0, 0, 0, 0, 0); | |||||||||||||||||||||||||||
| 45 | 14 | ▶ | if (defined $self->{weight}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 5 | ▶ | for (my $i = 0; $i < $self->{numXY}; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 213 | $sumX += $self->{weight}[$i] * $self->{x}[$i]; | |||||||||||||||||||||||||||
| 48 | 213 | $sumY += $self->{weight}[$i] * $self->{y}[$i]; | |||||||||||||||||||||||||||
| 49 | 213 | $sumXX += $self->{weight}[$i] * $self->{x}[$i] ** 2; | |||||||||||||||||||||||||||
| 50 | 213 | $sumYY += $self->{weight}[$i] * $self->{y}[$i] ** 2; | |||||||||||||||||||||||||||
| 51 | $sumXY += $self->{weight}[$i] * $self->{x}[$i] | ||||||||||||||||||||||||||||
| 52 | 213 | * $self->{y}[$i]; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | 9 | ▶ | for (my $i = 0; $i < $self->{numXY}; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 100030 | $sumX += $self->{x}[$i]; | |||||||||||||||||||||||||||
| 57 | 100030 | $sumY += $self->{y}[$i]; | |||||||||||||||||||||||||||
| 58 | 100030 | $sumXX += $self->{x}[$i] ** 2; | |||||||||||||||||||||||||||
| 59 | 100030 | $sumYY += $self->{y}[$i] ** 2; | |||||||||||||||||||||||||||
| 60 | 100030 | $sumXY += $self->{x}[$i] * $self->{y}[$i]; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 14 | return ($sumX, $sumY, $sumXX, $sumYY, $sumXY); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 16 | ▶ | sub durbinWatson { | ||||||||||||||||||||||||||
|
Subroutine
durbinWatson: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | # | ||||||||||||||||||||||||||||
| 68 | # Purpose: Return the Durbin-Watson statistic | ||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||
| 70 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 16 | ▶ | unless (defined $self->{durbinWatson}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 15 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 14 | my $sumErrDiff = 0; | |||||||||||||||||||||||||||
| 74 | my $errorTMinus1 = $self->{y}[0] - ($self->{intercept} + $self->{slope} | ||||||||||||||||||||||||||||
| 75 | 14 | * $self->{x}[0]); | |||||||||||||||||||||||||||
| 76 | 14 | ▶ | for (my $i = 1; $i < $self->{numXY}; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | my $error = $self->{y}[$i] - ($self->{intercept} + $self->{slope} | ||||||||||||||||||||||||||||
| 78 | 100229 | * $self->{x}[$i]); | |||||||||||||||||||||||||||
| 79 | 100229 | $sumErrDiff += ($error - $errorTMinus1) ** 2; | |||||||||||||||||||||||||||
| 80 | 100229 | $errorTMinus1 = $error; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 14 | ▶ | $self->{durbinWatson} = $self->sumSqErrors() > 0 ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | $sumErrDiff / $self->sumSqErrors() : 0; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 15 | return $self->{durbinWatson}; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 16 | ▶ | sub meanSqError { | ||||||||||||||||||||||||||
|
Subroutine
meanSqError: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||
| 90 | # Purpose: Return the mean squared error | ||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||
| 92 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 93 | 16 | ▶ | unless (defined $self->{meanSqError}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 15 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 14 | $self->{meanSqError} = $self->sumSqErrors() / $self->{numXY}; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 15 | return $self->{meanSqError}; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 28 | ▶ | sub predictedYs { | ||||||||||||||||||||||||||
|
Subroutine
predictedYs: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | # | ||||||||||||||||||||||||||||
| 102 | # Purpose: Return the predicted y values | ||||||||||||||||||||||||||||
| 103 | # | ||||||||||||||||||||||||||||
| 104 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 105 | 28 | ▶ | unless (defined $self->{predictedYs}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 15 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 14 | $self->{predictedYs} = []; | |||||||||||||||||||||||||||
| 108 | 14 | ▶ | for (my $i = 0; $i < $self->{numXY}; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | $self->{predictedYs}[$i] = $self->{intercept} | ||||||||||||||||||||||||||||
| 110 | 100243 | + $self->{slope} * $self->{x}[$i]; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 27 | return @{$self->{predictedYs}}; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 131 | ▶ | sub regress { | ||||||||||||||||||||||||||
|
Subroutine
regress: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||
| 118 | # Purpose: Do weighted or unweighted least squares 2-D line fit (if needed) | ||||||||||||||||||||||||||||
| 119 | # | ||||||||||||||||||||||||||||
| 120 | # Description: | ||||||||||||||||||||||||||||
| 121 | # The equations below apply to both the weighted and unweighted fit: the | ||||||||||||||||||||||||||||
| 122 | # weights are normalized in setWeights(), so the sum of the weights is | ||||||||||||||||||||||||||||
| 123 | # equal to numXY. | ||||||||||||||||||||||||||||
| 124 | # | ||||||||||||||||||||||||||||
| 125 | 131 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | 131 | ▶ | return $self->{regressOK} if $self->{doneRegress}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 24 | ▶ | unless ($self->{gotData}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 10 | ▶ | carp "No valid data input - can't do regression" unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 10 | return 0; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 14 | my ($sumX, $sumY, $sumYY, $sumXY); | |||||||||||||||||||||||||||
| 132 | 14 | ($sumX, $sumY, $self->{sumXX}, $sumYY, $sumXY) = $self->computeSums(); | |||||||||||||||||||||||||||
| 133 | 14 | $self->{sumSqDevX} = $self->{sumXX} - $sumX ** 2 / $self->{numXY}; | |||||||||||||||||||||||||||
| 134 | 14 | ▶ | if ($self->{sumSqDevX} != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 14 | $self->{sumSqDevY} = $sumYY - $sumY ** 2 / $self->{numXY}; | |||||||||||||||||||||||||||
| 136 | 14 | $self->{sumSqDevXY} = $sumXY - $sumX * $sumY / $self->{numXY}; | |||||||||||||||||||||||||||
| 137 | 14 | $self->{slope} = $self->{sumSqDevXY} / $self->{sumSqDevX}; | |||||||||||||||||||||||||||
| 138 | 14 | $self->{intercept} = ($sumY - $self->{slope} * $sumX) / $self->{numXY}; | |||||||||||||||||||||||||||
| 139 | 14 | $self->{regressOK} = 1; | |||||||||||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | carp "Can't fit line when x values are all equal" unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | $self->{sumXX} = $self->{sumSqDevX} = undef; | |||||||||||||||||||||||||||
| 143 | 0 | $self->{regressOK} = 0; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 14 | $self->{doneRegress} = 1; | |||||||||||||||||||||||||||
| 146 | 14 | return $self->{regressOK}; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 15 | ▶ | sub residuals { | ||||||||||||||||||||||||||
|
Subroutine
residuals: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | # | ||||||||||||||||||||||||||||
| 151 | # Purpose: Return the predicted Y values minus the observed Y values | ||||||||||||||||||||||||||||
| 152 | # | ||||||||||||||||||||||||||||
| 153 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | 15 | ▶ | unless (defined $self->{residuals}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 14 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 13 | $self->{residuals} = []; | |||||||||||||||||||||||||||
| 157 | 13 | ▶ | for (my $i = 0; $i < $self->{numXY}; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | $self->{residuals}[$i] = $self->{y}[$i] - ($self->{intercept} | ||||||||||||||||||||||||||||
| 159 | 243 | + $self->{slope} * $self->{x}[$i]); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 14 | return @{$self->{residuals}}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 16 | ▶ | sub rSquared { | ||||||||||||||||||||||||||
|
Subroutine
rSquared: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | # | ||||||||||||||||||||||||||||
| 167 | # Purpose: Return the correlation coefficient | ||||||||||||||||||||||||||||
| 168 | # | ||||||||||||||||||||||||||||
| 169 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 170 | 16 | ▶ | unless (defined $self->{rSquared}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 15 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 14 | my $denom = $self->{sumSqDevX} * $self->{sumSqDevY}; | |||||||||||||||||||||||||||
| 173 | 14 | ▶ | $self->{rSquared} = $denom != 0 ? $self->{sumSqDevXY} ** 2 / $denom : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 15 | return $self->{rSquared}; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 29 | ▶ | sub setData { | ||||||||||||||||||||||||||
|
Subroutine
setData: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | # | ||||||||||||||||||||||||||||
| 180 | # Purpose: Initialize (x,y) values and optional weights | ||||||||||||||||||||||||||||
| 181 | # | ||||||||||||||||||||||||||||
| 182 | 29 | my ($self, $x, $y, $weights) = @_; | |||||||||||||||||||||||||||
| 183 | 29 | $self->{doneRegress} = 0; | |||||||||||||||||||||||||||
| 184 | $self->{x} = $self->{y} = $self->{numXY} = $self->{weight} | ||||||||||||||||||||||||||||
| 185 | = $self->{intercept} = $self->{slope} = $self->{rSquared} | ||||||||||||||||||||||||||||
| 186 | = $self->{sigma} = $self->{durbinWatson} = $self->{meanSqError} | ||||||||||||||||||||||||||||
| 187 | = $self->{sumSqErrors} = $self->{tStatInt} = $self->{tStatSlope} | ||||||||||||||||||||||||||||
| 188 | = $self->{predictedYs} = $self->{residuals} = $self->{sumXX} | ||||||||||||||||||||||||||||
| 189 | = $self->{sumSqDevX} = $self->{sumSqDevY} = $self->{sumSqDevXY} | ||||||||||||||||||||||||||||
| 190 | 29 | = undef; | |||||||||||||||||||||||||||
| 191 | 29 | ▶ | if (@$x < 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 2 | ▶ | carp "Must input more than one data point!" unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 2 | return 0; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 27 | $self->{numXY} = @$x; | |||||||||||||||||||||||||||
| 196 | 27 | ▶ | if (ref $x->[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 5 | ▶ | $self->setWeights($y) or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 2 | $self->{x} = [ ]; | |||||||||||||||||||||||||||
| 199 | 2 | $self->{y} = [ ]; | |||||||||||||||||||||||||||
| 200 | 2 | foreach my $xy (@$x) { | |||||||||||||||||||||||||||
| 201 | 8 | push @{$self->{x}}, $xy->[0]; | |||||||||||||||||||||||||||
| 202 | 8 | push @{$self->{y}}, $xy->[1]; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } else { | ||||||||||||||||||||||||||||
| 205 | 22 | ▶ | if (@$x != @$y) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 1 | ▶ | carp "Length of x and y arrays must be equal!" unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 1 | return 0; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 21 | ▶ | $self->setWeights($weights) or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 16 | $self->{x} = [ @$x ]; | |||||||||||||||||||||||||||
| 211 | 16 | $self->{y} = [ @$y ]; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | 18 | ▶ | if ($self->{validate}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 5 | ▶ | unless ($self->validData()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 4 | $self->{x} = $self->{y} = $self->{weights} = $self->{numXY} = undef; | |||||||||||||||||||||||||||
| 216 | 4 | return 0; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 14 | $self->{gotData} = 1; | |||||||||||||||||||||||||||
| 220 | 14 | return 1; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 26 | ▶ | sub setWeights { | ||||||||||||||||||||||||||
|
Subroutine
setWeights: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 224 | # | ||||||||||||||||||||||||||||
| 225 | # Purpose: Normalize and initialize line fit weighting factors (private method) | ||||||||||||||||||||||||||||
| 226 | # | ||||||||||||||||||||||||||||
| 227 | 26 | my ($self, $weights) = @_; | |||||||||||||||||||||||||||
| 228 | 26 | ▶ | return 1 unless defined $weights; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 13 | ▶ | if (@$weights != $self->{numXY}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | carp "Length of weight array must equal length of data array!" | ||||||||||||||||||||||||||||
| 231 | 2 | ▶ | unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 2 | return 0; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 11 | ▶ | if ($self->{validate}) { $self->validWeights($weights) or return 0 } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 235 | 9 | my $sumW = my $numNonZero = 0; | |||||||||||||||||||||||||||
| 236 | 9 | foreach my $weight (@$weights) { | |||||||||||||||||||||||||||
| 237 | 221 | ▶ | if ($weight < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 2 | ▶ | carp "Weights must be non-negative numbers!" unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 2 | return 0; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 219 | $sumW += $weight; | |||||||||||||||||||||||||||
| 242 | 219 | ▶ | if ($weight != 0) { ++$numNonZero } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | 7 | ▶ | if ($numNonZero < 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 2 | ▶ | carp "At least two weights must be nonzero!" unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 2 | return 0; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | 5 | my $factor = @$weights / $sumW; | |||||||||||||||||||||||||||
| 249 | 5 | foreach my $weight (@$weights) { $weight *= $factor } | |||||||||||||||||||||||||||
| 250 | 5 | $self->{weight} = [ @$weights ]; | |||||||||||||||||||||||||||
| 251 | 5 | return 1; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 44 | ▶ | sub sigma { | ||||||||||||||||||||||||||
|
Subroutine
sigma: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 255 | # | ||||||||||||||||||||||||||||
| 256 | # Purpose: Return the estimated homoscedastic standard deviation of the | ||||||||||||||||||||||||||||
| 257 | # error term | ||||||||||||||||||||||||||||
| 258 | # | ||||||||||||||||||||||||||||
| 259 | 44 | my $self = shift; | |||||||||||||||||||||||||||
| 260 | 44 | ▶ | unless (defined $self->{sigma}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 15 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | $self->{sigma} = $self->{numXY} > 2 ? | ||||||||||||||||||||||||||||
| 263 | 14 | ▶ | sqrt($self->sumSqErrors() / ($self->{numXY} - 2)) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 43 | return $self->{sigma}; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 62 | ▶ | sub sumSqErrors { | ||||||||||||||||||||||||||
|
Subroutine
sumSqErrors: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 269 | # | ||||||||||||||||||||||||||||
| 270 | # Purpose: Return the sum of the squared errors (private method) | ||||||||||||||||||||||||||||
| 271 | # | ||||||||||||||||||||||||||||
| 272 | 62 | my $self = shift; | |||||||||||||||||||||||||||
| 273 | 62 | ▶ | unless (defined $self->{sumSqErrors}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 14 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | $self->{sumSqErrors} = $self->{sumSqDevY} - $self->{sumSqDevX} | ||||||||||||||||||||||||||||
| 276 | 14 | * $self->{slope} ** 2; | |||||||||||||||||||||||||||
| 277 | 0 | ▶ | if ($self->{sumSqErrors} < 0) { $self->{sumSqErrors} = 0 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | 62 | return $self->{sumSqErrors}; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 16 | ▶ | sub tStatistics { | ||||||||||||||||||||||||||
|
Subroutine
tStatistics: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 283 | # | ||||||||||||||||||||||||||||
| 284 | # Purpose: Return the T statistics | ||||||||||||||||||||||||||||
| 285 | # | ||||||||||||||||||||||||||||
| 286 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 287 | 16 | ▶ | unless (defined $self->{tStatInt} and defined $self->{tStatSlope}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"tStatInt"} and defined $self->{"tStatSlope"}
MC/DC50%1/2
defined $self->{"tStatInt"} and defined $self->{"tStatSlope"}
defined $self->{"tStatInt"}
defined $self->{"tStatSlope"}
Truth table66%2/3
A and B
A= defined $self->{"tStatInt"}B= defined $self->{"tStatSlope"}
| |||||||||||||||||||||||||||||
| 288 | 15 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | my $biasEstimateInt = $self->sigma() * sqrt($self->{sumXX} | ||||||||||||||||||||||||||||
| 290 | 14 | / ($self->{sumSqDevX} * $self->{numXY})); | |||||||||||||||||||||||||||
| 291 | $self->{tStatInt} = $biasEstimateInt != 0 ? | ||||||||||||||||||||||||||||
| 292 | 14 | ▶ | $self->{intercept} / $biasEstimateInt : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 14 | my $biasEstimateSlope = $self->sigma() / sqrt($self->{sumSqDevX}); | |||||||||||||||||||||||||||
| 294 | $self->{tStatSlope} = $biasEstimateSlope != 0 ? | ||||||||||||||||||||||||||||
| 295 | 14 | ▶ | $self->{slope} / $biasEstimateSlope : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | 15 | return ($self->{tStatInt}, $self->{tStatSlope}); | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 5 | ▶ | sub validData { | ||||||||||||||||||||||||||
|
Subroutine
validData: called CC=10 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 301 | # | ||||||||||||||||||||||||||||
| 302 | # Purpose: Verify that the input x-y data are numeric (private method) | ||||||||||||||||||||||||||||
| 303 | # | ||||||||||||||||||||||||||||
| 304 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 305 | 5 | ▶ | for (my $i = 0; $i < $self->{numXY}; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 12 | ▶ | if (not defined $self->{x}[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 1 | ▶ | carp "Input x[$i] is not defined" unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 1 | return 0; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | 11 | ▶ | if ($self->{x}[$i] !~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) | ||||||||||||||||||||||||||||
| 312 | { | ||||||||||||||||||||||||||||
| 313 | carp "Input x[$i] is not a number: $self->{x}[$i]" | ||||||||||||||||||||||||||||
| 314 | 1 | ▶ | unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 1 | return 0; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | 10 | ▶ | if (not defined $self->{y}[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 1 | ▶ | carp "Input y[$i] is not defined" unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | 1 | return 0; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 9 | ▶ | if ($self->{y}[$i] !~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) | ||||||||||||||||||||||||||||
| 323 | { | ||||||||||||||||||||||||||||
| 324 | carp "Input y[$i] is not a number: $self->{y}[$i]" | ||||||||||||||||||||||||||||
| 325 | 1 | ▶ | unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 1 | return 0; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | 1 | return 1; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 3 | ▶ | sub validWeights { | ||||||||||||||||||||||||||
|
Subroutine
validWeights: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 333 | # | ||||||||||||||||||||||||||||
| 334 | # Purpose: Verify that the input weights are numeric (private method) | ||||||||||||||||||||||||||||
| 335 | # | ||||||||||||||||||||||||||||
| 336 | 3 | my ($self, $weights) = @_; | |||||||||||||||||||||||||||
| 337 | 3 | ▶ | for (my $i = 0; $i < @$weights; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | 9 | ▶ | if (not defined $weights->[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 1 | ▶ | carp "Input weights[$i] is not defined" unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 1 | return 0; | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | 8 | ▶ | if ($weights->[$i] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) | ||||||||||||||||||||||||||||
| 344 | { | ||||||||||||||||||||||||||||
| 345 | carp "Input weights[$i] is not a number: $weights->[$i]" | ||||||||||||||||||||||||||||
| 346 | 1 | ▶ | unless $self->{hush}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | 1 | return 0; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | 1 | return 1; | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 16 | ▶ | sub varianceOfEstimates { | ||||||||||||||||||||||||||
|
Subroutine
varianceOfEstimates: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 354 | # | ||||||||||||||||||||||||||||
| 355 | # Purpose: Return the variances in the estimates of the intercept and slope | ||||||||||||||||||||||||||||
| 356 | # | ||||||||||||||||||||||||||||
| 357 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 358 | 16 | ▶ | unless (defined $self->{intercept} and defined $self->{slope}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"intercept"} and defined $self->{"slope"}
MC/DC50%1/2
defined $self->{"intercept"} and defined $self->{"slope"}
defined $self->{"intercept"}
defined $self->{"slope"}
Truth table66%2/3
A and B
A= defined $self->{"intercept"}B= defined $self->{"slope"}
| |||||||||||||||||||||||||||||
| 359 | 1 | ▶ | $self->regress() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | 15 | my @predictedYs = $self->predictedYs(); | |||||||||||||||||||||||||||
| 362 | 15 | my ($s, $sx, $sxx) = (0, 0, 0); | |||||||||||||||||||||||||||
| 363 | 15 | ▶ | if (defined $self->{weight}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | 5 | ▶ | for (my $i = 0; $i < $self->{numXY}; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 213 | my $variance = ($predictedYs[$i] - $self->{y}[$i]) ** 2; | |||||||||||||||||||||||||||
| 366 | 213 | ▶ | next if 0 == $variance; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | 209 | $s += 1.0 / $variance; | |||||||||||||||||||||||||||
| 368 | 209 | $sx += $self->{weight}[$i] * $self->{x}[$i] / $variance; | |||||||||||||||||||||||||||
| 369 | 209 | $sxx += $self->{weight}[$i] * $self->{x}[$i] ** 2 / $variance; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | } else { | ||||||||||||||||||||||||||||
| 372 | 10 | ▶ | for (my $i = 0; $i < $self->{numXY}; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 100034 | my $variance = ($predictedYs[$i] - $self->{y}[$i]) ** 2; | |||||||||||||||||||||||||||
| 374 | 100034 | ▶ | next if 0 == $variance; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 100027 | $s += 1.0 / $variance; | |||||||||||||||||||||||||||
| 376 | 100027 | $sx += $self->{x}[$i] / $variance; | |||||||||||||||||||||||||||
| 377 | 100027 | $sxx += $self->{x}[$i] ** 2 / $variance; | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | 15 | my $denominator = ($s * $sxx - $sx ** 2); | |||||||||||||||||||||||||||
| 381 | 15 | ▶ | if (0 == $denominator) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | 3 | return; | |||||||||||||||||||||||||||
| 383 | } else { | ||||||||||||||||||||||||||||
| 384 | 12 | return ($sxx / $denominator, $s / $denominator); | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 14 | 1; | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | __END__ | ||||||||||||||||||||||||||||