Published on
· July 10, 2026

Linear Regression: What It Is, How to Model and Evaluate

Blog
  • Photo of Henrico Piubello
    Henrico Piubello
    Henrico Piubello
    IT Specialist - Grupo Voitto

    IT Specialist - Grupo Voitto

Colorful scatter plot with a linear regression line crossing the data points

Linear regression is a statistical method that models the relationship between a dependent variable and one or more independent variables by means of a line. It finds the line that minimizes the difference between observed and predicted values, serving to predict continuous values and interpret each predictor.

How does linear regression work?

Linear regression fits a linear function that links the dependent variable (or response) to one or more independent variables (or predictors). The goal is to find the line that minimizes the sum of squared residuals — the difference between actual and predicted values. This technique, called Ordinary Least Squares (OLS), is the basis of the LinearRegression implementation in scikit-learn, which fits the coefficients to minimize the residual between observed and predicted targets.

The model can be expressed mathematically as:

Y=β0+β1X1+β2X2+...+βnXn+εY = β0 + β1X1 + β2X2 + ... + βn*Xn + ε

Where:

  • YY represents the dependent variable we are trying to predict.
  • X1,X2,...,XnX1, X2, ..., Xn are the independent variables that influence the dependent variable.
  • β0,β1,β2,...,βnβ0, β1, β2, ..., βn are the model coefficients that represent the influence of the independent variables.
  • εε is the error term, which captures the discrepancies between observed and predicted values.

Linear regression assumes there is a linear relationship between response and predictors, that is, the relationship can be represented by a line in the data space. Even when the relationship is not strictly linear, the model is often useful as a first approximation and as an interpretable baseline.

What are the assumptions of linear regression?

Linear regression only produces valid and reliable results when certain assumptions are met. Verifying them before interpreting the coefficients avoids distorted conclusions. The main assumptions are:

  1. Linearity: there is a linear relationship between the dependent and independent variables.
  2. Independence: the residuals (difference between observed and predicted values) show no correlation with each other.
  3. Homoscedasticity: the variance of residuals is constant across all ranges of the independent variables' values.
  4. Absence of multicollinearity: the independent variables are not highly correlated with each other, which would make coefficient interpretation difficult.
  5. Absence of outliers: there are no extreme atypical values that distort coefficient estimation.
  6. Normality of residuals: the residuals follow a normal distribution, important for statistical tests and confidence intervals.

When these assumptions are violated, additional techniques such as variable transformation or alternative models help correct the problem. Understanding these fundamentals is essential to apply and interpret linear regression correctly in Data Science.

What is linear regression for?

Linear regression has a wide range of applications in Data Science, whenever the goal is to predict a number or understand the weight of each factor. It is so widespread that, in the Kaggle State of Data Science and Machine Learning 2022 survey, linear and logistic regression appears as the most used method by data scientists at work. Some practical examples:

  1. Sales forecasting: estimate sales based on advertising, price, historical trends, and other relevant variables.
  2. Market analysis: understand how economic, demographic, or social factors influence demand for a product or service.
  3. Credit risk analysis: model the risk of an individual or company from payment history, income, and age.
  4. Demand forecasting: anticipate future demand based on historical sales data, prices, and competition.
  5. Success factor analysis: identify which factors most influence the outcome of a campaign or launch.

Besides prediction, the estimated coefficients reveal the direction and magnitude of each variable's influence, providing valuable insights for decision-making. It is this combination of predictive power and interpretability that keeps linear regression relevant even in the face of more complex algorithms.

How to prepare data for linear regression?

Proper data preparation is crucial to obtain reliable results with linear regression. Before fitting the model, four steps are fundamental: exploratory analysis, handling of missing data, normalization, and train-test split.

Exploratory data analysis examines the characteristics of the dataset, identifies patterns, detects outliers, and reveals the distribution of variables, guiding which predictors to include. Next, handling of missing data deals with missing values through exclusion, simple imputation (mean, median), or advanced methods like MICE (Multiple Imputation by Chained Equations), avoiding bias in the model.

Normalization and standardization scale variables to ranges like [0,1][0, 1] or to mean zero and standard deviation one, preventing scale differences from distorting the comparison between coefficients. Finally, splitting the data into training and test sets allows fitting the coefficients on one portion and evaluating performance on unseen data, helping to detect overfitting and realistically estimate the model's generalization ability.

How to evaluate a linear regression model?

Evaluating a linear regression combines quantitative metrics with residual analysis and cross-validation. The metrics measure how well the model fits the data and how accurate the predictions are. The three most used are summarized below:

MetricWhat it measuresHow to interpret
Variance explained by the modelThe closer to 1, the better
RMSERoot mean squared errorThe lower, the better the fit
MAEMean absolute errorThe lower, the less average error

What do R², RMSE, and MAE indicate?

R² (coefficient of determination) indicates the proportion of the total variance of the dependent variable explained by the model, ranging from 00 to 11. RMSE (Root Mean Square Error) is the root of the mean of squared errors; see the Oracle RMSE definition for the full calculation. Meanwhile, MAE (Mean Absolute Error) measures the average magnitude of prediction errors. For RMSE and MAE, the lower the value, the better the model fit.

How does residual analysis work?

Residual analysis checks whether the differences between observed and predicted values meet the assumptions of independence, homoscedasticity, and normality. It uses plots like the residual scatter plot, the residuals vs. fitted values plot, and the normality plot. These plots reveal patterns or deviations and indicate whether the model adequately captures the information in the data.

