By · Updated

Linear Regression Calculator

Least Squares Regression Equation, Slope, Intercept and R-Squared From x and y

No ratings yet
Result

—

OneTask is free, with no sign-up, and built by one person.
If this tool saved you time, a coffee helps keep new ones coming.

How the Linear Regression Calculator works

This linear regression calculator fits the straight line that best describes how y changes with x. Paste your paired values and it returns the equation, the slope, the intercept, R², the p-value for the slope and a prediction, plus the fitted value and residual for every point.

Simple linear regression draws the one straight line through a scatter of points that keeps the squared vertical misses as small as possible, which is why it is called least squares. The slope tells you how much y changes for each extra unit of x; the intercept is the predicted y at x = 0.

How the linear regression calculator works

The formula for the slope is b = Sxy ÷ Sxx, where Sxy = Σ(x − x̄)(y − ȳ) and Sxx = Σ(x − x̄)². The intercept is a = ȳ − b × x̄, giving ŷ = a + bx. R² is the share of the variation in y the line explains, and the slope's t statistic tests whether the true slope could be zero.

First example. The default data, hours revised against exam mark for 22 students, gives a slope of about 4 marks per hour and an R² close to 0.99.

Second example. Points (1, 2), (2, 4) and (3, 6) sit exactly on y = 2x, so R² = 1 and every residual is zero.

How many points do you need? At least three for a p-value, but ten or more for a trend you can trust.

Fitted values and residuals for each data point

x y Predicted ŷ Residual y − ŷ
1 38 40.0772 -2.0772
2 42 44.1371 -2.1371
2 45 44.1371 0.8629
3 47 48.1969 -1.1969
3 50 48.1969 1.8031
4 52 52.2568 -0.2568
4 55 52.2568 2.7432
5 54 56.3166 -2.3166
5 58 56.3166 1.6834
6 60 60.3764 -0.3764
6 63 60.3764 2.6236
7 62 64.4363 -2.4363
7 66 64.4363 1.5637
8 68 68.4961 -0.4961
8 71 68.4961 2.5039
9 70 72.556 -2.556
9 74 72.556 1.444
10 76 76.6158 -0.6158
10 78 76.6158 1.3842
11 80 80.6757 -0.6757
12 83 84.7355 -1.7355
12 85 84.7355 0.2645

Tips

FAQ

What does R² tell me?

R² is the proportion of the variation in y explained by the straight line, from 0 to 1. An R² of 0.8 means the line accounts for 80% of the spread in y and the rest is scatter around it. A high R² does not prove that x causes y, only that they move together.

What is a residual?

A residual is the actual y minus the y the line predicts for that x. Positive residuals sit above the line and negative ones below. They always sum to zero for a least-squares line, and a large one flags a point worth checking for a typing error or a genuine outlier.

What is the difference between regression and correlation?

Correlation measures how tightly two variables move together on a scale from −1 to 1. Regression goes further and gives an equation for predicting one from the other. In simple regression R² is just the correlation coefficient squared, so the two always agree on strength.

See also: all Statistics tools, Arithmetic, Number Theory, Algebra and Geometry, Trigonometry and Calculus.

Related tools