# 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.

Interactive version: https://www.calcopenly.com/statistics/regression-calculator
Subject: Statistics and probability calculators

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. Each coefficient gets a standard error, a t statistic and a p-value, and R² gives the share of the variation in y that the fit accounts for.

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.

The standard errors and p-values assume independent residuals with constant spread. 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.

## Inputs

- **x values**: Weekly study hours (default example).
- **y values**: Exam scores, in the same order as x.
- **Model** (options: Linear, Polynomial, Exponential)
- **Degree** (options: 2 (quadratic), 3 (cubic), 4 (quartic))
- **Predict y at x**

## Results

- Fitted equation — main result
- Intercept b₀
- Slope / x coefficient b₁
- x² coefficient b₂
- x³ coefficient b₃
- x⁴ coefficient b₄
- a in y = a·e^(bx)
- b in y = a·e^(bx)
- Correlation r
- R²
- Adjusted R²
- Residual standard error
- Standard error of b₀
- Standard error of b₁
- Predicted y
- Points n

## Formula

$$
b_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}
$$

## 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: Python fractions: 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: Python fractions solving the 3×3 normal equations exactly (b₀ = 57/20, b₁ = −208/105, b₂ = 131/105); agrees with numpy.polyfit(x, y, 2)

### 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

### Exponential growth

- x values: 0, 1, 2, 3, 4, 5
- y values: 3.0, 4.4, 6.7, 9.8, 14.9, 22.1
- Model: Exponential
- Predict y at x: 6
- **a in y = a·e^(bx): 2.979832**
- **b in y = a·e^(bx): 0.400696**
- **R²: 0.99985**
- **Predicted y: 32.98473**
- Checked against: Python decimal ln(y) at 50 digits, least squares with fractions, a = exp(intercept); numpy.polyfit(x, log(y), 1) gives b = 0.40069633

## 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.

### How accurate is the linear regression calculator?

Accuracy depends on your inputs and the method's assumptions. Decimal arithmetic uses 50 significant digits, but estimates, numerical methods and source data can be less precise; the displayed rounding does not remove those limits. It is checked against 5 worked examples whose answers come from independent sources; for example, “Exact straight line y = 1 + 2x” is checked against Constructed points (1,3), (2,5), (3,7) lie exactly on y = 1 + 2x; at x = 4, y = 9.

### Where does the method come from?

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

## Sources

- [NIST/SEMATECH e-Handbook of Statistical Methods, §4.1.4.1 Linear least squares regression](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd141.htm)
- [NIST Statistical Reference Datasets — linear regression](https://www.itl.nist.gov/div898/strd/lls/lls.shtml)
- Draper & Smith, Applied Regression Analysis, 3rd ed. (Wiley, 1998), ch. 1 and 12
