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

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

#1345 in Algorithms

Download history 2106/week @ 2024-03-14 1750/week @ 2024-03-21 1348/week @ 2024-03-28 2031/week @ 2024-04-04 1788/week @ 2024-04-11 2350/week @ 2024-04-18 2207/week @ 2024-04-25 1781/week @ 2024-05-02 1743/week @ 2024-05-09 2015/week @ 2024-05-16 1911/week @ 2024-05-23 1098/week @ 2024-05-30 1529/week @ 2024-06-06 1130/week @ 2024-06-13 1674/week @ 2024-06-20 1556/week @ 2024-06-27

6,183 downloads per month
Used in 12 crates (9 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
~38K SLoC