Sha256: 0785a9659811b4655e55a7af7a4ad21ce5bdf0bc45d26621d600942371f04161

Contents?: true

Size: 777 Bytes

Versions: 5

Compression:

Stored size: 777 Bytes

Contents

require "active_support/core_ext"
require "linefit"

module FlexStationData
  class LinearRegression
    attr_reader :x, :y, :min_r_squared

    def initialize(x, y, min_r_squared: 0.0, **)
      @x = x
      @y = y
      @min_r_squared = min_r_squared.to_f
    end

    def slope
      coefficients[1]
    end

    def intercept
      coefficients[0]
    end

    def r_squared
      @r_squared ||= line_fit.rSquared
    rescue TypeError
      nil
    end

    def quality
      "poor fit" if r_squared.present? && r_squared < min_r_squared
    end

    private

    def coefficients
      @coefficients ||= line_fit.coefficients
    rescue TypeError
      [ nil, nil ]
    end

    def line_fit
      @line_fit ||= LineFit.new.tap { |lf| lf.setData(x, y) }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
flex-station-data-1.0.1 lib/flex_station_data/linear_regression.rb
flex-station-data-1.0.0 lib/flex_station_data/linear_regression.rb
flex-station-data-0.3.2 lib/flex_station_data/linear_regression.rb
flex-station-data-0.3.1 lib/flex_station_data/linear_regression.rb
flex-station-data-0.3.0 lib/flex_station_data/linear_regression.rb