HOME / COMPUTER SCIENCE & DATA / DATA SCIENCE / LINEAR REGRESSION CALCULATOR
Computer Science & Data / Data Science

Linear Regression Calculator

Separate x and y with a space, comma or tab. Two columns pasted from a spreadsheet work as is.

Optional

Try an example

Best-fit line (least squares)

y^=4.131 x+47.7857\hat{y} = 4.131\,x + 47.7857
Slope
4.131
Intercept
47.7857
Correlation r
0.9937
R²
0.9874

8 points. y changes by 4.131 for each 1-unit increase in x; the line explains 98.7% of the variation in y (very strong positive relationship).

At x = 10: ŷ = 89.0952 (95% prediction interval 84.9895 to 93.201). This is outside the range of your data, so treat it with caution.

Scatter plot

123456789105060708090
Fitted line

Significance

EstimateStd. error95% CI
Slope4.1310.19063.6646 to 4.5973
Intercept47.78570.962545.4306 to 50.1408

Residual standard error 1.2352 on 6 degrees of freedom. Slope t = 21.673, p < 0.0001: the relationship is statistically significant at the 5% level.

Ordinary least squares: slope = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)², intercept = ȳ − slope·x̄. The significance figures assume the residuals are independent and roughly normal with constant spread.

Fit a least-squares regression line to x, y data pasted from a spreadsheet: slope, intercept, correlation r and R², a scatter plot with the fitted line, standard errors, confidence intervals and the slope's significance, plus a prediction with its 95% prediction interval.

Formula

slope=∑(x−xˉ)(y−yˉ)/∑(x−xˉ)2\text{slope} = \sum (\text{x} - \bar{\text{x}})(\text{y} - \bar{\text{y}}) / \sum (\text{x} - \bar{\text{x}})^{2}
intercept=yˉ−slope×xˉ\text{intercept} = \bar{\text{y}} - \text{slope} \times \bar{\text{x}}
R2=1−SSres/SStot\text{R}^{2} = 1 - \text{SS}_{\text{res}} / \text{SS}_{\text{tot}}

Worked Example

Data (study hours, score)(1, 52) (2, 55) (3, 61) (4, 64) (5, 70) (6, 71) (7, 78) (8, 80)
Predict atx = 10
Result
Lineŷ = 4.131x + 47.79
R²0.987
Prediction at x = 1089.1 (95% prediction interval 85.0 to 93.2)

Each extra hour adds about 4.1 points, and the line explains 98.7% of the variation in scores. The slope's t statistic of 21.7 on 6 degrees of freedom makes the relationship clearly significant. x = 10 lies outside the data, so the prediction is an extrapolation.

Frequently Asked Questions

What does R² tell me?

The share of the variation in y that the line explains, from 0 to 1. An R² of 0.987 means 98.7% of the differences in y line up with differences in x. A high R² does not prove that x causes y, and a low one can still come with a real, useful slope.

What is the difference between a confidence interval and a prediction interval?

A confidence interval brackets the average y at a given x; a prediction interval brackets a single new observation, which also varies around that average, so it is always wider.

When is linear regression the wrong tool?

When the relationship is curved, when a few outliers dominate the fit, or when the spread of the residuals grows with x. Look at the scatter plot first: a straight line through a curve still produces a slope and an R², but they mislead.