pub struct MultipleLinearRegression<T = f64>{
pub coefficients: Vec<T>,
pub r_squared: T,
pub adjusted_r_squared: T,
pub standard_error: T,
pub n: usize,
pub p: usize,
}Expand description
Multiple linear regression model that fits a hyperplane to multivariate data points.
Fields§
§coefficients: Vec<T>Regression coefficients, including intercept as the first element
r_squared: TCoefficient of determination (R²) - goodness of fit
adjusted_r_squared: TAdjusted R² which accounts for the number of predictors
standard_error: TStandard error of the estimate
n: usizeNumber of data points used for regression
p: usizeNumber of predictor variables (excluding intercept)
Implementations§
Source§impl<T> MultipleLinearRegression<T>
impl<T> MultipleLinearRegression<T>
Sourcepub fn fit<U, V>(
&mut self,
x_values: &[Vec<U>],
y_values: &[V],
) -> StatsResult<()>
pub fn fit<U, V>( &mut self, x_values: &[Vec<U>], y_values: &[V], ) -> StatsResult<()>
Fit a multiple linear regression model to the provided data
§Arguments
x_values- 2D array where each row is an observation and each column is a predictory_values- Dependent variable values (observations)
§Returns
StatsResult<()>- Ok if successful, Err with StatsError if the inputs are invalid
§Errors
Returns StatsError::EmptyData if input arrays are empty.
Returns StatsError::DimensionMismatch if X and Y arrays have different lengths.
Returns StatsError::InvalidInput if rows in X have inconsistent lengths.
Returns StatsError::ConversionError if value conversion fails.
Returns StatsError::MathematicalError if the linear system cannot be solved.
Sourcepub fn predict<U>(&self, x: &[U]) -> StatsResult<T>
pub fn predict<U>(&self, x: &[U]) -> StatsResult<T>
Predict y value for a given set of x values using the fitted model
§Arguments
x- Vector of x values for prediction (must match the number of features used during fitting)
§Returns
StatsResult<T>- The predicted y value
§Errors
Returns StatsError::NotFitted if the model has not been fitted (coefficients is empty).
Returns StatsError::DimensionMismatch if the number of features doesn’t match the model (x.len() != p).
Returns StatsError::ConversionError if type conversion fails.
§Examples
use rs_stats::regression::multiple_linear_regression::MultipleLinearRegression;
let mut model = MultipleLinearRegression::<f64>::new();
let x = vec![
vec![1.0, 2.0],
vec![2.0, 1.0],
vec![3.0, 3.0],
vec![4.0, 2.0],
];
let y = vec![5.0, 4.0, 9.0, 8.0];
model.fit(&x, &y).unwrap();
let prediction = model.predict(&[3.0, 4.0]).unwrap();Sourcepub fn predict_many<U>(&self, x_values: &[Vec<U>]) -> StatsResult<Vec<T>>
pub fn predict_many<U>(&self, x_values: &[Vec<U>]) -> StatsResult<Vec<T>>
Calculate predictions for multiple observations
§Arguments
x_values- 2D array of feature values for prediction
§Returns
StatsResult<Vec<T>>- Vector of predicted y values
§Errors
Returns StatsError::NotFitted if the model has not been fitted.
Returns an error if any prediction fails (dimension mismatch or conversion error).
§Examples
use rs_stats::regression::multiple_linear_regression::MultipleLinearRegression;
let mut model = MultipleLinearRegression::<f64>::new();
let x = vec![
vec![1.0, 2.0],
vec![2.0, 1.0],
vec![3.0, 3.0],
vec![4.0, 2.0],
];
let y = vec![5.0, 4.0, 9.0, 8.0];
model.fit(&x, &y).unwrap();
let predictions = model.predict_many(&[vec![3.0, 4.0], vec![5.0, 6.0]]).unwrap();
assert_eq!(predictions.len(), 2);Trait Implementations§
Source§impl<T> Clone for MultipleLinearRegression<T>
impl<T> Clone for MultipleLinearRegression<T>
Source§fn clone(&self) -> MultipleLinearRegression<T>
fn clone(&self) -> MultipleLinearRegression<T>
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for MultipleLinearRegression<T>
impl<T> Debug for MultipleLinearRegression<T>
Source§impl<T> Default for MultipleLinearRegression<T>
impl<T> Default for MultipleLinearRegression<T>
Source§impl<'de, T> Deserialize<'de> for MultipleLinearRegression<T>
impl<'de, T> Deserialize<'de> for MultipleLinearRegression<T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl<T> Freeze for MultipleLinearRegression<T>where
T: Freeze,
impl<T> RefUnwindSafe for MultipleLinearRegression<T>where
T: RefUnwindSafe,
impl<T> Send for MultipleLinearRegression<T>where
T: Send,
impl<T> Sync for MultipleLinearRegression<T>where
T: Sync,
impl<T> Unpin for MultipleLinearRegression<T>where
T: Unpin,
impl<T> UnwindSafe for MultipleLinearRegression<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more