Linear Regression Calculator — OLS Fit from Summary Statistics
Fit Y = aX + b from aggregate sums and evaluate slope, intercept, R², RMSE, coefficient uncertainty, and optional 95% prediction intervals.
Enter the known values and review the calculated result
Input parameters
Use consistent values and select the intended engineering units.
Linear regression — aggregated data
Prediction and comparison (optional)
Observed X domain (optional)
Results
Calculation tools Save, export and manage your results
Save your calculation, export available results, or store a finalized engineering artifact in your account.
Method, application and limitations
Review the calculation method, intended application and engineering assumptions before using the result in a design decision.
Formula and calculation method
Simple linear regression from aggregate statistics
This calculator fits the ordinary least-squares model Y = aX + b from summary statistics rather than raw X-Y rows. In this calculator, a is the slope and b is the intercept.
The required inputs are the number of paired observations n and five sums calculated from the same dataset:
- Σx — sum of X values [X]
- Σy — sum of Y values [Y]
- Σx² — sum of squared X values [X²]
- Σy² — sum of squared Y values [Y²]
- Σxy — sum of X·Y products [X·Y]
Means and centered sums
x̄ = Σx / n
ȳ = Σy / n
Sxx = Σx² − (Σx)² / n
Syy = Σy² − (Σy)² / n
Sxy = Σxy − (Σx)(Σy) / n
A valid regression requires Sxx > 0, so the X values cannot all be identical.
Least-squares regression equation
a = Sxy / Sxx
b = ȳ − a·x̄
Ŷ = aX + b
The slope a has units [Y/X]. The intercept b has units [Y].
Correlation and coefficient of determination
For a non-constant Y dataset:
r = Sxy / √(Sxx·Syy)
R² = r²
r is the Pearson linear correlation coefficient. R², the coefficient of determination, is the fraction of the sample Y sum of squares accounted for by the fitted straight-line model.
If all Y values are identical, then Syy = 0. The fitted slope becomes zero and the intercept equals the constant Y value, but r and R² are undefined; the calculator reports this condition as a warning rather than assigning a misleading fit statistic.
Regression and residual sums of squares
SST = Syy
SSR = SST·R²
SSE = SST·(1 − R²)
For a non-constant response, these satisfy SST = SSR + SSE.
Fit error
The calculator reports two related error measures:
RMSE = √(SSE / n)
This is the in-sample root mean squared error and has units [Y].
For n > 2, the residual degrees of freedom are:
df = n − 2
MSE = SSE / (n − 2)
s = √MSE
The residual standard error s therefore differs from the displayed RMSE because it uses the residual degrees of freedom rather than n.
Coefficient uncertainty
When n > 2:
SE(a) = s / √Sxx
SE(b) = s·√(1/n + x̄²/Sxx)
The calculator also reports:
Adjusted R² = 1 − (1 − R²)(n − 1)/(n − 2)
t = a / SE(a)
The t statistic is reported for the fitted slope when residual variance is non-zero. The calculator does not calculate a p-value or issue a statistical-significance verdict.
Prediction at a selected X value
If x_test is supplied, prediction is evaluated in the numerically centered form:
ŷ = ȳ + a(x_test − x̄)
This is mathematically equivalent to ŷ = a·x_test + b.
If an observed comparison value y_actual is also supplied:
e = y_actual − ŷ
Relative error = e / y_actual
Relative error is omitted when y_actual = 0.
95% confidence and prediction intervals
When n > 2, residual standard error is greater than zero, and x_test is provided, the calculator uses a two-sided Student-t critical value with df = n − 2.
Define the leverage term:
h = 1/n + (x_test − x̄)²/Sxx
The 95% confidence interval for the mean response is:
ŷ ± t0.975, n−2·s·√h
The 95% prediction interval for a new individual observation is:
ŷ ± t0.975, n−2·s·√(1 + h)
The prediction interval is wider because it includes both uncertainty in the fitted mean response and residual variation of a new observation.
Observed X range
Optional x_min and x_max values do not change the regression fit. They define the observed X domain so the calculator can flag a prediction made outside the supplied range as extrapolation.
When to use this calculator
When to use this linear regression calculator
Use this calculator when you need a simple linear regression or least-squares regression line but your data are already available as aggregate statistics rather than as individual X-Y pairs.
Typical applications include engineering calibration data, summarized test results, process studies, experimental datasets, trend estimation, and checking a previously calculated regression equation from spreadsheet or reporting-system totals.
The calculator is particularly useful when you need to determine:
- the slope and intercept of the least-squares line;
- the linear regression equation Y = aX + b;
- Pearson correlation r and R squared;
- SSE, SSR, SST, RMSE and residual standard error;
- standard errors of the fitted slope and intercept;
- adjusted R² and the slope t statistic;
- a predicted Y value at a selected X;
- prediction residual and relative error when an actual Y value is known;
- 95% mean-response and prediction intervals when sufficient residual degrees of freedom exist;
- whether a requested prediction lies outside the supplied observed X range.
Required preparation
The calculator does not accept a raw table of X-Y observations. If raw measurements are available, first calculate n, Σx, Σy, Σx², Σy² and Σxy from exactly the same paired observations.
Keep the physical units consistent. If X is measured in kN and Y in mm, for example, Σx² must be in kN², Σy² in mm² and Σxy in kN·mm.
When this calculator is not the appropriate model
Do not use this tool as a substitute for multiple regression, nonlinear regression, weighted least squares, robust regression, orthogonal regression, a forced-through-origin fit, or a time-series model. It implements one-predictor ordinary least-squares regression with an intercept.
Because only aggregate statistics are supplied, the calculator cannot display individual residuals, identify influential observations, inspect a scatter plot, detect curvature, or diagnose residual patterns. If raw observations are available and model validity matters, inspect the original data and residual diagnostics in addition to the numerical regression summary.
How to interpret the result
How to interpret the regression results
| Result | Meaning | Important caution |
|---|---|---|
| Slope a | Fitted change in Y per unit change in X. | A slope describes association in the fitted model; it does not establish causation. |
| Intercept b | Fitted Y when X = 0. | Its physical meaning may be limited when zero lies outside the meaningful or observed X range. |
| R² | Fraction of sample Y sum of squares represented by the fitted line. | It does not test residual assumptions or prove that a linear model is appropriate. |
| r | Direction and strength of the linear association. | Correlation is undefined when Y is constant. |
| RMSE | In-sample root mean squared residual using divisor n. | Interpret its magnitude in the physical units and required accuracy of Y. |
| Residual standard error s | Residual scatter estimate using n−2 degrees of freedom. | Available only when n > 2. |
| SE(a), SE(b) | Estimated uncertainty of the fitted slope and intercept. | Their inferential interpretation depends on the underlying regression assumptions. |
| Adjusted R² | R² corrected for sample size and the fitted predictor count used by this simple model. | Unlike the primary R², adjusted R² can be negative for a sufficiently weak fit. |
| t statistic | Slope divided by its standard error. | The calculator does not return a p-value or automatically decide statistical significance. |
| Predicted Y | Value of the fitted line at x_test. | A fitted value is not a guarantee that a future observation will equal the prediction. |
| Prediction residual | y_actual minus predicted Y. | Its sign indicates whether the supplied actual observation lies above or below the fitted prediction. |
Warnings and edge cases
Extrapolation warning: when x_min and x_max are provided and x_test lies outside that range, the prediction is flagged. The regression equation itself is unchanged, but confidence in the model outside the observed domain normally requires additional engineering or statistical justification.
Two-point warning: a line can be fitted to two distinct X values, but there are no residual degrees of freedom for residual inference.
Constant-response warning: when all Y values are identical, the constant fitted response is valid but r and R² are undefined.
Aggregate-precision warning: large nearly cancelling aggregate terms can reduce the numerical reliability of Sxx or Syy.
This calculator does not apply a universal PASS, WARNING or FAIL threshold to R², RMSE, slope, residual error or interval width. Acceptability depends on the measurement system, process, engineering tolerance, required predictive accuracy and intended use of the regression model.
Calculation example
Worked example — regression from calibration summary statistics
Suppose five calibration observations relate an applied load X [kN] to a sensor output Y [mV]. The original pairs are used here only to show how the aggregate inputs are formed:
| X [kN] | Y [mV] | X² [kN²] | Y² [mV²] | X·Y [kN·mV] |
|---|---|---|---|---|
| 1 | 2 | 1 | 4 | 2 |
| 2 | 4 | 4 | 16 | 8 |
| 3 | 5 | 9 | 25 | 15 |
| 4 | 4 | 16 | 16 | 16 |
| 5 | 5 | 25 | 25 | 25 |
The calculator inputs are therefore:
- n = 5
- Σx = 15 kN
- Σy = 20 mV
- Σx² = 55 kN²
- Σy² = 86 mV²
- Σxy = 66 kN·mV
For an optional prediction check, use:
- x_test = 3.5 kN
- y_actual = 4.1 mV
- x_min = 1 kN
- x_max = 5 kN
Step 1 — means
x̄ = 15 / 5 = 3 kN
ȳ = 20 / 5 = 4 mV
Step 2 — centered sums
Sxx = 55 − 15²/5 = 10 kN²
Syy = 86 − 20²/5 = 6 mV²
Sxy = 66 − (15·20)/5 = 6 kN·mV
Step 3 — slope, intercept and regression equation
a = 6 / 10 = 0.6 mV/kN
b = 4 − 0.6·3 = 2.2 mV
The fitted linear regression equation is:
Ŷ = 0.6X + 2.2
Step 4 — correlation and R²
r = 6 / √(10·6) = 0.774597
R² = 0.774597² = 0.600000
The fitted line accounts for 60% of the sample Y sum of squares. This is a fit statistic, not proof that X causes Y or that the linear model is adequate for the underlying process.
Step 5 — residual error
SSR = 6·0.6 = 3.6 mV²
SSE = 6·(1 − 0.6) = 2.4 mV²
RMSE = √(2.4/5) = 0.692820 mV
With df = 5 − 2 = 3:
MSE = 2.4/3 = 0.800000 mV²
s = √0.8 = 0.894427 mV
The coefficient standard errors are:
SE(a) = 0.894427/√10 = 0.282843 mV/kN
SE(b) = 0.938083 mV
The additional reported statistics are:
Adjusted R² = 0.466667
t = 0.6/0.282843 = 2.121320 with 3 residual degrees of freedom.
Step 6 — prediction at X = 3.5 kN
ŷ = 4 + 0.6(3.5 − 3) = 4.3 mV
Compared with the supplied actual value of 4.1 mV:
e = 4.1 − 4.3 = −0.2 mV
Relative error = −0.2/4.1 = −0.048780, or approximately −4.88%.
Step 7 — 95% intervals
For x_test = 3.5 kN:
h = 1/5 + (3.5 − 3)²/10 = 0.225
Using the two-sided 95% Student-t critical value for df = 3, the calculator returns approximately:
- 95% confidence interval for the mean response: 2.949802 to 5.650198 mV
- 95% prediction interval for a new observation: 1.149539 to 7.450461 mV
The prediction point lies inside the supplied observed range of 1 to 5 kN, so no extrapolation warning is generated. The broad intervals also show why a five-point dataset with substantial residual scatter should not be judged from the fitted equation or R² alone.
Assumptions and limitations
Assumptions and limitations
- Simple linear model: the implemented model contains one predictor X, one response Y, one slope and one intercept.
- Ordinary least squares: the fit minimizes squared vertical residuals in Y. It does not model uncertainty in X as a separate measurement-error process.
- Common source dataset: n, Σx, Σy, Σx², Σy² and Σxy must all be calculated from exactly the same paired observations.
- Variation in X: Sxx must be positive. A dataset with constant X cannot define a regression slope.
- Consistent units: all aggregate statistics must preserve the corresponding X, Y, squared and cross-product units.
- Model form: a straight-line fit is only meaningful when a linear relationship is appropriate over the region of interest.
- Inference: standard errors, the slope t statistic, and the 95% intervals rely on the usual simple-OLS error model assumptions. The calculator cannot verify those assumptions from aggregate sums alone.
- No residual diagnostics: aggregate statistics do not preserve individual residuals, so the tool cannot detect curvature, outliers, influential points, changing residual variance, serial dependence or non-normal residual patterns.
- No causal conclusion: regression and correlation quantify an observed linear relationship; they do not establish that changes in X cause changes in Y.
- Extrapolation: predictions outside the observed X domain may be unreliable even when the fitted equation has a high R². The calculator can flag extrapolation only when x_min and x_max are supplied.
- No alternate regression models: weighted, robust, nonlinear, polynomial, multivariable and zero-intercept regressions are outside the implemented scope.
- Floating-point precision: regression from large aggregate sums can suffer cancellation when the underlying spread is small relative to the magnitudes of the totals. The calculator detects important precision-risk cases but cannot reconstruct lost information from the raw observations.
Validation
Input and consistency validation
The calculator validates the aggregate dataset before returning regression results. The principal checks are:
- n must be a safe integer of at least 2;
- Σx, Σy, Σx², Σy² and Σxy must all be finite numbers;
- Σx² and Σy² cannot be negative;
- Sxx must be positive;
- Syy cannot be materially negative after allowance for floating-point rounding;
- the aggregates must satisfy the covariance consistency bound |Sxy| ≤ √(Sxx·Syy), within numerical tolerance;
- y_actual requires x_test;
- x_min and x_max must either both be supplied or both omitted;
- when supplied, x_min must be less than x_max;
- the supplied observed X range must contain the sample mean x̄;
- optional prediction and domain values must be finite.
Inputs that violate these conditions are rejected rather than being converted into apparently valid regression statistics.
