pub struct LinearRegression<T = f64>{
pub slope: T,
pub intercept: T,
pub r_squared: T,
pub standard_error: T,
pub n: usize,
}Expand description
Linear regression model that fits a line to data points.
Fields§
§slope: TSlope of the regression line (coefficient of x)
intercept: TY-intercept of the regression line
r_squared: TCoefficient of determination (R²) - goodness of fit
standard_error: TStandard error of the estimate
n: usizeNumber of data points used for regression
Implementations§
Source§impl<T> LinearRegression<T>
impl<T> LinearRegression<T>
Sourcepub fn fit<U, V>(&mut self, x_values: &[U], y_values: &[V]) -> StatsResult<()>
pub fn fit<U, V>(&mut self, x_values: &[U], y_values: &[V]) -> StatsResult<()>
Fit a linear model to the provided x and y data points
§Arguments
x_values- Independent variable valuesy_values- Dependent variable values (observations)
§Returns
StatsResult<()>- Ok if successful, Err with StatsError if the inputs are invalid
§Errors
Returns StatsError::DimensionMismatch if X and Y arrays have different lengths.
Returns StatsError::EmptyData if the input arrays are empty.
Returns StatsError::ConversionError if value conversion fails.
Returns StatsError::InvalidParameter if there’s no variance in X values.
Sourcepub fn predict<U>(&self, x: U) -> StatsResult<T>
pub fn predict<U>(&self, x: U) -> StatsResult<T>
Predict y value for a given x using the fitted model
§Arguments
x- The x value to predict for
§Returns
StatsResult<T>- The predicted y value
§Errors
Returns StatsError::NotFitted if the model has not been fitted (n == 0).
Returns StatsError::ConversionError if type conversion fails.
§Examples
use rs_stats::regression::linear_regression::LinearRegression;
let mut model = LinearRegression::<f64>::new();
model.fit(&[1.0, 2.0, 3.0], &[2.0, 4.0, 6.0]).unwrap();
let prediction = model.predict(4.0).unwrap();
assert!((prediction - 8.0).abs() < 1e-10);Sourcepub fn predict_many<U>(&self, x_values: &[U]) -> StatsResult<Vec<T>>
pub fn predict_many<U>(&self, x_values: &[U]) -> StatsResult<Vec<T>>
Calculate predictions for multiple x values
§Arguments
x_values- Slice of x values to predict for
§Returns
StatsResult<Vec<T>>- Vector of predicted y values
§Errors
Returns StatsError::NotFitted if the model has not been fitted.
Returns StatsError::ConversionError if type conversion fails for any value.
§Examples
use rs_stats::regression::linear_regression::LinearRegression;
let mut model = LinearRegression::<f64>::new();
model.fit(&[1.0, 2.0, 3.0], &[2.0, 4.0, 6.0]).unwrap();
let predictions = model.predict_many(&[4.0, 5.0]).unwrap();
assert_eq!(predictions.len(), 2);Sourcepub fn confidence_interval<U>(
&self,
x: U,
confidence_level: f64,
) -> StatsResult<(T, T)>
pub fn confidence_interval<U>( &self, x: U, confidence_level: f64, ) -> StatsResult<(T, T)>
Calculate confidence intervals for the regression line
§Arguments
x- The x value to calculate confidence interval forconfidence_level- Confidence level (0.95 for 95% confidence)
§Returns
StatsResult<(T, T)>- Tuple of (lower_bound, upper_bound), or an error if invalid
§Errors
Returns StatsError::InvalidInput if there are fewer than 3 data points.
Returns StatsError::InvalidParameter if confidence level is not supported (only 0.90, 0.95, 0.99).
Returns StatsError::ConversionError if value conversion fails.
Sourcepub fn correlation_coefficient(&self) -> StatsResult<T>
pub fn correlation_coefficient(&self) -> StatsResult<T>
Get the correlation coefficient (r)
The correlation coefficient ranges from -1 to 1, indicating the strength and direction of the linear relationship between x and y.
§Returns
StatsResult<T>- The correlation coefficient
§Errors
Returns StatsError::NotFitted if the model has not been fitted (n == 0).
§Examples
use rs_stats::regression::linear_regression::LinearRegression;
let mut model = LinearRegression::<f64>::new();
model.fit(&[1.0, 2.0, 3.0], &[2.0, 4.0, 6.0]).unwrap();
let r = model.correlation_coefficient().unwrap();
assert!((r - 1.0).abs() < 1e-10); // Perfect positive correlationTrait Implementations§
Source§impl<T> Clone for LinearRegression<T>
impl<T> Clone for LinearRegression<T>
Source§fn clone(&self) -> LinearRegression<T>
fn clone(&self) -> LinearRegression<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 LinearRegression<T>
impl<T> Debug for LinearRegression<T>
Source§impl<T> Default for LinearRegression<T>
impl<T> Default for LinearRegression<T>
Source§impl<'de, T> Deserialize<'de> for LinearRegression<T>
impl<'de, T> Deserialize<'de> for LinearRegression<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 LinearRegression<T>where
T: Freeze,
impl<T> RefUnwindSafe for LinearRegression<T>where
T: RefUnwindSafe,
impl<T> Send for LinearRegression<T>where
T: Send,
impl<T> Sync for LinearRegression<T>where
T: Sync,
impl<T> Unpin for LinearRegression<T>where
T: Unpin,
impl<T> UnwindSafe for LinearRegression<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