Class: Statsample::Regression::Simple

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#aObject

Returns the value of attribute a.



11
12
13
# File 'lib/statsample/regression/simple.rb', line 11

def a
  @a
end

#bObject

Returns the value of attribute b.



11
12
13
# File 'lib/statsample/regression/simple.rb', line 11

def b
  @b
end

#chisqObject

Returns the value of attribute chisq.



11
12
13
# File 'lib/statsample/regression/simple.rb', line 11

def chisq
  @chisq
end

#cov00Object

Returns the value of attribute cov00.



11
12
13
# File 'lib/statsample/regression/simple.rb', line 11

def cov00
  @cov00
end

#cov01Object

Returns the value of attribute cov01.



11
12
13
# File 'lib/statsample/regression/simple.rb', line 11

def cov01
  @cov01
end

#covx1Object

Returns the value of attribute covx1.



11
12
13
# File 'lib/statsample/regression/simple.rb', line 11

def covx1
  @covx1
end

#statusObject

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

#rObject

Value of r



49
50
51
# File 'lib/statsample/regression/simple.rb', line 49

def r
  @b * (@vx.sds / @vy.sds)
end

#r2Object

Value of r^2



53
54
55
# File 'lib/statsample/regression/simple.rb', line 53

def r2
  r**2
end

#sseObject

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

#ssrObject

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

#sstObject

Sum of square total



45
46
47
# File 'lib/statsample/regression/simple.rb', line 45

def sst
  @vy.sum_of_squared_deviation
end

#standard_errorObject



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