Linear Regression Calculator - Best Fit Line and Prediction

    Which straight line fits your data, and how far can you trust it? Get the least-squares equation, a test of the slope, R squared and a forecast with intervals from two lists or pasted columns.

    Parameters

    Enter data for calculations

    What you know or control

    Same order as X, or empty for pasted columns

    Optional, leave empty for the equation only

    Width of every interval and the slope test

    Form progress0 / 2 fields

    💡 Fill in all required fields to unlock the calculate button

    Linear regression equation, predictions and the pair that moves the line

    Four famous data sets by the statistician Frank Anscombe all produce the same fitted line, y = 3.00 + 0.500x, the same R squared of 0.67 and the same p-value of 0.0022. One of them is a clean straight cloud, one is a curve, one has a single outlier and one has a slope set by a single point. This calculator fits the least-squares line to your pairs and then tells you which of those four situations you are in. Paste X and Y as two lists, or two columns straight from a spreadsheet, and you get the equation, the slope with its standard error, t, p and confidence interval, R squared and adjusted R squared, Pearson's r, the F statistic, a prediction for any X with both kinds of interval, and a residual table that flags the pairs worth a second look.

    Four boxes, filled in this order

    1. X values - the variable you know or control: hours studied, temperature, miles driven, the month number. Separate them with spaces, commas or new lines. To paste two spreadsheet columns at once, put them here, one pair per line, and leave the Y box empty; a header row such as "hours score" becomes the axis names.
    2. Y values - the variable you want to predict, in the same order, so the fourth Y belongs to the fourth X. Use a dot for decimals and no thousands separators: 1000, not 1,000, which the calculator refuses rather than guessing.
    3. X to predict for - optional. One X value; the result gives the predicted Y, a prediction interval for a single new observation and a narrower confidence interval for the average Y. Values outside your data range get an extrapolation warning.
    4. Confidence level - 95% is the usual choice, 99% gives wider intervals and a stricter test of the slope, 90% suits a first look. Nothing is preselected, so the level in the report is always the one you picked.
    5. Read the result - the equation and the verdict on the slope first, then the five summary tiles, the prediction, the coefficient table and the residuals. A yellow note appears only when something needs your attention: one pair steering the slope, a slope resting on a single X value, extrapolation or a very small sample.

    The least-squares line, done by hand on five pairs

    Every number in the result comes from four sums. Take X = 1, 2, 3, 4, 5 and Y = 2, 4, 5, 4, 5. The means are 3 and 4.

    Sxx = sum of (x - mean x)^2 = 10
    Sxy = sum of (x - mean x)(y - mean y) = 6
    slope b = Sxy / Sxx = 0.6
    intercept a = mean y - b × mean x = 4 - 0.6 × 3 = 2.2
    SSE = sum of squared residuals = 2.4, SST = sum of (y - mean y)^2 = 6
    R squared = 1 - SSE / SST = 0.6
    s = square root of SSE / (n - 2) = 0.8944
    SE of slope = s / square root of Sxx = 0.2828, t = 0.6 / 0.2828 = 2.121

    With 3 degrees of freedom a t of 2.121 gives a two-sided p of 0.1240, so this slope is not significant at 0.05 even though R squared is 0.60. Five points are simply too few to rule out chance. The calculator prints exactly these figures, plus adjusted R squared (0.4667), and it also notices that dropping pair 1 alone would cut the slope from 0.6000 to 0.2000.

    The same fit comes out of Excel's SLOPE, INTERCEPT, RSQ, STEYX and LINEST functions and from lm(y ~ x) in R, so you can use the calculator to check a spreadsheet, or the other way round.

    Regression line or correlation coefficient: which question each one answers

    Both come from the same sums, and r is simply the signed square root of R squared in a one-predictor model. The difference is the question. The table sets them side by side.

    Question Regression (this tool) Correlation Who should pick it
    How much does Y change per unit of X? yes, the slope with its units no, r has no units anyone budgeting, pricing or dosing
    What Y should I expect at X = 27? yes, with a prediction interval no forecasting and planning
    Does it matter which variable is X? yes, Y on X and X on Y are different lines no, r is symmetric regression when one variable is the outcome
    Is the relationship there at all? t test of the slope t test of r, same p-value either
    Robust version when an outlier dominates influence check on each pair Spearman's rank correlation run both and compare
    Verdict Use regression when you want a number out (a slope, a forecast); use correlation when you only need the strength of the association, for which the Pearson correlation calculator adds Spearman's rho and a Fisher interval.

    Anscombe's quartet: one equation, four stories

    All four sets have eleven pairs. Paste any of them and the headline numbers barely move. What changes is the residual table and the notes under it, and that is the part a plain line-fitting tool leaves out.

    Set Equation R squared What the calculator adds
    I, straight cloud 3.000 + 0.5001x 0.6665 nothing, no note: this is the case the line was made for
    II, a curve 3.001 + 0.5000x 0.6662 residuals run negative, positive, negative along X; the ends at X = 4 and 14 both sit 1.86 standard errors below the line
    III, one outlier 3.002 + 0.4997x 0.6663 pair 3 (13, 12.74) in red with a standardized residual of 3.00, Cook's distance 1.39, and the slope without it: 0.3454
    IV, one lever 3.002 + 0.4999x 0.6667 ten X values are all 8, so the note says the slope rests on pair 8 (x = 19) alone; its residual is exactly 0, which is why no residual check could ever catch it

    All four report p = 0.0022 for the slope. A p-value only measures how far the slope is from zero relative to the scatter; it cannot tell a straight relationship from a curve or from a line held up by one point.

    Five everyday data sets through the calculator

    Temperature and ice cream sales. Daily highs of 14, 17, 19, 22, 24, 26, 29 and 31 degrees against 120 to 300 cones.
    Result: y = -31.86 + 10.71x, R squared 0.9983; at 27 degrees the forecast is 257.4 cones, with a 95% prediction interval of 249.8 to 265.0.
    Hours studied and test score, pasted as two columns with the header "hours score": 1 to 6 hours, scores 52, 55, 61, 64, 70, 71.
    Result: y = 47.87 + 4.086x, p = 0.0002; each extra hour is worth about 4.1 points, with a 95% interval for the slope of 3.226 to 4.945.
    Trip length and fuel cost. Eight trips from 22 to 130 miles costing 4.10 to 14.10 dollars.
    Result: y = 2.122 + 0.09057x, so about 9.1 cents a mile on top of a fixed 2.12; a 100-mile trip is predicted at 11.18 dollars.
    No relationship at all. X from 1 to 8, Y = 5, 3, 6, 2, 5, 4, 3, 6.
    Result: slope 0.04762, p = 0.8536, R squared 0.0061 and adjusted R squared -0.1595: negative, because the line explains less than it costs in degrees of freedom.
    Forecasting past the data. The ice cream line at 35 degrees, 99% level.
    Result: 343.1 cones, prediction interval 329.3 to 356.9, and a warning that 35 lies outside the measured range of 14 to 31.

    Mistakes that make a regression line misleading

    Reading R squared as proof of a straight line. Anscombe II has R squared 0.666 and is a curve. Instead: look at the signs of the residuals in order of X; long runs of the same sign mean the relationship bends.
    Quoting the confidence interval as the range for the next value. For the ice cream data at 27 degrees the interval for the average is 254.3 to 260.5, while a single new day falls between 249.8 and 265.0. Instead: use the prediction interval for individual forecasts.
    Extrapolating without saying so. The arithmetic is happy to predict at any X, and the intervals assume the line stays straight. Instead: treat anything outside the data range as a scenario, not a forecast.
    Swapping X and Y. Regressing Y on X minimizes vertical distances; X on Y minimizes horizontal ones and gives a different line whenever r is not exactly 1 or -1. Instead: put the variable you want to predict in the Y box.
    Reading the slope as a cause. Hot days sell ice cream, but a slope alone cannot tell which way an effect runs or whether a third variable drives both. Instead: say "is associated with" unless the X values were set by an experiment.

    Regression questions, answered with the numbers

    What is a good R squared value?
    It depends on the field. Physical measurements often reach 0.95 or more, while data about people rarely pass 0.5. More useful than a threshold is the slope's interval and the standard error of the estimate: with the hours and score data, R squared is 0.9776 and predictions still miss by about 1.3 points on average.
    Why is adjusted R squared lower, or even negative?
    Adjusted R squared charges for the degree of freedom the slope uses: 1 - (1 - R squared)(n - 1)/(n - 2). With few pairs and a weak line it drops below zero, as in the no-relationship example (-0.1595). A negative value simply means the line explains nothing.
    How many data points do I need for linear regression?
    The calculator accepts 3, which leaves one degree of freedom. In practice ten pairs is a sensible minimum, and below that the result carries a small-sample note. The five-pair example shows why: R squared 0.60, yet p = 0.1240.
    What does Cook's distance mean in the note?
    It measures how far the whole fitted line would move if that one pair were removed. The note appears when the most influential pair is above 4/n, is also far from the line or above 1, and removing it changes the slope by more than 20%. It then prints the slope without that pair, calculated exactly, so you can see what is at stake.
    Is the p-value for the slope the same as for the F test?
    Yes, with one predictor F equals t squared. For Anscombe I, t = 4.241 and F = 17.99, both with p = 0.0022. The test of r in a correlation calculator gives that same p as well.

    Related tools

    R Squared Calculator

    Adjusted R squared, the overall F test and the Excel RSQ gap for any model - See calculator

    Z-Score Calculator

    Standardized residuals are z-scores; score each value and flag outliers with the modified z - See calculator

    Pearson Correlation Calculator

    Strength of the association with a p-value, Fisher interval and Spearman's rho - See calculator

    Coefficient of Variation Calculator

    Spread of a list as a share of its mean - See calculator

    Interquartile Range Calculator

    Quartiles and outlier fences to screen each variable before fitting a line - See calculator

    Skewness Calculator

    Checks whether a variable is lopsided enough to need a log transform - See calculator

    Standard Deviation Calculator

    Spread of one list, sample or population - See calculator

    Linear Function Calculator

    Zero, intercepts and direction of a line once you know its coefficients - See calculator

    Similar calculators from this section