Statistics / Relationships
Linear Regression Calculator (Line of Best Fit)
Fit a straight line to paired data by least squares: the equation, slope and intercept with standard errors, R², the p-value for the slope, predictions, a scatter plot with the line, and the residuals.
Linear Regression Calculator (Line of Best Fit): Least squares chooses the slope b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² and intercept a = ȳ − b·x̄, so the line passes through the means and the squared vertical distances are as small as possible. R² is 1 minus the residual sum of squares over the total. For the example data the line is y = 1.998x + 0.036 with R² = 0.999. Standard errors and the p-value match SciPy's linregress. Runs 100% locally in your browser with zero server file uploads.
- Category
- School & study tools
- Runs
- In your browser
- Cost
- Free · no sign-up
- Availability
- Ready to use
Runs entirely in your browser
| X | Y | Fitted | Residual |
|---|---|---|---|
| 1 | 2.1 | 2.0333 | 0.0667 |
| 2 | 3.9 | 4.031 | -0.131 |
| 3 | 6.2 | 6.0286 | 0.1714 |
| 4 | 7.8 | 8.0262 | -0.2262 |
| 5 | 10.1 | 10.0238 | 0.0762 |
| 6 | 12.2 | 12.0214 | 0.1786 |
| 7 | 13.8 | 14.019 | -0.219 |
| 8 | 16.1 | 16.0167 | 0.0833 |
Least squares picks the line that makes the squared vertical distances from the points as small as possible: slope = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², and the line passes through the means. R² is the share of Y's variation the line explains. Predictions outside the range of your X values are guesses, and a pattern in the residuals means a straight line is the wrong shape.
Worked example
Weight against height for 160–185 cm in steps of 5 with weights 55, 60, 65, 72, 78, and 85 kg: weight = 1.206 × height − 138.8, R² = 0.996, predicting 74.6 kg at 177 cm.
To plot other functions, use the graphing calculator.
Slope and correlation
The slope equals r × (SD of Y ÷ SD of X), so the slope's p-value is the same as the correlation's. R² for a simple line is exactly r².
To measure the strength of the relation without fitting a line, use the correlation calculator.
How to use it
- Paste the X values and the Y values in the same order.
- Read the equation, R², and the slope's p-value.
- Enter an X value to predict Y, and check the residuals.
Privacy & limitations
Your data stay in your browser.
Related tools
Frequently asked questions
What does R² tell me?
The share of the variation in Y that the line accounts for: 0.9 means 90%. A high R² does not prove the line is the right model.
Can I predict outside my data?
You can, but extrapolating beyond the range of X is risky: the relation may bend or stop.
What are residuals for?
They are the vertical gaps between each point and the line. If they show a pattern, such as a curve, a straight line is the wrong shape.
Free tool · runs in your browser · no account required