Class: Rust::Models::Regression::LinearRegressionModel

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

Overview

Represents a linear regression model in R.

Instance Attribute Summary

Attributes inherited from RegressionModel

#data, #dependent_variable, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RegressionModel

#actuals, #backward_selection, #coefficients, #fitted, #initialize, #load_in_r_as, #method_missing, #model, #mse, #r_2, #r_2_adjusted, #r_hash, #residuals, #significant_variables, #summary, #variables

Methods inherited from RustDatatype

#load_in_r_as, #r_hash, #r_mirror, #r_mirror_to

Constructor Details

This class inherits a constructor from Rust::Models::Regression::RegressionModel

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rust::Models::Regression::RegressionModel

Class Method Details

.can_pull?(type, klass) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/rust/models/regression.rb', line 222

def self.can_pull?(type, klass)
    return type == "list" && klass == self.r_model_name
end

.generate(dependent_variable, independent_variables, data, **options) ⇒ Object

Generates a linear regression model, given its dependent_variable and independent_variables and its data. options can be specified and directly passed to the model.



244
245
246
247
248
249
250
251
252
253
# File 'lib/rust/models/regression.rb', line 244

def self.generate(dependent_variable, independent_variables, data, **options)
    RegressionModel.generate(
        LinearRegressionModel,
        self.r_model_name, 
        dependent_variable, 
        independent_variables, 
        data, 
        **options
    )
end

.pull_priorityObject



226
227
228
# File 'lib/rust/models/regression.rb', line 226

def self.pull_priority
    1
end

.pull_variable(variable, type, klass) ⇒ Object



230
231
232
233
234
# File 'lib/rust/models/regression.rb', line 230

def self.pull_variable(variable, type, klass)
    model = Rust::RustDatatype.pull_variable(variable, Rust::List)
    
    return LinearRegressionModel.new(model)
end

.r_model_nameObject



236
237
238
# File 'lib/rust/models/regression.rb', line 236

def self.r_model_name
    "lm"
end

Instance Method Details

#predict(line) ⇒ Object



302
303
304
# File 'lib/rust/models/regression.rb', line 302

def predict(line)
    self.to_proc.call(line)
end

#to_procObject

Returns the model as a proc that can be used to predict values



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/rust/models/regression.rb', line 258

def to_proc
    proc do |unnormalized_data|
        data = Rust::DataFrame.new(["__TOREM__"])
        unnormalized_data.rows.times do
            data << [0]
        end
        unnormalized_data.colnames.each do |col|
            if (unnormalized_data|col)[0].is_a?(Numeric)
                newcol = Rust::DataFrame.new([col])
                (unnormalized_data|col).each do |v|
                    newcol << [v]
                end
                data.cbind!(newcol)
            else
                (unnormalized_data|col).uniq.each do |val|
                    newcol = Rust::DataFrame.new([col + val])
                    (unnormalized_data|col).each do |v|
                        if v == val
                            newcol << [1]
                        else
                            newcol << [0]
                        end
                    end
                    data.cbind!(newcol)
                end
            end
        end
        data.delete_column("__TOREM__")
        value = 0
        @variables.each do |var|
            p var
            if var.name == "(Intercept)"
                value += var.coefficient
            else
                if data.colnames.include?(var.name)
                    value += (data|var.name)[0] * var.coefficient
                end
            end
        end

        value
    end
end