Regression & Fit Tool

Linear Regression Calculator

Fit an optimal least-squares trendline (y = mx + b) to your paired experimental or statistical data. Computes slope, y-intercept, Pearson correlation (r), coefficient of determination (R²), and live predictions.

Least Squares Linear Fit
Supports both 1.5 and 1,5 decimals
Enter one pair per line: x and y separated by comma, tab, or space. Both 1.5 and 1,5 formats supported.

What is Linear Regression and How Does the Calculator Work?

If you are wondering what is linear regression, it is a fundamental statistical method used to model the relationship between an independent explanatory variable ($x$) and a dependent response variable ($y$). In real-world data, observations rarely fall on a perfectly straight line due to measurement noise, biological variation, or experimental uncertainty.

Our linear regression calculator uses the ordinary least squares (OLS) method and the linear regression formula to determine the best-fit line. It minimizes the sum of squared vertical residuals to output the precise linear regression equation (ŷ = mx + b), Pearson correlation coefficient (r), and coefficient of determination (R²).

Equation

Ordinary Least Squares (OLS) Formula

m = [ nΣxy - (Σx)(Σy) ] / [ nΣx² - (Σx)² ], b = ȳ - m·x̄
Where:
  • ŷ = mx + b=Best-Fit Linear Model: The least-squares regression line that minimizes squared vertical residuals.
  • m=Slope (Regression Coefficient): The rate of change: m = [nΣxy - (Σx)(Σy)] / [nΣx² - (Σx)²].
  • b=Y-intercept: The predicted value when x = 0: b = ȳ - m·x̄.
  • r=Pearson Correlation Coefficient: A measure of linear strength from -1 (perfect negative) to +1 (perfect positive).
  • R²=Coefficient of Determination: The fraction of variance in the dependent variable explained by the regression line (0 to 1).

Note: Pearson correlation coefficient r is calculated as SS_xy / √(SS_xx · SS_yy), and R² is simply r².

Step-by-Step Worked Examples

Walk through two complete real-world calculation problems or click any example to load its numbers directly into the calculator.

Example 1

Marketing Ad Spend vs. Product Sales Revenue

An e-commerce retailer tracks monthly advertising spend (in $k) and corresponding sales revenues (in $k) across 5 campaigns: (1, 3.0), (2, 4.5), (3, 6.2), (4, 7.8), (5, 9.5). Find the best-fit line and predict revenue for a $6k spend.

Given Values:
Data Pairs (x, y): (1, 3.0), (2, 4.5), (3, 6.2), (4, 7.8), (5, 9.5)Target x: $6.0k
Solution Steps:
  1. Summary statistics (n = 5): Σx = 15, Σy = 31.0, x̄ = 3.0, ȳ = 6.20
  2. Sums of products: Σx² = 55, Σy² = 218.38, Σxy = 109.10
  3. SS_xx = 55 - (15² / 5) = 55 - 45 = 10.0
  4. SS_xy = 109.10 - (15 × 31.0 / 5) = 109.10 - 93.0 = 16.10
  5. Slope m = SS_xy / SS_xx = 16.10 / 10.0 = 1.610
  6. Intercept b = ȳ - m·x̄ = 6.20 - 1.610 × 3.0 = 6.20 - 4.83 = 1.370
  7. Equation: ŷ = 1.610x + 1.370 with R² = 0.9984
  8. Predict for x = 6: ŷ = 1.610 × 6 + 1.370 = 9.66 + 1.37 = $11.03k
Calculated Result:ŷ = 1.610x + 1.370 (R² = 0.9984)
Example 2

Study Preparation Hours vs. Test Score

An educational psychology experiment records study preparation hours (x) and final test scores (y) for 6 students: (2, 58), (3, 65), (5, 74), (6, 80), (7, 85), (9, 94). Fit a least squares regression line.

Given Values:
Data Pairs: (2, 58), (3, 65), (5, 74), (6, 80), (7, 85), (9, 94)Target x: 8 hours
Solution Steps:
  1. n = 6, Σx = 32, Σy = 456, Mean x̄ = 5.333, Mean ȳ = 76.000
  2. SS_xx = 204 - (32² / 6) = 204 - 170.667 = 33.333
  3. SS_xy = 2593 - (32 × 456 / 6) = 2593 - 2432 = 161.000
  4. Slope m = 161.000 / 33.333 = 4.830 score points per study hour
  5. Intercept b = 76.000 - 4.830 × 5.333 = 76.000 - 25.760 = 50.240
  6. Correlation r = 0.9934, R² = 0.9868
  7. Predicted score for 8 hours: ŷ = 4.830 × 8 + 50.240 = 38.64 + 50.24 = 88.88%
Calculated Result:ŷ = 4.830x + 50.240 (R² = 0.9868)

Linear Regression FAQs

What is ordinary least squares (OLS) linear regression?

Ordinary Least Squares regression is the standard mathematical method for fitting a straight line through a scatter plot of data points. It finds the line that minimizes the sum of squared vertical differences (residuals) between the actual observed y-values and the values predicted by the line.

How is linear regression different from linear interpolation?

Linear interpolation connects two known points with an exact line segment that passes directly through both points. Linear regression, on the other hand, deals with noisy or experimental datasets containing many points. Rather than passing through every point (which is impossible when data contains noise or variance), regression finds the optimal overall trend line representing the collective dataset.

What does the R-squared (R²) value mean?

R² (the coefficient of determination) represents the proportion of total variance in the dependent variable y that is predictable from the independent variable x. An R² of 1.0 means the line fits the data perfectly. An R² of 0.85 means that 85% of the variation in y is explained by the linear relationship with x, while the remaining 15% is due to noise or other unmodeled factors.

What assumptions must be met for linear regression to be valid?

Key assumptions include: 1) Linearity (the true underlying relationship between x and y is roughly linear); 2) Independence (observations are independent of one another); 3) Homoscedasticity (constant variance of residuals across the range of x); and 4) Normality of residuals.

Can I paste spreadsheet data directly into this calculator?

Yes! You can copy two columns directly from Microsoft Excel, Google Sheets, or CSV files and paste them into the input box. Our parser automatically handles tab delimiters, comma separations, and international decimal commas.