Class: LinearRegression
- Inherits:
-
Object
- Object
- LinearRegression
- Defined in:
- lib/plugins/open_flash_chart/lib/open_flash_chart/linear_regression.rb
Overview
by David Lowenfels @ InternautDesign.com example usage: fitted_data = LinearRegression.new(data).fit
Instance Attribute Summary (collapse)
-
- (Object) offset
Returns the value of attribute offset.
-
- (Object) slope
Returns the value of attribute slope.
Instance Method Summary (collapse)
- - (Object) fit
-
- (LinearRegression) initialize(dx, dy = nil)
constructor
A new instance of LinearRegression.
Constructor Details
- (LinearRegression) initialize(dx, dy = nil)
Returns a new instance of LinearRegression
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/plugins/open_flash_chart/lib/open_flash_chart/linear_regression.rb', line 7 def initialize dx, dy=nil @size = dx.size if @size == 1 @slope, @offset = 1,0 return end dy,dx = dx,axis() unless dy # make 2D if given 1D raise "arguments not same length!" unless @size == dy.size sxx = sxy = sx = sy = 0 dx.zip(dy).each do |x,y| sxy += x*y sxx += x*x sx += x sy += y end @slope = ( @size * sxy - sx*sy ) / ( @size * sxx - sx * sx ) @offset = (sy - @slope*sx) / @size end |
Instance Attribute Details
- (Object) offset
Returns the value of attribute offset
5 6 7 |
# File 'lib/plugins/open_flash_chart/lib/open_flash_chart/linear_regression.rb', line 5 def offset @offset end |
- (Object) slope
Returns the value of attribute slope
5 6 7 |
# File 'lib/plugins/open_flash_chart/lib/open_flash_chart/linear_regression.rb', line 5 def slope @slope end |
Instance Method Details
- (Object) fit
26 27 28 |
# File 'lib/plugins/open_flash_chart/lib/open_flash_chart/linear_regression.rb', line 26 def fit return axis.map{|data| predict(data) } end |