All about Linear Regression

SenapatiRajesh
5 min readJan 31, 2023

--

Linear regression is a statistical method used to model the relationship between a dependent variable (y) and one or more independent variables (x). The goal of linear regression is to find the line of best fit that best describes the relationship between the dependent and independent variables. The line of best fit is defined as the line that minimizes the sum of the squared differences between the actual y values and the predicted y values based on the line equation.

Linear regression has many real-world applications, such as predicting stock prices, house prices, and other economic trends. In this blog post, we will explore linear regression through two simple examples.

Example 1: Predicting Sales

Suppose we have data on the advertising budget for a company and the corresponding sales for each budget. Our goal is to use linear regression to predict the sales for a given advertising budget.

We can plot the data on a scatter plot, which shows the relationship between the advertising budget and the sales. Based on the scatter plot, it appears that there is a positive relationship between the advertising budget and the sales. The higher the advertising budget, the higher the sales.

To find the line of best fit, we need to find the equation of a line that minimizes the sum of the squared differences between the actual y values and the predicted y values. The equation of a line is given by y = mx + b, where m is the slope and b is the y-intercept. The slope represents the change in y for a unit change in x, and the y-intercept represents the value of y when x is equal to zero.

Using the least squares method, we can calculate the slope and y-intercept that best fits the data. In this case, the equation of the line of best fit is y = 0.9x + 5, where y is the sales, x is the advertising budget, m = 0.9, and b = 5.

Using the line equation, we can predict the sales for a given advertising budget. For example, if the advertising budget is $100,000, the predicted sales are $100,000 * 0.9 + 5 = $95,000.

Example 2: Predicting House Prices

Suppose we have data on the size of a house and the corresponding price for each house. Our goal is to use linear regression to predict the price of a house given its size.

We can plot the data on a scatter plot, which shows the relationship between the size of a house and its price. Based on the scatter plot, it appears that there is a positive relationship between the size of a house and its price. The larger the size of a house, the higher its price.

To find the line of best fit, we need to find the equation of a line that minimizes the sum of the squared differences between the actual y values and the predicted y values. Using the least squares method, we can calculate the slope and y-intercept that best fits the data. In this case, the equation of the line of best fit is y = 2x + 1, where y is the price, x is the size of the house, m = 2, and b = 1.

Using the line equation, we can predict the price of a house given its size. For example, if the size of a house is 1,000 square feet, the predicted price is 1,000 * 2 + 1 = $2,001.

Conclusion

Linear regression is a powerful tool for predicting the relationship between a dependent variable and one or more independent variables. In this blog post, we explored linear regression through two simple

Other examples:

Sales forecasting: Predicting sales based on advertising budgets, economic indicators, and other relevant factors.

Stock market analysis: Predicting stock prices based on historical data, economic indicators, and other relevant factors.

Housing price prediction: Estimating the value of a house based on its size, location, and other relevant factors.

Demand forecasting: Predicting demand for a product based on historical sales data and other relevant factors.

Medical diagnosis: Predicting the likelihood of a medical condition based on patient data such as age, gender, and test results.

Quality control: Predicting the quality of a product based on manufacturing process variables and other relevant factors.

Sports analysis: Predicting the outcome of a sports event based on team statistics and other relevant factors.

Customer behavior analysis: Predicting customer behavior based on demographic data, purchase history, and other relevant factors.

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# Load the data into a pandas DataFrame
df = pd.read_csv("data.csv")
# Split the data into independent and dependent variables
X = df.iloc[:, :-1].values
y = df.iloc[:, -1].values
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Train the linear regression model
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# Predict the dependent variable for the test set
y_pred = regressor.predict(X_test)
# Evaluate the model performance using mean squared error
mse = np.mean((y_test - y_pred)**2)
print("Mean Squared Error:", mse)

Problem with Linear Regression:

Linearity assumption: Linear regression assumes a linear relationship between the independent variables and the dependent variable. In reality, this relationship may not be linear, which can lead to inaccurate predictions.

Overfitting or underfitting: Overfitting occurs when the model fits the training data too well and is not able to generalize well to new data. Underfitting occurs when the model is too simple and unable to capture the underlying relationship between the variables.

Outliers: Outliers, or extreme values, can have a significant impact on the regression line and lead to incorrect predictions.

Multicollinearity: Multicollinearity occurs when two or more independent variables are highly correlated with each other, making it difficult to determine the individual effect of each variable on the dependent variable.

Non-linear relationships: Linear regression cannot capture complex non-linear relationships between variables. In these cases, more advanced regression techniques like Polynomial Regression or Non-Linear Regression should be used.

Limited to continuous data: Linear regression is limited to continuous data and may not be suitable for categorical or binary data.

Linear regression is called “linear” because it assumes a linear relationship between the predictor and outcome variables. However, in reality, this relationship may not be perfectly linear, but linear regression provides a good starting point for making predictions.

--

--