Why use cross-validation?

Cross-validation evaluates the model's predictive ability by splitting the data into train and test sets multiple times, fitting and measuring performance in each round. It checks whether the model generalizes to unseen data and helps identify overfitting, which occurs when the model fits the training data well but errs on new data. Thus, cross-validation provides a more realistic estimate of the model's future effectiveness.

How to interpret regression coefficients?

Coefficients are the interpretable heart of linear regression: each represents the expected average change in the dependent variable for each unit change in the independent variable, holding the others constant. Positive coefficients indicate a positive relationship; negative ones, an inverse relationship.

For example, in a simple regression with one independent variable XX and response YY, the coefficient β1β₁ is the average change in YY for each additional unit in XX. This reading lets you understand the direction and magnitude of the relationship between predictors and response.

What is the statistical significance of coefficients?

Besides the direct reading, you need to evaluate the statistical significance of each coefficient through hypothesis tests, such as the t-test or the F-test. A coefficient is considered statistically significant when the associated p-value is less than a predefined significance level (usually 0.05). This indicates the coefficient differs from zero and has a real effect on the dependent variable, providing a basis for reliable inferences.

How to compare the influence of variables?

Interpreting the coefficients also reveals the relative influence of each predictor. Coefficients with larger magnitude point to variables with stronger impact on the response. If, in a multiple regression, β1β₁ (associated with X1X₁) exceeds β2β₂ (associated with X2X₂) in magnitude, then X1X₁ influences the outcome more. This analysis is useful for prioritizing the most relevant variables in modeling and decision-making.

Which advanced techniques improve linear regression?

Several techniques improve model quality and solve specific challenges of linear regression, such as overfitting, irrelevant variables, and outliers. The three most important are regularization, feature selection, and outlier treatment.

Regularization combats overfitting by penalizing coefficient magnitude. The most common forms are Ridge (L2) regularization, which penalizes the square of coefficients, and Lasso (L1) regularization, which penalizes the absolute value. Both are recommended by the scikit-learn linear models documentation precisely because, in high dimensions, minimizing only the squared error can lead to overfitting.

Feature selection identifies the most relevant variables, discarding irrelevant predictors that hurt performance. Techniques like univariate selection, recursive elimination, and F-test-based methods reduce dimensionality and improve interpretability. Finally, outlier detection and treatment — via scatter plots, residual analysis, the quartile method, or z-score — prevent atypical values from distorting coefficients, whether by excluding them, imputing substitutes, or using robust regression techniques.

What are the limitations and extensions of linear regression?

Linear regression is powerful but has limitations that require caution. It assumes a linear relationship and normal residuals; when this does not hold, results distort. It is sensitive to outliers, suffers from multicollinearity among predictors, and does not capture non-linear relationships. In addition, it only models included variables: important factors left out lead to lost information. The scikit-learn documentation itself warns that coefficients become unstable when variables are strongly correlated.

To overcome these limits there are extensions. Logistic Regression handles categorical dependent variables and is the basis of many classification problems. Generalized Linear Models (GLM) allow other probability distributions for the response, and time series regression deals with temporal dependencies. When combined with machine learning techniques, such as decision trees and ensembles, linear regression gains the ability to model more complex relationships — a natural path for those who already master the fundamentals and want to advance in machine learning projects. To dig deeper into the variants, CodeCrush has a dedicated guide to types of linear regression, such as simple, multiple, polynomial, and regularized.

Conclusion

Linear regression remains the first tool every data scientist should master — not because it is the most modern, but because it is the most interpretable. Before moving on to neural networks or ensembles, fitting a well-prepared linear regression, validating its assumptions, and reading its coefficients reveals almost always most of what the data have to say. Treat it as a mandatory baseline: if a complex model does not clearly outperform an honest linear regression, the simplicity and transparency of the line almost always wins in practice.

## faq

Frequently asked questions

What is linear regression for?

Linear regression is used to predict continuous numerical values and quantify how independent variables influence a dependent variable. It is used in sales forecasting, credit risk analysis, demand estimation, and pricing, whenever there is an approximately linear relationship between the factors studied.

What is the difference between simple and multiple linear regression?

Simple linear regression uses only one independent variable to predict the dependent variable, resulting in a line. Multiple linear regression uses two or more independent variables, fitting a plane or hyperplane. Multiple captures richer relationships but requires attention to multicollinearity among predictors.

What does R² mean in linear regression?

R² (coefficient of determination) indicates the proportion of the dependent variable's variance explained by the model. It ranges from 0 to 1: close to 1, the model explains almost all the variation; close to 0, almost none. Alone it does not guarantee a good model, so combine it with RMSE and residual analysis.

When should linear regression not be used?

Avoid linear regression when the relationship between variables is clearly non-linear, when residuals violate homoscedasticity or normality, or when there is strong multicollinearity and many outliers. In these cases, prefer transformations, tree models, regularized regression, or non-linear methods.

Is linear regression machine learning?

Yes. Linear regression is one of the most used supervised algorithms in machine learning for regression tasks. It learns coefficients from training data and generalizes to new examples. Its simplicity and interpretability make it a classic baseline before more complex models.

Topics in this article

## continue lendo

Keep browsing

About the author

Photo of Henrico Piubello

Henrico Piubello

IT Specialist - Grupo Voitto · Grupo Voitto

See profile and all articles