Statistics

Linear Regression Calculator

Paste two columns of numbers and get the line of best fit, the regression equation, R², p-values, confidence and prediction intervals, and a residual plot. Every step of the least squares calculation is shown, so you can check it or copy the working into an assignment.

Your data

Copy two columns from Excel or Google Sheets and paste. Tabs, commas, semicolons or spaces between X and Y all work. A header row is picked up as the variable names.

Paste your data and press Calculate, or load the example to see how it works.

What this calculator does

Simple linear regression fits a straight line through a set of (X, Y) pairs so that you can describe how Y changes when X changes, and predict Y for a value of X you have not observed. The calculator uses ordinary least squares: it picks the line that makes the sum of squared vertical distances between the points and the line as small as possible.

You get the fitted equation, how well the line fits (R² and the standard error of the estimate), whether the slope is statistically different from zero (t-test and p-value), confidence intervals for both coefficients, an ANOVA table with the F-test, and a residual plot for checking assumptions.

How to use it

  1. Put your predictor in one column and your outcome in the next, in Excel, Google Sheets or a CSV file.
  2. Select both columns, copy, and paste into the box above. Include the header row if you want the variable names carried through.
  3. Press Calculate. Use the tabs to switch between the chart, residuals, coefficient table, ANOVA table and the fitted values.
  4. Enter a new X in the Predict box to get a predicted Y with its confidence and prediction intervals.

The formulas behind the line

The fitted line has the form

ŷ = b₀ + b₁x

The slope and intercept come from the means of X and Y and two sums of squares:

Sxx = Σ(x − x̄)²   ·   Sxy = Σ(x − x̄)(y − ȳ)
b₁ = Sxy / Sxx   ·   b₀ = ȳ − b₁x̄

Fit is measured from the residuals, the gaps e = y − ŷ between each actual value and the line. The sum of squared residuals (SSE) and the total variation in Y (SST) give

R² = 1 − SSE / SST   ·   s = √(SSE / (n − 2))   ·   SE(b₁) = s / √Sxx

The slope’s t statistic is b₁ / SE(b₁), tested on n − 2 degrees of freedom. Open Show the working under any result to see each of these numbers for your own data.

Worked example: does ad spend move online sales?

Press Load example to use this data. A Bengaluru D2C brand recorded its monthly digital ad spend and its online sales for 12 months, both in ₹ lakh. Spend ranged from ₹1.2 lakh to ₹5.8 lakh.

  • Equation: Sales = 5.423 + 3.557 × Ad spend. Each extra ₹1 lakh of ad spend is associated with about ₹3.56 lakh more in sales, with a 95% confidence interval of ₹3.09 lakh to ₹4.02 lakh.
  • Fit: R² = 0.9665, so ad spend explains about 96.7% of the month-to-month variation in sales. The typical miss (standard error of the estimate) is about ₹1.01 lakh.
  • Significance: the slope’s p-value is below 0.0001, so a relationship this strong is very unlikely to be chance.
  • Prediction: at ₹4.5 lakh of spend the model predicts ₹21.43 lakh of sales. The 95% prediction interval for a single month is ₹19.03 lakh to ₹23.83 lakh.

Two cautions apply. The data is observational, so other things that rose with ad spend (a festive season, a price cut) may be doing part of the work. And predicting for spend well outside ₹1.2 to ₹5.8 lakh is extrapolation: nothing in this data says the straight line holds at ₹15 lakh.

Checking the assumptions

The p-values and intervals are only trustworthy if the data roughly meets four conditions:

  • Linearity: the relationship is a straight line, not a curve. A curved pattern in the residual plot says otherwise.
  • Independence: each observation is separate. Monthly or daily data often breaks this, because one period affects the next.
  • Constant variance: residuals spread out evenly across the fitted values. A funnel shape means the error grows with Y.
  • Roughly normal errors: matters most for small samples. A few extreme residuals can distort the slope and its p-value.

The residual tab is the quickest check. For a fuller treatment, with tests and fixes for each violation, see the chapter on regression assumptions and diagnostics.

Getting the same result in Excel and Python

In Excel, =SLOPE(B2:B13, A2:A13), =INTERCEPT(B2:B13, A2:A13) and =RSQ(B2:B13, A2:A13) return the slope, intercept and R². =LINEST(B2:B13, A2:A13, TRUE, TRUE) returns the full table including standard errors. In Python:

import statsmodels.api as sm

x = [1.2, 1.8, 2.0, 2.5, 2.9, 3.1, 3.6, 4.0, 4.4, 4.9, 5.3, 5.8]
y = [9.8, 12.1, 11.4, 14.9, 15.2, 17.8, 16.9, 20.3, 21.0, 22.6, 25.9, 24.8]

model = sm.OLS(y, sm.add_constant(x)).fit()
print(model.summary())   # same coefficients, R², t and p as the calculator

Common mistakes

  • Swapping X and Y. Regressing sales on ad spend is not the same model as regressing ad spend on sales. Put the variable you want to predict in the second column.
  • Reading R² as accuracy. A high R² says the line follows the points closely. It says nothing about whether X causes Y, and a low R² can still come with a real, useful slope.
  • Ignoring outliers. One extreme point can tilt the whole line. Check the fitted values tab for unusually large residuals before you trust the slope.

Related learning on Codeayan

FAQ

Linear regression questions

What is the difference between r and R² in linear regression?

r is the correlation coefficient. It runs from −1 to +1 and tells you the direction and strength of the straight-line relationship. R² is r squared in simple linear regression. It runs from 0 to 1 and tells you the share of the variation in Y that the line explains. An r of 0.9 gives an R² of 0.81, so the line explains 81% of the variation.

What does the p-value of the slope mean?

It is the probability of seeing a slope at least this far from zero if X truly had no linear effect on Y. A p-value below your significance level (usually 0.05) means the relationship is unlikely to be a fluke of this sample. It does not tell you the relationship is causal or that it is large enough to matter.

How many data points do I need for linear regression?

The calculator needs at least 3 pairs, because two points always fit a line perfectly and leave nothing to estimate the error from. For results you can rely on, aim for 20 or more pairs. With very small samples the confidence intervals become wide and one unusual point can swing the slope.

What is the difference between the confidence interval and the prediction interval?

The confidence interval is the range for the average Y at a given X. The prediction interval is the range for a single new observation at that X. The prediction interval is always wider because a single value carries its own random noise on top of the uncertainty in the line.

Can I use this calculator for multiple linear regression?

No. This tool fits one predictor (X) against one outcome (Y). For two or more predictors you need multiple linear regression, which you can run in Python with statsmodels or in Excel with the LINEST function.

More tools

Keep going