Linear regression calculator (line of best fit)

Linear regression calculator: least-squares line of best fit with R², standard errors, p-values and predictions, plus polynomial and exponential fits.

Drafted with AI writing toolsUpdated Checked examples: 5

Weekly study hours (default example).
Exam scores, in the same order as x.
Try
Fitted equation
y = 48.890909 + 3.109091x
Fitted equation: y = 48.890909 + 3.109091x
Intercept b₀
48.890909
Slope / x coefficient b₁
3.109091
Correlation r
0.99662
R²
0.993252
Adjusted R²
0.992408
Residual standard error
0.822966
Standard error of b₀
0.643873
Standard error of b₁
0.090606
Predicted y
72.209091
Points n
10

Each extra unit of x corresponds to a fitted change of 3.1091 in y. The fit accounts for 99.33% of the observed variation in y. At x = 7.5 the fitted value is 72.2091.

Data and fitted curve

607080246810xyŷ = 72.2091
Datay = 48.890909 + 3.109091x

Residuals

-2-1012246810xObserved − fitted
ResidualZero
Coefficients (t tests with 8 degrees of freedom) Rows: 2
TermCoefficientStandard errortp-value
Intercept48.89090.643975.93251.008 × 10⁻¹²
x3.10910.090634.31465.686 × 10⁻¹⁰
How it's calculated S
  1. Means

    xˉ=6.5,yˉ=69.1\bar x = 6.5,\quad \bar y = 69.1
  2. Sums of squares

    Sxx=∑(xi−xˉ)2=82.5,Sxy=∑(xi−xˉ)(yi−yˉ)=256.5S_{xx} = \sum (x_i-\bar x)^2 = 82.5,\quad S_{xy} = \sum (x_i-\bar x)(y_i-\bar y) = 256.5
  3. Slope

    b1=SxySxx=256.582.5=3.109091b_1 = \frac{S_{xy}}{S_{xx}} = \frac{256.5}{82.5} = 3.109091
  4. Intercept

    b0=yˉ−b1xˉ=69.1−3.109091×6.5=48.890909b_0 = \bar y - b_1 \bar x = 69.1 - 3.109091 \times 6.5 = 48.890909
  5. Goodness of fit

    R2=1−SSESST=1−5.418182802.9=0.993252,s=SSEn−2=0.822966R^2 = 1 - \frac{SSE}{SST} = 1 - \frac{5.418182}{802.9} = 0.993252,\quad s = \sqrt{\frac{SSE}{n - 2}} = 0.822966
  6. Prediction

    y^(7.5)=72.209091\hat y(7.5) = 72.209091

About the linear regression calculator

Least squares picks the line or curve that makes the sum of squared vertical distances from the points as small as possible. For a straight line the slope is b₁ = Sxy/Sxx and the intercept b₀ = ȳ − b₁x̄; polynomial fits of degree 2 to 4 solve the normal equations, and the exponential model fits a straight line to ln y. When residual degrees of freedom and standard errors permit, coefficients include t statistics and p-values. R² describes variation in y for linear and polynomial fits, and in ln y for exponential fits.

The default relates weekly study hours to exam scores for 10 students. The fitted line is y = 48.89 + 3.109x, so each extra hour goes with about 3.1 more points, R² is 0.9933, and at 7.5 hours the line predicts 72.2.

Standard errors assume independent errors with constant variance and a correct model; exact small-sample t inference also assumes normally distributed errors on the fitted scale. A curved or funnel-shaped residual plot means the model is missing something, and predictions outside the range of the x data are flagged as extrapolations.

How to calculate the least-squares line of best fit

The least-squares line y = b₀ + b₁x has slope b₁ = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² and intercept b₀ = ȳ − b₁x̄. For 10 students' study hours and exam scores, the two sums are 256.5 and 82.5, so b₁ = 3.109, b₀ = 48.89 and the line predicts 72.2 at 7.5 hours.

The data pair hours 2, 3, 4, 5, 6, 7, 8, 9, 10 and 11 with scores 55, 58, 62, 64, 68, 71, 72, 78, 80 and 83. Worked by hand:

  1. Means: x̄ = 65 ÷ 10 = 6.5 and ȳ = 691 ÷ 10 = 69.1.
  2. Sums of deviations: Sxx = Σ(x − 6.5)² = 82.5 and Sxy = Σ(x − 6.5)(y − 69.1) = 256.5.
  3. Slope: b₁ = 256.5 ÷ 82.5 = 3.109091.
  4. Intercept: b₀ = 69.1 − 3.109091 × 6.5 = 48.890909.
  5. Prediction at 7.5 hours: 48.890909 + 3.109091 × 7.5 = 72.209091.

Least squares picks the only line that makes the sum of squared vertical distances from the points as small as possible (NIST).

