Skip to content

Linear Regression Calculator

Least-squares line ŷ = a + bx from (x, y) data: slope, intercept, r, r², standard error, residuals and predictions.

Your details

Data points

One row per observation: x is the explanatory variable, y the response you want to predict.

Optional. Leave blank to skip the prediction.

Results update as you type

Regression line

ŷ = 49.538 + 2.8696x

Slope b
2.869565
Intercept a
49.538043
Predicted ŷ
78.2337
Correlation r
0.9793
Coefficient of determination r²
0.9591
Standard error of estimate s
2.3206
Standard error of the slope
0.2419
p-value (slope = 0)
2.2 × 10⁻⁵
Number of points n
8

Step-by-step solution
  1. x̄ = 7 ; ȳ = 69.625
  2. Sxx = Σ(x − x̄)² = 92 ; Sxy = Σ(x − x̄)(y − ȳ) = 264
  3. b = Sxy / Sxx = 2.869565 ; a = ȳ − b·x̄ = 49.538043
  4. r = Sxy / √(Sxx·Syy) = 0.979334 ; r² = 0.959095
  5. s = √(SSE / (n − 2)) = √(32.309783 / 6) = 2.320552
  6. ŷ(10) = 49.538043 + 2.869565 × 10 = 78.2337

Fitted values and residuals

Simple linear regression finds the straight line that best describes how a response yy changes with an explanatory variable xx: exam score against hours of study, fuel used against distance, sales against advertising spend. "Best" has a precise meaning here: the least-squares line is the one that makes the sum of the squared vertical distances between the points and the line as small as possible.

Type or paste your pairs, one row per observation, and the calculator returns:

  • the regression equation y^=a+bx\hat{y} = a + bx, with the slope bb and the intercept aa;
  • the correlation coefficient rr and the coefficient of determination r2r^2;
  • the standard error of estimate ss, the typical size of a prediction error;
  • the standard error of the slope and the p-value for "no linear relationship";
  • a prediction y^\hat{y} at any xx you choose;
  • a table of fitted values and residuals, and a chart of the data with the fitted line.

Each step of the calculation is shown with your numbers, so you can follow the method or check homework done by hand.

How to use this calculator

  1. Enter one (x,y)(x, y) pair per row. Put the variable you want to predict in the y column. Add rows as needed (up to 200); blank rows are ignored.
  2. Optionally enter an xx value in Predict y at x = to get the predicted y^\hat{y}.
  3. Read the equation at the top, then rr, r2r^2 and the standard error. Open the steps to see SxxS_{xx}, SxyS_{xy} and how bb and aa were obtained.
  4. Scan the residual column: large residuals point to outliers, and a curved pattern means a straight line is the wrong model.

Swapping the columns gives a different line: regressing xx on yy is not the same as regressing yy on xx, because the errors are measured vertically.

The formula

With xˉ\bar{x} and yˉ\bar{y} the means, define the sums of squares and cross-products

Sxx=∑(xi−xˉ)2Syy=∑(yi−yˉ)2Sxy=∑(xi−xˉ)(yi−yˉ)S_{xx} = \sum (x_i - \bar{x})^2 \qquad S_{yy} = \sum (y_i - \bar{y})^2 \qquad S_{xy} = \sum (x_i - \bar{x})(y_i - \bar{y})

The least-squares slope and intercept are

b=SxySxxa=yˉ−b xˉb = \frac{S_{xy}}{S_{xx}} \qquad a = \bar{y} - b\,\bar{x}

so the line always passes through the point of means (xˉ,yˉ)(\bar{x}, \bar{y}). The correlation and the standard error of estimate are

r=SxySxx Syys=∑(yi−y^i)2n−2r = \frac{S_{xy}}{\sqrt{S_{xx}\,S_{yy}}} \qquad s = \sqrt{\frac{\sum (y_i - \hat{y}_i)^2}{n - 2}}

The slope's standard error is s/Sxxs/\sqrt{S_{xx}}, and t=b / (s/Sxx)t = b \,/\, (s/\sqrt{S_{xx}}) follows a Student tt distribution with n−2n - 2 degrees of freedom when the true slope is zero.

Worked example

Eight students report their hours of study xx and their exam score yy: (2, 54), (3, 61), (5, 62), (6, 68), (8, 70), (9, 77), (11, 79), (12, 86) — the calculator's default data.

The means are xˉ=7\bar{x} = 7 and yˉ=69.625\bar{y} = 69.625, with Sxx=92S_{xx} = 92 and Sxy=264S_{xy} = 264. Hence

b=26492=2.8696a=69.625−2.8696×7=49.538b = \frac{264}{92} = 2.8696 \qquad a = 69.625 - 2.8696 \times 7 = 49.538

The regression line is ŷ = 49.538 + 2.8696x: each extra hour of study is associated with about 2.8696 more points. The correlation is r=0.9793r = 0.9793, so r2=0.9591r^2 = 0.9591: about 95.9 % of the variation in scores is explained by study time. The standard error of estimate is s=2.3206s = 2.3206 points.

For a student who studies 10 hours, the predicted score is y^=78.23\hat{y} = 78.23, give or take roughly 2.32 points.

Reading r, r² and the standard error

The slope is the change in y^\hat{y} for a one-unit increase in xx, in the units of yy per unit of xx. The intercept is the predicted yy at x=0x = 0, which is only meaningful when 0 lies inside the range of your data.

r measures the strength and direction of the linear relationship, from −1 to +1; it has the same sign as the slope. r² is the share of the variance of yy accounted for by the line: r2=0.81r^2 = 0.81 means 81 % of the spread in yy is explained by xx and 19 % is left in the residuals. The standard error of estimate ss is in the units of yy: roughly two-thirds of the points lie within ±s\pm s of the line when the residuals are about normal. For correlation alone, including Spearman's rank coefficient, use the correlation calculator.

Residuals, outliers and extrapolation

A residual y−y^y - \hat{y} is the vertical gap between a point and the line. Residuals always add up to zero. Plotted against xx, they should look like a random cloud; a U-shape means the relationship is curved, and a funnel means the spread grows with xx. A single point far from the others can tilt the whole line, so check large residuals before drawing conclusions.

Predictions are reliable only within the range of the observed xx values. Outside it (extrapolation) nothing guarantees that the straight-line pattern continues, and the calculator warns you. Finally, a strong regression shows association, not causation: a third variable can drive both xx and yy.

Frequently asked questions

How do I find the line of best fit?

Compute the means of x and y, then Sxx and Sxy. The slope is b = Sxy / Sxx and the intercept is a = ȳ − b·x̄. The calculator does this exactly and shows each intermediate value in the steps.

What is the difference between r and r²?

r is the correlation coefficient, between −1 and 1, and tells you the direction and strength of the linear relationship. r² is its square, between 0 and 1, and tells you the share of the variation in y explained by the line.

How many data points do I need?

Two points define a line but tell you nothing about the error. You need at least three for the standard error and the p-value, and in practice ten or more to judge whether a straight line is a reasonable model.

What does the p-value of the slope mean?

It tests the hypothesis that the true slope is zero, i.e. that y has no linear relationship with x. A small p-value (below 0.05, say) indicates that the observed slope would be unlikely if there were no relationship at all.

Can I use this for a nonlinear relationship?

Not directly. If the residuals show a curve, transform a variable first (for example take the logarithm of y for exponential growth) and run the regression on the transformed data, or use a polynomial model.

Sources

Last updated:

More Statistics calculators

People also use