Class: Statsample::Regression::Multiple::AlglibEngine

Inherits:
BaseEngine show all
Defined in:
lib/statsample/regression/multiple/alglibengine.rb

Overview

Class for Multiple Regression Analysis Requires Alglib gem and uses a listwise aproach. Faster than GslEngine on massive prediction use, because process is c-based. Prefer GslEngine if you need good memory use. If you need pairwise, use RubyEngine Example:

@a = Daru::Vector.new([1,3,2,4,3,5,4,6,5,7])
@b = Daru::Vector.new([3,3,4,4,5,5,6,6,4,4])
@c = Daru::Vector.new([11,22,30,40,50,65,78,79,99,100])
@y = Daru::Vector.new([3,4,5,6,7,8,9,10,20,30])
ds = Daru::DataFrame.new({:a => @a,:b => @b,:c => @c,:y => @y})
lr=Statsample::Regression::Multiple::AlglibEngine.new(ds, :y)

Instance Attribute Summary

Attributes inherited from BaseEngine

#cases, #digits, #name, #total_cases, #valid_cases

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseEngine

#anova, #assign_names, #coeffs_se, #coeffs_t, #coeffs_tolerances, #constant_se, #constant_t, #df_e, #df_r, #estimated_variance_covariance_matrix, #f, #mse, #msr, #predicted, #probability, #r2_adjusted, #report_building, #residuals, #se_estimate, #se_r2, #sse, #sse_direct, #ssr, #ssr_direct, #standarized_predicted, #tolerance, univariate?

Methods included from Summarizable

#summary

Constructor Details

#initialize(ds, y_var, opts = Hash.new) ⇒ AlglibEngine

Returns a new instance of AlglibEngine


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 20

def initialize(ds,y_var, opts=Hash.new)
  super    
  @ds       = ds.dup_only_valid
  @ds_valid = @ds
  @dy       = @ds[@y_var]
  @ds_indep = ds.dup(ds.vectors.to_a - [y_var])
  # Create a custom matrix
  columns = []
  @fields = []
  @ds.vectors.each do |f|
    if f != @y_var
      columns.push(@ds[f].to_a)
      @fields.push(f)
    end
  end
  @dep_columns = columns.dup
  columns.push(@ds[@y_var])
  matrix=Matrix.columns(columns)
  @lr_s=nil
  @lr=::Alglib::LinearRegression.build_from_matrix(matrix)
  @coeffs=assign_names(@lr.coeffs)
end

Class Method Details

._load(data) ⇒ Object


47
48
49
50
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 47

def self._load(data)
  h=Marshal.load(data)
  self.new(h['ds'], h['y_var'])
end

Instance Method Details

#_dump(i) ⇒ Object


43
44
45
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 43

def _dump(i)
  Marshal.dump({'ds'=>@ds,'y_var'=>@y_var})
end

#build_standarizedObject


96
97
98
99
100
101
102
103
104
105
106
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 96

def build_standarized
  @ds_s=@ds.standardize
  columns=[]
  @ds_s.vectors.each{|f|
    columns.push(@ds_s[f].to_a) unless f == @y_var
  }
  @dep_columns_s=columns.dup
  columns.push(@ds_s[@y_var])
  matrix=Matrix.columns(columns)
  @lr_s=Alglib::LinearRegression.build_from_matrix(matrix)
end

#coeffsObject


52
53
54
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 52

def coeffs
  @coeffs
end

#constantObject


80
81
82
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 80

def constant
  @lr.constant
end

#lr_sObject


89
90
91
92
93
94
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 89

def lr_s
  if @lr_s.nil?
    build_standarized
  end
  @lr_s
end

#matrix_resolutionObject

Coefficients using a constant Based on www.xycoon.com/ols1.htm


57
58
59
60
61
62
63
64
65
66
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 57

def matrix_resolution
  mse_p=mse
  columns=@dep_columns.dup.map {|xi| xi.map{|i| i.to_f}}
  columns.unshift([1.0]*@ds.cases)
  y=Matrix.columns([@dy.data.map  {|i| i.to_f}])
  x=Matrix.columns(columns)
  xt=x.t
  matrix=((xt*x)).inverse*xt
  matrix*y
end

#process(v) ⇒ Object


108
109
110
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 108

def process(v)
  @lr.process(v)
end

#process_s(v) ⇒ Object


112
113
114
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 112

def process_s(v)
  lr_s.process(v)
end

#rObject


72
73
74
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 72

def r
  Bivariate::pearson(@dy,predicted)
end

#r2Object


68
69
70
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 68

def r2
  r**2
end

#sstObject


76
77
78
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 76

def sst
  @dy.ss
end

#standarized_coeffsObject


84
85
86
87
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 84

def standarized_coeffs
  l=lr_s
  assign_names(l.coeffs)
end

#standarized_residualsObject

???? Not equal to SPSS output


116
117
118
119
120
# File 'lib/statsample/regression/multiple/alglibengine.rb', line 116

def standarized_residuals
  res    = residuals
  red_sd = residuals.sds
  Daru::Vector.new(res.collect {|v| v.quo(red_sd) })
end