Data ScienceStatistics 2025-05-18

What is Simple Linear Regression?

Understanding the basics of predicting values with straight lines. Learn the regression equation and key assumptions for accurate predictions.

What is Simple Linear Regression?

Understanding the basics of predicting values with straight lines.

What is Simple Linear Regression?

Imagine you want to predict something – like a house price, a student’s score, or maybe sales figures. Often, you suspect that another factor influences it. For example, maybe the size of a house affects its price. Regression is a statistical method we use to understand and model these kinds of relationships.

Specifically, Simple Linear Regression (SLR) is the most basic type. It’s used when we believe there’s a straight-line relationship between just two variables:

  • One Independent Variable (Feature, Input, Predictor, usually denoted as X): This is the factor we think influences the outcome (e.g., house size).
  • One Dependent Variable (Target, Output, Response, usually denoted as Y): This is the outcome we want to predict (e.g., house price).

SLR tries to find the best possible straight line that describes how Y changes as X changes.

The Straight Line Equation

The Heart of SLR

You might remember the equation for a straight line from school: y = mx + c. Simple Linear Regression uses the exact same idea, just with slightly different letters:

y = b₀ + b₁x

Where:

  • y is the predicted value of the Dependent Variable (e.g., predicted price).
  • x is the value of the Independent Variable (e.g., house size).
  • b₁ is the Slope: How much y changes for a one-unit increase in x.
  • b₀ is the Intercept: The predicted value of y when x is 0.

The goal of training an SLR model is to find the best possible values for b₀ and b₁ that make the line fit our data points as closely as possible.

Important Rules (Assumptions) for SLR

Simple Linear Regression works best (and gives reliable results) only if certain conditions are met:

  1. Linearity: There must actually be a straight-line relationship between X and Y. If the real relationship is curved, forcing a straight line won’t work well.
  2. Independence: Each data point should be independent of the others. This is especially important in time-series data.
  3. Homoscedasticity (Constant Variance): The spread (variance) of the errors should be roughly constant across all values of X. We don’t want errors to fan out or funnel in.
  4. Normality of Errors: The errors (residuals) should ideally follow a normal distribution (a bell curve).

Why care about assumptions? If these are badly violated, the slope and intercept estimates might be biased, and predictions unreliable.

How Does it Find the “Best” Line?

Minimizing Errors with a Cost Function

How does the computer know which line is the “best fit”? It tries to minimize the error between the line’s predictions and the actual data points.

A very common way to measure this error is the Mean Squared Error (MSE):

  • For each data point (xᵢ, yᵢ), the model predicts a value (ŷᵢ = b₀ + b₁xᵢ).
  • Calculate the difference (error or residual): yᵢ - ŷᵢ.
  • Square each difference: (yᵢ - ŷᵢ)².
  • Average all these squared differences.

Mean Squared Error (MSE) = Average of (Actual Y - Predicted Y)²

Goal: Find b₀ and b₁ that make MSE as small as possible.

Algorithms like Gradient Descent or mathematical formulas (Ordinary Least Squares) are used to find the b₀ and b₁ that minimize MSE.

Steps to Build an SLR Model

  1. Gather & Prepare Data: Collect data for X and Y. Handle missing values, outliers, and ensure correct data types.
  2. Split Data: Divide into Training Set (to learn coefficients) and Test Set (to evaluate performance). Typically 80% training, 20% testing.
  3. Train the Model: Use a library (like Scikit-learn) to fit the model. The library calculates the best b₀ and b₁.
  4. Make Predictions: Use the trained model to predict Y values for X values in the test set.
  5. Evaluate: Compare predictions with actual values using metrics like MSE, RMSE, and R².
  6. Visualize (Optional): Plot the original data and the fitted regression line.

Simple Python Example

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import numpy as np
import matplotlib.pyplot as plt

# 1. Split Data
X_train, X_test, y_train, y_test = train_test_split(X_processed, y, test_size=0.2, random_state=42)

# 2. Create and Train the Model
model = LinearRegression()
model.fit(X_train, y_train)

# 3. Get Coefficients
b0 = model.intercept_
b1 = model.coef_[0]
print(f"Intercept (b0): {b0:.4f}")
print(f"Slope (b1):     {b1:.4f}")

# 4. Make Predictions on Test Set
y_pred = model.predict(X_test)

# 5. Evaluate
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"MSE: {mse:.4f}")
print(f"R-squared: {r2:.4f}")

# 6. Visualize
plt.scatter(X_test, y_test, color='#3b82f6', label='Actual Data', alpha=0.6)
plt.plot(X_test, y_pred, color='#f59e0b', linewidth=2, label='Regression Line')
plt.xlabel("Independent Variable (X)")
plt.ylabel("Dependent Variable (Y)")
plt.title("Simple Linear Regression Fit")
plt.legend()

Tips for Better Results

  • Clean Data is King: Spend time on data preprocessing. Handle missing values and outliers appropriately.
  • Check Assumptions: Especially linearity. If the relationship isn’t linear, SLR won’t work well. Consider Polynomial Regression.
  • Feature Scaling: Can help the underlying algorithms find the best fit faster.
  • Evaluate Properly: Don’t just look at one metric. Understand what MSE, RMSE, and R² tell you.

Simple Linear Regression: Key Points

  • Regression predicts continuous values based on inputs.
  • SLR models a straight-line relationship between one independent (X) and one dependent (Y) variable.
  • The equation is y = b₀ + b₁x.
  • It relies on key assumptions: Linearity, Independence, Homoscedasticity, Normality of Errors.
  • The “best fit” line is found by minimizing Mean Squared Error (MSE).
  • Good data preprocessing is essential for accurate results.
← All articles
Nerchuko Academy · Free DS Interview Prep