What the slope and intercept mean

The slope is the fitted change in y per unit of x: each extra study hour goes with about 3.1 more points. That is an association in these 10 students, not proof that one more hour causes 3.1 points.

The intercept is the fitted y at x = 0, here 48.89 points for zero hours. No student studied fewer than 2 hours, so that value is an extrapolation that holds the line in place rather than a finding.

R² and adjusted R²

R² = 1 − SSE ÷ SST is the share of the variation in y the line accounts for. Here SSE = 5.418 and SST = 802.9, so R² = 0.99325 and hours account for 99.3% of the spread in scores.

For a straight line, R² is the square of the correlation, r = 0.99662. Adjusted R² = 1 − (1 − R²)(n − 1) ÷ (n − k) charges for each coefficient, with k counting the intercept; for n = 10 and k = 2 it is 0.99241.

Plain R² never falls when a term is added, but adjusted R² can. An x² term added to these data gets a coefficient of exactly 0, so R² stays 0.99325 while adjusted R² drops to 0.99132.

Is the slope significant?

The test of a zero slope uses t = b₁ ÷ SE(b₁) on n − 2 degrees of freedom, where SE(b₁) = s ÷ √Sxx and s = √(SSE ÷ (n − 2)) is the residual standard error.

Here s = 0.8230, SE(b₁) = 0.8230 ÷ √82.5 = 0.0906 and t = 3.109 ÷ 0.0906 = 34.3 on 8 df. The two-sided p-value is 5.7 × 10⁻¹⁰, so a slope this steep would be very unlikely if hours and scores had no linear relation.

These standard errors assume independent errors with constant variance and a correctly shaped model. Exact small-sample t inference also assumes normally distributed errors.

Predictions and extrapolation

A prediction inside the observed x range, like 72.2 at 7.5 hours, is a fitted average for that x, not a guaranteed score. Outside the range the line keeps going: at 20 hours it predicts 111.1, impossible on a 100-point exam.

Polynomial and exponential fits

A polynomial of degree 2 to 4 adds x², x³ and x⁴ terms, and a degree-d curve needs at least d + 1 distinct x values. With exactly d + 1 points it passes through every one and leaves no residual degrees of freedom, which is no evidence of good prediction.

An exponential fit, y = a·e^(bx), fits a straight line to ln y, so every y must be positive. For x = 0 to 5 and y = 3.0, 4.4, 6.7, 9.8, 14.9 and 22.1, the fit is y = 2.98·e^(0.4007x). Each unit of x multiplies y by e^0.4007 = 1.493, a 49.3% increase.

The exponential R², 0.99985, describes ln y. A straight line on the same data has R² = 0.924 on the original scale, so the two numbers measure different things.

Regression examples

ScenarioInterceptSlopePrediction
Default48.8909093.10909172.209091
Exact line1.0000002.0000009.000000
Negative slope11.800000-1.80000011.800000
Repeated x0.5000002.0000004.500000

"Default" is the study-hours line at x = 7.5. "Exact line" uses (1, 3), (2, 5) and (3, 7), predicting at 4.

"Negative slope" uses x = 1 to 5 with y = 10, 8, 7, 4 and 3, predicting at 0. "Repeated x" uses x = 1, 1, 2, 2, 3, 3 with y = 2 to 7, predicting at 2. Repeated x values are fine as long as at least two distinct values remain.

Entering the data

  • x and y stay in pairs. Sorting one list without the other creates different pairs and a different line.
  • Separate values with spaces, new lines or a comma and a space. A token like 1,234 could be read as one thousands-grouped number.
  • One predictor only. Polynomial terms are powers of the same x, not separate variables.

Mistakes that mislead

  • Reading the slope as cause. Students who study more may differ in other ways that also raise scores.
  • Choosing the highest degree for the highest R². Check adjusted R² and the residual plot; a curved or funnel-shaped residual pattern means the model is missing something.
  • Comparing R² across scales. An exponential fit's R² is on ln y, so it cannot be ranked against a straight line's R² on y.

To measure the strength of the relationship directly, or with ranks through Spearman's coefficient, use the correlation coefficient calculator.

Worked examples

Exact straight line y = 1 + 2x

x values
1, 2, 3
y values
3, 5, 7
Model
Linear
Predict y at x
4
Fitted equation
y = 1 + 2x
Intercept b₀
1
Slope / x coefficient b₁
2
R²
1
Predicted y
9

Checked against: Constructed points (1,3), (2,5), (3,7) lie exactly on y = 1 + 2x; at x = 4, y = 9.

Study hours vs score (linear)

