Statistics & Probability

Linear Regression Calculator

This linear regression calculator finds the line of best fit y^=a+bx\hat{y} = a + bx by least squares, with slope b=SxySxxb = \frac{S_{xy}}{S_{xx}} and intercept a=y¯−bx¯a = \bar{y} - b\bar{x}.

Try

At least 3 numbers, separated by commas, spaces or new lines.

The same number of values as x, in the same order.

Leave blank to skip the prediction.

Result

Best-fit linear equation: y = 47.78571 + 4.047619x

Best-fit linear equation

y=47.78571+4.047619xy = 47.78571 + 4.047619x

Predicted y at x = 10
88.26190476
Coefficient a (intercept)
47.78571429
Coefficient b (slope)
4.047619048
Correlation coefficient r
0.9971746489
Coefficient of determination r²99.44% of the variation in y is explained
0.9943572805
p-value for the slopeTwo-sided t-test of slope = 0: t = 32.5164, df = 6
5.626×10⁻⁸
95% confidence interval for the slopeb ± t*·SE(b), with SE(b) = 0.124479 and t* = 2.44691
3.743029 to 4.352209
95% confidence interval for the interceptSE(a) = 0.628589
46.24761 to 49.32382
Residual standard error√(SSE/(n − 2)), the typical size of a residual
0.806717826
Number of points (n)
8
95% confidence interval for the mean y at x = 10Where the average y at this x is likely to be
86.4471 to 90.07671
95% prediction interval for one new y at x = 10Where a single new observation is likely to fall
85.58048 to 90.94333

The fit explains 99.44% of the variation in y (r² = 0.9944), a very strong fit. Each increase of 1 in x changes y by about 4.048. The slope is statistically different from 0 at the 5% level (p = 5.626×10⁻⁸). At x = 10, the model predicts y ≈ 88.2619.

x = 10 is outside the range of the data (1 to 8), so this prediction is an extrapolation. The fitted pattern may not continue.

Linear fit: y = 47.78571 + 4.047619x
5060708012345678xyDataFitted curve
Residual plot: y − ŷ against x
−1−0.500.5112345678xResidualResidualsZero line
Fitted values and residuals
xyPredicted ŷResidual y − ŷ
15251.833333330.1666666667
25555.88095238−0.880952381
36159.928571431.071428571
46463.976190480.02380952381
56868.02380952−0.02380952381
67172.07142857−1.071428571
77776.119047620.880952381
88080.16666667−0.1666666667
Step-by-step working (6 steps)
  1. Means

    x¯=4.5,y¯=66\bar{x} = 4.5, \quad \bar{y} = 66
  2. Sums of squares and cross-products

    Sxx=∑(x−x¯)2=42,Sxy=∑(x−x¯)(y−y¯)=170S_{xx} = \sum (x - \bar{x})^2 = 42, \quad S_{xy} = \sum (x - \bar{x})(y - \bar{y}) = 170
  3. Slope and intercept of the line

    slope=SxySxx=4.047619048,intercept=y¯−slope⋅x¯=47.78571429\text{slope} = \frac{S_{xy}}{S_{xx}} = 4.047619048, \quad \text{intercept} = \bar{y} - \text{slope} \cdot \bar{x} = 47.78571429
  4. Correlation

    r=SxySxxSyy=17042⋅692=0.9971746489r = \frac{S_{xy}}{\sqrt{S_{xx} S_{yy}}} = \frac{170}{\sqrt{42 \cdot 692}} = 0.9971746489
  5. Standard error, t-test and confidence interval for the slope

    With n − 2 = 6 degrees of freedom, the residual standard error is s = √(SSE/(n − 2)) = 0.8067178.

    SE(b)=sSxx=0.1244793,t=bSE(b)=32.5164,p=5.626×10−8,b±2.44691⋅SE(b)=[3.743029,4.352209]SE(b) = \frac{s}{\sqrt{S_{xx}}} = 0.1244793, \quad t = \frac{b}{SE(b)} = 32.5164, \quad p = 5.626 \times 10^{-8}, \quad b \pm 2.44691 \cdot SE(b) = [3.743029, \; 4.352209]
  6. Equation

    y=47.78571+4.047619xy = 47.78571 + 4.047619x

Paste paired x and y data to get the equation, r, r², the slope p-value, 95% confidence intervals, residuals, a scatter plot and every step. Quadratic, exponential, logarithmic, power and inverse fits are one click away.

By Hira Noor, mathematicianLast reviewed Free, no sign-up, runs in your browserReport a problem

