Linear Regression: A Simple Guide with Examples

Linear regression is one of the most fundamental and widely used techniques in statistics and machine learning. Whether you’re a data science beginner or looking to refresh your knowledge, this guide will walk you through the basics of linear regression with clear explanations and examples.


What is Linear Regression?

Linear regression is a statistical method used to model the relationship between a dependent variable and one or more independent variables. It aims to find the best-fitting straight line (the “regression line”) that predicts the dependent variable based on the independent variables.

In simpler terms, think of linear regression as a way to draw a straight line through a scatter plot of data points that best represents their trend.


Why Use Linear Regression?

Linear regression is popular because:

  • Simplicity: It’s easy to understand and interpret.
  • Versatility: It can be applied to a wide range of fields, from economics to biology.
  • Foundation: It serves as the basis for more complex statistical methods.

Key Concepts in Linear Regression

1. Dependent and Independent Variables

  • Dependent Variable (Y): The outcome or the variable you want to predict or explain.
  • Independent Variable (X): The predictor or the variable you use to predict the dependent variable.

2. The Regression Line

The goal is to find the line that best fits the data points. The equation of this line in simple linear regression is:

[ Y = \beta_0 + \beta_1 X ]

Where:

  • ( Y ) is the predicted value.
  • ( \beta_0 ) is the intercept (the value of ( Y ) when ( X = 0 )).
  • ( \beta_1 ) is the slope (how much ( Y ) changes for a unit change in ( X )).
  • ( X ) is the independent variable.

3. Residuals

Residuals are the differences between the observed values and the values predicted by the regression line. The best-fitting line minimizes the sum of the squared residuals.


How to Perform Linear Regression

Let’s walk through the steps of performing a simple linear regression using an example.

Example: Predicting House Prices

Imagine you have data on house prices and the size of the houses. You want to predict the price of a house based on its size.

Step 1: Collect the Data

You have the following data:

Size (sq ft)Price ($1000)
1500200
1600220
1700240
1800260
1900280

Step 2: Plot the Data

Plot the data points on a graph with Size on the x-axis and Price on the y-axis.

Step 3: Calculate the Regression Line

Using statistical software or a tool like Excel, you can calculate the best-fitting line. In this case, the equation might be:

[ \text{Price} = 40 + 0.12 \times \text{Size} ]

This means the intercept ( \beta_0 ) is 40, and the slope ( \beta_1 ) is 0.12.

Step 4: Interpret the Results

  • Intercept (( \beta_0 )): When the size is 0, the price is predicted to be $40,000 (though in real-world scenarios, having a house of size 0 is impractical).
  • Slope (( \beta_1 )): For each additional square foot, the price increases by $120 (0.12 × 1000).

Python Examples of Linear Regression

To further illustrate how linear regression works, here are three Python examples using different libraries and approaches.


1. Simple Linear Regression Using Numpy

This example demonstrates a basic implementation using the NumPy library to perform linear regression from scratch.

import numpy as np
import matplotlib.pyplot as plt

# Sample data
X = np.array([1500, 1600, 1700, 1800, 1900])
Y = np.array([200, 220, 240, 260, 280])

# Calculate the means of X and Y
X_mean = np.mean(X)
Y_mean = np.mean(Y)

# Calculate the slope (β1) and intercept (β0)
numerator = np.sum((X - X_mean) * (Y - Y_mean))
denominator = np.sum((X - X_mean) ** 2)
beta1 = numerator / denominator
beta0 = Y_mean - beta1 * X_mean

# Print the slope and intercept
print(f"Intercept (β0): {beta0}")
print(f"Slope (β1): {beta1}")

# Predict values
Y_pred = beta0 + beta1 * X

# Plot the data and the regression line
plt.scatter(X, Y, color='blue', label='Data points')
plt.plot(X, Y_pred, color='red', label='Regression line')
plt.xlabel('Size (sq ft)')
plt.ylabel('Price ($1000)')
plt.legend()
plt.show()

2. Simple Linear Regression Using Scikit-Learn

Scikit-Learn, a powerful library for machine learning in Python, provides a straightforward way to implement linear regression.

from sklearn.linear_model import LinearRegression
import numpy as np

# Sample data
X = np.array([[1500], [1600], [1700], [1800], [1900]])
Y = np.array([200, 220, 240, 260, 280])

# Create and train the model
model = LinearRegression()
model.fit(X, Y)

# Coefficients
print(f"Intercept (β0): {model.intercept_}")
print(f"Slope (β1): {model.coef_[0]}")

# Predict values
Y_pred = model.predict(X)

# Plot the data and the regression line
import matplotlib.pyplot as plt

plt.scatter(X, Y, color='blue', label='Data points')
plt.plot(X, Y_pred, color='red', label='Regression line')
plt.xlabel('Size (sq ft)')
plt.ylabel('Price ($1000)')
plt.legend()
plt.show()

3. Multiple Linear Regression Using Statsmodels

Statsmodels provides more detailed outputs for linear regression and is useful for statistical analysis.

import statsmodels.api as sm
import numpy as np

# Sample data
X = np.array([[1500, 3], [1600, 3], [1700, 4], [1800, 4], [1900, 5]])
Y = np.array([200, 220, 240, 260, 280])

# Add a constant (for the intercept)
X = sm.add_constant(X)

# Create and train the model
model = sm.OLS(Y, X).fit()

# Print the summary of the model
print(model.summary())

# Predict values
Y_pred = model.predict(X)

# Plot the data and the regression line (for simplicity, let's consider only the size variable)
plt.scatter(X[:, 1], Y, color='blue', label='Data points')
plt.plot(X[:, 1], Y_pred, color='red', label='Regression line')
plt.xlabel('Size (sq ft)')
plt.ylabel('Price ($1000)')
plt.legend()
plt.show()

Evaluating the Model

1. R-squared ( ( R^2 ))

R-squared measures how well the regression line fits the data. It ranges from 0 to 1, with 1 indicating a perfect fit. An ( R^2 ) of 0.9, for example, means 90% of the variance in the dependent variable is explained by the independent variable.

2. P-Value

The p-value tests the hypothesis that the coefficient (slope) is not significantly different from zero. A low p-value (< 0.05) indicates that the independent variable is a significant predictor of the dependent variable.

3. Residual Analysis

Check the residuals to ensure they are randomly distributed. This indicates that the model’s assumptions hold true.


Multiple Linear Regression

When you have more than one independent variable, you use multiple linear regression. The equation expands to:

[ Y = \beta_0 + \beta_1 X_1 + \beta_2 X_2 + … + \beta_n X_n ]

Where ( X_1, X_2, …, X_n ) are the independent variables.

Example: Predicting house prices based on size, location, and number of bedrooms.


Common Applications of Linear Regression

  • Economics: Forecasting economic indicators like GDP or unemployment rates.
  • Finance: Predicting stock prices or credit risk.
  • Marketing: Analyzing the impact of advertising spend on sales.
  • Healthcare: Estimating patient outcomes based on medical histories.

Limitations of Linear Regression

While linear regression is powerful, it has limitations:

  • Linearity Assumption: It assumes a linear relationship between the variables.
  • Outliers: Highly sensitive to outliers, which can skew the results.
  • Multicollinearity: When using multiple regression, high correlation between independent variables can make the model unreliable.

Linear regression is a versatile and essential tool in data analysis.