Linear Regression Calculator

Compute the best-fit line using Ordinary Least Squares (OLS). Instantly get slope, intercept, correlation coefficient (r), R², and residuals. Visualize data points and regression line on an interactive canvas.

? Strong Positive (r≈0.99)
? Weak Correlation (r≈0.6)
? Negative Correlation
✨ Perfect Linear (y=2x+1)
⚠️ Outlier Example

Click or drag file here

Supports CSV, TSV, JSON (array of objects with x,y or [x,y] pairs)
Sample Formats:
CSV/TSV:
x,y
1,2
2,3.5
3,4.8

JSON:
[{"x":1,"y":2},{"x":2,"y":3.5}]  OR  [[1,2],[2,3.5]]
At least 2 points required. All calculations are local and private.
Privacy first: All calculations are performed locally in your browser. Your data never leaves your device.

? Interactive Case Study: Sales Forecasting with Linear Regression

Imagine you're a marketing analyst trying to predict sales based on advertising spend. Use the simulation below to see how changes in ad budget affect predicted sales.

$8,000

Current Regression Model: ŷ = ? + ?·x

Predicted Sales: (in $1000)

* This simulation uses the current data points from your analysis above. Click "Compute Regression" first to load a model.

? Understanding Linear Regression & The Least Squares Principle

Linear regression is a fundamental statistical method that models the relationship between a dependent variable (Y) and one independent variable (X) using a linear equation ŷ = b₀ + b₁x. The coefficients are estimated by minimizing the sum of squared vertical distances (residuals) between observed data and the line — the Ordinary Least Squares (OLS) criterion. This method was developed by Carl Friedrich Gauss and Adrien-Marie Legendre in the early 19th century and remains the backbone of predictive analytics, econometrics, and machine learning.

Formulas:

Slope: b₁ = Σ[(xᵢ - x̄)(yᵢ - ȳ)] / Σ(xᵢ - x̄)²

Intercept: b₀ = ȳ - b₁·x̄

Pearson correlation r = Cov(X,Y) / (σₓ·σᵧ)    R² = r²

? Key Assumptions of Linear Regression

1. Linearity

The relationship between X and Y is linear. Check by examining if residuals are randomly scattered around zero.

2. Independence

Observations are independent of each other. Particularly important in time series data.

3. Homoscedasticity

Constant variance of residuals across all levels of X. Look for funnel shapes in residual plots.

4. Normality of Residuals

Residuals should be approximately normally distributed for valid confidence intervals and p-values.

? Real‑World Applications

  • Economics & Finance: Forecasting consumer spending based on disposable income, stock return prediction using beta.
  • Healthcare: Estimating blood pressure from age or BMI, drug dosage response modeling.
  • Engineering: Calibration curves, material strength vs. temperature.
  • Marketing Analytics: Sales prediction from advertising spend (ROI analysis).
  • Environmental Science: CO₂ emissions vs. global temperature anomalies.

? How To Use This Interactive Tool

  1. Add or remove data points using the dynamic inputs (minimum 2 points required).
  2. Click Compute Regression & Draw to instantly calculate the best-fit line, r, R², slope, and intercept.
  3. Visualize the scatter plot: each blue point is actual data, orange line shows predicted values, gray dashed lines represent residuals.
  4. Review the residual table to assess model accuracy. Smaller residuals indicate a better fit.
  5. Experiment with preset examples (perfect linear, weak correlation, negative trend, outlier effect) to see how regression responds.
Pedagogical Note: Interpreting r and R²

The correlation coefficient r ranges from -1 to +1, indicating strength and direction of linear association. R² (coefficient of determination) represents the proportion of variance in Y explained by X. For instance, R² = 0.85 means 85% of the variability is captured by the model. Our calculator also helps detect outliers — points that heavily influence slope — by visualizing residuals.

⚙️ Assumptions & Limitations

Classical linear regression assumes linearity, independence of errors, homoscedasticity (constant variance), and normality of residuals for valid inference. Our tool is designed for exploratory analysis and educational demonstration; for rigorous inferential statistics, additional diagnostics (like p-values, confidence intervals) are required. However, the numerical accuracy meets academic standards with double‑precision arithmetic.

? Advanced: The Mathematics Behind the Scenes

The algorithm implemented uses the closed-form solution derived from calculus: partial derivatives of the sum of squared errors lead to normal equations. The slope formula reduces to the ratio of the covariance of X and Y to the variance of X. For computational stability, we use the two-pass method (mean centering) to avoid catastrophic cancellation. All calculations are performed locally on your device — no data is transmitted, ensuring absolute privacy.

? Frequently Asked Questions

It depends on the field. In physical sciences, R² > 0.9 is common; in social sciences, 0.3-0.5 may be acceptable. Our tool helps you explore.

This calculator requires numeric continuous variables. For categorical predictors, consider multiple regression with dummy variables.

At least 2 distinct points are required to define a line. The tool will show a warning and prevent calculation.

We use JavaScript's 64-bit floating point arithmetic. Typical rounding errors are below 1e-12, more than enough for practical use.
Methodology & References: Implementation based on standard statistical literature (Montgomery, D.C., "Introduction to Linear Regression Analysis", 6th ed.). Aligned with educational standards (AP Statistics, introductory econometrics). Reviewed by the GetZenQuery Tech team. Last updated March 2026.