class

LinearRegression

Inherits JSON::Serializable < Reference < Object

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.)

Constructors

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.

Source
new(pull : JSON::PullParser)
Source
new(*, __pull_for_json_serializable pull : JSON::PullParser)
Source

Instance methods

at(x : Float64) : Float64

Evaluate the regression line at a given x value.

Source
cov_xy
Source
intercept
Source
mean_x
Source
mean_y
Source
pearson_r
Source
pearson_r_squared
Source
slope
Source
stdev_x
Source
stdev_y
Source
var_x
Source
var_y
Source

Nested types