Math

Linear Regression Calculator (Least Squares)

Calculator Free · Private
Reviewed by: (política editorial ) · Last reviewed:
Was this calculator helpful?

Simple linear regression finds the best-fit straight line through a set of (x, y) data points using Ordinary Least Squares (OLS) — it minimizes the total squared vertical distance between each observed point and the fitted line. The result is the equation ŷ = mx + b, where m is the slope (how much y changes per unit increase in x) and b is the y-intercept (predicted y when x = 0). The coefficient of determination R² (0 to 1) quantifies goodness of fit: for example, R² = 0.85 means the linear model accounts for 85% of the variability in y. OLS was developed independently by Carl Friedrich Gauss (1809) and Adrien-Marie Legendre (1805) and remains the workhorse of empirical research two centuries later. Applications span econometrics, epidemiology, finance (CAPM beta estimation), engineering calibration curves, machine learning (baseline before ridge/lasso/neural nets), sports analytics and education research. When to use this calculator: quantify a linear relationship between two continuous variables, forecast future values from past data, verify whether two variables are linearly associated, or simply reproduce and verify the regression equation that statistical software produces. For confidence intervals, hypothesis tests, residual diagnostics and multicollinearity, step up to R, Python statsmodels, or SPSS.

Last reviewed: June 3, 2026 Verified by Source: NIST/SEMATECH e-Handbook of Statistical Methods — Linear Least Squares Regression, Wikipedia — Simple Linear Regression, NIST/SEMATECH e-Handbook — Measures of Fit (R²), Khan Academy — Introduction to residuals and least-squares regression 100% private

Simple linear regression fits the line ŷ = mx + b by minimizing squared residuals. The slope is m = [n·Σxy − Σx·Σy] / [n·Σx² − (Σx)²] and the intercept is b = (Σy − m·Σx) / n. R² measures goodness of fit: R² = 0.85 means x explains 85% of the variance in y. For example, with X = (1, 2, 3) and Y = (2, 4, 5): m ≈ 1.500, b ≈ 0.667, giving ŷ = 1.5x + 0.667 with R² ≈ 0.964.

When to use this calculator

  • Estimating a student's exam score (y) from hours studied (x) — example: y = 8.5x + 20, predicting 105 points for 10 hours of study with R² = 0.78.
  • Analyzing whether monthly ad spend (x, USD) predicts monthly sales revenue (y, USD) — example: an extra $1,000 ad spend yields $4,200 incremental revenue (m = 4.2) with R² = 0.83.
  • Fitting a chemistry calibration curve relating instrument signal (x, absorbance) to known concentration (y, mg/L) to back-calculate unknowns — R² ≥ 0.999 is typical.
  • Modeling outdoor temperature (x, °F) vs daily household electricity use (y, kWh) — example: each 1°F drop adds 0.6 kWh/day, enabling utility-bill forecasting.
  • Estimating used-car prices (y, USD) as a function of vehicle age (x, years) — example: each year of age drops resale value by $1,500 (m = −1,500) with R² = 0.65.
  • Tracking e-commerce conversion rate (y, %) vs page load time (x, seconds) — example: each extra second costs 2.3 percentage points in conversion (m = −2.3).
  • Sports analytics: predicting marathon finish time (y, min) from weekly training mileage (x) — each extra weekly mile shaves 1.8 min off finish time (m = −1.8).
  • Real estate: estimating home prices (y, $1000s) from square footage (x) — each extra sq ft adds $180 (m = 0.18), R² typically 0.55–0.75.

Worked Example

  1. X = 1, 2, 3, 4, 5 | Y = 2, 4, 5, 4, 5
  2. Σx=15, Σy=20, Σxy=63, Σx²=55, n=5
  3. m = (5·63 − 15·20) / (5·55 − 15²) = (315−300)/(275−225) = 15/50 = 0.300
  4. b = (20 − 0.3·15)/5 = (20−4.5)/5 = 3.100
  5. ŷ = 0.300x + 3.100, R² ≈ 0.375 (weak-to-moderate fit)
Result: ŷ = 0.300x + 3.100, R² ≈ 0.375

How it works

3 min read

How Least-Squares Regression Is Calculated

OLS finds the unique slope m and intercept b that minimize the sum of squared residuals:

Minimize: S(m, b) = Σᵢ [yᵢ − (m·xᵢ + b)]²

Solving the normal equations gives the closed-form solution:

Given n data pairs (x₁,y₁), …, (xₙ,yₙ):

─── Intermediate sums ────────────────────────────────────
Σx   = x₁ + x₂ + … + xₙ
Σy   = y₁ + y₂ + … + yₙ
Σxy  = x₁y₁ + x₂y₂ + … + xₙyₙ
Σx²  = x₁² + x₂² + … + xₙ²
x̄   = Σx / n
ȳ   = Σy / n

