pub struct RegressionModel { /* private fields */ }
Expand description

A fitted regression model.

Is the result of FormulaRegressionBuilder.fit().

Implementations§

source§

impl RegressionModel

source

pub fn regressor_names(&self) -> &[String]

The names of the regressor columns

source

pub fn p_values(&self) -> &[f64]

The two-tailed p-values for the t-statistics of the parameters

source

pub fn iter_p_value_pairs(&self) -> impl Iterator<Item = (&str, f64)> + '_

Iterates over pairs of regressor columns and their associated p-values

Note

This does not include the value for the intercept.

Usage
use linregress::{FormulaRegressionBuilder, RegressionDataBuilder};

let y = vec![1.,2. ,3. , 4.];
let x1 = vec![4., 3., 2., 1.];
let x2 = vec![1., 2., 3., 4.];
let data = vec![("Y", y), ("X1", x1), ("X2", x2)];
let data = RegressionDataBuilder::new().build_from(data)?;
let model = FormulaRegressionBuilder::new().data(&data).formula("Y ~ X1 + X2").fit()?;
let pairs: Vec<(&str, f64)> = model.iter_p_value_pairs().collect();
assert_eq!(pairs[0], ("X1", 1.7052707580549508e-28));
assert_eq!(pairs[1], ("X2", 2.522589878779506e-31));
source

pub fn residuals(&self) -> &[f64]

The residuals of the model

source

pub fn parameters(&self) -> &[f64]

The model’s intercept and slopes (also known as betas)

source

pub fn iter_parameter_pairs(&self) -> impl Iterator<Item = (&str, f64)> + '_

Iterates over pairs of regressor columns and their associated slope values

Note

This does not include the value for the intercept.

Usage
use linregress::{FormulaRegressionBuilder, RegressionDataBuilder};

let y = vec![1.,2. ,3. , 4.];
let x1 = vec![4., 3., 2., 1.];
let x2 = vec![1., 2., 3., 4.];
let data = vec![("Y", y), ("X1", x1), ("X2", x2)];
let data = RegressionDataBuilder::new().build_from(data)?;
let model = FormulaRegressionBuilder::new().data(&data).formula("Y ~ X1 + X2").fit()?;
let pairs: Vec<(&str, f64)> = model.iter_parameter_pairs().collect();
assert_eq!(pairs[0], ("X1", -0.03703703703703709));
assert_eq!(pairs[1], ("X2", 0.9629629629629626));
source

pub fn se(&self) -> &[f64]

The standard errors of the parameter estimates

source

pub fn iter_se_pairs(&self) -> impl Iterator<Item = (&str, f64)> + '_

Iterates over pairs of regressor columns and their associated standard errors

Note

This does not include the value for the intercept.

Usage
use linregress::{FormulaRegressionBuilder, RegressionDataBuilder};

let y = vec![1.,2. ,3. , 4.];
let x1 = vec![4., 3., 2., 1.];
let x2 = vec![1., 2., 3., 4.];
let data = vec![("Y", y), ("X1", x1), ("X2", x2)];
let data = RegressionDataBuilder::new().build_from(data)?;
let model = FormulaRegressionBuilder::new().data(&data).formula("Y ~ X1 + X2").fit()?;
let pairs: Vec<(&str, f64)> = model.iter_parameter_pairs().collect();
assert_eq!(pairs[0], ("X1", -0.03703703703703709));
assert_eq!(pairs[1], ("X2", 0.9629629629629626));
source

pub fn ssr(&self) -> f64

Sum of squared residuals

source

pub fn rsquared(&self) -> f64

R-squared of the model

source

pub fn rsquared_adj(&self) -> f64

Adjusted R-squared of the model

source

pub fn scale(&self) -> f64

A scale factor for the covariance matrix

Note that the square root of scale is often called the standard error of the regression.

source

pub fn predict<'a, I, S>(&self, new_data: I) -> Result<Vec<f64>, Error>where I: IntoIterator<Item = (S, Vec<f64>)>, S: Into<Cow<'a, str>>,

Evaluates the model on given new input data and returns the predicted values.

The new data is expected to have the same columns as the original data. See RegressionDataBuilder.build for details on the type of the new_data parameter.

Note

This function does no special handling of non real values (NaN or infinity or negative infinity). Such a value in new_data will result in a corresponding meaningless prediction.

Example
let y = vec![1., 2., 3., 4., 5.];
let x1 = vec![5., 4., 3., 2., 1.];
let x2 = vec![729.53, 439.0367, 42.054, 1., 0.];
let x3 = vec![258.589, 616.297, 215.061, 498.361, 0.];
let data = vec![("Y", y), ("X1", x1), ("X2", x2), ("X3", x3)];
let data = RegressionDataBuilder::new().build_from(data).unwrap();
let formula = "Y ~ X1 + X2 + X3";
let model = FormulaRegressionBuilder::new()
    .data(&data)
    .formula(formula)
    .fit()?;
let new_data = vec![
    ("X1", vec![2.5, 3.5]),
    ("X2", vec![2.0, 8.0]),
    ("X3", vec![2.0, 1.0]),
];
let prediction: Vec<f64> = model.predict(new_data)?;
assert_slices_almost_eq!(&prediction, &[3.500000000000028, 2.5000000000000644]);

Trait Implementations§

source§

impl Clone for RegressionModel

source§

fn clone(&self) -> RegressionModel

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 Debug for RegressionModel

source§

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

Formats the value using the given formatter. 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.