Skip to main content

LinearRegressionResult

Struct LinearRegressionResult 

Source
pub struct LinearRegressionResult {
    pub slope: f64,
    pub intercept: f64,
    pub r_squared: f64,
}
Expand description

Result of linear regression analysis

Fields§

§slope: f64

Slope of the regression line (beta_1)

§intercept: f64

Y-intercept of the regression line (beta_0)

§r_squared: f64

Coefficient of determination (R²)

Implementations§

Source§

impl LinearRegressionResult

Source

pub fn predict(&self, x: f64) -> f64

Predict y value for a given x using the regression line

Examples found in repository?
examples/advanced_statistics.rs (line 49)
6fn main() {
7    println!("=== Advanced Statistics Demo ===\n");
8
9    // Sample dataset: student test scores
10    let scores = vec![
11        85.0, 92.0, 78.0, 95.0, 88.0, 76.0, 89.0, 94.0, 81.0, 87.0, 90.0, 83.0, 86.0, 91.0, 79.0,
12    ];
13
14    println!("📊 Student Test Scores: {:?}\n", scores);
15
16    // Basic statistics
17    println!("Basic Statistics:");
18    println!("  Mean: {:.2}", scores.mean().unwrap());
19    println!("  Median: {:.2}", scores.median().unwrap());
20    println!("  Std Dev: {:.2}", scores.std_dev().unwrap());
21    println!("  Coefficient of Variation: {:.2}%", coefficient_of_variation(&scores).unwrap());
22    println!("  Standard Error: {:.2}", standard_error(&scores).unwrap());
23
24    // Distribution shape
25    println!("\n📈 Distribution Shape:");
26    println!("  Skewness: {:.4}", skewness(&scores).unwrap());
27    println!("  Kurtosis: {:.4}", kurtosis(&scores).unwrap());
28
29    // Normalization
30    println!("\n🔄 Normalized Scores (0-1 scale):");
31    let normalized = normalize_min_max(&scores).unwrap();
32    println!("  First 5: {:?}", &normalized[0..5].iter().map(|&x| format!("{:.2}", x)).collect::<Vec<_>>());
33
34    // Grade scaling (60-100 scale)
35    println!("\n📏 Scaled to 60-100:");
36    let scaled = normalize_range(&scores, 60.0, 100.0).unwrap();
37    println!("  First 5: {:?}", &scaled[0..5].iter().map(|&x| format!("{:.2}", x)).collect::<Vec<_>>());
38
39    // Linear regression: study hours vs test scores
40    println!("\n📉 Linear Regression: Study Hours vs Test Scores");
41    let study_hours = vec![2.0, 3.5, 1.5, 5.0, 3.0, 1.0, 3.5, 4.5, 2.5, 3.0, 4.0, 2.0, 3.0, 4.0, 1.5];
42    
43    let regression = linear_regression(&study_hours, &scores).unwrap();
44    println!("  Slope: {:.2} (points per hour)", regression.slope);
45    println!("  Intercept: {:.2}", regression.intercept);
46    println!("  R²: {:.4}", regression.r_squared);
47    
48    // Predict score for 6 hours of study
49    let predicted_score = regression.predict(6.0);
50    println!("  Predicted score for 6 hours of study: {:.2}", predicted_score);
51
52    // Correlation analysis
53    println!("\n🔗 Correlation Analysis:");
54    let corr = correlation(&study_hours, &scores).unwrap();
55    println!("  Correlation between study hours and scores: {:.4}", corr);
56
57    // Normal distribution analysis
58    println!("\n📊 Normal Distribution Analysis:");
59    let mean = scores.mean().unwrap();
60    let std_dev = scores.std_dev().unwrap();
61    
62    println!("  Using mean={:.2}, std_dev={:.2}", mean, std_dev);
63    
64    // Probability of scoring exactly 85
65    let pdf_85 = normal_pdf(85.0, mean, std_dev).unwrap();
66    println!("  PDF at score 85: {:.6}", pdf_85);
67    
68    // Probability of scoring 85 or less
69    let cdf_85 = normal_cdf(85.0, mean, std_dev).unwrap();
70    println!("  Probability of scoring ≤85: {:.2}%", cdf_85 * 100.0);
71    
72    // Probability of scoring 90 or less
73    let cdf_90 = normal_cdf(90.0, mean, std_dev).unwrap();
74    println!("  Probability of scoring ≤90: {:.2}%", cdf_90 * 100.0);
75    
76    // Probability of scoring above 90
77    println!("  Probability of scoring >90: {:.2}%", (1.0 - cdf_90) * 100.0);
78
79    // Real-world interpretation
80    println!("\n💡 Insights:");
81    if regression.r_squared > 0.7 {
82        println!("  ✓ Strong correlation between study hours and test scores!");
83    } else if regression.r_squared > 0.4 {
84        println!("  ≈ Moderate correlation between study hours and test scores.");
85    } else {
86        println!("  ✗ Weak correlation between study hours and test scores.");
87    }
88    
89    let skew_val = skewness(&scores).unwrap();
90    if skew_val.abs() < 0.5 {
91        println!("  ✓ The score distribution is approximately symmetric.");
92    } else if skew_val > 0.0 {
93        println!("  ⚠ The distribution is right-skewed (more low scores).");
94    } else {
95        println!("  ⚠ The distribution is left-skewed (more high scores).");
96    }
97    
98    let cv = coefficient_of_variation(&scores).unwrap();
99    if cv < 10.0 {
100        println!("  ✓ Low variability: scores are consistent.");
101    } else if cv < 20.0 {
102        println!("  ≈ Moderate variability in scores.");
103    } else {
104        println!("  ⚠ High variability: scores are widely spread.");
105    }
106}
Source

pub fn predict_many(&self, x_values: &[f64]) -> Vec<f64>

Predict multiple y values for given x values

Trait Implementations§

Source§

impl Clone for LinearRegressionResult

Source§

fn clone(&self) -> LinearRegressionResult

Returns a duplicate 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 LinearRegressionResult

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for LinearRegressionResult

Source§

fn eq(&self, other: &LinearRegressionResult) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for LinearRegressionResult

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.