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
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.
-
#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
. - #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.
-
#summary ⇒ Object
Returns a summary for the model using the summary function in R.
Methods inherited from RustDatatype
pull_priority, pull_variable, #r_mirror, #r_mirror_to
Constructor Details
#initialize(model) ⇒ RegressionModel
Creates a new model
.
55 56 57 58 |
# File 'lib/rust/models/regression.rb', line 55 def initialize(model) raise StandardError if model.is_a?(RegressionModel) @model = model end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
121 122 123 |
# File 'lib/rust/models/regression.rb', line 121 def method_missing(name, *args) return model|name.to_s end |
Class Method Details
.can_pull?(type, klass) ⇒ Boolean
17 18 19 20 |
# File 'lib/rust/models/regression.rb', line 17 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rust/models/regression.rb', line 32 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(" + ")) 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"] result.r_mirror_to("#{model_type}.model.result") return result end end |
Instance Method Details
#actuals ⇒ Object
Returns the actual values in the dataset.
89 90 91 |
# File 'lib/rust/models/regression.rb', line 89 def actuals return self.fitted.zip(self.residuals).map { |couple| couple.sum } end |
#coefficients ⇒ Object
Returns the coefficients of the model.
117 118 119 |
# File 'lib/rust/models/regression.rb', line 117 def coefficients a = self.summary|"coefficients" end |
#fitted ⇒ Object
Returns the fitted values of the model.
78 79 80 81 82 83 84 |
# File 'lib/rust/models/regression.rb', line 78 def fitted Rust.exclusive do @fitted = Rust["fitted(#{self.r_mirror})"] unless @fitted end return @fitted end |
#load_in_r_as(variable_name) ⇒ Object
22 23 24 |
# File 'lib/rust/models/regression.rb', line 22 def load_in_r_as(variable_name) @model.load_in_r_as(variable_name) end |
#model ⇒ Object
60 61 62 |
# File 'lib/rust/models/regression.rb', line 60 def model @model end |
#mse ⇒ Object
Returns the mean squared error of the model.
110 111 112 |
# File 'lib/rust/models/regression.rb', line 110 def mse Rust::Descriptive.variance(self.residuals) end |
#r_2 ⇒ Object
Returns the r-squared of the model.
96 97 98 |
# File 'lib/rust/models/regression.rb', line 96 def r_2 return self.summary|"r.squared" end |
#r_2_adjusted ⇒ Object
Returns the adjusted r-squared of the model.
103 104 105 |
# File 'lib/rust/models/regression.rb', line 103 def r_2_adjusted return self.summary|"adj.r.squared" end |
#r_hash ⇒ Object
138 139 140 |
# File 'lib/rust/models/regression.rb', line 138 def r_hash @model.r_hash end |