lib/eps/linear_regression.rb in eps-0.3.9 vs lib/eps/linear_regression.rb in eps-0.4.0
- old
+ new
@@ -35,12 +35,11 @@
str += "r2: %.3f\n" % [r2] if extended
str += "adjusted r2: %.3f\n" % [adjusted_r2]
str
end
- # TODO use keyword arguments for gsl and intercept in 0.4.0
- def _train(**options)
+ def _train(intercept: true, gsl: nil)
raise "Target must be numeric" if @target_type != "numeric"
check_missing_value(@train_set)
check_missing_value(@validation_set) if @validation_set
data = prep_x(@train_set)
@@ -49,44 +48,28 @@
raise "Number of data points must be at least two more than number of features"
end
x = data.map_rows(&:to_a)
- gsl =
- if options.key?(:gsl)
- options[:gsl]
- elsif defined?(GSL)
- true
- elsif defined?(GSLR)
- :gslr
- else
- false
- end
+ gsl = defined?(GSLR) if gsl.nil?
- intercept = options.key?(:intercept) ? options[:intercept] : true
- if intercept && gsl != :gslr
+ if intercept && !gsl
data.size.times do |i|
x[i].unshift(1)
end
end
v3 =
- if gsl == :gslr
+ if gsl
model = GSLR::OLS.new(intercept: intercept)
model.fit(x, data.label, weight: data.weight)
@covariance = model.covariance
coefficients = model.coefficients.dup
coefficients.unshift(model.intercept) if intercept
coefficients
- elsif gsl
- x = GSL::Matrix.alloc(*x)
- y = GSL::Vector.alloc(data.label)
- w = GSL::Vector.alloc(data.weight) if data.weight
- c, @covariance, _, _ = w ? GSL::MultiFit.wlinear(x, w, y) : GSL::MultiFit.linear(x, y)
- c.to_a
else
x = Matrix.rows(x)
y = Matrix.column_vector(data.label)
# weighted OLS
@@ -193,16 +176,10 @@
end
def p_value
@p_value ||= begin
Hash[@coefficients.map do |k, _|
- tp =
- if @gsl
- GSL::Cdf.tdist_P(t_value[k].abs, degrees_of_freedom)
- else
- Eps::Statistics.tdist_p(t_value[k].abs, degrees_of_freedom)
- end
-
+ tp = Eps::Statistics.tdist_p(t_value[k].abs, degrees_of_freedom)
[k, 2 * (1 - tp)]
end]
end
end