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 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more