How to use this calculator

  1. Enter the x-values (explanatory variable) and the y-values (response variable) in the same order, separated by commas, spaces or new lines. You can paste two columns from a spreadsheet one at a time. You need at least 3 pairs.
  2. Keep Linear selected for a straight line of best fit. Switch the regression model to quadratic, exponential, logarithmic, power or inverse when the scatter plot curves.
  3. Optionally type an x-value in Predict y at x to get the fitted y there, plus a 95% confidence interval for the mean and a 95% prediction interval for one new value.
  4. Read the equation and r² first, then the slope p-value, then check the residual plot: a good model leaves residuals scattered randomly around zero.

Formulas

Least-squares regression line

y^=a+bx,b=∑(x−x¯)(y−y¯)∑(x−x¯)2=SxySxx,a=y¯−bx¯\hat{y} = a + bx, \qquad b = \frac{\sum (x - \bar{x})(y - \bar{y})}{\sum (x - \bar{x})^2} = \frac{S_{xy}}{S_{xx}}, \qquad a = \bar{y} - b\bar{x}

Plain-text formula: y^ = a + bx, b = (∑(x − x̄)(y − ȳ))/(∑(x − x̄)^2) = S_(xy)/S_(xx), a = ȳ − bx̄

bb
slope: the change in predicted y for each 1-unit increase in x
aa
intercept: the predicted y when x = 0
Sxx,SxyS_{xx}, S_{xy}
sum of squared x deviations, and sum of x and y deviation products

The line that makes the sum of squared vertical distances from the points to the line as small as possible. The line always passes through the point of means (x¯,y¯)(\bar{x}, \bar{y}). The calculator uses centered sums, which avoid the rounding errors of the shortcut formulas.

Correlation coefficient and r²

r=SxySxxSyy,R2=1−∑(y−y^)2∑(y−y¯)2r = \frac{S_{xy}}{\sqrt{S_{xx} S_{yy}}}, \qquad R^2 = 1 - \frac{\sum (y - \hat{y})^2}{\sum (y - \bar{y})^2}

Plain-text formula: r = S_(xy)/√(S_(xx)S_(yy)), R^2 = 1 − (∑(y − y^)^2)/(∑(y − ȳ)^2)

r measures the strength and direction of a straight-line relationship, from −1 to 1. R² is the share of the variation in y that the model explains; for a straight line it equals r².

Slope standard error, t-test and 95% confidence interval

s=∑(y−y^)2n−2,SE(b)=sSxx,t=bSE(b),b±tn−2∗⋅SE(b)s = \sqrt{\frac{\sum (y - \hat{y})^2}{n - 2}}, \qquad SE(b) = \frac{s}{\sqrt{S_{xx}}}, \qquad t = \frac{b}{SE(b)}, \qquad b \pm t^{*}_{n-2} \cdot SE(b)

Plain-text formula: s = √((∑(y − y^)^2)/(n − 2)), SE(b) = s/√(S_(xx)), t = b/(SE(b)), b ± t_(n − 2)^∗ · SE(b)

The p-value is the two-sided probability, under Student’s t distribution with n − 2 degrees of freedom, of a slope at least this far from 0 if x and y were unrelated. tn−2∗t^{*}_{n-2} is the 97.5th percentile of that distribution (2.447 for n = 8). The intercept uses SE(a)=s1/n+x¯2/SxxSE(a) = s\sqrt{1/n + \bar{x}^2/S_{xx}}, and a prediction at x0x_0 uses s1/n+(x0−x¯)2/Sxxs\sqrt{1/n + (x_0 - \bar{x})^2/S_{xx}} for the mean, with an extra 1+1 + under the root for one new value.

Curved models

ln⁡y=ln⁡a+xln⁡b,ln⁡y=ln⁡a+bln⁡x,y=a+bln⁡x,y=a+b⋅1x,y=a+bx+cx2\ln y = \ln a + x \ln b, \quad \ln y = \ln a + b \ln x, \quad y = a + b \ln x, \quad y = a + b \cdot \tfrac{1}{x}, \quad y = a + bx + cx^2

Plain-text formula: ln y = ln a + x ln b, ln y = ln a + b ln x, y = a + b ln x, y = a + b · 1/x, y = a + bx + cx^2

Exponential, power, logarithmic and inverse models become straight lines after transforming x, y or both, and are fitted with the line formulas; for the exponential and power models, r and r² therefore refer to ln y, and R² on the original y-values is listed too. The quadratic model is fitted by multiple least squares with a Householder QR factorization of centered x-values, which stays accurate even when x is large (for example calendar years).

