[system] / trunk / pg / lib / Regression.pm Repository:
ViewVC logotype

Diff of /trunk/pg/lib/Regression.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1050 Revision 1079
1#!/usr/bin/perl -w 1
2 2
3package Regression; 3package Regression;
4 4
5$VERSION = 0.1; 5$VERSION = 0.1;
6 6
40 $reg->include( $y[$i], [ 1.0, $x1[$i], $x2[$i] ] ); 40 $reg->include( $y[$i], [ 1.0, $x1[$i], $x2[$i] ] );
41} 41}
42 42
43@coeff= $reg->theta(); 43@coeff= $reg->theta();
44 44
45$b0 = $coeff[0][0]; 45$b0 = $coeff[0][0];
46$b1 = $coeff[0][1]; 46$b1 = $coeff[0][1];
47$b2 = $coeff[0][2]; 47$b2 = $coeff[0][2];
48 48
49=head1 ALGORITHM 49=head1 ALGORITHM
50 50
51=head2 Original Algorithm (ALGOL-60): 51=head2 Original Algorithm (ALGOL-60):
93 93
94 94
95#### let's start with handling of missing data ("nan" or "NaN") 95#### let's start with handling of missing data ("nan" or "NaN")
96 96
97my $nan= "NaN"; 97my $nan= "NaN";
98sub isNaN { 98sub isNaN {
99 if ($_[0] !~ /[0-9nan]/) { die "definitely not a number in NaN: '$_[0]'"; } 99 if ($_[0] !~ /[0-9nan]/) { die "definitely not a number in NaN: '$_[0]'"; }
100 return ($_[0]=~ /NaN/i) || ($_[0] != $_[0]); 100 return ($_[0]=~ /NaN/i) || ($_[0] != $_[0]);
101} 101}
102 102
103 103
123 if (!defined($K)) { die "Regression->new needs at least one argument for the number of variables"; } 123 if (!defined($K)) { die "Regression->new needs at least one argument for the number of variables"; }
124 if ($K<=1) { die "Cannot run a regression without at least two variables."; } 124 if ($K<=1) { die "Cannot run a regression without at least two variables."; }
125 125
126 sub zerovec { 126 sub zerovec {
127 my @rv; 127 my @rv;
128 for (my $i=0; $i<=$_[0]; ++$i) { $rv[$i]=0; } 128 for (my $i=0; $i<=$_[0]; ++$i) { $rv[$i]=0; }
129 return \@rv; 129 return \@rv;
130 } 130 }
131 131
132 bless { 132 bless {
133 k => $K, 133 k => $K,
257 # omit observations with missing observations; 257 # omit observations with missing observations;
258 if (!defined($yelement)) { die "Internal Error: yelement is undef"; } 258 if (!defined($yelement)) { die "Internal Error: yelement is undef"; }
259 if (isNaN($yelement)) { return $this->{n}; } 259 if (isNaN($yelement)) { return $this->{n}; }
260 260
261 my @xcopy; 261 my @xcopy;
262 for (my $i=1; $i<=$this->{k}; ++$i) { 262 for (my $i=1; $i<=$this->{k}; ++$i) {
263 if (!defined($xrow->[$i-1])) { die "Internal Error: xrow [ $i-1 ] is undef"; } 263 if (!defined($xrow->[$i-1])) { die "Internal Error: xrow [ $i-1 ] is undef"; }
264 if (isNaN($xrow->[$i-1])) { return $this->{n}; } 264 if (isNaN($xrow->[$i-1])) { return $this->{n}; }
265 $xcopy[$i]= $xrow->[$i-1]; 265 $xcopy[$i]= $xrow->[$i-1];
266 } 266 }
267 267
418 my $reg= Statistics::Regression->new( 3, "sample regression", [ "const", "someX", "someY" ] ); 418 my $reg= Statistics::Regression->new( 3, "sample regression", [ "const", "someX", "someY" ] );
419 $reg->include( 2.0, [ 1.0, 3.0, -1.0 ] ); 419 $reg->include( 2.0, [ 1.0, 3.0, -1.0 ] );
420 $reg->include( 1.0, [ 1.0, 5.0, 2.0 ] ); 420 $reg->include( 1.0, [ 1.0, 5.0, 2.0 ] );
421 $reg->include( 20.0, [ 1.0, 31.0, 0.0 ] ); 421 $reg->include( 20.0, [ 1.0, 31.0, 0.0 ] );
422 $reg->include( 15.0, [ 1.0, 11.0, 2.0 ] ); 422 $reg->include( 15.0, [ 1.0, 11.0, 2.0 ] );
423 423
424# $reg->print(); or: my $coefs= $reg->theta(); print @coefs; print $reg->rsq; 424# $reg->print(); or: my $coefs= $reg->theta(); print @coefs; print $reg->rsq;
425# my $coefs= $reg->theta(); print $coeff[0]; 425# my $coefs= $reg->theta(); print $coeff[0];
426} 426}
427 427
428################################################################ 428################################################################
453=back 453=back
454 454
455=head1 INSTALLATION and DOCUMENTATION 455=head1 INSTALLATION and DOCUMENTATION
456 456
457Installation consists of moving the file 'Regression.pm' into a subdirectory 457Installation consists of moving the file 'Regression.pm' into a subdirectory
458Statistics of your modules path (e.g., /usr/lib/perl5/site_perl/5.6.0/). 458Statistics of your modules path (e.g., /usr/lib/perl5/site_perl/5.6.0/).
459 459
460The documentation was produced from the module: 460The documentation was produced from the module:
461 461
462pod2html -noindex -title "perl weighted least squares regression package" Regression.pm > Regression.html 462pod2html -noindex -title "perl weighted least squares regression package" Regression.pm > Regression.html
463 463

Legend:
Removed from v.1050  
changed lines
  Added in v.1079

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9