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