Class: Statsample::Regression::Multiple::GslEngine
- Inherits:
-
BaseEngine
- Object
- BaseEngine
- Statsample::Regression::Multiple::GslEngine
- Defined in:
- lib/statsample/regression/multiple/gslengine.rb
Overview
Class for Multiple Regression Analysis Requires rbgsl and uses a listwise aproach. Slower on prediction of values than Alglib, because predict is ruby based. Better memory management on multiple (+1000) series of regression. 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::GslEngine.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
- #_dump(i) ⇒ Object
- #build_standarized ⇒ Object
- #coeffs ⇒ Object
-
#coeffs_se ⇒ Object
Standard error for coeffs.
- #constant ⇒ Object
-
#initialize(ds, y_var, opts = Hash.new) ⇒ GslEngine
constructor
A new instance of GslEngine.
- #lr_s ⇒ Object
-
#matrix_resolution ⇒ Object
Coefficients using a constant Based on www.xycoon.com/ols1.htm.
- #process_s(v) ⇒ Object
- #r ⇒ Object
- #r2 ⇒ Object
- #sst ⇒ Object
- #standarized_coeffs ⇒ Object
-
#standarized_residuals ⇒ Object
???? Not equal to SPSS output.
Methods inherited from BaseEngine
#anova, #assign_names, #coeffs_t, #coeffs_tolerances, #constant_se, #constant_t, #df_e, #df_r, #estimated_variance_covariance_matrix, #f, #mse, #msr, #predicted, #probability, #process, #r2_adjusted, #report_building, #residuals, #se_estimate, #se_r2, #sse, #sse_direct, #ssr, #ssr_direct, #standarized_predicted, #tolerance, univariate?
Methods included from Summarizable
Constructor Details
permalink #initialize(ds, y_var, opts = Hash.new) ⇒ GslEngine
Returns a new instance of GslEngine.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 20 def initialize(ds,y_var, opts=Hash.new) super @ds = ds.reject_values(*Daru::MISSING_VALUES) @ds_valid = @ds @valid_cases = @ds_valid.nrows @dy = @ds[@y_var] @ds_indep = ds.dup(ds.vectors.to_a - [y_var]) # Create a custom matrix columns=[] @fields=[] max_deps = GSL::Matrix.alloc(@ds.nrows, @ds.vectors.size) constant_col=@ds.vectors.size-1 for i in 0...@ds.nrows max_deps.set(i,constant_col,1) end j = 0 @ds.vectors.each do |f| if f != @y_var @ds[f].each_index do |i1| max_deps.set(i1,j,@ds[f][i1]) end columns.push(@ds[f].to_a) @fields.push(f) j += 1 end end @dep_columns = columns.dup @lr_s = nil c, @cov, @chisq, @status = GSL::MultiFit.linear(max_deps, @dy.to_gsl) @constant=c[constant_col] @coeffs_a=c.to_a.slice(0...constant_col) @coeffs=assign_names(@coeffs_a) c=nil end |
Class Method Details
permalink ._load(data) ⇒ Object
[View source] [View on GitHub]
59 60 61 62 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 59 def self._load(data) h=Marshal.load(data) self.new(h['ds'], h['y_var']) end |
Instance Method Details
permalink #_dump(i) ⇒ Object
[View source] [View on GitHub]
56 57 58 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 56 def _dump(i) Marshal.dump({'ds'=>@ds,'y_var'=>@y_var}) end |
permalink #build_standarized ⇒ Object
[View source] [View on GitHub]
100 101 102 103 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 100 def build_standarized @ds_s=@ds.standardize @lr_s=GslEngine.new(@ds_s,@y_var) end |
permalink #coeffs ⇒ Object
[View source] [View on GitHub]
64 65 66 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 64 def coeffs @coeffs end |
permalink #coeffs_se ⇒ Object
Standard error for coeffs
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 115 def coeffs_se out = {} evcm = estimated_variance_covariance_matrix @ds_valid.vectors.to_a.each_with_index do |f,i| mi = i+1 next if f == @y_var out[f] = evcm[mi,mi] end out end |
permalink #constant ⇒ Object
[View source] [View on GitHub]
87 88 89 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 87 def constant @constant end |
permalink #lr_s ⇒ Object
[View source] [View on GitHub]
94 95 96 97 98 99 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 94 def lr_s if @lr_s.nil? build_standarized end @lr_s end |
permalink #matrix_resolution ⇒ Object
Coefficients using a constant Based on www.xycoon.com/ols1.htm
69 70 71 72 73 74 75 76 77 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 69 def matrix_resolution 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 |
permalink #process_s(v) ⇒ Object
[View source] [View on GitHub]
104 105 106 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 104 def process_s(v) lr_s.process(v) end |
permalink #r ⇒ Object
[View source] [View on GitHub]
81 82 83 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 81 def r Bivariate::pearson(@dy, predicted) end |
permalink #r2 ⇒ Object
[View source] [View on GitHub]
78 79 80 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 78 def r2 r**2 end |
permalink #sst ⇒ Object
[View source] [View on GitHub]
84 85 86 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 84 def sst @dy.ss end |
permalink #standarized_coeffs ⇒ Object
[View source] [View on GitHub]
90 91 92 93 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 90 def standarized_coeffs l=lr_s l.coeffs end |
permalink #standarized_residuals ⇒ Object
???? Not equal to SPSS output
108 109 110 111 112 |
# File 'lib/statsample/regression/multiple/gslengine.rb', line 108 def standarized_residuals res=residuals red_sd=residuals.sds Daru::Vector.new(res.collect {|v| v.quo(red_sd) }) end |