Class: Statsample::Regression::Simple

Inherits:
Object
  • Object
show all
Includes:
Summarizable
Defined in:
lib/statsample/regression/simple.rb

Overview

Class for calculation of linear regressions with form

y = a+bx

To create a Statsample::Regression::Simple object:

  • Statsample::Regression::Simple.new_from_dataset(ds,x,y)

  • Statsample::Regression::Simple.new_from_vectors(vx,vy)

  • Statsample::Regression::Simple.new_from_gsl(gsl)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Summarizable

#summary

Constructor Details

#initialize(init_method, *argv) ⇒ Simple

Returns a new instance of Simple.



15
16
17
# File 'lib/statsample/regression/simple.rb', line 15

def initialize(init_method, *argv)
  self.send(init_method, *argv)
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



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

def a
  @a
end

#bObject

Returns the value of attribute b.



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

def b
  @b
end

#chisqObject

Returns the value of attribute chisq.



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

def chisq
  @chisq
end

#cov00Object

Returns the value of attribute cov00.



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

def cov00
  @cov00
end

#cov01Object

Returns the value of attribute cov01.



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

def cov01
  @cov01
end

#covx1Object

Returns the value of attribute covx1.



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

def covx1
  @covx1
end

#digitsObject

Returns the value of attribute digits.



14
15
16
# File 'lib/statsample/regression/simple.rb', line 14

def digits
  @digits
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/statsample/regression/simple.rb', line 13

def name
  @name
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Class Method Details

.new_from_dataset(ds, x, y, opts = Hash.new) ⇒ Object

Create a simple regression using a dataset and two vector names.



70
71
72
# File 'lib/statsample/regression/simple.rb', line 70

def new_from_dataset(ds,x,y, opts=Hash.new)
  new(:init_vectors,ds[x],ds[y], opts)
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.



62
63
64
# File 'lib/statsample/regression/simple.rb', line 62

def new_from_gsl(ar)
  new(:init_gsl, *ar)
end

.new_from_vectors(vx, vy, opts = Hash.new) ⇒ Object

Create a simple regression using two vectors



66
67
68
# File 'lib/statsample/regression/simple.rb', line 66

def new_from_vectors(vx,vy, opts=Hash.new)
  new(:init_vectors,vx,vy, opts)
end

Instance Method Details

#rObject

Value of r



51
52
53
# File 'lib/statsample/regression/simple.rb', line 51

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

#r2Object

Value of r^2



55
56
57
# File 'lib/statsample/regression/simple.rb', line 55

def r2
  r**2
end

#report_building(gen) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/statsample/regression/simple.rb', line 106

def report_building(gen)
f="%0.#{digits}f"
  gen.section(:name=>name) do |s|
    s.table(:header=>[_("Variable"), _("Value")]) do |t|
      t.row [_("r"), f % r]
      t.row [_("r^2"), f % r2]
      t.row [_("a"), f % a]
      t.row [_("b"), f % b]
      t.row [_("s.e"), f % standard_error]
    end
  end
end

#sseObject

Sum of square error



31
32
33
34
# File 'lib/statsample/regression/simple.rb', line 31

def sse
  (0...@vx.size).inject(0) {|acum,i| acum+((@vy[i]-y(@vx[i]))**2)
  }
end

#ssrObject

Sum of square regression



39
40
41
42
43
44
45
# File 'lib/statsample/regression/simple.rb', line 39

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



47
48
49
# File 'lib/statsample/regression/simple.rb', line 47

def sst
  @vy.sum_of_squared_deviation
end

#standard_errorObject



35
36
37
# File 'lib/statsample/regression/simple.rb', line 35

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



27
28
29
# File 'lib/statsample/regression/simple.rb', line 27

def x(val_y)
  (val_y-@a) / @b.to_f
end

#y(val_x) ⇒ Object

Obtain y value given x value x=a+bx



22
23
24
# File 'lib/statsample/regression/simple.rb', line 22

def y(val_x)
  @a+@b*val_x
end