Regression Calculator

Run simple linear regression on your data. Get the equation, R-squared, scatter plot with best-fit line, residuals, and predictions.

This regression calculator takes paired X and Y data and fits a least-squares regression line. It computes the equation, R-squared, correlation coefficient, standard error, slope significance, and residuals for every data point. It also draws a scatter plot with the best-fit line and a residual plot so you can visually check how well the model fits.

Ad
Ad

About Regression Calculator

How Linear Regression Works - the Least Squares Method

Simple linear regression finds the straight line that best fits a set of data points. "Best fit" means the line that minimises the total squared distance between each observed Y value and the Y value the line predicts. This approach is called the ordinary least squares (OLS) method, introduced by Adrien-Marie Legendre in 1805 and Carl Friedrich Gauss around the same time.

The formulas are:

slope (b) = Σ((xi - x̄)(yi - ȳ)) / Σ(xi - x̄)²

intercept (a) = ȳ - b × x̄

where x̄ and ȳ are the means of the X and Y values. The slope tells you how much Y changes for each one-unit increase in X. The intercept is where the line crosses the Y-axis.

Worked example: Take five data points: (1, 2), (2, 4), (3, 5), (4, 4), (5, 5). The mean of X is 3, the mean of Y is 4. Computing the sums: Σ(dx × dy) = (1-3)(2-4) + (2-3)(4-4) + (3-3)(5-4) + (4-3)(4-4) + (5-3)(5-4) = 4 + 0 + 0 + 0 + 2 = 6. And Σdx² = 4 + 1 + 0 + 1 + 4 = 10. So slope = 6/10 = 0.6, and intercept = 4 - 0.6 × 3 = 2.2. The regression line is y = 0.6x + 2.2. To predict Y when X = 7, plug in: y = 0.6(7) + 2.2 = 6.4.

You can verify regression results against your scatter plot using the graphing calculator, which lets you plot equations alongside raw data.

Understanding R-Squared and Model Fit

R-squared (R²) is the square of the Pearson correlation coefficient. It tells you what fraction of the total variance in Y is accounted for by the regression model. The formula breaks down like this:

R² = 1 - (SSres / SStot)

where SSres is the sum of squared residuals and SStot is the total sum of squares (the variance of Y around its mean). If the regression line passes through every point, SSres = 0 and R² = 1. If the line is no better than just guessing the mean of Y, R² = 0.

R² RangeInterpretationTypical Example
0.90 - 1.00Excellent fit - model explains most of the varianceOhm's law (voltage vs current in a resistor)
0.70 - 0.89Good fit - strong linear pattern with some scatterHeight vs arm span in adults
0.50 - 0.69Moderate fit - clear trend but substantial unexplained varianceStudy hours vs exam score
0.30 - 0.49Weak fit - some linear tendency, lots of noiseIncome vs self-reported happiness
0.00 - 0.29Poor fit - model barely explains the dataShoe size vs IQ

Keep in mind that R-squared alone does not tell you if linear regression is the right model. A curved relationship can produce a modest R-squared that would jump dramatically with a polynomial model. Always check the residual plot before trusting R-squared.

The standard error of the estimate (Se) gives the average distance of observed Y values from the regression line, measured in the same units as Y. Smaller Se means predictions are more precise. Together, R-squared and Se give a complete picture of model quality.

Checking Assumptions with Residual Analysis

Linear regression makes four key assumptions. Residual analysis is the primary way to check them:

Linearity. The relationship between X and Y should be roughly linear. If the residual plot shows a U-shape or any systematic curve, the data has a non-linear pattern that a straight line cannot capture. Consider polynomial regression or transforming the variables.

Homoscedasticity (constant variance). The spread of residuals should be roughly the same across all X values. If the residual plot fans out (wider on the right than the left, or vice versa), the model has heteroscedastic errors. This does not bias the slope estimate, but it makes the standard errors and p-values unreliable.

Independence. Each data point should be independent of the others. This assumption is often violated in time-series data, where consecutive observations tend to be correlated. If your residuals show a wave-like pattern when plotted in order, the independence assumption is broken.

Normality of residuals. For small samples, the residuals should be approximately normally distributed. This matters mainly for the accuracy of confidence intervals and p-values. With large samples (over 30 data points), moderate departures from normality have little practical effect thanks to the central limit theorem. You can assess normality using the normal distribution calculator to compare your residuals against a normal curve.

What to look for in a healthy residual plot: random scatter above and below zero, no patterns, no obvious outliers, and roughly equal spread across the range of X values. If you see clear structure, the linear model needs rethinking.

When to Use Linear Regression

Linear regression is one of several approaches to modelling relationships in data. The right method depends on what your data looks like and what you are trying to predict. Here is a comparison:

MethodBest ForRelationship ShapeOutcome Type
Simple linear regressionOne predictor, continuous outcomeStraight lineContinuous (e.g. weight, temperature)
Multiple linear regressionSeveral predictors, continuous outcomeHyperplane (flat surface)Continuous
Polynomial regressionCurved relationshipsQuadratic, cubic, etc.Continuous
Logistic regressionBinary classificationS-shaped curveBinary (yes/no, 0/1)
Correlation analysisMeasuring strength of associationAny monotonic trendNot predictive