─── Slope ──────────────────────────────────────────────────
m = [n·Σxy − Σx·Σy] / [n·Σx² − (Σx)²]
  = Σ[(xᵢ − x̄)(yᵢ − ȳ)] / Σ[(xᵢ − x̄)²]

─── Intercept ──────────────────────────────────────────────
b = (Σy − m·Σx) / n  =  ȳ − m·x̄

─── Fitted line ────────────────────────────────────────────
ŷ = m·x + b

─── Residuals & R² ─────────────────────────────────────────
eᵢ     = yᵢ − ŷᵢ                (residual for point i)
SS_res = Σeᵢ²                   (sum of squared residuals)
SS_tot = Σ(yᵢ − ȳ)²            (total sum of squares)
R²     = 1 − SS_res / SS_tot   (coefficient of determination)

> Note: In simple regression with one predictor, R² = r², where r is the Pearson correlation coefficient.

---

R² Reference Table

R² RangeInterpretationTypical Field
0.00 – 0.19Very weak / negligibleSocial-science surveys
0.20 – 0.39WeakEarly-stage exploratory biology
0.40 – 0.59ModerateEconomics / marketing models
0.60 – 0.79StrongClimate vs. energy-use regression
0.80 – 0.94Very strongEngineering calibration curves
0.95 – 1.00Near-perfectPhysical law verification

Also useful — Pearson |r| interpretation:

rStrength
0.00 – 0.10Negligible
0.10 – 0.39Weak
0.40 – 0.69Moderate
0.70 – 0.89Strong
0.90 – 1.00Very strong

---

Common Values Table

Here are common X datasets and resulting regression parameters for quick reference:

X valuesY valuesmb
1, 2, 32, 4, 51.5000.6670.964
1, 2, 3, 4, 52, 4, 5, 4, 50.3003.1000.375
1, 2, 3, 4, 55, 4, 3, 2, 1−1.0006.0001.000
10, 20, 30, 40, 5014, 17, 19, 22, 250.23011.8000.993
1, 2, 3, 4, 51, 4, 9, 16, 255.400−5.0000.953

---

Worked Examples

Example 1 — Exam scores vs. hours studied (n = 3)

x (hours)y (score)
12
24
35

Σx=6, Σy=11, Σxy=25, Σx²=14, n=3

m = (3·25 − 6·11) / (3·14 − 6²) = (75−66)/(42−36) = 9/6 = 1.5
b = (11 − 1.5·6)/3 = 2/3 ≈ 0.667
→ ŷ = 1.5x + 0.667,  R² ≈ 0.964

Example 2 — Ad spend vs. revenue (n = 5)

x ($00s)y ($000s)
114
217
319
422
525

m = 2.3,  b = 11.5
→ ŷ = 2.3x + 11.5,  R² ≈ 0.993

---

Common Errors

1. Reversing x and y — OLS is NOT symmetric. Always put the predictor as x and the response as y.
2. Extrapolating far beyond the data range — the fitted line is only reliable within (and near) the observed x range.
3. Confusing R² with causation — a high R² only means linear fit, not that x causes y (spurious correlations can produce R² ≈ 0.95).
4. Using regression with too few data points — n = 2 always gives R² = 1.0 (meaningless); use n ≥ 10–20 for reliable OLS estimates.
5. Ignoring outliers — OLS squares residuals, so a single extreme point can dramatically shift m and b. Always plot your data first.
6. Assuming linearity without checking — if the true relationship is curved, a linear fit gives misleading m and deflated R².

---

