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

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

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

Returns a new instance of RegressionModel.

Raises:

  • (StandardError)


40
41
42
43
# File 'lib/rust/models/regression.rb', line 40

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



85
86
87
# File 'lib/rust/models/regression.rb', line 85

def method_missing(name, *args)
    return model|name.to_s
end

Class Method Details

.can_pull?(type, klass) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/rust/models/regression.rb', line 10

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rust/models/regression.rb', line 20

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



65
66
67
# File 'lib/rust/models/regression.rb', line 65

def actuals            
    return self.fitted.zip(self.residuals).map { |couple| couple.sum }
end

#coefficientsObject



81
82
83
# File 'lib/rust/models/regression.rb', line 81

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

#fittedObject



57
58
59
60
61
62
63
# File 'lib/rust/models/regression.rb', line 57

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

#load_in_r_as(variable_name) ⇒ Object



15
16
17
# File 'lib/rust/models/regression.rb', line 15

def load_in_r_as(variable_name)
    @model.load_in_r_as(variable_name)
end

#modelObject



45
46
47
# File 'lib/rust/models/regression.rb', line 45

def model
    @model
end

#mseObject



77
78
79
# File 'lib/rust/models/regression.rb', line 77

def mse
    Rust::Descriptive.variance(self.residuals)
end

#r_2Object



69
70
71
# File 'lib/rust/models/regression.rb', line 69

def r_2
    return self.summary|"r.squared"
end

#r_2_adjustedObject



73
74
75
# File 'lib/rust/models/regression.rb', line 73

def r_2_adjusted
    return self.summary|"adj.r.squared"
end

#r_hashObject



99
100
101
# File 'lib/rust/models/regression.rb', line 99

def r_hash
    @model.r_hash
end

#residualsObject



49
50
51
52
53
54
55
# File 'lib/rust/models/regression.rb', line 49

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

#summaryObject



89
90
91
92
93
94
95
96
97
# File 'lib/rust/models/regression.rb', line 89

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