ThisCalculator

Everyday

Linear Regression Calculation Example

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

Linear Regression Calculation Example

This calculator works out the slope and intercept of a simple linear regression line from your paired data, then predicts Y for a given X. You enter your X and Y values as comma-separated lists, and the calculator returns the equation of the best-fit line, the correlation coefficient, and the predicted Y. The formula and a step-by-step example using the default data are explained below.

Formula

The simple linear regression line is written as:

ŷ = a + b·x

Where:

  • ŷ is the predicted value of the dependent variable (Y).
  • x is the independent variable (the predictor).
  • b (slope) is the change in Y for a one-unit change in X.
  • a (intercept) is the predicted Y when X = 0.

The slope and intercept are calculated using the least squares method, which minimizes the sum of squared vertical distances between the data points and the line. The formulas are:

b = [n·Σ(xy) − Σx·Σy] / [n·Σ(x²) − (Σx)²]

a = [Σy − b·Σx] / n

Here, n is the number of data pairs, Σ means “sum of,” and xy is the product of each X and Y pair.

Worked Example

Using the default data: X = 1, 2, 3, 4, 5 and Y = 2, 4, 5, 4, 5.

  1. Compute the sums: Σx = 15, Σy = 20, Σ(xy) = (1×2)+(2×4)+(3×5)+(4×4)+(5×5) = 2+8+15+16+25 = 66, Σ(x²) = 1+4+9+16+25 = 55, Σ(y²) = 4+16+25+16+25 = 86.
  2. Slope: b = [5×66 − 15×20] / [5×55 − 15²] = (330 − 300) / (275 − 225) = 30 / 50 = 0.6.
  3. Intercept: a = (20 − 0.6×15) / 5 = (20 − 9) / 5 = 11 / 5 = 2.2.
  4. Regression equation: ŷ = 2.2 + 0.6x.
  5. For x = 6, predicted Y = 2.2 + 0.6×6 = 2.2 + 3.6 = 5.8.

The calculator rounds the slope to 0.6 and the intercept to 2.2, giving the same equation. The correlation coefficient r is 0.693 (rounded to three decimals), indicating a moderate positive relationship.

When to Use Linear Regression

Linear regression is appropriate when you have two continuous variables and you want to:

  • Quantify the strength and direction of a linear relationship (using r).
  • Predict one variable from another (using the equation).
  • Adjust for a confounding variable in a simple analysis.

It assumes a roughly linear relationship, independence of observations, homoscedasticity (constant variance of errors), and normally distributed errors for valid hypothesis tests. For a quick check, plot your data first; if the points follow a straight line, regression is suitable. If the relationship is curved, consider polynomial regression or transforming the variables.

How to Read the Results

The slope tells you the average change in Y when X increases by one unit. In the example, a slope of 0.6 means that for each additional unit of X, Y increases by 0.6 on average. The intercept is the predicted Y when X is zero; sometimes it has no practical meaning if X never equals zero in your data (e.g., predicting weight from height, where height zero is impossible).

The correlation coefficient r ranges from −1 to +1. Values near +1 indicate a strong positive linear relationship, near −1 a strong negative relationship, and near 0 little to no linear relationship. The square of r (r²) is the coefficient of determination; it tells you the proportion of variance in Y explained by X. In the example, r² = 0.48, meaning about 48% of the variation in Y is explained by X.

Common Mistakes and Pitfalls

  • Using the wrong variable as X. The independent variable (X) is the predictor; the dependent variable (Y) is the outcome. Swapping them changes the equation.
  • Ignoring outliers. A single extreme point can heavily influence the slope and intercept. Always plot the data first.
  • Extrapolating beyond the range of X. The regression line is only reliable within the observed X range. Predicting for X values far outside that range can be wildly inaccurate.
  • Assuming causation. A strong correlation does not imply that X causes Y; there may be a third variable involved.
  • Rounding too early. Round the slope and intercept only at the end; rounding intermediate steps can change the result noticeably.

Limitations of the Formula

Linear regression only captures linear relationships. If the true relationship is curved, the line will be a poor fit. It is also sensitive to outliers and to violations of the underlying assumptions. For small samples, the estimates can be unstable. Additionally, the intercept may be meaningless in some contexts, and the model does not account for interactions between multiple predictors (that would require multiple regression).

Alternatives to Simple Linear Regression

If your data does not meet the assumptions, or if you have more than one predictor, consider:

  • Multiple linear regression – for two or more independent variables.
  • Polynomial regression – for curved relationships (e.g., y = a + bx + cx²).
  • Logistic regression – when Y is binary (0/1).
  • Nonparametric methods – like Spearman’s rank correlation, which does not assume linearity.

Each has its own assumptions and uses; choosing the right one depends on your data and research question.

Frequently Asked Questions

What is the difference between correlation and regression?

Correlation (r) measures the strength and direction of a linear relationship between two variables, but does not imply prediction. Regression goes further and provides an equation that lets you predict Y from X. Correlation is symmetric (r is the same regardless of which variable is X), while regression is not: swapping X and Y yields a different line.

Can I use linear regression if my data is not normally distributed?

Yes, the Y values do not need to be normally distributed for the coefficients to be unbiased. However, for valid confidence intervals and p-values, the residuals (errors) should be approximately normally distributed. With large samples, the central limit theorem often makes the tests robust to non-normality.

What does the intercept mean when X is never zero?

In that case, the intercept is just a mathematical constant that positions the line; it has no practical interpretation. For example, predicting height from age, the intercept would be the predicted height at age zero, which is not meaningful. You can center X (subtract the mean) to give the intercept a more interpretable meaning: it becomes the predicted Y at the average X.

How do I know if my linear regression is a good fit?

Check the coefficient of determination (r²) – the closer to 1, the better the line explains the variation. Also examine a scatter plot with the regression line: if the points are scattered randomly around the line, the fit is likely good. Residual plots (residuals vs. fitted values) should show no clear pattern.

What is the least squares method?

It is the standard technique for finding the line that minimizes the sum of the squared vertical distances between each data point and the line. Squaring ensures that positive and negative errors do not cancel out, and it gives more weight to larger errors. The formulas for slope and intercept are derived from this minimization.

Can I use linear regression with only two data points?

Yes, you can calculate a line through any two points, but it will have no statistical validity – it perfectly fits the two points and gives no information about the relationship. You need at least three points to have any idea about variability, and more points are needed for reliable estimates.

Related

Formula used
in x | X values | - | 1, 2, 3, 4, 5 | 0
in y | Y values | - | 2, 4, 5, 4, 5 | 0
in xp | X for prediction | - | 6 | 0
out slope | Slope (b) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/(count(x)*sum(x^2)-sum(x)^2) | 3
out intercept | Intercept (a) | - | (sum(y)-slope*sum(x))/count(x) | 3
out r | Correlation (r) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/sqrt((count(x)*sum(x^2)-sum(x)^2)*(count(x)*sum(y^2)-sum(y)^2)) | 3
out pred | Predicted Y | - | intercept + slope * xp | 3

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.