Class: Alglib::LinearRegression

Inherits:
Object
  • Object
show all
Defined in:
lib/alglib/linearregression.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lm, lr, cases, ivars) ⇒ LinearRegression

Use with care…



24
25
26
27
28
29
# File 'lib/alglib/linearregression.rb', line 24

def initialize(lm,lr,cases,ivars)
    @model=lm
    @report=lr
    @cases=cases
    @ivars=ivars
end

Instance Attribute Details

#casesObject (readonly)

Returns the value of attribute cases.



3
4
5
# File 'lib/alglib/linearregression.rb', line 3

def cases
  @cases
end

#ivarsObject (readonly)

Returns the value of attribute ivars.



3
4
5
# File 'lib/alglib/linearregression.rb', line 3

def ivars
  @ivars
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/alglib/linearregression.rb', line 3

def model
  @model
end

Class Method Details

.build_from_matrix(matrix) ⇒ Object

Creates a Linear Regression object based on a Matrix EXAMPLE:

require 'alglib'
matrix=Matrix[[2,3,4],[2,5,5],[1,5,3],[4,6,5]]
lr=Alglib::LinearRegression.build_from_matrix(matrix)
  => #<Alglib::LinearRegression:0x7ffaf6c05dc0 @model=#<Alglib_ext::LinearModel:0x7ffaf6c05e60>, @cases=4, @report=#<Alglib_ext::LrReport:0x7ffaf6c05e10>, @ivars=2>
  lr.coeffs
  => [0.585714285714286, -0.0142857142857142]


13
14
15
16
17
18
19
20
21
22
# File 'lib/alglib/linearregression.rb', line 13

def self.build_from_matrix(matrix)
    raise "Argument should be a matrix" unless matrix.is_a? Matrix
    cases=matrix.row_size
    ivars=matrix.column_size-1
    lm=Alglib_ext::LinearModel.new
    lr=Alglib_ext::LrReport.new
    am=matrix.to_alglib_matrix
    Alglib_ext::lrbuild(am,cases,ivars,lm,lr)
    self.new(lm,lr,cases,ivars)
end

Instance Method Details

#coeffsObject

Array with b coeffs.



39
40
41
42
43
# File 'lib/alglib/linearregression.rb', line 39

def coeffs
    v=Alglib_ext::Real1dArray.new
    Alglib_ext::lrunpack(@model,v, @ivars);
    Alglib_ext.real1d_to_array(v)[0,v.size]
end

#constantObject

Constant value. a on y= a+b1x1+b2x2…



33
34
35
36
37
# File 'lib/alglib/linearregression.rb', line 33

def constant
    v=Alglib_ext::Real1dArray.new
    Alglib_ext::lrunpack(@model,v, @ivars);
    Alglib_ext.real1d_to_array(v).slice(-1)
end

#process(vector) ⇒ Object

Predict an y value based on an array of predictors.



45
46
47
# File 'lib/alglib/linearregression.rb', line 45

def process(vector)
    Alglib_ext::lrprocess(@model,vector);
end

#reportObject

A wrapper for lm_report



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/alglib/linearregression.rb', line 49

def report
    {:c=>@report.c,
        :rmserror=>@report.rmserror,
        :avgerror=>@report.avgerror,
        :avgrelerror=>@report.avgrelerror,
        :cvrmserror=>@report.cvrmserror,
        :cvavgerror=>@report.cvavgerror,
        :cvavgrelerror=>@report.cvavgrelerror,
        :ncvdefects=>@report.ncvdefects,
        :cvdefects=>@report.cvdefects
    }
end