Class: Rust::Models::Regression::RegressionModel
- Inherits:
-
RustDatatype
- Object
- RustDatatype
- Rust::Models::Regression::RegressionModel
- Defined in:
- lib/rust/models/regression.rb
Overview
Generic regression model in R.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#dependent_variable ⇒ Object
Returns the value of attribute dependent_variable.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
- .can_pull?(type, klass) ⇒ Boolean
-
.generate(object_type, model_type, dependent_variable, independent_variables, data, **options) ⇒ Object
Generates a new regression model.
Instance Method Summary collapse
-
#actuals ⇒ Object
Returns the actual values in the dataset.
-
#backward_selection(excluded = []) ⇒ Object
Runs backward selection (recursively removes a variable until the best model is found).
-
#coefficients ⇒ Object
Returns the coefficients of the model.
-
#fitted ⇒ Object
Returns the fitted values of the model.
-
#initialize(model) ⇒ RegressionModel
constructor
Creates a new model based on
model
. - #load_in_r_as(variable_name) ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #model ⇒ Object
-
#mse ⇒ Object
Returns the mean squared error of the model.
-
#r_2 ⇒ Object
Returns the r-squared of the model.
-
#r_2_adjusted ⇒ Object
Returns the adjusted r-squared of the model.
- #r_hash ⇒ Object
-
#residuals ⇒ Object
Returns the residuals of the model.
-
#significant_variables(a = 0.05) ⇒ Object
Returns only the significant variables as ModelVariable instances.
-
#summary ⇒ Object
Returns a summary for the model using the summary function in R.
-
#variables ⇒ Object
Returns object variables for the model with basic data (coefficients and p-values).
Methods inherited from RustDatatype
pull_priority, pull_variable, #r_mirror, #r_mirror_to
Constructor Details
#initialize(model) ⇒ RegressionModel
Creates a new model based on model
.
68 69 70 71 |
# File 'lib/rust/models/regression.rb', line 68 def initialize(model) raise "Expected a R list, given a #{model.class}" if !model.is_a?(Rust::List) @model = model end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
186 187 188 |
# File 'lib/rust/models/regression.rb', line 186 def method_missing(name, *args) return model|name.to_s end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
18 19 20 |
# File 'lib/rust/models/regression.rb', line 18 def data @data end |
#dependent_variable ⇒ Object
Returns the value of attribute dependent_variable.
19 20 21 |
# File 'lib/rust/models/regression.rb', line 19 def dependent_variable @dependent_variable end |
#options ⇒ Object
Returns the value of attribute options.
20 21 22 |
# File 'lib/rust/models/regression.rb', line 20 def end |
Class Method Details
.can_pull?(type, klass) ⇒ Boolean
22 23 24 25 |
# File 'lib/rust/models/regression.rb', line 22 def self.can_pull?(type, klass) # Can only pull specific sub-types return false end |
.generate(object_type, model_type, dependent_variable, independent_variables, data, **options) ⇒ Object
Generates a new regression model. object_type
is the Ruby class of the model object; model_type
represents the type of model at hand; dependent_variable
and independent_variables
are directly used as part of the model formula. data
represents the dataset to be used. options
can be specified and directly passed to the model.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rust/models/regression.rb', line 37 def self.generate(object_type, model_type, dependent_variable, independent_variables, data, **) mapped = "" if .size > 0 mapped = .map { |k, v| "#{k}=#{v}" }.join(", ") mapped = ", " + mapped end formula = Rust::Formula.new(dependent_variable, independent_variables.join(" + ")) result = nil Rust.exclusive do Rust["#{model_type}.data"] = data Rust._eval("#{model_type}.model.result <- #{model_type}(#{formula.to_R}, data=#{model_type}.data#{mapped})") result = Rust["#{model_type}.model.result"] raise "An error occurred while building the model" unless result result.r_mirror_to("#{model_type}.model.result") end result.dependent_variable = dependent_variable result.data = data result. = return result end |
Instance Method Details
#actuals ⇒ Object
Returns the actual values in the dataset.
102 103 104 |
# File 'lib/rust/models/regression.rb', line 102 def actuals return self.fitted.zip(self.residuals).map { |couple| couple.sum } end |
#backward_selection(excluded = []) ⇒ Object
Runs backward selection (recursively removes a variable until the best model is found). Returns both the best model and the list of excluded variable at each step Note: Not fully tested
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/rust/models/regression.rb', line 162 def backward_selection(excluded = []) candidates = self.variables.select { |v| !v.intercept? && !v.significant? }.sort_by { |v| v.pvalue }.reverse all = self.variables.select { |v| !v.intercept? } candidates.each do |candidate| new_model = RegressionModel.generate( self.class, self.class.r_model_name, self.dependent_variable, (all - [candidate]).map { |v| v.name }, self.data, **self. ) if new_model.r_2_adjusted >= self.r_2_adjusted puts "Excluded #{candidate}" if Rust.debug? return *new_model.backward_selection(excluded + [candidate]) end end return self, excluded end |
#coefficients ⇒ Object
Returns the coefficients of the model.
130 131 132 |
# File 'lib/rust/models/regression.rb', line 130 def coefficients a = self.summary|"coefficients" end |
#fitted ⇒ Object
Returns the fitted values of the model.
91 92 93 94 95 96 97 |
# File 'lib/rust/models/regression.rb', line 91 def fitted Rust.exclusive do @fitted = Rust["fitted(#{self.r_mirror})"] unless @fitted end return @fitted end |
#load_in_r_as(variable_name) ⇒ Object
27 28 29 |
# File 'lib/rust/models/regression.rb', line 27 def load_in_r_as(variable_name) @model.load_in_r_as(variable_name) end |
#model ⇒ Object
73 74 75 |
# File 'lib/rust/models/regression.rb', line 73 def model @model end |
#mse ⇒ Object
Returns the mean squared error of the model.
123 124 125 |
# File 'lib/rust/models/regression.rb', line 123 def mse Rust::Descriptive.variance(self.residuals) end |
#r_2 ⇒ Object
Returns the r-squared of the model.
109 110 111 |
# File 'lib/rust/models/regression.rb', line 109 def r_2 return self.summary|"r.squared" end |
#r_2_adjusted ⇒ Object
Returns the adjusted r-squared of the model.
116 117 118 |
# File 'lib/rust/models/regression.rb', line 116 def r_2_adjusted return self.summary|"adj.r.squared" end |
#r_hash ⇒ Object
203 204 205 |
# File 'lib/rust/models/regression.rb', line 203 def r_hash @model.r_hash end |
#residuals ⇒ Object
Returns the residuals of the model.
80 81 82 83 84 85 86 |
# File 'lib/rust/models/regression.rb', line 80 def residuals Rust.exclusive do @residuals = Rust["residuals(#{self.r_mirror})"] unless @residuals end return @residuals end |
#significant_variables(a = 0.05) ⇒ Object
Returns only the significant variables as ModelVariable instances. See the method ‘variables`.
153 154 155 |
# File 'lib/rust/models/regression.rb', line 153 def significant_variables(a = 0.05) self.variables.select { |v| v.significant?(a) } end |
#summary ⇒ Object
Returns a summary for the model using the summary function in R.
193 194 195 196 197 198 199 200 201 |
# File 'lib/rust/models/regression.rb', line 193 def summary unless @summary Rust.exclusive do @summary = Rust["summary(#{self.r_mirror})"] end end return @summary end |
#variables ⇒ Object
Returns object variables for the model with basic data (coefficients and p-values). Use the method ‘coefficients` to get more data.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rust/models/regression.rb', line 138 def variables unless @variables coefficients = self.coefficients @variables = coefficients.rownames.map do |name| ModelVariable.new(name, coefficients[name, "Estimate"], coefficients[name, "Pr(>|t|)"]) end end return @variables end |