Start with a scatter plot. If the relationship looks like a straight line, simple linear regression is a natural choice. If you see curvature, try adding a polynomial term (x²) or log-transforming one of the variables. If your outcome is categorical (pass/fail, buy/not buy), logistic regression is more appropriate.

One common mistake is forcing linear regression on data that is clearly non-linear. The model will still produce an equation and R-squared, but the predictions will be systematically wrong in parts of the range. The residual plot is your best diagnostic tool - if it shows a pattern, the model is missing something.

For exploring the relationship between variables before fitting a regression, the correlation calculator helps you quantify the strength of association. And if you are testing whether a regression slope is significantly different from zero, that is essentially a t-test on the slope coefficient, which this calculator reports automatically.

Real-World Applications of Linear Regression

Linear regression is one of the most widely used statistical methods across disciplines. Here are some examples of where it appears in practice:

Economics and business. Economists use regression to model relationships like advertising spend versus revenue, or GDP growth versus unemployment. The simplest demand curve - quantity demanded as a function of price - is often modelled as a linear regression. Retail companies use it to forecast sales based on historical trends.

Science and engineering. Hooke's Law (force = spring constant times extension) is a linear relationship. Calibration curves in analytical chemistry plot instrument readings against known concentrations to predict unknown samples. In physics labs, students routinely fit regression lines to experimental data and compare the slope to theoretical predictions.

Health and medicine. Dose-response studies often begin with linear regression to check if a drug's effect increases proportionally with dose. Growth charts that track a child's height over time use regression lines. Epidemiologists regress disease rates against risk factors to estimate effect sizes.

Education. Admissions offices model the relationship between standardised test scores and college GPA. Research on class size and academic performance frequently uses regression to control for confounding variables.

Practical Tips for Better Regression Analysis

Plot your data first. Never run a regression blind. A scatter plot takes seconds and can reveal outliers, non-linearity, or clusters that would invalidate the model. Anscombe's Quartet (1973) famously demonstrated four datasets with identical regression statistics but wildly different scatter plots.

Watch out for extrapolation. Predictions are only reliable within the range of your X data. If your data covers X = 1 to 10, predicting Y at X = 50 assumes the linear trend continues far beyond what you have observed. That assumption is often wrong.

Check for influential points. A single outlier near the edge of your X range can dramatically change the slope and intercept. Look at the residual table for points with unusually large residuals, and consider whether they are data errors or genuine observations.

Report the standard error alongside R-squared. R-squared tells you proportional fit, but the standard error tells you the typical prediction error in the same units as Y. An R-squared of 0.95 is less impressive if the standard error is 20 kg and you are predicting body weight.

Sample size matters. With only 5 data points, your regression coefficients have wide confidence intervals and the p-value may not be trustworthy. A common guideline is at least 10 to 15 observations for simple regression, and at least 10 per predictor for multiple regression. Use the sample size calculator to plan ahead.

Do not confuse statistical significance with practical significance. A large dataset can produce a highly significant slope (tiny p-value) for a relationship that explains very little variance. Always consider both the p-value and R-squared together before drawing conclusions.

Sources

Frequently Asked Questions

What is simple linear regression?

Simple linear regression fits a straight line through paired data points to model the relationship between an independent variable (X) and a dependent variable (Y). The line is chosen to minimise the sum of squared differences between actual Y values and predicted Y values. The result is an equation in the form y = a + bx, where b is the slope and a is the y-intercept.

What does R-squared tell me about my data?

R-squared represents the proportion of variance in Y that is explained by the linear relationship with X. It ranges from 0 to 1. An R-squared of 0.85, for example, means 85% of the variation in Y can be accounted for by X. The remaining 15% comes from other factors or random variation. Higher R-squared values indicate a better fit, but a high value alone does not prove the model is correct.

How many data points do I need for reliable linear regression?

You need at least 2 points to draw a line, but that gives a perfect fit with no useful statistics. For meaningful regression analysis, aim for 20 or more data points. With fewer than 10, the standard errors are large and the p-value is unreliable. A common rule of thumb in statistics is at least 10 to 15 observations per predictor variable.

When should I use linear regression versus polynomial or logistic regression?

Use linear regression when the relationship between X and Y is roughly a straight line. If your scatter plot shows a curve, polynomial regression may be a better fit. Logistic regression is for binary outcomes (yes or no, pass or fail) rather than continuous Y values. Always plot your data first to check if a straight line is reasonable.

What are residuals and why do they matter?

Residuals are the differences between the observed Y values and the predicted Y values from the regression line. They tell you how far off the model is for each data point. In a good linear model, residuals should be randomly scattered around zero with no clear pattern. If you see a curve or funnel shape in the residual plot, it suggests the linear model is not capturing the full relationship in the data.

Link to this tool

Copy this HTML to link to this tool from your website or blog.

<a href="https://toolboxkit.io/tools/regression-calculator/" title="Regression Calculator - Free Online Tool">Try Regression Calculator on ToolboxKit.io</a>