Linear Regression Calculator

Calculate the line of best fit y = mx + b, Pearson correlation coefficient (r), coefficient of determination (R²), and scatter plot with residuals.

Try:
Best-Fit Line: y = 0.9x + 1.3
Slope (m)
0.9
Intercept (b)
1.3
Correlation (r)
0.9037
R² (Fit)
0.8167
Points (n)
5
Mean (x̄, ȳ)
(3, 4)
⟹ Predicted y: 6.7

Live Residuals Table

x y ŷ (Predicted) Residual (y − ŷ)
Show the work (step-by-step)

Scatter Plot & Regression Line

Points with line of best fit

  • Regression line
  • Data points

What linear regression does

Linear regression is a way to find a trend line through scattered points. In science and math class, real data pairs rarely form a perfectly straight line. Some points sit slightly higher. Other points sit slightly lower. A linear regression line passes through the middle of the scatter plot. It shows the overall pattern of the data points.

We write this trend line as ŷ = mx + b. The symbol ŷ is called "y-hat". It represents the predicted value of y for any given x. The letter m gives the slope. The letter b gives the y-intercept.

The least squares formulas

ŷ = m x + b

The standard way to fit a line is called ordinary least squares. This method picks the slope and intercept that minimize errors. Specifically, it squares the vertical distance from each point to the line. Then it adds all those squared distances together. The best line is the one with the smallest sum.

  • Slope formula: m = Sxy / S xx = Σ((x − x̄)(y − ȳ)) / Σ(x − x̄)²
  • Y-intercept formula: b = ȳ − m x̄

Here, x̄ is the mean of all x-values. The symbol ȳ is the mean of all y-values. Squaring each gap stops positive and negative errors from cancelling out. It also punishes large mistakes more than small ones. This keeps the line centered nicely among all data points.

Reading the slope and intercept in context

Looking at m and b helps you make sense of real-world problems:

  • Slope (m): The slope tells you how much y changes when x increases by one unit. Suppose you track study hours (x) and test scores (y). If the slope m equals 4.5, studying one extra hour adds about 4.5 points to the test score.
  • Y-intercept (b): The y-intercept gives the baseline value of y when x equals zero. In our study example, a y-intercept of 52 means a student with zero study hours has a predicted score of 52 points.

What r and R² tell you

Two numbers help you judge how well your line fits the data points:

Comparison of Pearson r and Coefficient of Determination R²
Metric Symbol Possible Range Practical Interpretation
Pearson Correlation r −1 to +1 Tells if points rise or fall together. Closer to 1 or −1 means a tighter line.
Coefficient of Determination R² 0 to 1 (0% to 100%) Share of total spread in y explained by the linear rule.
Direction Sign Same sign as slope m Always positive (squared) The letter r shows direction. The value R² shows fit quality.

Worked example: Calculating a regression line by hand

Working through the arithmetic step by step shows how the formulas operate:

Worked Example: Fitting a line to 5 data pairs

Suppose you record study hours (x) and exam scores (y) for five students: (2, 4), (4, 7), (6, 9), (8, 13), and (10, 17). Find the line of best fit, the correlation coefficient r, and the coefficient of determination R².

  1. Step 1: Compute sample sums and means.
    There are 5 data pairs.
    Σx = 2 + 4 + 6 + 8 + 10 = 30. The mean x̄ is 30 / 5 = 6.
    Σy = 4 + 7 + 9 + 13 + 17 = 50. The mean ȳ is 50 / 5 = 10.
  2. Step 2: Calculate sum of squares and products.
    Σx² = 4 + 16 + 36 + 64 + 100 = 220.
    Σxy = 8 + 28 + 54 + 104 + 170 = 364.
    Σy² = 16 + 49 + 81 + 169 + 289 = 604.
  3. Step 3: Calculate deviation sums.
    SSxx = 220 − (30² / 5) = 220 − 180 = 40
    SSxy = 364 − ((30 × 50) / 5) = 364 − 300 = 64
    SSyy = 604 − (50² / 5) = 604 − 500 = 104
  4. Step 4: Find the slope m and intercept b.
    m = SSxy / SSxx = 64 / 40 = 1.6
    b = ȳ − m(x̄) = 10 − 1.6(6) = 10 − 9.6 = 0.4
    The final equation is: ŷ = 1.6x + 0.4.
  5. Step 5: Compute correlation r and R².
    r = 64 / √(40 × 104) = 64 / √4160 ≈ 0.9923
    R² = (0.9923)² ≈ 0.9846
    About 98.5% of the score change connects directly to study hours.

