Least Squares Line Calculator
Paste your X and Y data points to find the best-fit line that minimizes the squared residuals.
Result
How it is calculated
Paste the X values and the Y values as two lists, separated by commas, spaces or semicolons; the first X pairs with the first Y, and so on. The tool fits the straight line y = m·x + b that makes the sum of squared vertical errors as small as possible, using sums over the n pairs:
m = (n·Σxy − Σx·Σy) ÷ (n·Σx² − (Σx)²)
b = (Σy − m·Σx) ÷ n
r = (n·Σxy − Σx·Σy) ÷ √[(n·Σx² − (Σx)²)(n·Σy² − (Σy)²)]
R² is r squared. You need at least two pairs. If the lists have different lengths, only as many pairs as the shorter list are used and the extra values are ignored. A non-numeric entry gives no result, and so does a data set where all X values are equal, because the slope would be vertical.
Worked example
Take x = 1, 2, 3, 4, 5 and y = 2.2, 2.8, 3.6, 4.5, 5.1.
- n = 5, Σx = 15, Σy = 18.2, Σxy = 62.1, Σx² = 55.
- Slope m = (5 × 62.1 − 15 × 18.2) ÷ (5 × 55 − 15²) = 37.5 ÷ 50 = 0.75.
- Intercept b = (18.2 − 0.75 × 15) ÷ 5 = 1.39.
- The correlation works out to r = 0.997609, so R² = 0.995223.
The best-fit line is y = 0.7500x + 1.3900: each extra unit of x adds about 0.75 to y, and the line explains over 99 percent of the variation in this data.
Common mistakes
- Writing decimals with a comma, such as 2,2. Commas separate values, so 2,2 is read as two separate numbers and the pairs shift out of line. Use a dot for decimals.
- Giving lists of different lengths and assuming every value was used. The tool silently trims to the shorter list, so a missing Y value pairs the wrong points together.
- Swapping X and Y. Regression is not symmetric: the slope of y on x is not the reciprocal of the slope of x on y unless r is exactly 1 or -1.
- Treating a high R² as proof of cause and effect, or of a straight-line pattern. A curved data set can still return a large R², so plot the points first.
- Reading r = 0 for a flat set of Y values as zero correlation. If every Y is identical the true correlation is undefined, and the tool shows 0 along with a horizontal line.
Sources
About this calculator
Ordinary least squares fits the straight line through a set of X-Y data points that minimizes the sum of the squared vertical distances (residuals) between each point and the line — the reason it's called 'least squares.' The slope and intercept have closed-form solutions (slope = Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)², intercept = ȳ − slope×x̄), and the correlation coefficient r indicates how well the line actually captures the relationship in the data, from -1 (perfect negative) to +1 (perfect positive).
This calculator takes pasted X and Y data pairs and returns the fitted line's equation, slope, intercept, and correlation coefficient. Statistics and science students learning regression, researchers doing a quick trend analysis, and analysts checking a correlation before committing to a full statistical model use it as a fast way to quantify a linear relationship in a data set without opening a spreadsheet or stats package.
Was this helpful?
You can pick up to 3 reactions.
Popular calculators
Loan Calculator
Monthly payment, total interest and full amortization schedule.
Percentage Calculator
Percent change, percent of, and ratio calculations.
Discount Calculator
Calculate final price, savings, and discount amount.
BMI Calculator
Body mass index from height and weight (metric or imperial).
Age Calculator
Compute your exact age in years, months and days from a birth date.
Compound Interest Calculator
Calculate the final amount and interest earned with compounding.
All Calculators
- Loan Calculator
- Profit Calculator
- Discount Calculator
- BMI Calculator
- Percentage Calculator
- Ideal Weight Calculator
- Age Calculator
- Pregnancy Due Date Calculator
- Compound Interest Calculator
- BMR Calculator (Basal Metabolic Rate)
- Tip Calculator
- Fuel Cost Calculator
- Date Difference Calculator
- Sleep Cycle Calculator
- Daily Water Intake Calculator
- Simple Interest Calculator
- Heart Rate Zones Calculator
- Running Pace Calculator
- Pet Age Calculator
- Calories Burned Calculator
- Macro Calculator
- TDEE Calculator (Total Daily Energy Expenditure)
- Waist-to-Hip Ratio Calculator
- Honor Roll Calculator
- One Rep Max (1RM) Calculator
- Ovulation Calculator
- Electricity Cost Calculator
- Unit Price Calculator
- Savings Goal Calculator
- Break-Even Point Calculator
- Paint Calculator
- Course Grade Average Calculator
- Business Days Calculator
- Time Duration Calculator
- Fuel Economy Calculator
- Temperature Converter
- Tile Calculator
- Aspect Ratio Calculator
- Roman Numeral Converter
- Goal Weight Timeline Calculator
- Salary Converter (Hourly to Annual)
- Credit Card Validator (Luhn)
- Body Surface Area (BSA) Calculator
- Feels-Like Temperature Calculator
- Ohm’s Law Calculator
- PPI (Pixel Density) Calculator
- Molar Mass Calculator
- Zodiac Sign Calculator (Western + Chinese)
- IBAN Validator
- Shoe Size Converter
- Country Info & Dialing Code Lookup
- Time Zone Difference (World Clock)
- CAGR (Compound Annual Growth Rate) Calculator
- Rule of 72 Calculator
- Blood Alcohol Content (BAC) Calculator
- Lean Body Mass (LBM) Calculator
- Pythagorean Theorem Calculator
- Speed Converter
- Download Time Calculator
- Date Calculator (Add/Subtract)
No calculators found
Comments (1)
A quick way to judge a fit is to run it twice: once with all points and once without the one you suspect is an outlier. If the slope barely moves, the line is stable; if it swings, a single point is steering it. Keep in mind the tool reports r and R squared but no standard error or significance, so a high r from only three or four points proves little. Which kind of data are you fitting: lab measurements, prices, test scores?