class LinearRegression
- LinearRegression
- Reference
- Object
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
- JSON::Serializable
Defined in:
linear-regression.crConstructors
-
.new(xs : Array(Float64), ys : Array(Float64))
Computes the regression given input data, where (xs[0], ys[0]) represents a single data point.
- .new(pull : JSON::PullParser)
Instance Method Summary
-
#at(x : Float64) : Float64
Evaluate the regression line at a given x value.
- #cov_xy : Float64
- #intercept : Float64
- #mean_x : Float64
- #mean_y : Float64
- #n : Int32
- #pearson_r : Float64
- #pearson_r_squared : Float64
- #slope : Float64
- #stdev_x : Float64
- #stdev_y : Float64
- #var_x : Float64
- #var_y : Float64
Constructor Detail
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.