Line of Best Fit Calculator

Compute the line of best fit using the least squares method. Instantly obtain the slope, intercept, correlation coefficient (r), and coefficient of determination (R²). Visualize your data and the regression line on an interactive chart.

# x y
Please enter a valid number.
Please enter a valid number.
Examples: ? Positive ? Negative ? Weak ? Nonlinear ⭐ Perfect
Regression Results
Regression Equation
y = x +
Slope (m)
Intercept (b)
R² (Coeff. of Determination)
Correlation (r)
Number of Points
0
Privacy first: All calculations and visualizations are performed locally in your browser. No data is sent to any server.
Data Points Best Fit Line

What Is Linear Regression?

Linear regression is a fundamental statistical method used to model the relationship between a dependent variable (y) and one or more independent variables (x). In its simplest form — simple linear regression — we assume that the relationship can be approximated by a straight line: y = mx + b, where m is the slope and b is the y‑intercept. The goal is to find the line that "best fits" the observed data points.

The most common criterion for "best fit" is the method of least squares, first formalized by Carl Friedrich Gauss and Adrien-Marie Legendre in the early 19th century. This method minimizes the sum of the squared vertical distances (residuals) between each observed data point and the corresponding point on the regression line. The resulting line is called the least‑squares regression line or the line of best fit.

The least‑squares estimates for slope and intercept are:

m = Σ(xᵢ − x̄)(yᵢ − ȳ) / Σ(xᵢ − x̄)²    and    b = ȳ − m·x̄

where x̄ and ȳ are the sample means of x and y, respectively.

Understanding the Key Metrics

Slope (m)

The slope indicates the average change in y for a one‑unit increase in x. A positive slope means y tends to increase as x increases; a negative slope indicates the opposite. The magnitude of the slope reflects the strength of the linear relationship.

Intercept (b)

The intercept is the predicted value of y when x = 0. In many practical contexts, the intercept may not have a meaningful interpretation if x = 0 is outside the observed range, but it is essential for correctly positioning the regression line.

R² (Coefficient of Determination)

R² measures the proportion of the variance in y that is explained by the linear model. It ranges from 0 to 1, with higher values indicating a better fit. An R² of 0.80 means that 80% of the variability in y is accounted for by the linear relationship with x.

Correlation Coefficient (r)

The correlation coefficient r measures the strength and direction of the linear relationship between x and y. It ranges from −1 to +1. Values near +1 indicate a strong positive correlation, near −1 a strong negative correlation, and near 0 a weak or no linear correlation. Note that = r² for simple linear regression.

How to Use This Calculator

  1. Enter your data points by typing x and y values in the input fields and clicking Add.
  2. Each point appears in the table. Click the button to remove a specific point.
  3. For bulk entry, click Batch Import, paste your data, and choose Replace or Append.
  4. Load one of the preset examples (Positive, Negative, Weak, Nonlinear, Perfect) to see different correlation patterns.
  5. The regression results — equation, slope, intercept, R², and correlation — update automatically.
  6. The scatter plot displays your data points and the best‑fit line. Hover over points for details.
  7. Use the Clear All button to reset and start a new dataset.

Real‑World Applications of Linear Regression

Economics & Finance

Predict consumer spending based on income, forecast stock prices using historical trends, or model the relationship between interest rates and housing demand.

Science & Engineering

Calibrate sensors, analyze experimental data, model the relationship between temperature and pressure, or estimate material properties from test results.

Social Sciences

Study the correlation between education level and income, analyze survey data, or explore the relationship between public health interventions and outcomes.

Case Study: Real Estate Price Prediction

A real estate analyst collects data on 20 recent home sales, recording the size of each house (in square feet) and its sale price. Using linear regression, they find a slope of $150 per square foot and an intercept of $50,000. This model suggests that each additional square foot adds about $150 to the price, and the base price for a 0‑sq‑ft house (hypothetically) is $50,000. With an R² of 0.82, the model explains 82% of the price variability, indicating a strong linear relationship. The analyst can now use this model to estimate the fair market value of new properties based on their size — a practical application of regression in the real estate industry.

Common Misconceptions About Linear Regression

  • Correlation does not imply causation: A strong correlation between two variables does not mean that one causes the other. There may be confounding factors or coincidence.
  • R² alone is not enough: A high R² does not guarantee that the model is appropriate. Always examine the residual plot for patterns that indicate nonlinearity or heteroscedasticity.
  • Outliers can distort the regression line: A single extreme data point can significantly change the slope and intercept. It is important to identify and handle outliers appropriately.
  • Linear regression assumes linearity: If the true relationship is curved, a linear model will produce biased predictions. Always check the scatter plot for curvature.

Historical Roots and Theoretical Foundations

The method of least squares was independently discovered by Carl Friedrich Gauss (1809) and Adrien-Marie Legendre (1805). Gauss used it in his work on celestial mechanics to predict the orbit of the dwarf planet Ceres. Legendre published the first explicit description of the method in his work Nouvelles méthodes pour la détermination des orbites des comètes. Their contributions laid the groundwork for modern regression analysis, which was later extended by Sir Francis Galton, Karl Pearson, and Ronald Fisher.

The term "regression" was coined by Galton in the 19th century when he observed that the heights of children of tall parents tended to "regress" toward the population mean. Today, regression analysis is a cornerstone of statistics, machine learning, and data science, underpinning countless predictive models in industry and academia.

Assumptions of Linear Regression

For the results of a linear regression to be valid, several key assumptions must be met:

  • Linearity: The relationship between x and y is linear.
  • Independence: The residuals (errors) are independent of each other.
  • Homoscedasticity: The variance of the residuals is constant across all levels of x.
  • Normality: The residuals are approximately normally distributed (important for hypothesis testing and confidence intervals).

While this calculator does not perform diagnostic tests, we encourage users to examine the scatter plot and consider these assumptions when interpreting results.

Frequently Asked Questions

Correlation measures the strength and direction of the linear relationship between two variables (r), whereas regression models the relationship and allows predictions of y from x. Correlation is symmetric, but regression is asymmetric (predicting y from x is different from predicting x from y).

An R² of 0 means that the linear model explains none of the variance in y. This indicates that there is no linear relationship between x and y, or that the model is a poor fit (e.g., a horizontal line with zero slope).

This calculator is designed for simple linear regression with one independent variable. For multiple regression (two or more predictors), please see our dedicated Multiple Regression Calculator.

At least two points are required to fit a line. However, for meaningful results, we recommend at least 10–15 points. With too few points, the regression may be unreliable and sensitive to outliers.

A perfect correlation (r = ±1) means that all data points lie exactly on a straight line. The "Perfect" preset in this calculator demonstrates this ideal case. In real data, perfect correlations are rare and often indicate a deterministic relationship.

A negative slope indicates that as x increases, y tends to decrease. This is often called an inverse relationship. For example, as the price of a product increases, demand may decrease, yielding a negative slope.

Excellent resources include Wikipedia's Linear Regression article, Khan Academy, and the textbook "Introduction to Statistical Learning" by James, Witten, Hastie, and Tibshirani. For a deeper mathematical treatment, see "The Elements of Statistical Learning" by Hastie et al.
References: Wikipedia: Linear Regression; MathWorld: Least Squares Fitting; Draper, N. R. & Smith, H. (1998). Applied Regression Analysis. Wiley.
Reviewed by the GetZenQuery tech team. Last updated June 2026.