in reply to finding R-squared..please help
Don't reinvent the wheel. Use one of the existing libraries/packages for this. For example Math::GSL, which is a binding to the GSL library.
The R-squared for a linear least squares fit of two vectors @$x, @$y (with x predicting y) can be computed as follows:
#!/usr/bin/perl -w use strict; use Math::GSL::Fit "gsl_fit_linear"; use Math::GSL::Statistics "gsl_stats_tss"; my $x = [1,2,3,4,5]; my $y = [5,7,8,12,13]; my $n = @$y; my ($status, $c0, $c1, $cov00, $cov01, $cov11, $ss_resid) = gsl_fit_linear($x, 1, $y, 1, $n); my $ss_total = gsl_stats_tss($y, 1, $n); print "SS residual = $ss_resid\n"; print "SS total = $ss_total\n"; my $R2 = 1 - $ss_resid / $ss_total; print "R^2 = $R2\n";
SS residual = 1.9 SS total = 46 R^2 = 0.958695652173913
If I'm understanding you correctly, you'd want to do this computation for every pair of lines.
(Note that for R^2 to be computable, there has to be some variance in the data to be predicted (as often with statistics). In other words, $y = [2,2,2,2,2,2] wouldn't work, because here $ss_total would be 0, which would cause a division by zero error.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: finding R-squared..please help
by david_lyon (Sexton) on Feb 19, 2012 at 23:51 UTC | |
by Eliya (Vicar) on Feb 20, 2012 at 01:10 UTC | |
by david_lyon (Sexton) on Feb 20, 2012 at 16:36 UTC |