Reading the result

Read the equation first: the slope is how much y changes, on average, for each 1-unit increase in x. Then read r²: an r² of 0.95 means the line accounts for 95% of the variation in y. A high r² does not prove that x causes y, and it does not show whether a curved pattern was missed, so always look at the plot.

The p-value for the slope tests whether the true slope could be 0. A p-value below 0.05 means a slope this steep would be unlikely by chance alone; the 95% confidence interval gives the range of slopes consistent with the data. With many points, a tiny slope can be significant yet have a low r², so report both.

The residual plot shows where the line misses. Random scatter around zero is what you want; a curve suggests a different model, and a funnel suggests the spread of y changes with x. Predictions inside the range of your x-values (interpolation) are usually reliable when the fit is good. Predictions outside it (extrapolation) assume the pattern continues and can be badly wrong.

How to calculate linear regression by hand

Step 1. Find the means x¯\bar{x} and y¯\bar{y}.

Step 2. For each pair, compute the deviations x−x¯x - \bar{x} and y−y¯y - \bar{y}.

Step 3. Add up the squared x deviations to get SxxS_{xx}, and the products of the deviations to get SxyS_{xy}.

Step 4. Divide to get the slope b=Sxy/Sxxb = S_{xy}/S_{xx}, then the intercept a=y¯−bx¯a = \bar{y} - b\bar{x}.

Step 5. For r, also add up the squared y deviations (SyyS_{yy}) and compute r=Sxy/SxxSyyr = S_{xy}/\sqrt{S_{xx}S_{yy}}.

The worked example below follows these steps with real numbers, and the calculator shows the same steps for your own data under the result.

Linear regression assumptions and choosing a model

The p-value and confidence intervals assume four things, often remembered as LINE: a linear relationship, independent observations (no repeated measures or time trends in the errors), roughly normal residuals, and equal spread of residuals across x. The fitted line itself is still the least-squares line when these fail, but the p-value and intervals can mislead.

Check the residual plot. A U shape or arch means the relationship is curved: try the quadratic, logarithmic or exponential model and compare. A funnel shape means unequal spread, and a single point far from the rest can pull the whole line, so check it before trusting the slope.

This calculator also works as a quadratic regression calculator and an exponential, logarithmic, power or inverse regression calculator: change the model and the same data are refitted. To find which regression equation best fits the data, compare the residual plots first and R² on the original y-values second. For multiple regression with two or more x variables, use LINEST in a spreadsheet or a statistics package; this tool fits one x variable.

Which regression model to try
Pattern in the scatter plotModelRestriction
Constant change per unit of xLinear: y = a + bxNone
One bend or turning point (for example a projectile’s height)Quadratic: y = a + bx + cx²At least 3 different x-values
Constant percentage growth or decayExponential: y = a·bˣy > 0
Fast growth that levels offLogarithmic: y = a + b ln xx > 0
Scaling law (doubling x multiplies y by a fixed factor)Power: y = a·xᵇx > 0 and y > 0
Falls toward a floor as x growsInverse: y = a + b/xx ≠ 0

Matching TI-84, Casio and Excel output

TI-84: LinReg(a+bx) gives the same a and b as this calculator. LinReg(ax+b) swaps the letters, so its a is the slope. Turn on DiagnosticOn to see r and r². LinRegTTest gives the same t and p-value.

Casio fx-991EX and fx-991ES PLUS: in Statistics mode choose y = a + bx; a is the intercept and b the slope, matching the order here.

Excel and Google Sheets: SLOPE, INTERCEPT and RSQ match the slope, intercept and r². LINEST(y, x, TRUE, TRUE) also returns the standard errors. Excel’s exponential and power trendlines are fitted on ln y, the same as here, so compare their R² with the r² on the transformed data.

Worked example

Hours of study and exam score

