Class: Statsample::Regression::Simple
- Inherits:
-
Object
- Object
- Statsample::Regression::Simple
- Defined in:
- lib/statsample/regression/simple.rb
Overview
Class for calculation of linear regressions with form
y = a+bx
To create a SimpleRegression object:
-
SimpleRegression.new_from_dataset(ds,x,y) -
SimpleRegression.new_from_vectors(vx,vy) -
SimpleRegression.new_from_gsl(gsl)
Instance Attribute Summary collapse
-
#a ⇒ Object
Returns the value of attribute a.
-
#b ⇒ Object
Returns the value of attribute b.
-
#chisq ⇒ Object
Returns the value of attribute chisq.
-
#cov00 ⇒ Object
Returns the value of attribute cov00.
-
#cov01 ⇒ Object
Returns the value of attribute cov01.
-
#covx1 ⇒ Object
Returns the value of attribute covx1.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
-
.new_from_dataset(ds, x, y) ⇒ Object
Create a simple regression using a dataset and two vector names.
-
.new_from_gsl(ar) ⇒ Object
Create a regression object giving an array with following parameters:
a,b,cov00, cov01, covx1, chisq, statusUseful to obtain x and y values with a and b values. -
.new_from_vectors(vx, vy) ⇒ Object
Create a simple regression using two vectors.
Instance Method Summary collapse
-
#initialize(init_method, *argv) ⇒ Simple
constructor
A new instance of Simple.
-
#r ⇒ Object
Value of r.
-
#r2 ⇒ Object
Value of r^2.
-
#sse ⇒ Object
Sum of square error.
-
#ssr ⇒ Object
Sum of square regression.
-
#sst ⇒ Object
Sum of square total.
- #standard_error ⇒ Object
-
#x(val_y) ⇒ Object
Obtain x value given y value x=(y-a)/b.
-
#y(val_x) ⇒ Object
Obtain y value given x value x=a+bx.
Constructor Details
#initialize(init_method, *argv) ⇒ Simple
Returns a new instance of Simple.
13 14 15 |
# File 'lib/statsample/regression/simple.rb', line 13 def initialize(init_method, *argv) self.send(init_method, *argv) end |
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
11 12 13 |
# File 'lib/statsample/regression/simple.rb', line 11 def a @a end |
#b ⇒ Object
Returns the value of attribute b.
11 12 13 |
# File 'lib/statsample/regression/simple.rb', line 11 def b @b end |
#chisq ⇒ Object
Returns the value of attribute chisq.
11 12 13 |
# File 'lib/statsample/regression/simple.rb', line 11 def chisq @chisq end |
#cov00 ⇒ Object
Returns the value of attribute cov00.
11 12 13 |
# File 'lib/statsample/regression/simple.rb', line 11 def cov00 @cov00 end |
#cov01 ⇒ Object
Returns the value of attribute cov01.
11 12 13 |
# File 'lib/statsample/regression/simple.rb', line 11 def cov01 @cov01 end |
#covx1 ⇒ Object
Returns the value of attribute covx1.
11 12 13 |
# File 'lib/statsample/regression/simple.rb', line 11 def covx1 @covx1 end |
#status ⇒ Object
Returns the value of attribute status.
11 12 13 |
# File 'lib/statsample/regression/simple.rb', line 11 def status @status end |
Class Method Details
.new_from_dataset(ds, x, y) ⇒ Object
Create a simple regression using a dataset and two vector names.
68 69 70 |
# File 'lib/statsample/regression/simple.rb', line 68 def new_from_dataset(ds,x,y) new(:init_vectors,ds[x],ds[y]) end |
.new_from_gsl(ar) ⇒ Object
Create a regression object giving an array with following parameters: a,b,cov00, cov01, covx1, chisq, status Useful to obtain x and y values with a and b values.
60 61 62 |
# File 'lib/statsample/regression/simple.rb', line 60 def new_from_gsl(ar) new(:init_gsl, *ar) end |
.new_from_vectors(vx, vy) ⇒ Object
Create a simple regression using two vectors
64 65 66 |
# File 'lib/statsample/regression/simple.rb', line 64 def new_from_vectors(vx,vy) new(:init_vectors,vx,vy) end |
Instance Method Details
#r ⇒ Object
Value of r
49 50 51 |
# File 'lib/statsample/regression/simple.rb', line 49 def r @b * (@vx.sds / @vy.sds) end |
#r2 ⇒ Object
Value of r^2
53 54 55 |
# File 'lib/statsample/regression/simple.rb', line 53 def r2 r**2 end |
#sse ⇒ Object
Sum of square error
29 30 31 32 |
# File 'lib/statsample/regression/simple.rb', line 29 def sse (0...@vx.size).inject(0) {|acum,i| acum+((@vy[i]-y(@vx[i]))**2) } end |
#ssr ⇒ Object
Sum of square regression
37 38 39 40 41 42 43 |
# File 'lib/statsample/regression/simple.rb', line 37 def ssr vy_mean=@vy.mean (0...@vx.size).inject(0) {|a,i| a+((y(@vx[i])-vy_mean)**2) } end |
#sst ⇒ Object
Sum of square total
45 46 47 |
# File 'lib/statsample/regression/simple.rb', line 45 def sst @vy.sum_of_squared_deviation end |
#standard_error ⇒ Object
33 34 35 |
# File 'lib/statsample/regression/simple.rb', line 33 def standard_error Math::sqrt(sse / (@vx.size-2).to_f) end |
#x(val_y) ⇒ Object
Obtain x value given y value x=(y-a)/b
25 26 27 |
# File 'lib/statsample/regression/simple.rb', line 25 def x(val_y) (val_y-@a) / @b.to_f end |
#y(val_x) ⇒ Object
Obtain y value given x value x=a+bx
20 21 22 |
# File 'lib/statsample/regression/simple.rb', line 20 def y(val_x) @a+@b*val_x end |