#linear-regression #squares #data #stdlib #tuple #dimensional #calculate

linreg

Calculates linear regresssions of two-dimensional data. Does not use stdlib, only depends on numeric traits.

4 releases

0.2.0 Dec 8, 2019
0.1.3 Jun 9, 2018
0.1.2 Jun 9, 2018
0.1.1 Jun 9, 2018

#1989 in Algorithms

Download history 1779/week @ 2024-07-20 1213/week @ 2024-07-27 1756/week @ 2024-08-03 2085/week @ 2024-08-10 2021/week @ 2024-08-17 1972/week @ 2024-08-24 2492/week @ 2024-08-31 2059/week @ 2024-09-07 1669/week @ 2024-09-14 2518/week @ 2024-09-21 2571/week @ 2024-09-28 2793/week @ 2024-10-05 2341/week @ 2024-10-12 2379/week @ 2024-10-19 3152/week @ 2024-10-26 2051/week @ 2024-11-02

10,393 downloads per month
Used in 13 crates (10 directly)

MIT license

13KB
209 lines

linreg-rs

linreg is a small crate that calculates linear regressions. It works without stdlib or memory allocation and has few dependencies. Example:

let xs: Vec<f64> = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let ys: Vec<f64> = vec![2.0, 4.0, 5.0, 4.0, 5.0];

assert_eq!(Some((0.6, 2.2)), linear_regression(&xs, &ys));

It supports tuples, separate vectors for x and y values and template floating point types.


lib.rs:

Linear regression

linreg calculates linear regressions for two dimensional measurements, also known as simple linear regression.

Base for all calculations of linear regression is the simple model found in https://en.wikipedia.org/wiki/Ordinary_least_squares#Simple_linear_regression_model.

Example use

   use linreg::{linear_regression, linear_regression_of};

   // Example 1: x and y values stored in two different vectors
   let xs: Vec<f64> = vec![1.0, 2.0, 3.0, 4.0, 5.0];
   let ys: Vec<f64> = vec![2.0, 4.0, 5.0, 4.0, 5.0];

   assert_eq!(Ok((0.6, 2.2)), linear_regression(&xs, &ys));


   // Example 2: x and y values stored as tuples
   let tuples: Vec<(f32, f32)> = vec![(1.0, 2.0),
                                      (2.0, 4.0),
                                      (3.0, 5.0),
                                      (4.0, 4.0),
                                      (5.0, 5.0)];

   assert_eq!(Ok((0.6, 2.2)), linear_regression_of(&tuples));


   // Example 3: directly operating on integer (converted to float as required)
   let xs: Vec<u8> = vec![1, 2, 3, 4, 5];
   let ys: Vec<u8> = vec![2, 4, 5, 4, 5];

   assert_eq!(Ok((0.6, 2.2)), linear_regression(&xs, &ys));

Dependencies

~1.5MB
~39K SLoC