pub struct LinearRegressionResult {
pub slope: f64,
pub intercept: f64,
pub r_squared: f64,
}Expand description
Result of linear regression analysis
Fields§
§slope: f64Slope of the regression line (beta_1)
intercept: f64Y-intercept of the regression line (beta_0)
r_squared: f64Coefficient of determination (R²)
Implementations§
Source§impl LinearRegressionResult
impl LinearRegressionResult
Sourcepub fn predict(&self, x: f64) -> f64
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}Sourcepub fn predict_many(&self, x_values: &[f64]) -> Vec<f64>
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
impl Clone for LinearRegressionResult
Source§fn clone(&self) -> LinearRegressionResult
fn clone(&self) -> LinearRegressionResult
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for LinearRegressionResult
impl Debug for LinearRegressionResult
Source§impl PartialEq for LinearRegressionResult
impl PartialEq for LinearRegressionResult
impl StructuralPartialEq for LinearRegressionResult
Auto Trait Implementations§
impl Freeze for LinearRegressionResult
impl RefUnwindSafe for LinearRegressionResult
impl Send for LinearRegressionResult
impl Sync for LinearRegressionResult
impl Unpin for LinearRegressionResult
impl UnwindSafe for LinearRegressionResult
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more