| 1 | package Statistics::Regression; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | $VERSION = '0.53'; | |||||||||||||||||||||||||||
| 4 | 1 | my $DATE = "2007/07/07"; | |||||||||||||||||||||||||||
| 5 | 1 | my $MNAME= "$0::Statistics::Regression"; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings FATAL => qw{ uninitialized }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | ################################################################ | ||||||||||||||||||||||||||||
| 13 | =pod | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Regression.pm - weighted linear regression package (line+plane fitting) | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use Statistics::Regression; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Create regression object | ||||||||||||||||||||||||||||
| 25 | my $reg = Statistics::Regression->new( "sample regression", [ "const", "someX", "someY" ] ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Add data points | ||||||||||||||||||||||||||||
| 28 | $reg->include( 2.0, [ 1.0, 3.0, -1.0 ] ); | ||||||||||||||||||||||||||||
| 29 | $reg->include( 1.0, [ 1.0, 5.0, 2.0 ] ); | ||||||||||||||||||||||||||||
| 30 | $reg->include( 20.0, [ 1.0, 31.0, 0.0 ] ); | ||||||||||||||||||||||||||||
| 31 | $reg->include( 15.0, [ 1.0, 11.0, 2.0 ] ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | or | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | my %d; | ||||||||||||||||||||||||||||
| 36 | $d{const} = 1.0; $d{someX}= 5.0; $d{someY}= 2.0; $d{ignored}="anything else"; | ||||||||||||||||||||||||||||
| 37 | $reg->include( 3.0, \%d ); # names are picked off the Regression specification | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Please note that *you* must provide the constant if you want one. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Finally, print the result | ||||||||||||||||||||||||||||
| 42 | $reg->print(); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | This prints the following: | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | **************************************************************** | ||||||||||||||||||||||||||||
| 47 | Regression 'sample regression' | ||||||||||||||||||||||||||||
| 48 | **************************************************************** | ||||||||||||||||||||||||||||
| 49 | Name Theta StdErr T-stat | ||||||||||||||||||||||||||||
| 50 | [0='const'] 0.2950 6.0512 0.05 | ||||||||||||||||||||||||||||
| 51 | [1='someX'] 0.6723 0.3278 2.05 | ||||||||||||||||||||||||||||
| 52 | [2='someY'] 1.0688 2.7954 0.38 | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | R^2= 0.808, N= 4 | ||||||||||||||||||||||||||||
| 55 | **************************************************************** | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | The hash input method has the advantage that you can now just | ||||||||||||||||||||||||||||
| 60 | fill the observation hashes with all your variables, and use the | ||||||||||||||||||||||||||||
| 61 | same code to run regression, changing the regression | ||||||||||||||||||||||||||||
| 62 | specification at one and only one spot (the new() invokation). | ||||||||||||||||||||||||||||
| 63 | You do not need to change the inputs in the include() statement. | ||||||||||||||||||||||||||||
| 64 | For example, | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | my @obs; ## a global variable. observations are like: %oneobs= %{$obs[1]}; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub run_regression { | ||||||||||||||||||||||||||||
| 69 | my $reg = Statistics::Regression->new( $_[0], $_[2] ); | ||||||||||||||||||||||||||||
| 70 | foreach my $obshashptr (@obs) { $reg->include( $_[1], $_[3] ); } | ||||||||||||||||||||||||||||
| 71 | $reg->print(); | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | run_regression("bivariate regression", $obshashptr->{someY}, [ "const", "someX" ] ); | ||||||||||||||||||||||||||||
| 75 | run_regression("trivariate regression", $obshashptr->{someY}, [ "const", "someX", "someZ" ] ); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Of course, you can use the subroutines to do the printing work yourself: | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | my @theta = $reg->theta(); | ||||||||||||||||||||||||||||
| 82 | my @se = $reg->standarderrors(); | ||||||||||||||||||||||||||||
| 83 | my $rsq = $reg->rsq(); | ||||||||||||||||||||||||||||
| 84 | my $adjrsq = $reg->adjrsq(); | ||||||||||||||||||||||||||||
| 85 | my $ybar = $reg->ybar(); ## the average of the y vector | ||||||||||||||||||||||||||||
| 86 | my $sst = $reg->sst(); ## the sum-squares-total | ||||||||||||||||||||||||||||
| 87 | my $sigmasq= $reg->sigmasq(); ## the variance of the residual | ||||||||||||||||||||||||||||
| 88 | my $k = $reg->k(); ## the number of variables | ||||||||||||||||||||||||||||
| 89 | my $n = $reg->n(); ## the number of observations | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | In addition, there are some other helper routines, and a | ||||||||||||||||||||||||||||
| 92 | subroutine linearcombination_variance(). If you don't know what | ||||||||||||||||||||||||||||
| 93 | this is, don't use it. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 BACKGROUND WARNING | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | You should have an understanding of OLS regressions if you want | ||||||||||||||||||||||||||||
| 99 | to use this package. You can get this from an introductory | ||||||||||||||||||||||||||||
| 100 | college econometrics class and/or from most intermediate college | ||||||||||||||||||||||||||||
| 101 | statistics classes. If you do not have this background | ||||||||||||||||||||||||||||
| 102 | knowledge, then this package will remain a mystery to you. | ||||||||||||||||||||||||||||
| 103 | There is no support for this package--please don't expect any. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Regression.pm is a multivariate linear regression package. That | ||||||||||||||||||||||||||||
| 109 | is, it estimates the c coefficients for a line-fit of the type | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | y= c(0)*x(0) + c(1)*x1 + c(2)*x2 + ... + c(k)*xk | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | given a data set of N observations, each with k independent x | ||||||||||||||||||||||||||||
| 114 | variables and one y variable. Naturally, N must be greater than | ||||||||||||||||||||||||||||
| 115 | k---and preferably considerably greater. Any reasonable | ||||||||||||||||||||||||||||
| 116 | undergraduate statistics book will explain what a regression is. | ||||||||||||||||||||||||||||
| 117 | Most of the time, the user will provide a constant ('1') as x(0) | ||||||||||||||||||||||||||||
| 118 | for each observation in order to allow the regression package to | ||||||||||||||||||||||||||||
| 119 | fit an intercept. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 ALGORITHM | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 Original Algorithm (ALGOL-60): | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | W. M. Gentleman, University of Waterloo, "Basic | ||||||||||||||||||||||||||||
| 127 | Description For Large, Sparse Or Weighted Linear Least | ||||||||||||||||||||||||||||
| 128 | Squares Problems (Algorithm AS 75)," Applied Statistics | ||||||||||||||||||||||||||||
| 129 | (1974) Vol 23; No. 3 | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Gentleman's algorithm is I<the> statistical standard. Insertion | ||||||||||||||||||||||||||||
| 132 | of a new observation can be done one observation at any time | ||||||||||||||||||||||||||||
| 133 | (WITH A WEIGHT!), and still only takes a low quadratic time. | ||||||||||||||||||||||||||||
| 134 | The storage space requirement is of quadratic order (in the | ||||||||||||||||||||||||||||
| 135 | indep variables). A practically infinite number of observations | ||||||||||||||||||||||||||||
| 136 | can easily be processed! | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 Internal Data Structures | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | R=Rbar is an upperright triangular matrix, kept in normalized | ||||||||||||||||||||||||||||
| 141 | form with implicit 1's on the diagonal. D is a diagonal scaling | ||||||||||||||||||||||||||||
| 142 | matrix. These correspond to "standard Regression usage" as | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | X' X = R' D R | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | A backsubsitution routine (in thetacov) allows to invert the R | ||||||||||||||||||||||||||||
| 147 | matrix (the inverse is upper-right triangular, too!). Call this | ||||||||||||||||||||||||||||
| 148 | matrix H, that is H=R^(-1). | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | (X' X)^(-1) = [(R' D^(1/2)') (D^(1/2) R)]^(-1) | ||||||||||||||||||||||||||||
| 151 | = [ R^-1 D^(-1/2) ] [ R^-1 D^(-1/2) ]' | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 BUGS/PROBLEMS | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | None known. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =over 4 | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item Perl Problem | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Unfortunately, perl is unaware of IEEE number representations. | ||||||||||||||||||||||||||||
| 163 | This makes it a pain to test whether an observation contains any | ||||||||||||||||||||||||||||
| 164 | missing variables (coded as 'NaN' in Regression.pm). | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =back | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =for comment | ||||||||||||||||||||||||||||
| 169 | pod2html -noindex -title "perl weighted least squares regression package" Regression.pm > Regression.html | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head1 VERSION and RECENT CHANGES | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 2007/04/04: Added Coefficient Standard Errors | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 2007/07/01: Added self-test use (if invoked as perl Regression.pm) | ||||||||||||||||||||||||||||
| 177 | at the end. cleaned up some print sprintf. | ||||||||||||||||||||||||||||
| 178 | changed syntax on new() to eliminate passing K. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 2007/07/07: allowed passing hash with names to include(). | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Naturally, Gentleman invented this algorithm. It was adaptated | ||||||||||||||||||||||||||||
| 186 | by Ivo Welch. Alan Miller (alan\@dmsmelb.mel.dms.CSIRO.AU) | ||||||||||||||||||||||||||||
| 187 | pointed out nicer ways to compute the R^2. Ivan Tubert-Brohman | ||||||||||||||||||||||||||||
| 188 | helped wrap the module as as a standard CPAN distribution. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | This module is released for free public use under a GPL license. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | (C) Ivo Welch, 2001,2004, 2007. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | ################################################################ | ||||||||||||||||||||||||||||
| 200 | #### let's start with handling of missing data ("nan" or "NaN") | ||||||||||||||||||||||||||||
| 201 | ################################################################ | ||||||||||||||||||||||||||||
| 202 | 1 | ▶ | use constant TINY => 1e-8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 203 | 1 | my $nan= "NaN"; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 16 | ▶ | sub isNaN { | ||||||||||||||||||||||||||
|
Subroutine
isNaN: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if ($_[0] !~ /[0-9nan]/) { confess "$MNAME:isNaN: definitely not a number in NaN: '$_[0]'"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 16 | ▶ | return ($_[0]=~ /NaN/i) || ($_[0] != $_[0]); | ||||||||||||||||||||||||||
|
Condition33%1/3
$_[0] =~ /NaN/i || $_[0] != $_[0]
MC/DC0%0/2
$_[0] =~ /NaN/i || $_[0] != $_[0]
$_[0] =~ /NaN/i
$_[0] != $_[0]
Truth table33%1/3
A || B
A= $_[0] =~ /NaN/iB= $_[0] != $_[0]
| |||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | ################################################################ | ||||||||||||||||||||||||||||
| 212 | ### my $reg = Statistics::Regression->new($regname, \@var_names) | ||||||||||||||||||||||||||||
| 213 | ### | ||||||||||||||||||||||||||||
| 214 | ### Receives the number of variables on each observations (i.e., | ||||||||||||||||||||||||||||
| 215 | ### an integer) and returns the blessed data structure as a | ||||||||||||||||||||||||||||
| 216 | ### Statistics::Regression object. Also takes an optional name | ||||||||||||||||||||||||||||
| 217 | ### for this regression to remember, as well as a reference to a | ||||||||||||||||||||||||||||
| 218 | ### k*1 array of names for the X coefficients. | ||||||||||||||||||||||||||||
| 219 | ### | ||||||||||||||||||||||||||||
| 220 | ### I have now made it mandatory to give some names. | ||||||||||||||||||||||||||||
| 221 | ### | ||||||||||||||||||||||||||||
| 222 | ################################################################ | ||||||||||||||||||||||||||||
| 223 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=23.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 224 | 1 | ▶ | my $classname= shift; (!ref($classname)) or confess "$MNAME:new: bad class call to new ($classname).\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 1 | ▶ | my $regname= shift || "no-name"; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'no-name'
MC/DC0%0/1
shift() || 'no-name'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 226 | 1 | my $xnameptr= shift; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 1 | ▶ | (defined($regname)) or confess "$MNAME:new: bad name in for regression. no undef allowed.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 1 | ▶ | (!ref($regname)) or confess "$MNAME:new: bad name in for regression.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 1 | ▶ | (defined($xnameptr)) or confess "$MNAME:new: You must provide variable names, because this tells me the number of columns. no undef allowed.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 1 | ▶ | (ref($xnameptr) eq "ARRAY") or confess "$MNAME:new: bad xnames for regression. Must be pointer.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 1 | my $K= (@{$xnameptr}); | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | ▶ | if (!defined($K)) { confess "$MNAME:new: cannot determine the number of variables"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | if ($K<=1) { confess "$MNAME:new: Cannot run a regression without at least two variables."; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 3 | ▶ | sub zerovec { | ||||||||||||||||||||||||||
|
Subroutine
zerovec: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 239 | 3 | my @rv; | |||||||||||||||||||||||||||
| 240 | 3 | ▶ | for (my $i=0; $i<=$_[0]; ++$i) { $rv[$i]=0; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 3 | return \@rv; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 1 | bless { | |||||||||||||||||||||||||||
| 245 | k => $K, | ||||||||||||||||||||||||||||
| 246 | regname => $regname, | ||||||||||||||||||||||||||||
| 247 | xnames => $xnameptr, | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # constantly updated | ||||||||||||||||||||||||||||
| 250 | n => 0, | ||||||||||||||||||||||||||||
| 251 | sse => 0, | ||||||||||||||||||||||||||||
| 252 | syy => 0, | ||||||||||||||||||||||||||||
| 253 | sy => 0, | ||||||||||||||||||||||||||||
| 254 | wghtn => 0, | ||||||||||||||||||||||||||||
| 255 | d => zerovec($K), | ||||||||||||||||||||||||||||
| 256 | thetabar => zerovec($K), | ||||||||||||||||||||||||||||
| 257 | rbarsize => ($K+1)*$K/2+1, | ||||||||||||||||||||||||||||
| 258 | rbar => zerovec(($K+1)*$K/2+1), | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # other constants | ||||||||||||||||||||||||||||
| 261 | neverabort => 0, | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | # computed on demand | ||||||||||||||||||||||||||||
| 264 | theta => undef, | ||||||||||||||||||||||||||||
| 265 | sigmasq => undef, | ||||||||||||||||||||||||||||
| 266 | rsq => undef, | ||||||||||||||||||||||||||||
| 267 | adjrsq => undef | ||||||||||||||||||||||||||||
| 268 | }, $classname; | ||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | ################################################################ | ||||||||||||||||||||||||||||
| 273 | ### $reg->include( $y, [ $x1, $x2, $x3 ... $xk ], $weight ); | ||||||||||||||||||||||||||||
| 274 | ### | ||||||||||||||||||||||||||||
| 275 | ### Add one new observation. The weight is optional. Note that | ||||||||||||||||||||||||||||
| 276 | ### inclusion with a weight of -1 can be used to delete an | ||||||||||||||||||||||||||||
| 277 | ### observation. | ||||||||||||||||||||||||||||
| 278 | ### | ||||||||||||||||||||||||||||
| 279 | ### The error checking and transfer of arguments is clutzy, but | ||||||||||||||||||||||||||||
| 280 | ### works. if I had POSIX assured, I could do better number | ||||||||||||||||||||||||||||
| 281 | ### checking. right now, I don't do any. | ||||||||||||||||||||||||||||
| 282 | ### | ||||||||||||||||||||||||||||
| 283 | ### Returns the number of observations so far included. | ||||||||||||||||||||||||||||
| 284 | ################################################################ | ||||||||||||||||||||||||||||
| 285 | 4 | ▶ | sub include { | ||||||||||||||||||||||||||
|
Subroutine
include: called CC=22 SCAR=33.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 286 | 4 | my $this = shift; | |||||||||||||||||||||||||||
| 287 | 4 | my $yelement= shift; | |||||||||||||||||||||||||||
| 288 | 4 | my $xin= shift; | |||||||||||||||||||||||||||
| 289 | 4 | ▶ | my $weight= shift || 1.0; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 1.0
MC/DC0%0/1
shift() || 1.0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # modest input checking; | ||||||||||||||||||||||||||||
| 292 | 4 | ▶ | (ref($this)) or confess "$MNAME:include: bad class call to include.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 4 | ▶ | (defined($yelement)) or confess "$MNAME:include: bad call for y to include. no undef allowed.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 4 | ▶ | (!ref($yelement)) or confess "$MNAME:include: bad call for y to include. need scalar.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 4 | ▶ | (defined($xin)) or confess "$MNAME:include: bad call for x to include. no undef allowed.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 4 | ▶ | (ref($xin)) or confess "$MNAME:include: bad call for x to include. need reference.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | 4 | ▶ | (!ref($weight)) or confess "$MNAME:include: bad call for weight to include. need scalar.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # omit observations with missing observations; | ||||||||||||||||||||||||||||
| 301 | 4 | ▶ | (defined($yelement)) or confess "$MNAME:include: you must give a y value (predictor)."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 4 | ▶ | (isNaN($yelement)) and return $this->{n}; # ignore this observation; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | ## should check for number, not string | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | # check and transfer the X vector | ||||||||||||||||||||||||||||
| 307 | 4 | my @xrow; | |||||||||||||||||||||||||||
| 308 | 4 | ▶ | if (ref($xin) eq "ARRAY") { @xrow= @{$xin}; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | else { | ||||||||||||||||||||||||||||
| 310 | 0 | my $xctr=0; | |||||||||||||||||||||||||||
| 311 | 0 | foreach my $nm (@{$this->{xnames}}) { | |||||||||||||||||||||||||||
| 312 | 0 | ▶ | (defined($xin->{$nm})) or confess "$MNAME:include: Variable '$nm' needs to be set in hash.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 0 | $xrow[$xctr]= $xin->{$nm}; | |||||||||||||||||||||||||||
| 314 | 0 | ++$xctr; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 4 | my @xcopy; | |||||||||||||||||||||||||||
| 319 | 4 | ▶ | for (my $i=1; $i<=$this->{k}; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | (defined($xrow[$i-1])) | ||||||||||||||||||||||||||||
| 321 | 12 | ▶ | or confess "$MNAME:include: Internal Error: at N=".($this->{n}).", the x[".($i-1)."] is undef. use NaN for missing."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 12 | ▶ | (isNaN($xrow[$i-1])) and return $this->{n}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 12 | $xcopy[$i]= $xrow[$i-1]; | |||||||||||||||||||||||||||
| 324 | ## should check for number, not string | ||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | ################ now comes the real routine | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 4 | $this->{syy}+= ($weight*($yelement*$yelement)); | |||||||||||||||||||||||||||
| 330 | 4 | $this->{sy}+= ($weight*($yelement)); | |||||||||||||||||||||||||||
| 331 | 0 | ▶ | if ($weight>=0.0) { ++$this->{n}; } else { --$this->{n}; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 4 | $this->{wghtn}+= $weight; | |||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 4 | ▶ | for (my $i=1; $i<=$this->{k};++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 11 | ▶ | if ($weight==0.0) { return $this->{n}; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | 9 | ▶ | if (abs($xcopy[$i])>(TINY)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | 9 | my $xi=$xcopy[$i]; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 9 | my $di=$this->{d}->[$i]; | |||||||||||||||||||||||||||
| 341 | 9 | my $dprimei=$di+$weight*($xi*$xi); | |||||||||||||||||||||||||||
| 342 | 9 | my $cbar= $di/$dprimei; | |||||||||||||||||||||||||||
| 343 | 9 | my $sbar= $weight*$xi/$dprimei; | |||||||||||||||||||||||||||
| 344 | 9 | $weight*=($cbar); | |||||||||||||||||||||||||||
| 345 | 9 | $this->{d}->[$i]=$dprimei; | |||||||||||||||||||||||||||
| 346 | 9 | my $nextr=int( (($i-1)*( (2.0*$this->{k}-$i))/2.0+1) ); | |||||||||||||||||||||||||||
| 347 | 0 | ▶ | if (!($nextr<=$this->{rbarsize}) ) { confess "$MNAME:include: Internal Error 2"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 9 | my $xk; | |||||||||||||||||||||||||||
| 349 | 9 | ▶ | for (my $kc=$i+1;$kc<=$this->{k};++$kc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 11 | $xk=$xcopy[$kc]; $xcopy[$kc]=$xk-$xi*$this->{rbar}->[$nextr]; | |||||||||||||||||||||||||||
| 351 | 11 | $this->{rbar}->[$nextr]= $cbar * $this->{rbar}->[$nextr]+$sbar*$xk; | |||||||||||||||||||||||||||
| 352 | 11 | ++$nextr; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | 9 | $xk=$yelement; $yelement-= $xi*$this->{thetabar}->[$i]; | |||||||||||||||||||||||||||
| 355 | 9 | $this->{thetabar}->[$i]= $cbar*$this->{thetabar}->[$i]+$sbar*$xk; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | 2 | $this->{sse}+=$weight*($yelement*$yelement); | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | # indicate that Theta is garbage now | ||||||||||||||||||||||||||||
| 361 | 2 | $this->{theta}= undef; | |||||||||||||||||||||||||||
| 362 | 2 | $this->{sigmasq}= undef; $this->{rsq}= undef; $this->{adjrsq}= undef; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 2 | return $this->{n}; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | ################################################################ | ||||||||||||||||||||||||||||
| 369 | ### | ||||||||||||||||||||||||||||
| 370 | ### $reg->rsq(), $reg->adjrsq(), $reg->sigmasq(), $reg->ybar(), | ||||||||||||||||||||||||||||
| 371 | ### $reg->sst(), $reg->k(), $reg->n() | ||||||||||||||||||||||||||||
| 372 | ### | ||||||||||||||||||||||||||||
| 373 | ### These methods provide common auxiliary information. rsq, | ||||||||||||||||||||||||||||
| 374 | ### adjrsq, sigmasq, sst, and ybar have not been checked but are | ||||||||||||||||||||||||||||
| 375 | ### likely correct. The results are stored for later usage, | ||||||||||||||||||||||||||||
| 376 | ### although this is somewhat unnecessary because the | ||||||||||||||||||||||||||||
| 377 | ### computation is so simple anyway. | ||||||||||||||||||||||||||||
| 378 | ################################################################ | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 1 | ▶ | sub rsq { | ||||||||||||||||||||||||||
|
Subroutine
rsq: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 381 | 1 | my $this= shift; | |||||||||||||||||||||||||||
| 382 | 1 | return $this->{rsq}= 1.0- $this->{sse} / $this->sst(); | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 0 | ▶ | sub adjrsq { | ||||||||||||||||||||||||||
|
Subroutine
adjrsq: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 386 | 0 | my $this= shift; | |||||||||||||||||||||||||||
| 387 | 0 | return $this->{adjrsq}= 1.0- (1.0- $this->rsq())*($this->{n}-1)/($this->{n} - $this->{k}); | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 0 | ▶ | sub sigmasq { | ||||||||||||||||||||||||||
|
Subroutine
sigmasq: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 391 | 0 | my $this= shift; | |||||||||||||||||||||||||||
| 392 | 0 | ▶ | return $this->{sigmasq}= ($this->{n}<=$this->{k}) ? "Inf" : ($this->{sse}/($this->{n} - $this->{k})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 1 | ▶ | sub ybar { | ||||||||||||||||||||||||||
|
Subroutine
ybar: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 396 | 1 | my $this= shift; | |||||||||||||||||||||||||||
| 397 | 1 | return $this->{ybar}= $this->{sy}/$this->{wghtn}; | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 1 | ▶ | sub sst { | ||||||||||||||||||||||||||
|
Subroutine
sst: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 401 | 1 | my $this= shift; | |||||||||||||||||||||||||||
| 402 | 1 | return $this->{sst}= ($this->{syy} - $this->{wghtn}*($this->ybar())**2); | |||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 0 | ▶ | sub k { | ||||||||||||||||||||||||||
|
Subroutine
k: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 406 | 0 | my $this= shift; | |||||||||||||||||||||||||||
| 407 | 0 | return $this->{k}; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | 0 | ▶ | sub n { | ||||||||||||||||||||||||||
|
Subroutine
n: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 410 | 0 | my $this= shift; | |||||||||||||||||||||||||||
| 411 | 0 | return $this->{n}; | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | ################################################################ | ||||||||||||||||||||||||||||
| 417 | ### $reg->print() [no arguments!] | ||||||||||||||||||||||||||||
| 418 | ### | ||||||||||||||||||||||||||||
| 419 | ### prints the estimated coefficients, and R^2 and N. For an | ||||||||||||||||||||||||||||
| 420 | ### example see the Synopsis. | ||||||||||||||||||||||||||||
| 421 | ################################################################ | ||||||||||||||||||||||||||||
| 422 | 0 | ▶ | sub print { | ||||||||||||||||||||||||||
|
Subroutine
print: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 423 | 0 | my $this= shift; | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 0 | print "****************************************************************\n"; | |||||||||||||||||||||||||||
| 426 | 0 | print "Regression '$this->{regname}'\n"; | |||||||||||||||||||||||||||
| 427 | 0 | print "****************************************************************\n"; | |||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 0 | my $theta= $this->theta(); | |||||||||||||||||||||||||||
| 430 | 0 | my @standarderrors= $this->standarderrors(); | |||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 0 | printf "%-15s\t%12s\t%12s\t%7s\n", "Name", "Theta", "StdErr", "T-stat"; | |||||||||||||||||||||||||||
| 433 | 0 | ▶ | for (my $i=0; $i< $this->k(); ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 0 | ▶ | my $name= "[$i".(defined($this->{xnames}->[$i]) ? "='$this->{xnames}->[$i]'":"")."]"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | 0 | printf "%-15s\t", $name; | |||||||||||||||||||||||||||
| 436 | 0 | printf "%12.4f\t", $theta->[$i]; | |||||||||||||||||||||||||||
| 437 | 0 | printf "%12.4f\t", $standarderrors[$i]; | |||||||||||||||||||||||||||
| 438 | 0 | printf "%7.2f", ($theta->[$i]/$standarderrors[$i]); | |||||||||||||||||||||||||||
| 439 | 0 | printf "\n"; | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | print "\nR^2= ".sprintf("%.3f", $this->rsq()).", N= ".$this->n().", K= ".$this->k()."\n"; | |||||||||||||||||||||||||||
| 443 | 0 | print "****************************************************************\n"; | |||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | ################################################################ | ||||||||||||||||||||||||||||
| 449 | ### $theta = $reg->theta or @theta = $reg->theta | ||||||||||||||||||||||||||||
| 450 | ### | ||||||||||||||||||||||||||||
| 451 | ### This is the work horse. It estimates and returns the vector | ||||||||||||||||||||||||||||
| 452 | ### of coefficients. In scalar context returns an array | ||||||||||||||||||||||||||||
| 453 | ### reference; in list context it returns the list of | ||||||||||||||||||||||||||||
| 454 | ### coefficients. | ||||||||||||||||||||||||||||
| 455 | ################################################################ | ||||||||||||||||||||||||||||
| 456 | 1 | ▶ | sub theta { | ||||||||||||||||||||||||||
|
Subroutine
theta: called CC=8 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 457 | 1 | my $this= shift; | |||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 1 | ▶ | if (defined($this->{theta})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | 0 | ▶ | return wantarray ? @{$this->{theta}} : $this->{theta}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 0 | ▶ | if ($this->{n} < $this->{k}) { return; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 464 | 1 | ▶ | for (my $i=($this->{k}); $i>=1; --$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 465 | 3 | $this->{theta}->[$i]= $this->{thetabar}->[$i]; | |||||||||||||||||||||||||||
| 466 | 3 | my $nextr= int (($i-1)*((2.0*$this->{k}-$i))/2.0+1); | |||||||||||||||||||||||||||
| 467 | 0 | ▶ | if (!($nextr<=$this->{rbarsize})) { confess "$MNAME:theta: Internal Error 3"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 468 | 3 | ▶ | for (my $kc=$i+1;$kc<=$this->{k};++$kc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 469 | 3 | $this->{theta}->[$i]-=($this->{rbar}->[$nextr]*$this->{theta}->[$kc]); | |||||||||||||||||||||||||||
| 470 | 3 | ++$nextr; | |||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 1 | my $ref = $this->{theta}; shift(@$ref); # we are counting from 0 | |||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | # if in a scalar context, otherwise please return the array directly | ||||||||||||||||||||||||||||
| 478 | 1 | ▶ | wantarray ? @{$this->{theta}} : $this->{theta}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | ################################################################ | ||||||||||||||||||||||||||||
| 482 | ### @se= $reg->standarderrors() | ||||||||||||||||||||||||||||
| 483 | ### | ||||||||||||||||||||||||||||
| 484 | ### This is the most difficult routine. Take it on faith. | ||||||||||||||||||||||||||||
| 485 | ### | ||||||||||||||||||||||||||||
| 486 | ### R=Rbar is an upperright triangular matrix, kept in normalized | ||||||||||||||||||||||||||||
| 487 | ### form with implicit 1's on the diagonal. D is a diagonal scaling | ||||||||||||||||||||||||||||
| 488 | ### matrix. These correspond to "standard Regression usage" as | ||||||||||||||||||||||||||||
| 489 | ### | ||||||||||||||||||||||||||||
| 490 | ### X' X = R' D R | ||||||||||||||||||||||||||||
| 491 | ### | ||||||||||||||||||||||||||||
| 492 | ### A backsubsitution routine (in thetacov) allows to invert the R | ||||||||||||||||||||||||||||
| 493 | ### matrix (the inverse is upper-right triangular, too!). Call this | ||||||||||||||||||||||||||||
| 494 | ### matrix H, that is H=R^(-1). | ||||||||||||||||||||||||||||
| 495 | ### | ||||||||||||||||||||||||||||
| 496 | ### (X' X)^(-1) = [(R' D^(1/2)') (D^(1/2) R)]^(-1) | ||||||||||||||||||||||||||||
| 497 | ### = [ R^-1 D^(-1/2) ] [ R^-1 D^(-1/2) ]' | ||||||||||||||||||||||||||||
| 498 | ### | ||||||||||||||||||||||||||||
| 499 | ### Let's work this for our example, where | ||||||||||||||||||||||||||||
| 500 | ### | ||||||||||||||||||||||||||||
| 501 | ### $reg->include( 2.0, [ 1.0, 3.0, -1.0 ] ); | ||||||||||||||||||||||||||||
| 502 | ### $reg->include( 1.0, [ 1.0, 5.0, 2.0 ] ); | ||||||||||||||||||||||||||||
| 503 | ### $reg->include( 20.0, [ 1.0, 31.0, 0.0 ] ); | ||||||||||||||||||||||||||||
| 504 | ### $reg->include( 15.0, [ 1.0, 11.0, 2.0 ] ); | ||||||||||||||||||||||||||||
| 505 | ### | ||||||||||||||||||||||||||||
| 506 | ### For debuggin, the X'X matrix for our example is | ||||||||||||||||||||||||||||
| 507 | ### 4, 50, 3 | ||||||||||||||||||||||||||||
| 508 | ### 50 1116 29 | ||||||||||||||||||||||||||||
| 509 | ### 3 29 9 | ||||||||||||||||||||||||||||
| 510 | ### | ||||||||||||||||||||||||||||
| 511 | ### Its inverse is | ||||||||||||||||||||||||||||
| 512 | ### 0.70967 -0.027992 -0.146360 | ||||||||||||||||||||||||||||
| 513 | ### -0.02799 0.002082 0.002622 | ||||||||||||||||||||||||||||
| 514 | ### -0.14636 0.002622 0.151450 | ||||||||||||||||||||||||||||
| 515 | ### | ||||||||||||||||||||||||||||
| 516 | ### Internally, this is kept as follows | ||||||||||||||||||||||||||||
| 517 | ### | ||||||||||||||||||||||||||||
| 518 | ### R is 1, 0, 0 | ||||||||||||||||||||||||||||
| 519 | ### 12.5 1 0 | ||||||||||||||||||||||||||||
| 520 | ### 0.75 -0.0173 1 | ||||||||||||||||||||||||||||
| 521 | ### | ||||||||||||||||||||||||||||
| 522 | ### d is the diagonal(4,491,6.603) matrix, which as 1/sqrt becomes dhi= 0.5, 0.04513, 0.3892 | ||||||||||||||||||||||||||||
| 523 | ### | ||||||||||||||||||||||||||||
| 524 | ### R * d * R' is indeed the X' X matrix. | ||||||||||||||||||||||||||||
| 525 | ### | ||||||||||||||||||||||||||||
| 526 | ### The inverse of R is | ||||||||||||||||||||||||||||
| 527 | ### | ||||||||||||||||||||||||||||
| 528 | ### 1, 0, 0 | ||||||||||||||||||||||||||||
| 529 | ### -12.5 1 0 | ||||||||||||||||||||||||||||
| 530 | ### -0.9664 0.01731 1 | ||||||||||||||||||||||||||||
| 531 | ### | ||||||||||||||||||||||||||||
| 532 | ### in R, t(solve(R) %*% dhi) %*% t( t(solve(R) %*% dhi) ) is the correct inverse. | ||||||||||||||||||||||||||||
| 533 | ### | ||||||||||||||||||||||||||||
| 534 | ### The routine has a debug switch which makes it come out very verbose. | ||||||||||||||||||||||||||||
| 535 | ################################################################ | ||||||||||||||||||||||||||||
| 536 | 1 | my $debug=0; | |||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | 0 | ▶ | sub standarderrors { | ||||||||||||||||||||||||||
|
Subroutine
standarderrors: not called CC=31 SCAR=69.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 539 | 0 | my $this= shift; | |||||||||||||||||||||||||||
| 540 | 0 | our $K= $this->{k}; # convenience | |||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | 0 | our @u; | |||||||||||||||||||||||||||
| 543 | 0 | ▶ | sub ui { | ||||||||||||||||||||||||||
|
Subroutine
ui: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 544 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 545 | 0 | ▶ | ($_[0]<1)||($_[0]>$K) and confess "$MNAME:standarderrors: bad index 0 $_[0]\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_[0] < 1 or $_[0] > $Statistics::Regression::K
MC/DC0%0/2
$_[0] < 1 or $_[0] > $Statistics::Regression::K
$_[0] < 1
$_[0] > $Statistics::Regression::K
Truth table0%0/3
A or B
A= $_[0] < 1B= $_[0] > $Statistics::Regression::K
| |||||||||||||||||||||||||||||
| 546 | 0 | ▶ | ($_[1]<1)||($_[1]>$K) and confess "$MNAME:standarderrors: bad index 1 $_[0]\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_[1] < 1 or $_[1] > $Statistics::Regression::K
MC/DC0%0/2
$_[1] < 1 or $_[1] > $Statistics::Regression::K
$_[1] < 1
$_[1] > $Statistics::Regression::K
Truth table0%0/3
A or B
A= $_[1] < 1B= $_[1] > $Statistics::Regression::K
| |||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||
| 548 | 0 | return (($K*($_[0]-1))+($_[1]-1)); | |||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||
| 550 | 0 | ▶ | sub giveuclear { | ||||||||||||||||||||||||||
|
Subroutine
giveuclear: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 551 | 0 | ▶ | for (my $i=0; $i<($K**2); ++$i) { $u[$i]=0.0; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 552 | 0 | ▶ | return (wantarray) ? @u : \@u; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | 0 | ▶ | sub u { return $u[ui($_[0], $_[1])]; } | ||||||||||||||||||||||||||
|
Subroutine
u: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 556 | 0 | ▶ | sub setu { return $u[ui($_[0], $_[1])]= $_[2]; } | ||||||||||||||||||||||||||
|
Subroutine
setu: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 557 | 0 | ▶ | sub add2u { return $u[ui($_[0], $_[1])]+= $_[2]; } | ||||||||||||||||||||||||||
|
Subroutine
add2u: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 558 | 0 | ▶ | sub mult2u { return $u[ui($_[0], $_[1])]*= $_[2]; } | ||||||||||||||||||||||||||
|
Subroutine
mult2u: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | 0 | ▶ | (defined($K)) or confess "$MNAME:standarderrors: Internal Error: I forgot the number of variables.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 561 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 562 | 0 | print "The Start Matrix is:\n"; | |||||||||||||||||||||||||||
| 563 | 0 | ▶ | for (my $i=1; $i<=$K; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 564 | 0 | print "[$i]:\t"; | |||||||||||||||||||||||||||
| 565 | 0 | ▶ | for (my $j=1; $j<=$K; ++$j) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 566 | 0 | print $this->rbr($i, $j)."\t"; | |||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||
| 568 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||
| 570 | 0 | print "The Start d vector is:\n"; | |||||||||||||||||||||||||||
| 571 | 0 | ▶ | for (my $i=1; $i<=$K; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 572 | 0 | print "".$this->{d}[$i]."\t"; | |||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||
| 574 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | 0 | ▶ | sub rbrindex { | ||||||||||||||||||||||||||
|
Subroutine
rbrindex: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 578 | 0 | ▶ | return ($_[0] == $_[1]) ? -9 : | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 579 | ($_[0]>$_[1]) ? -8 : | ||||||||||||||||||||||||||||
| 580 | ((($_[0]-1.0)* (2.0*$K-$_[0])/2.0+1.0) + $_[1] - 1 - $_[0] ); } | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | # now a real member routine; | ||||||||||||||||||||||||||||
| 583 | 0 | ▶ | sub rbr { | ||||||||||||||||||||||||||
|
Subroutine
rbr: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 584 | 0 | my $this= shift; | |||||||||||||||||||||||||||
| 585 | 0 | ▶ | return ($_[0] == $_[1]) ? 1 : ( ($_[0]>$_[1]) ? 0 : ($this->{rbar}[rbrindex($_[0],$_[1])])); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | 0 | my $u= giveuclear(); | |||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | 0 | ▶ | for (my $j=$K; $j>=1; --$j) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 591 | 0 | setu($j,$j, 1.0/($this->rbr($j,$j))); | |||||||||||||||||||||||||||
| 592 | 0 | ▶ | for (my $k=$j-1; $k>=1; --$k) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 593 | 0 | setu($k,$j,0); | |||||||||||||||||||||||||||
| 594 | 0 | ▶ | for (my $i=$k+1; $i<=$j; ++$i) { add2u($k,$j, $this->rbr($k,$i)*u($i,$j)); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 595 | 0 | mult2u($k,$j, (-1.0)/$this->rbr($k,$k)); | |||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 600 | 0 | print "The Inverse Matrix of R is:\n"; | |||||||||||||||||||||||||||
| 601 | 0 | ▶ | for (my $i=1; $i<=$K; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 602 | 0 | print "[$i]:\t"; | |||||||||||||||||||||||||||
| 603 | 0 | ▶ | for (my $j=1; $j<=$K; ++$j) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 604 | 0 | print $u[ui($i,$j)]."\t"; | |||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||
| 606 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | 0 | ▶ | for (my $i=1;$i<=$K;++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 611 | 0 | ▶ | for (my $j=1;$j<=$K;++$j) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 612 | 0 | ▶ | if (abs($this->{d}[$j])<TINY) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 613 | 0 | mult2u($i,$j, sqrt(1.0/TINY)); | |||||||||||||||||||||||||||
| 614 | 0 | ▶ | if (abs($this->{d}[$j])==0.0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 615 | 0 | ▶ | if ($this->{neverabort}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 616 | 0 | ▶ | for (my $i=0; $i<($K**2); ++$i) { $u[$i]= "NaN"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 617 | 0 | return undef; | |||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||
| 619 | 0 | else { confess "$MNAME:standarderrors: I cannot compute the theta-covariance matrix for variable $j ".($this->{d}[$j])."\n"; } | |||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||
| 622 | 0 | else { mult2u($i,$j, sqrt(1.0/$this->{d}[$j])); } | |||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||
| 624 | } | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 627 | 0 | print "The Inverse Matrix of R multipled by D^(-1/2) is:\n"; | |||||||||||||||||||||||||||
| 628 | 0 | ▶ | for (my $i=1; $i<=$K; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 629 | 0 | print "[$i]:\t"; | |||||||||||||||||||||||||||
| 630 | 0 | ▶ | for (my $j=1; $j<=$K; ++$j) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 631 | 0 | print $u[ui($i,$j)]."\t"; | |||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||
| 633 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | 0 | ▶ | $this->{sigmasq}= ($this->{n}<=$K) ? "Inf" : ($this->{sse}/($this->{n} - $K)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 638 | 0 | my @xpxinv; | |||||||||||||||||||||||||||
| 639 | 0 | ▶ | for (my $i=1;$i<=$K; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 640 | 0 | ▶ | for (my $j=$i;$j<=$K;++$j) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 641 | 0 | my $indexij= ui($i,$j); | |||||||||||||||||||||||||||
| 642 | 0 | $xpxinv[$indexij]= 0.0; | |||||||||||||||||||||||||||
| 643 | 0 | ▶ | for (my $k=1;$k<=$K;++$k) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 644 | 0 | $xpxinv[$indexij] += $u[ui($i,$k)]*$u[ui($j,$k)]; | |||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||
| 646 | 0 | $xpxinv[ui($j,$i)]= $xpxinv[$indexij]; # this is symmetric | |||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 651 | 0 | print "The full inverse matrix of X'X is:\n"; | |||||||||||||||||||||||||||
| 652 | 0 | ▶ | for (my $i=1; $i<=$K; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 653 | 0 | print "[$i]:\t"; | |||||||||||||||||||||||||||
| 654 | 0 | ▶ | for (my $j=1; $j<=$K; ++$j) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 655 | 0 | print $xpxinv[ui($i,$j)]."\t"; | |||||||||||||||||||||||||||
| 656 | } | ||||||||||||||||||||||||||||
| 657 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||||||||
| 659 | 0 | print "The sigma^2 is ".$this->{sigmasq}."\n"; | |||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | ## 99% of the usage here will be to print the diagonal elements of sqrt ( (X' X) sigma^2 ) | ||||||||||||||||||||||||||||
| 663 | ## so, let's make this our first returned object; | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | 0 | my @secoefs; | |||||||||||||||||||||||||||
| 666 | 0 | ▶ | for (my $i=1; $i<=$K; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 667 | 0 | $secoefs[$i-1]= sqrt($xpxinv[ui($i,$i)] * $this->{sigmasq}); | |||||||||||||||||||||||||||
| 668 | } | ||||||||||||||||||||||||||||
| 669 | 0 | ▶ | if ($debug) { for (my $i=0; $i<$K; ++$i) { print " $secoefs[$i] "; } print "\n"; } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | # the following are clever return methods; if the user goes over the secoefs, | ||||||||||||||||||||||||||||
| 672 | # almost surely an error will result, because he will run into xpxinv. For special | ||||||||||||||||||||||||||||
| 673 | # usage, however, xpxinv may still be useful. | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | 0 | return ( @secoefs, \@xpxinv, $this->sigmasq ); | |||||||||||||||||||||||||||
| 676 | } | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | ################################ | ||||||||||||||||||||||||||||
| 680 | 0 | ▶ | sub linearcombination_variance { | ||||||||||||||||||||||||||
|
Subroutine
linearcombination_variance: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 681 | 0 | my $this= shift; | |||||||||||||||||||||||||||
| 682 | 0 | our $K= $this->{k}; # convenience | |||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | 0 | my @linear= @_; | |||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | 0 | ▶ | ($#linear+1 == $K) or confess "$MNAME:linearcombination_variance: ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 687 | "Sorry, you must give a vector of length $K, not ".($#linear+1)."\n"; | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | 0 | my @allback= $this->standarderrors(); # compute everything we need; | |||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | 0 | my $xpxinv= $allback[$this->{k}]; | |||||||||||||||||||||||||||
| 692 | 0 | my $sigmasq= $allback[$this->{k}+1]; | |||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | 0 | my $sum=0; | |||||||||||||||||||||||||||
| 695 | 0 | ▶ | for (my $i=1; $i<=$K; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 696 | 0 | ▶ | for (my $j=1; $j<=$K; ++$j) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 697 | 0 | $sum+= $linear[$i-1]*$linear[$j-1]*$xpxinv->[ui($i,$j)]; | |||||||||||||||||||||||||||
| 698 | } | ||||||||||||||||||||||||||||
| 699 | } | ||||||||||||||||||||||||||||
| 700 | 0 | $sum*=$sigmasq; | |||||||||||||||||||||||||||
| 701 | 0 | return $sum; | |||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | ################################################################ | ||||||||||||||||||||||||||||
| 706 | ### sub dump() was used internally for debugging. | ||||||||||||||||||||||||||||
| 707 | ################################################################ | ||||||||||||||||||||||||||||
| 708 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=2 SCAR=16.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 709 | 0 | my $this= $_[0]; | |||||||||||||||||||||||||||
| 710 | 0 | print "****************************************************************\n"; | |||||||||||||||||||||||||||
| 711 | 0 | print "Regression '$this->{regname}'\n"; | |||||||||||||||||||||||||||
| 712 | 0 | print "****************************************************************\n"; | |||||||||||||||||||||||||||
| 713 | 0 | ▶ | sub print1val { | ||||||||||||||||||||||||||
|
Subroutine
print1val: not called CC=8 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 714 | 1 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 715 | 0 | ▶ | print "$_[1]($_[2])=\t". ((defined($_[0]->{ $_[2] }) ? $_[0]->{ $_[2] } : "intentionally undef")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | 0 | my $ref=$_[0]->{ $_[2] }; | |||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | 0 | ▶ | if (ref($ref) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 720 | 0 | my $arrayref= $ref; | |||||||||||||||||||||||||||
| 721 | 0 | print " $#$arrayref+1 elements:\n"; | |||||||||||||||||||||||||||
| 722 | 0 | ▶ | if ($#$arrayref>30) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 723 | 0 | print "\t"; | |||||||||||||||||||||||||||
| 724 | 0 | ▶ | for(my $i=0; $i<$#$arrayref+1; ++$i) { print "$i='$arrayref->[$i]';"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 725 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||
| 727 | else { | ||||||||||||||||||||||||||||
| 728 | 0 | ▶ | for(my $i=0; $i<$#$arrayref+1; ++$i) { print "\t$i=\t'$arrayref->[$i]'\n"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 729 | } | ||||||||||||||||||||||||||||
| 730 | } | ||||||||||||||||||||||||||||
| 731 | elsif (ref($ref) eq 'HASH') { | ||||||||||||||||||||||||||||
| 732 | 0 | my $hashref= $ref; | |||||||||||||||||||||||||||
| 733 | 0 | print " ".scalar(keys(%$hashref))." elements\n"; | |||||||||||||||||||||||||||
| 734 | 0 | ▶ | while (my ($key, $val) = each(%$hashref)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 735 | 0 | print "\t'$key'=>'$val';\n"; | |||||||||||||||||||||||||||
| 736 | } | ||||||||||||||||||||||||||||
| 737 | } | ||||||||||||||||||||||||||||
| 738 | else { | ||||||||||||||||||||||||||||
| 739 | 0 | print " [was scalar]\n"; } | |||||||||||||||||||||||||||
| 740 | } | ||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | 0 | ▶ | while (my ($key, $val) = each(%$this)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 743 | 0 | $this->print1val($key, $key); | |||||||||||||||||||||||||||
| 744 | } | ||||||||||||||||||||||||||||
| 745 | 0 | print "****************************************************************\n"; | |||||||||||||||||||||||||||
| 746 | } | ||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||
| 748 | ################################################################ | ||||||||||||||||||||||||||||
| 749 | ### The Test Program. Invoke as "perl Regression.pm". | ||||||||||||||||||||||||||||
| 750 | ################################################################ | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||
| 753 | 1 | ▶ | if ($0 eq "Regression.pm") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||
| 755 | # Create regression object | ||||||||||||||||||||||||||||
| 756 | 0 | my $reg = Statistics::Regression->new( "sample regression", [ "const", "someX", "someY" ] ); | |||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | # Add data points | ||||||||||||||||||||||||||||
| 759 | 0 | $reg->include( 2.0, [ 1.0, 3.0, -1.0 ] ); | |||||||||||||||||||||||||||
| 760 | 0 | $reg->include( 1.0, [ 1.0, 5.0, 2.0 ] ); | |||||||||||||||||||||||||||
| 761 | 0 | $reg->include( 20.0, [ 1.0, 31.0, 0.0 ] ); | |||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||
| 763 | 0 | my %inhash= ( const => 1.0, someX => 11.0, someY => 2.0, ignored => "ignored" ); | |||||||||||||||||||||||||||
| 764 | 0 | $reg->include( 15.0, \%inhash ); | |||||||||||||||||||||||||||
| 765 | # $reg->include( 15.0, [ 1.0, 11.0, 2.0 ] ); | ||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||
| 767 | # Print the result | ||||||||||||||||||||||||||||
| 768 | 0 | $reg->print(); | |||||||||||||||||||||||||||
| 769 | } | ||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||
| 772 | 1 | 1; | |||||||||||||||||||||||||||