What linear regression does and why you compute it

Linear regression finds the straight line that best fits a set of data points. You use it when you have two related measurements — say, hours studied and test scores, or temperature and ice cream sales — and you want to predict one from the other or understand how strongly they move together.

The computation produces two numbers: the slope (how steep the line is) and the intercept (where it crosses the vertical axis). Once you have those, you can plug in any new value and get a prediction. The math is the same whether you do it with pencil, a spreadsheet, or statistical software — only the speed and the number of decimal places change.

Key Takeaways

  • Linear regression finds the best-fit line through your data by minimizing the squared distances from each point to the line.
  • By hand, you calculate the means of both variables, then use formulas for slope and intercept that involve sums of products and squared differences.
  • Spreadsheet software like Excel or Google Sheets can compute regression in seconds using built-in functions or a chart trendline.
  • Statistical software like R, Python, or SPSS automates the calculation and also gives you measures of how well the line fits the data.
  • The result is always a slope and intercept; the method you choose depends only on how many data points you have and whether you need additional statistics.

Computing linear regression by hand: the step-by-step method

Start with two columns of data. Call one variable x (the input, like hours studied) and the other y (the output, like test score). You need at least three pairs, though the method works better with more.

First, find the mean of x and the mean of y. Add all the x values and divide by how many there are. Do the same for y. Write these down — you will use them twice.

Next, create three new columns. In the first, subtract the mean of x from each x value. In the second, subtract the mean of y from each y value. In the third, multiply the two results together for each row. Add up all the values in that third column — this is your numerator.

Then, square each value in your first new column (the x deviations). Add all those squares. This is your denominator. Divide the numerator by the denominator. That result is the slope. Finally, use this formula: intercept = mean of y minus (slope times mean of x). You now have both numbers. Your line is: predicted y = intercept + (slope times x).

Using a spreadsheet to compute regression

Excel, Google Sheets, and similar programs have built-in functions that do the calculation when ready. In Excel, use the SLOPE and INTERCEPT functions. Type =SLOPE(y_range, x_range) in an empty cell, where y_range is the column of output values and x_range is the column of input values. Do the same with INTERCEPT in another cell.

Google Sheets works the same way. If you want more detail — including how well the line fits — add a trendline to a scatter chart. Plot your data as points, right-click the points, select "Add trendline", choose "Linear", and check the box to display the equation. The chart will show your slope and intercept directly on the graph.

For a full regression report in Excel, use the Data Analysis Toolpak. Go to Data menu, select Data Analysis, choose Regression, and specify your input ranges. The output includes slope, intercept, and additional statistics like R-squared, which tells you how much of the variation in y is explained by x.

Computing regression in Python

Python's scikit-learn library is the standard tool. First, import the necessary modules: from sklearn.linear_model import LinearRegression and import numpy as np. Put your x values in one array and your y values in another.

Create a model object with model = LinearRegression(). Reshape your x data if needed (scikit-learn expects a 2D array), then fit the model: model.fit(x_reshaped, y). The slope is stored in model.coef_ and the intercept in model.intercept_. To make a prediction, use model.predict([[new_x_value]]).

If you want more statistics, use the statsmodels library instead. Import import statsmodels.api as sm, add a constant column to your x data with x_with_const = sm.add_constant(x), fit with results = sm.OLS(y, x_with_const).fit(), and print results.summary() to see slope, intercept, p-values, R-squared, and confidence intervals all at once.

Computing regression in R

R's lm() function (linear model) is the core tool. Put your data in a data frame or as separate vectors. The basic syntax is model <- lm(y ~ x, data = your_data). This reads as "fit a linear model where y depends on x".

Type summary(model) to see the slope (labeled "Estimate" under the x row), intercept (labeled "Intercept"), and statistics like p-value and R-squared. To extract just the slope and intercept, use coef(model). To make predictions on new data, use predict(model, newdata = data.frame(x = c(new_values))).

Understanding what the numbers mean

The slope tells you how much y changes when x increases by one unit. If the slope is 2.5, then for every one-unit increase in x, y goes up by 2.5 on average. A negative slope means y decreases as x increases.

The intercept is the predicted value of y when x is zero. It is not always meaningful — if x is temperature in Celsius, a prediction at zero degrees makes sense; if x is a person's age, zero does not. The intercept is mainly there to complete the equation.

R-squared (if your software reports it) ranges from 0 to 1 and tells you what fraction of the variation in y is explained by x. An R-squared of 0.85 means the line accounts for 85 percent of the ups and downs in your data. Lower values mean the relationship is weaker or other factors matter more.

Common mistakes and how to avoid them

Mixing up which variable is x and which is y changes the slope and intercept. x should be the variable you know or control (the input), and y should be what you are predicting (the output). If you reverse them, the line will be different.

Outliers — data points far from the rest — can pull the line away from where it should be. If you have one or two extreme values, check whether they are real measurements or errors. If they are errors, remove them. If they are real but unusual, note that your regression line may not predict well for typical cases.

Linear regression assumes the relationship is actually linear. If your data forms a curve or a cloud with no clear direction, a straight line will not fit well. Plot your data first to see the shape before you compute.

Frequently Asked Questions

Can I do linear regression with just two data points?

Technically yes — any two points define a line perfectly. But regression is meant to find the best line through many noisy points. With only two points, you have no way to know if the relationship is real or if you just happened to pick two points that line up. Use at least ten to twenty pairs for meaningful results.

What if my data does not form a straight line?

Linear regression will still compute a line, but it will not fit well. Check your R-squared value — if it is below 0.5, the line explains less than half the variation. You may need a curve (polynomial regression) or a different model. Always plot your data first to see its shape.

Do I need to standardize my data before computing regression?

No. Linear regression works on raw data. Standardizing (converting to a mean of zero and standard deviation of one) does not change the slope or intercept — it only changes the scale. Standardize only if you are comparing slopes across variables measured in different units.

What is the difference between slope and correlation?

Slope tells you how much y changes per unit of x — it depends on the units you used. Correlation (usually called r) ranges from -1 to 1 and measures only the strength and direction of the relationship, not the rate of change. A slope of 10 with correlation 0.5 means a weak relationship with a steep line; a slope of 0.1 with correlation 0.9 means a strong relationship with a shallow line.

Can I use linear regression to predict far into the future?

You can, but the further you go, the less reliable the prediction. Linear regression assumes the relationship stays the same forever, which is rarely true. Use predictions within the range of your original data. Going far beyond it is called extrapolation and carries high risk of error.