This project demonstrates a basic implementation of simple linear regression in Julia. It generates synthetic data, performs linear regression to find the equation of the line, and then plots the data points and the regression line.
- Julia 1.11.3 or newer
PlotspackageStatisticspackage
-
Install Julia:
If you haven't installed Julia yet, download and install it from the official website: https://julialang.org/downloads/. -
Install Required Packages:
Open Julia's REPL and run the following command to install the required packages:import Pkg Pkg.add("Plots") Pkg.add("Statistics")
-
Update Julia (optional but recommended):
If you're using an older version of Julia, it is recommended to update to the latest version (1.11.3+):juliaup update
-
Generate Synthetic Data: The function
generate_data(n::Int)generates a set ofndata points for the independent variablexand a dependent variableywith some random noise. -
Perform Linear Regression: The function
linear_regression(x, y)calculates the slope (m) and the intercept (b) of the best-fit line using the least squares method. -
Plot the Results: The function
plot_regression(x, y, m, b)uses thePlotspackage to display a scatter plot of the data points and overlay the regression line.
- The console will display the linear regression equation
y = mx + b, wheremis the slope andbis the intercept. - A plot will be generated showing the data points and the regression line.
Once the packages are installed and your environment is set up, you can run the project by simply calling the main() function:
main()This will:
- Generate synthetic data,
- Perform linear regression,
- Display the regression equation in the console, and
- Plot the data along with the regression line.
This project is open source and available under the MIT License.

