Class: Rust::Models::Regression::RegressionModel

Inherits:
RustDatatype show all
Defined in:
lib/rust/models/regression.rb

Overview

Generic regression model in R.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RustDatatype

pull_priority, pull_variable, #r_mirror, #r_mirror_to

Constructor Details

#initialize(model) ⇒ RegressionModel

Creates a new model.

Raises:

  • (StandardError)


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

Returns:

  • (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, **options)
    mapped = ""
    if options.size > 0
        mapped = options.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

#actualsObject

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

#coefficientsObject

Returns the coefficients of the model.



117
118
119
# File 'lib/rust/models/regression.rb', line 117

def coefficients
    a = self.summary|"coefficients"
end

#fittedObject

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

#modelObject



60
61
62
# File 'lib/rust/models/regression.rb', line 60

def model
    @model
end

#mseObject

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_2Object

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_adjustedObject

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_hashObject



138
139
140
# File 'lib/rust/models/regression.rb', line 138

def r_hash
    @model.r_hash
end

#residualsObject

Returns the residuals of the model.



67
68
69
70
71
72
73
# File 'lib/rust/models/regression.rb', line 67

def residuals
    Rust.exclusive do
        @residuals = Rust["residuals(#{self.r_mirror})"] unless @residuals
    end
    
    return @residuals
end

#summaryObject

Returns a summary for the model using the summary function in R.



128
129
130
131
132
133
134
135
136
# File 'lib/rust/models/regression.rb', line 128

def summary
    unless @summary
        Rust.exclusive do
            @summary = Rust["summary(#{self.r_mirror})"]
        end
    end
    
    return @summary
end