Fit a regression line to x = 1, 2, 3, 4, 5, 6, 7, 8 (hours studied) and y = 52, 55, 61, 64, 68, 71, 77, 80 (exam score), test the slope, then predict the score for 10 hours.

  1. Means: x¯=4.5\bar{x} = 4.5 and y¯=66\bar{y} = 66.
  2. Sums: Sxx=42S_{xx} = 42, Sxy=170S_{xy} = 170 and Syy=692S_{yy} = 692.
  3. Slope: b=170/42≈4.047619b = 170 / 42 \approx 4.047619. Intercept: a=66−4.047619×4.5≈47.785714a = 66 - 4.047619 \times 4.5 \approx 47.785714.
  4. Correlation: r=170/42×692≈0.9971746r = 170 / \sqrt{42 \times 692} \approx 0.9971746, so r2≈0.9943573r^2 \approx 0.9943573.
  5. Residuals: the squared residuals add up to 3.904762, so s=3.904762/6≈0.806718s = \sqrt{3.904762 / 6} \approx 0.806718 and SE(b)=0.806718/42≈0.124479SE(b) = 0.806718 / \sqrt{42} \approx 0.124479.
  6. Slope test: t=4.047619/0.124479≈32.52t = 4.047619 / 0.124479 \approx 32.52 with 6 degrees of freedom, p≈5.6×10−8p \approx 5.6 \times 10^{-8}. 95% CI: 4.047619±2.446912×0.1244794.047619 \pm 2.446912 \times 0.124479, about 3.743 to 4.352.
  7. Prediction: y^(10)=47.785714+4.047619×10≈88.26\hat{y}(10) = 47.785714 + 4.047619 \times 10 \approx 88.26, with a 95% prediction interval of about 85.58 to 90.94. Ten hours lies outside the data, so this is an extrapolation.

Answer: y ≈ 47.78571 + 4.047619x with r ≈ 0.9972 and r² ≈ 0.9944. Each extra hour adds about 4.05 points (95% CI 3.74 to 4.35, p < 0.001), and the predicted score for 10 hours is about 88.26.

Frequently asked questions

How do you calculate linear regression?

Compute the means of x and y, then Sxx=∑(x−x¯)2S_{xx} = \sum (x - \bar{x})^2 and Sxy=∑(x−x¯)(y−y¯)S_{xy} = \sum (x - \bar{x})(y - \bar{y}). The slope is b=Sxy/Sxxb = S_{xy}/S_{xx} and the intercept is a=y¯−bx¯a = \bar{y} - b\bar{x}, giving y^=a+bx\hat{y} = a + bx. The calculator shows each of these steps for your data.

Is the line of best fit the same as the regression line?

Yes, when "line of best fit" means the least-squares line, which is how statistics courses, graphing calculators and spreadsheets define it. A line drawn by eye through a scatter plot is only an estimate of it.

What does the p-value for the slope mean?

It is the probability of getting a slope at least this far from 0 if x and y were actually unrelated. Below 0.05, the relationship is usually called statistically significant. It says nothing about how large or useful the effect is, so read it together with the slope’s confidence interval and r².

What is a good r² value?

It depends on the field. In controlled physics or engineering measurements r² above 0.99 is common; in social or biological data an r² of 0.5 can be meaningful. Judge the fit by the plot and the residuals as well as the number.

Why does my TI-84 show a different a and b?

You probably used LinReg(ax+b), which calls the slope a and the intercept b. This calculator uses y=a+bxy = a + bx, the same as LinReg(a+bx) and Casio’s y = a + bx mode, so the numbers are the same with the letters swapped.

Why is r² for my exponential fit different from Excel’s?

Both Excel and this calculator fit the exponential trendline on ln y, so the r² shown first matches Excel’s trendline. The separate R² on the original y-values measures the fit on the original scale and is usually a little different.

Why can’t I use zero or negative values with some models?

Exponential and power models take ln y, and logarithmic and power models take ln x, which are only defined for positive numbers. The inverse model divides by x, so x cannot be 0. The linear and quadratic models accept any numbers.

Statistics Calculator

Mean, median, mode, five-number summary, IQR, outliers and SD with steps, a histogram and a box plot.

Statistics & Probability

Standard Deviation Calculator

Sample or population standard deviation and variance, with grouped data and a step-by-step deviation table.

Statistics & Probability

Graphing Calculator

Plot functions, equations, inequalities, parametric and polar curves; find roots and intersections.

Tools

Slope Calculator

Slope from two points, an equation or rise and run, as a fraction, angle and percent grade, with steps.

Algebra & Equations

Scientific Calculator

Type or tap expressions with exact fractions, memory, variables, constants and complex mode.

Tools

Sources and further reading

  1. NIST/SEMATECH e-Handbook of Statistical Methods, Chapter 4: Process Modeling (opens in a new tab), National Institute of Standards and Technology
  2. Introductory Statistics 2e, Chapter 12: Linear Regression and Correlation (opens in a new tab), OpenStax, Rice University
  3. STAT 501: Regression Methods (opens in a new tab), Penn State Eberly College of Science

Results are calculated in your browser with methods checked by automated tests, and are intended for learning and general use. For engineering, medical, financial, legal or safety-critical decisions, verify important values independently and follow the rounding rules your work requires. How accurate are these results?