pub struct FormulaRegressionBuilder<'a> { /* private fields */ }
Expand description

A builder to create and fit a linear regression model.

Given a dataset and a set of columns to use this builder will produce an ordinary least squared linear regression model.

See formula and data for details on how to configure this builder.

The pseudo inverse method is used to fit the model.

Usage

use linregress::{FormulaRegressionBuilder, RegressionDataBuilder, assert_almost_eq};

let y = vec![1., 2. ,3., 4.];
let x = vec![4., 3., 2., 1.];
let data = vec![("Y", y), ("X", x)];
let data = RegressionDataBuilder::new().build_from(data)?;
let model = FormulaRegressionBuilder::new().data(&data).formula("Y ~ X").fit()?;
// Alternatively
let model = FormulaRegressionBuilder::new().data(&data).data_columns("Y", ["X"]).fit()?;
let params = model.parameters();
assert_almost_eq!(params[0], 4.999999999999998);
assert_almost_eq!(params[1], -0.9999999999999989);
assert_eq!(model.regressor_names()[0], "X");

Implementations§

source§

impl<'a> FormulaRegressionBuilder<'a>

source

pub fn new() -> Self

Create as new FormulaRegressionBuilder with no data or formula set.

source

pub fn data(self, data: &'a RegressionData<'a>) -> Self

Set the data to be used for the regression.

The data has to be given as a reference to a RegressionData struct. See RegressionDataBuilder for details.

source

pub fn formula<T: Into<Cow<'a, str>>>(self, formula: T) -> Self

Set the formula to use for the regression.

The expected format is <regressand> ~ <regressor 1> + <regressor 2>.

E.g. for a regressand named Y and three regressors named A, B and C the correct format would be Y ~ A + B + C.

Note that there is currently no special support for categorical variables. So if you have a categorical variable with more than two distinct values or values that are not 0 and 1 you will need to perform “dummy coding” yourself.

Alternatively you can use data_columns.

source

pub fn data_columns<I, S1, S2>(self, regressand: S1, regressors: I) -> Selfwhere I: IntoIterator<Item = S2>, S1: Into<Cow<'a, str>>, S2: Into<Cow<'a, str>>,

Set the columns to be used as regressand and regressors for the regression.

Note that there is currently no special support for categorical variables. So if you have a categorical variable with more than two distinct values or values that are not 0 and 1 you will need to perform “dummy coding” yourself.

Alternatively you can use formula.

source

pub fn fit(self) -> Result<RegressionModel, Error>

Fits the model and returns a RegressionModel if successful. You need to set the data with data and a formula with formula before you can use it.

source

pub fn fit_without_statistics(self) -> Result<Vec<f64>, Error>

Like fit but does not perfom any statistics on the resulting model. Returns a Vec containing the model parameters (in the order intercept, column 1, column 2, …) if successfull.

This is usefull if you do not care about the statistics or the model and data you want to fit result in too few residual degrees of freedom to perform statistics.

Trait Implementations§

source§

impl<'a> Clone for FormulaRegressionBuilder<'a>

source§

fn clone(&self) -> FormulaRegressionBuilder<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for FormulaRegressionBuilder<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> Default for FormulaRegressionBuilder<'a>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.