Linear Regression Calculator

Fit a least-squares line to paired data and read its slope, R², p-value, confidence intervals, residuals and predictions in one place.

Data

Put x first, then y. Two columns copied from a spreadsheet paste straight in, header row included. Use a dot for decimals, like 12.5.
Optional. Up to 50 values, separated by spaces.

Regression line

Least-squares line

Coefficients

TermEstimateStd. errortp{level}% interval

Variance explained

SourcedfSum of squaresMean squareFp

Fit and residuals

Predictions

The mean interval covers the average y at that x. The individual interval covers one new observation, so it is always wider.

xŷMean {level}%Individual {level}%

Point by point

xyŷy − ŷ

Formula and substitution

b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²

a = ȳ − b·x̄

SSE = Σ(y − ŷ)²

SST = Σ(y − ȳ)²

R² = 1 − SSE ÷ SST

s = √(SSE ÷ (n − 2))

SE(b) = s ÷ √Σ(x − x̄)²

    Your data points and every statistic stay in this browser and are never uploaded.

    FAQ

    What does R² actually tell me?

    R² is the share of the up-and-down movement in y that the line accounts for: 0.96 means 96% of it lines up with x and 4% does not. It says nothing about whether a straight line was the right shape — a clear curve can still score high — so read it next to the residual plot. The correlation r is the square root of R² carrying the slope’s sign, which is why r is negative for a downward line.

    Does a small p-value mean x causes y?

    No. The p-value only answers one narrow question: if the true slope were 0, how often would a sample this size produce a slope at least this far from 0? A small value makes “no relationship at all” an awkward explanation, nothing more. Cause needs the design of the study behind it — a third factor moving both columns, or a sample that was not collected independently, produces small p-values just as easily.

    Why do predictions come with two intervals?

    They answer different questions. The mean interval asks where the average y sits for everyone at that x, so only the uncertainty in the line itself counts. The individual interval asks where one new observation will land, which adds the scatter of points around the line — that extra term is why it is always the wider of the two. Both widen as x moves away from the middle of your data.

    Does it matter which column I put in x?

    Yes. Least squares minimises the vertical gaps, so it treats y as the thing being explained and x as the thing doing the explaining. Swap them and you get a different slope and a different intercept — only r and R² stay the same. Put the variable you want to predict in y; if the columns went in the wrong way round, the swap button rewrites them in place.

    Mathematical Foundations of Ordinary Least Squares

    Ordinary least squares (OLS) is a mathematical method for fitting a straight line to a set of paired data points. The goal of OLS is to find the line that minimizes the sum of the squared vertical distances between the observed data points and the fitted line. This minimization process ensures that the resulting line represents the central trend of the data.

    To calculate the line, the tool first computes the sums of squares about the means. To prevent a loss of precision when dealing with widely separated magnitudes, these calculations are performed in two passes using compensated summation:

    • Sxx = Σ(xᵢ − x̄)²
    • Syy = Σ(yᵢ − ȳ)²
    • Sxy = Σ(xᵢ − x̄)(yᵢ − ȳ)

    Here, x̄ represents the mean of the x values, and ȳ represents the mean of the y values. The slope (b) and the y-intercept (a) of the least-squares line are then derived directly from these sums:

    • Slope b = Sxy ÷ Sxx
    • Intercept a = ȳ − b·x̄

    This mathematical approach guarantees a unique solution for any valid dataset containing at least two points, provided the x values are not all identical.


    Interpreting Regression Coefficients and Summary Statistics

    The fitted regression line is summarized by its slope and intercept, which describe the average relationship between the variables. The tool translates these mathematical values into plain-English interpretations:

    • Slope b: "On average y changes by ‹slope› for every 1 more in x." This represents the rate of change in the dependent variable for each unit increase in the predictor.
    • Intercept a: "Where x is 0 the line sits at y = ‹intercept›." This is the predicted value of y when the predictor x is exactly zero.

    To evaluate how well the line fits the data, the calculator provides several key summary statistics:

    • Correlation r: The correlation coefficient measures the strength and direction of the linear relationship. It is calculated as r = √R² carrying the sign of the slope, which is equivalent to Sxy ÷ √(Sxx·Syy).
    • R² (Coefficient of Determination): This represents the share of the variation explained by the model, calculated as R² = SSR ÷ SST.
    • Adjusted R²: This statistic adjusts R² to account for the degrees of freedom in the model, calculated as 1 − (1 − R²)(n − 1) ÷ (n − 2).
    • Residual spread s: Also known as the residual standard error, this measures the typical distance that data points fall from the regression line. It is calculated as s = √(SSE ÷ (n − 2)).

    Hypothesis Testing and Variance Analysis (ANOVA)

    The calculator performs hypothesis testing on the regression coefficients to determine if the relationship between x and y is statistically significant. The standard error of the slope, which measures the uncertainty of the estimate, is calculated as:

    SE(b) = s ÷ √Sxx

    Using this standard error, the tool calculates a t-statistic to test the null hypothesis that the true slope is zero:

    t = b ÷ SE(b)

    The corresponding two-sided p-value is determined using the regularized incomplete beta function with n − 2 degrees of freedom:

    I_{ν/(ν+t²)}(ν/2, ½)

    The analysis of variance (ANOVA) table partitions the total variation in the dependent variable into two components: the variation explained by the regression line and the unexplained variation left over in the residuals.

    Source df Sum of squares Mean square F p
    Explained by the line 1 SSR MSR = SSR ÷ 1 F = MSR ÷ MSE p
    Left over n − 2 SSE MSE = SSE ÷ (n − 2)
    Total n − 1 SST

    Because this model contains only one predictor, the overall F-statistic is equal to the square of the slope's t-statistic (F = t²). Consequently, both the F-test and the t-test yield the exact same p-value.


    Residual Analysis and Visual Diagnostics

    A regression model is only valid if its underlying assumptions are met. The calculator generates two visual plots to help users diagnose potential issues with their data:

    1. Fitted line: A scatter plot of the points with the least-squares line and the band around its mean. This plot allows users to visually assess how closely the data points cluster around the linear model.
    2. Residuals: A plot of the residuals (y − ŷ) against the predictor variable x, spread around the zero line.

    Analyzing the residual plot is critical. If the residuals are randomly scattered above and below the zero line with consistent variance, the linear model is appropriate. However, if the residual plot exhibits a curved pattern, it indicates that the relationship between x and y is non-linear. If the spread of the residuals widens or narrows as x increases (a phenomenon known as heteroscedasticity), it violates the assumption of constant variance, meaning the calculated standard errors and confidence intervals may be unreliable.


    Confidence Intervals vs. Prediction Intervals

    When predicting y values at specific x coordinates, the calculator distinguishes between two types of intervals:

    • Mean interval: This interval estimates the uncertainty of the average y value for a given x. It accounts only for the uncertainty in the position of the regression line itself. The standard error for the mean response is calculated as:

      s · √(1÷n + (x₀ − x̄)² ÷ Sxx)

    • Individual interval: This interval predicts where a single new observation will fall. Because an individual observation includes both the uncertainty of the line and the natural scatter of the data points around that line, it requires an extra variance term. The standard error for an individual observation is calculated as:

      s · √(1 + 1÷n + (x₀ − x̄)² ÷ Sxx)

    Because of this additional variance term, the individual interval is always wider than the mean interval. Both intervals are narrowest near the mean of the independent variable (x̄) and widen progressively as the prediction coordinate x₀ moves further away from the center of the data.


    Directional Nature of Regression and Variable Swapping

    Linear regression is a directional analysis. The ordinary least-squares method minimizes only the vertical distances (the residuals in the y-direction) between the data points and the fitted line. It assumes that x is the independent predictor variable and y is the dependent response variable.

    Because of this directional minimization, swapping the x and y variables changes the orientation of the residuals being minimized. If you swap the variables, the calculator will fit a completely different line with a different slope and y-intercept. The only statistics that remain unchanged after swapping x and y are the correlation coefficient (r) and the coefficient of determination (R²), as they measure the mutual strength of the linear association rather than a directional prediction. The tool includes a "Swap x and y" action to let users instantly exchange the columns and observe these changes.


    Data Entry Rules, Limits, and Privacy

    The calculator processes all data locally. Every calculation, plot generation, and statistical derivation happens entirely within the user's web browser; no data points or results are ever uploaded to an external server.

    Input Layouts and Formatting

    Users can choose between two input layouts:

    • Paired rows: One x and y value per row, separated by a space or a column delimiter. This layout supports copying and pasting two columns directly from a spreadsheet. If the first row contains non-numeric text, the tool automatically treats it as a header and skips it.
    • Two lists: One input field for "x values (predictor)" and another for "y values (response)".

    The separator rules depend on the selected decimal convention:

    • Dot decimal layout: Values must be separated by spaces, commas, or new lines.
    • Comma decimal layout: Values must be separated by spaces, semicolons, or new lines.

    Boundary Conditions and Errors

    The tool enforces several mathematical and system limits to ensure calculation integrity:

    • Minimum Data: At least 2 points are required. If only 1 point is entered, the tool displays: "Enter at least 2 points; one point does not define a line."
    • Exactly Two Points: If exactly two points are entered, the line is perfectly fixed. The tool displays: "Two points fix the line exactly, so nothing is left over to estimate uncertainty from."
    • Perfect Fit: If all points lie perfectly on the line, the residual spread is zero. The tool displays: "The points sit exactly on the line, so there is no residual spread and no interval or p-value can be estimated."
    • Constant Y: If all y values are identical, the line is flat. The tool displays: "Every y is the same, so the line is flat and r, R² and the significance test are undefined."
    • Constant X (Vertical Line): If all x values are identical, the slope calculation would require dividing by zero. The tool stops and displays: "Every x is the same, so the line would be vertical and the slope divides by zero."
    • Data Limits: The tool supports a maximum of 50,000 points and 1 MB of pasted text. Exceeding this triggers: "Keep the data under 50,000 points."
    • Prediction Limits: Users can predict up to 50 values at a time. Exceeding this triggers: "Predict at most 50 values at a time."

    Frequently Asked Questions

    What does R² actually tell me?

    R² is the share of the up-and-down movement in y that the line accounts for: 0.96 means 96% of it lines up with x and 4% does not. It says nothing about whether a straight line was the right shape — a clear curve can still score high — so read it next to the residual plot. The correlation r is the square root of R² carrying the slope’s sign, which is why r is negative for a downward line.

    Does a small p-value mean x causes y?

    No. The p-value only answers one narrow question: if the true slope were 0, how often would a sample this size produce a slope at least this far from 0? A small value makes “no relationship at all” an awkward explanation, nothing more. Cause needs the design of the study behind it — a third factor moving both columns, or a sample that was not collected independently, produces small p-values just as easily.

    Why do predictions come with two intervals?

    They answer different questions. The mean interval asks where the average y sits for everyone at that x, so only the uncertainty in the line itself counts. The individual interval asks where one new observation will land, which adds the scatter of points around the line — that extra term is why it is always the wider of the two. Both widen as x moves away from the middle of your data.

    Does it matter which column I put in x?

    Yes. Least squares minimises the vertical gaps, so it treats y as the thing being explained and x as the thing doing the explaining. Swap them and you get a different slope and a different intercept — only r and R² stay the same. Put the variable you want to predict in y; if the columns went in the wrong way round, the swap button rewrites them in place.