class LinearRegression

Overview

A LinearRegression instance represents the immutable result of a least-squares linear regression.

Call LinearRegression.new(xs, ys) with your equal-sized arrays of Float64s to find a fit.

On your instance, call .at(x) to evaluate the regression line at x, or call .slope, .intercept, .pearson_r, .pearson_r_squared for metrics about the fit.

The LinearRegression may be serialized .to_json and deserialized #from_json. (The serialized state does not store the raw data points that were used to find the regression line.)

Included Modules

Defined in:

linear-regression.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(xs : Array(Float64), ys : Array(Float64)) #

Computes the regression given input data, where (xs[0], ys[0]) represents a single data point.

The computation happens in linear time, proportional to O(xs.size).

Raises an IndexError unless these input conditions are met: xs and ys must be the same length, and must have at least two elements.

Raises an ZeroXVarianceException if the xs values are all the same.


[View source]
def self.new(pull : JSON::PullParser) #

[View source]

Instance Method Detail

def at(x : Float64) : Float64 #

Evaluate the regression line at a given x value.


[View source]
def cov_xy : Float64 #

[View source]
def intercept : Float64 #

[View source]
def mean_x : Float64 #

[View source]
def mean_y : Float64 #

[View source]
def n : Int32 #

[View source]
def pearson_r : Float64 #

[View source]
def pearson_r_squared : Float64 #

[View source]
def slope : Float64 #

[View source]
def stdev_x : Float64 #

[View source]
def stdev_y : Float64 #

[View source]
def var_x : Float64 #

[View source]
def var_y : Float64 #

[View source]