Related Calculators

  • Pearson Correlation Coefficient

  • Mean, Median & Mode

  • Standard Deviation Calculator

  • Z-Score Calculator

  • Variance Calculator

  • Frequently asked questions

    What does the slope m mean in linear regression?

    The slope m tells you how much y changes for each one-unit increase in x. For example, m = 2.3 means y rises by 2.3 units when x increases by 1. If m is negative (e.g., m = −1.5), y decreases as x increases — an inverse relationship. The slope has units equal to (units of y) / (units of x), so if x is hours and y is dollars, m is dollars per hour.

    What does an R² of 0.85 actually mean?

    R² = 0.85 means that 85% of the total variability in your y values is explained by the linear relationship with x. The remaining 15% is unexplained — measurement noise, omitted variables, or non-linear effects. According to NIST/SEMATECH guidelines, R² ≥ 0.80 is considered a very strong fit in most applied fields. However, a model with R² = 0.85 can still produce poor predictions if residuals show systematic patterns. Always combine R² with a residual plot.

    How many data points do I need for linear regression to be reliable?

    A commonly cited rule of thumb is at least n = 10–20 observations for simple linear regression. With n < 5 the estimates are highly unstable. For inference (hypothesis testing on the slope), n = 10 ensures the t-distribution approximation is reasonable. For reliable prediction, n = 30+ is preferred. For machine-learning baselines and industrial calibrations, n = 100+ is the working minimum.

    Can R² be negative?

    Technically yes — R² can be negative if you compute it for a model that was not fit by OLS on your specific data (e.g., you imposed a fixed slope from external knowledge, or you evaluate on a test set with a model trained on different data). A negative R² means the model fits worse than simply predicting the mean ȳ for every observation. When you use standard OLS on the same data you fit on, R² is always between 0 and 1 by construction.

    What is the difference between simple and multiple linear regression?

    Simple linear regression uses one predictor (x): ŷ = mx + b. Multiple linear regression uses two or more: ŷ = b₀ + b₁x₁ + b₂x₂ + … Adding more predictors almost always increases R², even if irrelevant — which is why adjusted R² penalizes unnecessary predictors in multiple regression. Multiple regression also introduces multicollinearity (predictors correlated with each other) and higher overfitting risk when predictors outnumber observations.

    Is a high R² always better?

    No. In social sciences and economics, R² values of 0.30–0.50 are common and meaningful because human behavior is noisy. In physics or chemistry, R² < 0.99 might indicate a measurement problem. Context matters: an overfitted model on small data can show R² ≈ 1.0 yet predict new observations poorly. Always use cross-validation (train/test split, or k-fold) for an honest assessment of predictive power.

    What assumptions does OLS regression make?

    OLS makes four classical assumptions (acronym LINE): (1) Linearity — the true relationship is linear in the parameters. (2) Independence — observations are not correlated with each other (critical violation: time-series autocorrelation). (3) Normality of residuals — residuals are approximately normal (important for inference, less critical for point estimates). (4) Equal variance (homoscedasticity) — residual variance is constant across all x values. Check assumptions via residual plots, Q-Q plots, Durbin-Watson (autocorrelation), and Breusch-Pagan (heteroscedasticity) tests.

    How is Pearson r related to R² in simple regression?

    In simple linear regression (one predictor), R² = r², where r is the Pearson correlation coefficient. For example, r = 0.90 gives R² = 0.81. The sign of r tells you the direction (positive or negative slope) which R² alone does not — R² is always ≥ 0 regardless of direction. This relationship breaks down in multiple regression. Use r when you want a symmetric association metric; use R² when you want the fraction of variance explained.

    Can I use linear regression if my data has outliers?

    Use it with caution. OLS minimizes squared residuals, giving disproportionate weight to extreme points. A single outlier can shift m dramatically and inflate or deflate R². Best practice: (1) always plot a scatter diagram first; (2) identify outliers via residual plots, Cook's distance, or studentized residuals; (3) investigate before removing — is the outlier a data-entry error or a genuine observation? (4) consider robust alternatives (Theil-Sen estimator, Huber regression) for a sanity check.

    When should I use log-transformations or polynomial regression instead?

    Use log-transformations when the relationship is multiplicative (e.g., economic elasticities, incomes spanning orders of magnitude, or when residuals fan out with x). Common transforms: log y on log x (elasticity model), log y on x (exponential growth), y on log x (logarithmic growth). Use polynomial regression (y = a + bx + cx² + …) when a scatter plot shows clear curvature. Limit polynomial degree to 2 or 3 to avoid wild oscillations (Runge's phenomenon). If the relationship is more complex, consider spline regression or theory-derived non-linear forms.

    What is the difference between regression and correlation?

    Correlation (Pearson r) measures the strength and direction of a linear association symmetrically — it tells you whether x and y move together but produces no predictive equation. Regression fits the equation ŷ = mx + b for prediction — it is asymmetric (regressing y on x gives a different line than regressing x on y). Use correlation when both variables are exchangeable and you only want to quantify association. Use regression when one variable is naturally the predictor and you want to predict or estimate effect sizes.

    How do I interpret confidence intervals and p-values for the slope?

    A 95% confidence interval for the slope like [1.8, 2.6] means that if you repeated the sampling and regression many times, 95% of such intervals would contain the true slope. The p-value for the slope tests H₀: m = 0 (no linear relationship). A small p-value (typically p < 0.05) means you reject H₀ and conclude there is evidence of a linear association. Note: statistical significance is not the same as practical significance — a tiny slope can have a tiny p-value with enough data. Always report the point estimate and confidence interval, not just the p-value.

    What should I do after the regression to validate the model?

    Standard post-regression checks: (1) Scatter plot — visually confirm the relationship looks linear and check for outliers. (2) Residual plot (residuals vs. fitted values ŷ) — random scatter around zero is good; systematic curves or fans indicate non-linearity or heteroscedasticity. (3) Q-Q plot of residuals — checks normality assumption needed for inference. (4) Cook's distance — flags influential observations that disproportionately affect the estimates. (5) Cross-validation — split data into train/test sets and compare predicted vs. actual values on the test set to estimate true prediction error.

    Sources and references