Skip to content

RobisonTorres/Linear_Regression

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📈 Linear Regression in Rust

This project showcases how to implement Linear Regression, including key statistical metrics such as the Coefficient of Determination (R²) and Mean Squared Error (MSE), entirely in the Rust programming language—without using any external crates.

Although the example dataset represents monthly water consumption based on the number of family members, this implementation is general-purpose and can be applied to any dataset where the data can be modeled as x and y values.


🚀 Main Functionality

These are the primary functions that power the linear regression analysis:

  • linear_regression Validates input and computes the regression line (ŷ) for a given x.

  • coefficient_determination Calculates , showing how well the model fits the data.

  • mse Computes the Mean Squared Error, indicating the average squared difference between actual and predicted values.

  • predict_values Predicts new y values using the derived regression line.


🔧 Auxiliary Functions

These helper functions support the core calculations for linear regression:

  • std_deviation Returns the standard deviation of a numeric vector.

  • correlation Computes the Pearson correlation coefficient between two datasets.

  • slope Calculates the slope (m) of the best-fit line.

  • intercept Determines the y-intercept (b) of the regression line.


📦 Prerequisites

  • Rust (installed and configured)

🛠️ Usage Instructions

  1. Clone this repository to your local machine.
  2. Navigate into the project directory.
  3. Run the program with the command:
cargo run

📊 Demo Output

Running cargo run will produce output similar to this:

📊 Linear Regression Analysis
------------------------------------------------------------
Input (Family Members):    [4.0, 7.0, 8.0, 3.0, 11.0]
Input (Water Consumption): [13.0, 21.0, 24.5, 11.0, 34.7] m³
------------------------------------------------------------
==================== Regression Summary ====================
R² (Coefficient of Determination): 0.9927
→ 99.27% of water consumption is explained by family size.

MSE (Mean Squared Error): 0.5337
→ Indicates difference between observed and predicted values.
============================================================

================== Predicted Consumption ===================
Family Size | Predicted Monthly Water Consumption (m³)
------------------------------------------------------------
2            |       7.22
5            |      16.10
13           |      39.79
9            |      27.95
15           |      45.71
============================================================

✅ Tests

Automated tests are included to ensure correct handling of input and core logic.

To run the tests, use:

cargo test

About

Linear Regression Implementation Using Rust Language.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages