Linear Regression Calculator
Calculate linear regression line y = mx + b with slope, intercept, and coefficient of determination (R²) using least squares method. Free online linear regression calculator.
What is Linear Regression?
Linear regression is a statistical method that fits a straight line y = mx + b to a set of (x, y) data points using ordinary least squares (OLS). The fitted line lets you predict y for any given x and quantifies how strongly x and y are related. OLS picks the slope (m) and intercept (b) that minimize the sum of squared vertical distances (residuals) between each observed point and the line. For predictions beyond your data range, use the Linear Extrapolation Calculator, or explore more statistical tools with our Statistics Calculator.
The coefficient of determination (R²) measures how well the line explains the variability in y. An R² of 1 means a perfect fit where every point lies exactly on the line, while R² = 0 means the line explains no more variance than using the mean of y as a constant prediction. Linear regression is the foundation of calibration curves, sales forecasting, dose-response analysis, A/B testing, and countless other scientific and business applications.
How to Use the Linear Regression Calculator
Enter your (x, y) data points in the input table on the left. The calculator supports 2 or more points. You can add or remove points using the buttons above the table. The regression results update automatically as you type, showing the slope, intercept, R² value, and per-point residuals.
Linear Regression Formula
The least-squares estimates are computed using these closed-form formulas:
Slope: m = Σ(xᵢ − x̄)(yᵢ − ṳ) / Σ(xᵢ − x̄)²
Intercept: b = ṳ − m · x̄
Coefficient of determination: R² = 1 − SSᵥₑₛ / SSₜₒₜ
Where x̄ and ṳ are the sample means of x and y, SSᵥₑₛ = Σ(yᵢ − ṹᵢ)² is the residual sum of squares, and SSₜₒₜ = Σ(yᵢ − ṳ)² is the total sum of squares. The fitted line always passes through the centroid (x̄, ṳ), and the residuals sum to zero by construction.
Applications
- Calibration curves: Analytical chemistry uses linear regression to convert instrument signal (absorbance, voltage, peak area) into concentration.
- Sales forecasting: Regression of historical sales on time or advertising spend produces trend lines for budget planning.
- Dose-response analysis: Linear models of drug dose versus measured effect underpin clinical trial baselines.
- Engineering: Stress-strain curves, beam deflection, and empirical equations in heat transfer start as linear fits.
- Real estate: Hedonic pricing models regress sale price on square footage, bedrooms, and location features.
Common Mistakes to Avoid
- Using R² to validate a non-linear relationship. A curved dataset can still produce a moderate R² with a fitted line that is meaningless.
- Extrapolating outside the data range. The line is only reliable within the x values you observed.
- Ignoring outliers. OLS squares residuals, so a single extreme point can dramatically pull the slope.
- Confusing correlation with causation. A high R² between x and y does not mean x causes y.
Frequently Asked Questions
What is linear regression?
Linear regression fits a straight line y = mx + b to (x, y) data using ordinary least squares. It lets you predict y from x and quantifies the strength of their linear relationship.
How do you calculate the slope in linear regression?
Compute the means x̄ and ȳ, then slope m = Σ(xᵢ − x̄)(yᵢ − ȳ) / Σ(xᵢ − x̄)². The intercept follows as b = ȳ − m·x̄.
What is R-squared (R²)?
R² is the coefficient of determination. It measures the fraction of variance in y that the regression line explains, ranging from 0 (no linear relationship) to 1 (perfect fit).
What is the difference between regression and correlation?
Pearson's r measures the strength and direction of a linear association on a scale from -1 to +1. Regression produces a predictive equation. For simple linear regression, R² equals Pearson's r squared.
When should you not use linear regression?
Avoid linear regression when the data is clearly curved, residuals fan out as x increases (heteroscedasticity), outliers dominate the fit, or the response is binary or count-valued.