Sha256: e6a744f133072861baeb2ac5b7d158ea50838759e9db978d525fd5d6a1007f1e

Contents?: true

Size: 808 Bytes

Versions: 1

Compression:

Stored size: 808 Bytes

Contents

# frozen_string_literal: true

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

1 entries across 1 versions & 1 rubygems

Version Path
flex-station-data-1.0.2 lib/flex_station_data/linear_regression.rb