Linear Regression Calculator - Slope, Intercept & R²
Math Calculators
Fit a least-squares line to complete (x, y) pairs. Get slope, intercept, R² when defined, Pearson correlation, predictions, and a scatter plot.
Each non-comment line must contain exactly two complete finite numbers, separated by a comma or space.
Slope (m)
1.990000
Intercept (b)
0.050000
R²
0.997305
Pearson r
0.998652
Least-Squares Method
The calculator uses the ordinary least-squares (OLS) method to find the line ŷ = mx + b that minimises the sum of squared residuals. It returns the slope m, y-intercept b, R² goodness-of-fit, and the Pearson correlation coefficient r.
Interpreting R²
| R² range | Fit quality | Interpretation |
|---|---|---|
| > 0.9 | Strong | The line explains more than 90% of the variance in the data. |
| 0.7 – 0.9 | Good | Solid predictive power for many applied contexts. |
| 0.5 – 0.7 | Moderate | The model captures a meaningful trend but other variables matter. |
| < 0.5 | Weak | The linear model explains little of the observed variation. |
Important: R² measures correlation, not causation. A high R² does not mean that X causes Y - both variables might be driven by a third confounding factor.
Residuals
A residual is the difference between an observed value and the model's prediction: e = y − ŷ. OLS minimises the sum of squared residuals. Examining a residual plot (residuals vs. fitted values) reveals model problems:
- Non-random patterns: suggest non-linearity - a higher-order or different model may fit better.
- Fan shape (heteroscedasticity): variance increases with fitted values - a log transformation of y often helps.
- Outliers: individual points with large residuals may unduly influence the slope estimate.
OLS assumptions
- Linearity: the true relationship between X and Y is linear.
- Independence: observations are independent of each other (violated by time-series data without correction).
- Homoscedasticity: the variance of residuals is constant across all values of X.
- Normality of residuals: residuals are approximately normally distributed (required for valid hypothesis tests and confidence intervals, not for the regression itself).
Worked example
| Hours studied (x) | Exam score (y) |
|---|---|
| 1 | 50 |
| 2 | 58 |
| 3 | 65 |
| 4 | 73 |
| 5 | 80 |
For this dataset: slope m ≈ 7.5, intercept b ≈ 42.5, giving the line ŷ = 7.5x + 42.5. R² ≈ 0.998 - a near-perfect linear fit. The slope says each additional hour of study is associated with ~7.5 more points on the exam.
Input validation and undefined metrics
Each data row must contain exactly two complete, finite numeric tokens. Rows such as
1foo, 2bar are rejected instead of being silently shortened to 1, 2.
This keeps the fitted line tied to the data you actually entered.
The calculator separates the fitted line from goodness-of-fit statistics. If y has no variation, the least-squares line can still be fitted when x varies, but R² is undefined because its total sum of squares is zero. Pearson r is also undefined whenever x or y has zero variance. In those cases the page displays N/A rather than presenting 0 or 1 as a meaningful correlation value.
Related calculators
- StatisticsSummarize each variable before analyzing their paired linear relationship.
- Function GrapherPlot the fitted equation with other functions after finding slope and intercept.
- Slope CalculatorCompare the best-fit slope with the exact slope between two selected points.
- Confidence IntervalEstimate uncertainty around a sample mean after exploring the relationship.