Residuals table for the worked example

A residual measures how far a real data point lands above or below the line. We find each residual by taking e = y − ŷ. When using least squares, the positive and negative errors balance out completely:

Residual Analysis for Worked Example Dataset (ŷ = 1.6x + 0.4)
Observation (i) x-value Observed y Predicted ŷ = 1.6x + 0.4 Residual e = y − ŷ Squared Error e²
Point 1 2 4 1.6(2) + 0.4 = 3.6 4 − 3.6 = +0.4 (+0.4)² = 0.16
Point 2 4 7 1.6(4) + 0.4 = 6.8 7 − 6.8 = +0.2 (+0.2)² = 0.04
Point 3 6 9 1.6(6) + 0.4 = 10 9 − 10 = −1 (−1)² = 1
Point 4 8 13 1.6(8) + 0.4 = 13.2 13 − 13.2 = −0.2 (−0.2)² = 0.04
Point 5 10 17 1.6(10) + 0.4 = 16.4 17 − 16.4 = +0.6 (+0.6)² = 0.36
Total Sum Σx = 30 Σy = 50 Σŷ = 50 Σe = 0 SSE = 1.6

Limits of linear models: correlation and extrapolation

Linear models are helpful, but students should remember three important rules when reading them:

  • Correlation is not causation: Just because two things change together does not mean one causes the other. A third factor often explains both. For example, ice cream sales and swimming pool visits both rise in summer. Warm sunny weather causes both to go up. Eating ice cream does not force people to go swimming.
  • Watch out for extrapolation: Guessing values far beyond your data points is risky. Real-world patterns rarely continue in a straight line forever. Most growth slows down or levels off eventually. For instance, studying for 80 hours would predict an impossible test score over 128 points.
  • Outliers can pull the line: One extreme point can tilt the entire slope toward itself. This distorts the trend for all the other points. Always check your scatter plot to spot rogue points.

Common mistakes in linear regression

Keep these tips in mind when building and reading linear models:

  • Swapping x and y: The variable x is the input, and y is the output. Minimizing vertical distance gives a different line than minimizing horizontal distance. Always place your explanatory variable on the horizontal axis.
  • Mixing up correlation r with slope m: Slope measures how steep the line rises or falls. Correlation r measures how closely points cluster along that line. A flat line can have points grouped tightly with r = 1. A steep line can have scattered points with a lower r.
  • Assuming high R² guarantees a straight line: A curved dataset can still produce an R² over 0.90 with a linear fit. The true relationship may still be a curve. Always look at the scatter plot first.
  • Ignoring patterns in residuals: If residual values form an arch or a wave, a straight line is the wrong choice. The data likely follows a curve instead.

After finding your model, you can write it in slope-intercept form to see the y-intercept clearly. If you only have two data points, you can find the line from two points directly. You can also use point-slope form when you know one point and the slope. To explore steepness and angle measurements, check our general slope calculator.

To learn more about least squares lines, review OpenStax Introductory Statistics on regression equations . You can also explore linear modeling in OpenStax College Algebra on fitting data .

Interactive Regression Scatter Plot

Move the slider to shift the height of point P(5, y). Because x = 5 sits far from the center, moving this point tilts the line and updates both m and r.

Movable Point P(5, 5.5) | Slope m = 1 | Intercept b = 0.5 ⟹ Correlation r = 1 | R² = 1

Maintained by the SlopeCalculator.cc Editorial Team · Updated

Linear regression FAQs

What is a linear regression line of best fit?

A linear regression line is the best straight line drawn through scattered data points. It keeps the line as close to every point as possible. Math students use it to model the link between two variables.

What does the correlation coefficient r mean?

The letter r shows how closely points follow a straight line. It ranges from −1 to +1. Values near +1 mean points climb together. Values near −1 mean points fall together. A value near 0 means no line pattern exists.

What is the difference between r and R²?

The value r shows direction and straight-line strength. The value R² shows the share of variation explained by the line. An R² of 0.80 means eighty percent of the change in y connects to x.

Why does the calculator show an error when all x values are the same?

When every x-value is the same, points stack straight up and down. A vertical line has an undefined slope. A single-line function cannot connect purely vertical points.

What are residuals in linear regression?

A residual is the vertical gap between an actual point and the line. It equals y minus ŷ. Points above the line have positive gaps. Points below have negative gaps.

Can you use a regression line to predict values far outside the data?

Guessing far outside your data range is called extrapolation. It can lead to bad estimates. Trends that look straight in small sets often curve or flatten out over larger spans.