x values
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
y values
55, 58, 62, 64, 68, 71, 72, 78, 80, 83
Model
Linear
Predict y at x
7.5
Slope / x coefficient b₁
3.109091
Intercept b₀
48.890909
R²
0.993252
Adjusted R²
0.992408
Residual standard error
0.822966
Standard error of b₁
0.090606
Standard error of b₀
0.643873
Predicted y
72.209091
Correlation r
0.99662

Checked against: b₁ = Sxy/Sxx, b₀ = ȳ − b₁x̄, se = √(SSE/(n − 2)) with decimal square roots

Quadratic trend

x values
1, 2, 3, 4, 5, 6, 7, 8
y values
2.1, 3.9, 8.2, 14.8, 24.1, 35.9, 50.2, 66.8
Model
Polynomial
Degree
2 (quadratic)
Intercept b₀
2.85
Slope / x coefficient b₁
-1.980952
x² coefficient b₂
1.247619
R²
0.999994
Residual standard error
0.069007

Checked against: Solving the 3×3 normal equations exactly (b₀ = 57/20, b₁ = −208/105, b₂ = 131/105)

Exact quartic y = 1 + x + x² + x³ + x⁴

x values
0 1 2 3 4 5 6 7 8 9 10
y values
1 5 31 121 341 781 1555 2801 4681 7381 11111
Model
Polynomial
Degree
4 (quartic)
Predict y at x
11
Intercept b₀
1
Slope / x coefficient b₁
1
x² coefficient b₂
1
x³ coefficient b₃
1
x⁴ coefficient b₄
1
R²
1
Predicted y
16,105

Checked against: Constructed data: y = 1 + x + x² + x³ + x⁴ exactly, so every coefficient is 1 and R² = 1; 1 + 11 + 121 + 1331 + 14641 = 16105

Questions

How do you calculate the line of best fit?

Find the means x̄ and ȳ, then the slope b₁ = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² and the intercept b₀ = ȳ − b₁x̄. For the default study-hours data, Sxy = 256.5 and Sxx = 82.5, so b₁ = 3.109 and b₀ = 48.89. This least-squares line minimizes the sum of squared vertical distances, as set out in the NIST/SEMATECH e-Handbook (§4.1.4.1).

What does R² tell you in regression?

R² is the fraction of the variation in y that the model accounts for: R² = 1 − SSE/SST. The default fit has R² = 0.9933, so study hours account for 99.3% of the spread in exam scores. For a straight line R² equals the square of the correlation r. A high R² does not show that x causes y, that the model has the right shape, or that predictions beyond the data will hold.

What is the difference between R² and adjusted R²?

Adjusted R² charges for each extra coefficient: 1 − (1 − R²)(n − 1)/(n − k), where k counts the coefficients including the intercept. R² never falls when a term is added, so a quartic always fits at least as well as a quadratic; adjusted R² falls when the new term explains less than it costs. For the default data, n = 10 and k = 2 turn R² = 0.99325 into 0.99241.

What does the p-value of the slope mean?

It tests the null hypothesis that the true slope is 0, using t = b₁/SE(b₁) with n − 2 degrees of freedom. For the default data t = 3.109/0.0906 = 34.3 with 8 df, giving p = 5.7 × 10⁻¹⁰: a slope this steep would be very unlikely if hours and scores had no linear relation. The p-value says nothing about how large or important the effect is.

When should I use an exponential fit instead of a straight line?

When y grows or shrinks by a roughly constant percentage for each unit of x, so that ln y against x is straight. The exponential example, with y rising from 3.0 to 22.1 as x goes from 0 to 5, fits y = 2.98·e^(0.4007x), an increase of 49.3% per unit of x. The fit is made on ln y, as Excel's LOGEST function does, so R² describes ln y rather than y.

About this calculator

b1=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2,b0=yˉ−b1xˉ,R2=1−∑(yi−y^i)2∑(yi−yˉ)2b_1 = \frac{\sum (x_i-\bar x)(y_i-\bar y)}{\sum (x_i-\bar x)^2},\quad b_0 = \bar y - b_1\bar x,\quad R^2 = 1 - \frac{\sum (y_i-\hat y_i)^2}{\sum (y_i-\bar y)^2}

Sources

  1. NIST/SEMATECH e-Handbook of Statistical Methods, §4.1.4.1 Linear least squares regression
  2. NIST Statistical Reference Datasets — linear regression
  3. Draper & Smith, Applied Regression Analysis, 3rd ed. (Wiley, 1998), ch. 1 and 12

Checked against references

This calculator includes 5 worked examples with independently sourced answers. They run in the test suite, and you can run them here too.

Put this calculator on your site

Paste this where you want the calculator to appear. It is free to use, it stays in step with corrections made here, and the credit line below it links back so your readers can find the full version.

Height
Preview the embed

Keep the credit line. It is the only condition, and it is what keeps these calculators free.