{
  "id": "linear-regression",
  "version": "750cd52555bb",
  "status": "published",
  "name": "Linear Regression Calculator",
  "question": "What is my linear regression line?",
  "summary": "Fits the least-squares line y = a + bx to paired x and y values and reports the slope, intercept, correlation r, r², residual standard deviation, and a prediction.",
  "category": "statistics",
  "subcategory": "descriptive",
  "url": "https://www.acalculator.org/statistics/linear-regression-calculator",
  "markdown": "https://www.acalculator.org/statistics/linear-regression-calculator.md",
  "kind": "function",
  "method": "b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²; a = ȳ − b x̄; the line is y = a + bx.",
  "assumptions": [
    "The first x value goes with the first y value, and so on, so both lists must be the same length.",
    "The line minimises the sum of squared vertical distances from the points (ordinary least squares).",
    "r and r² are not defined when every y is the same, and the residual standard deviation needs at least 3 points."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "x": {
        "title": "x values",
        "description": "The x values (the predictor), in order, separated by commas, spaces, semicolons, or new lines.",
        "type": "array",
        "items": {
          "type": "number"
        }
      },
      "y": {
        "title": "y values",
        "description": "The y values (the response), one for each x value, in the same order.",
        "type": "array",
        "items": {
          "type": "number"
        }
      },
      "at": {
        "title": "New x value",
        "description": "An x value at which to read y from the fitted line. Leave empty to skip.",
        "type": "number"
      }
    }
  },
  "outputs": {
    "equation": {
      "label": "Line of best fit",
      "description": "The least-squares line, with the slope and intercept rounded to 4 decimal places.",
      "format": "text"
    },
    "slope": {
      "label": "Slope (b)",
      "description": "How much y changes, on average, when x goes up by 1: Sxy ÷ Sxx.",
      "format": "number"
    },
    "intercept": {
      "label": "Intercept (a)",
      "description": "The value of y on the line where x = 0.",
      "format": "number"
    },
    "r": {
      "label": "Correlation (r)",
      "description": "Pearson’s correlation coefficient, from −1 to 1. Not shown when every y is the same.",
      "format": "number"
    },
    "r2": {
      "label": "r²",
      "description": "The share of the variation in y that the line explains, from 0 to 1. Not shown when every y is the same.",
      "format": "number"
    },
    "residualSd": {
      "label": "Residual standard deviation (s)",
      "description": "The typical distance of a point from the line: √(SSE ÷ (n − 2)). Needs 3 or more points.",
      "format": "number"
    },
    "predicted": {
      "label": "Predicted y",
      "description": "The value of y on the line at the new x value: a + b × x.",
      "format": "number"
    },
    "n": {
      "label": "Number of points (n)",
      "description": "How many (x, y) pairs there are.",
      "format": "integer"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "x": "1, 2, 3, 4, 5",
      "y": "2, 4, 5, 4, 5",
      "at": 6
    },
    "outputs": {
      "equation": "y = 0.6x + 2.2",
      "slope": 0.6,
      "intercept": 2.2,
      "r": 0.7745966692414834,
      "r2": 0.6000000000000001,
      "residualSd": 0.894427190999916,
      "predicted": 5.8,
      "n": 5
    },
    "text": "The line of best fit through 5 points is y = 0.6x + 2.2."
  },
  "examples": [
    {
      "given": {
        "x": [
          1,
          2,
          3,
          4,
          5
        ],
        "y": [
          2,
          4,
          5,
          4,
          5
        ],
        "at": 6
      },
      "expect": {
        "equation": "y = 0.6x + 2.2",
        "slope": 0.6,
        "intercept": 2.2,
        "r": 0.7745966692414834,
        "r2": 0.6,
        "residualSd": 0.894427190999916,
        "predicted": 5.8
      },
      "source": "hand calculation in content.mdx: Sxx = 10, Sxy = 6, Syy = 6, b = 0.6, a = 4 − 0.6 × 3 = 2.2, r = 6 ÷ √60, s = √(2.4 ÷ 3); Python statistics module, linear_regression and correlation"
    },
    {
      "given": {
        "x": [
          0.2,
          337.4,
          118.2,
          884.6,
          10.1,
          226.5,
          666.3,
          996.3,
          448.6,
          777,
          558.2,
          0.4,
          0.6,
          775.5,
          666.9,
          338,
          447.5,
          11.6,
          556,
          228.1,
          995.8,
          887.6,
          120.2,
          0.3,
          0.3,
          556.8,
          339.1,
          887.2,
          999,
          779,
          11.1,
          118.3,
          229.2,
          669.1,
          448.9,
          0.5
        ],
        "y": [
          0.1,
          338.8,
          118.1,
          888,
          9.2,
          228.1,
          668.5,
          998.5,
          449.1,
          778.9,
          559.2,
          0.3,
          0.1,
          778.1,
          668.8,
          339.3,
          448.9,
          10.8,
          557.7,
          228.3,
          998,
          888.8,
          119.6,
          0.3,
          0.6,
          557.6,
          339.3,
          888,
          998.5,
          778.9,
          10.2,
          117.6,
          228.9,
          668.4,
          449.2,
          0.2
        ]
      },
      "expect": {
        "slope": 1.00211681802045,
        "intercept": -0.262323073774029,
        "residualSd": 0.884796396144373,
        "r2": 0.999993745883712
      },
      "source": "NIST StRD linear least squares dataset Norris: certified B1, B0, residual standard deviation, and R-squared (15 digits, so tolerance 1e-12; Python agrees to 4.7e-14)",
      "tolerance": 1e-12
    },
    {
      "given": {
        "x": [
          0,
          1,
          2,
          3
        ],
        "y": [
          10,
          7,
          4,
          1
        ]
      },
      "expect": {
        "equation": "y = −3x + 10",
        "slope": -3,
        "intercept": 10,
        "r": -1,
        "r2": 1,
        "residualSd": 0
      },
      "source": "hand calculation in content.mdx: every step in x lowers y by 3, and y = 10 at x = 0"
    }
  ],
  "sources": [
    "NIST/SEMATECH e-Handbook of Statistical Methods, section 4.4.3.1, Least Squares (slope, intercept, and residual standard deviation). https://www.itl.nist.gov/div898/handbook/pmd/section4/pmd431.htm",
    "NIST/SEMATECH e-Handbook of Statistical Methods, section 4.1.4.1, Linear Least Squares Regression. https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd141.htm",
    "NIST Statistical Reference Datasets, linear least squares regression: Norris, certified values. https://www.itl.nist.gov/div898/strd/lls/data/Norris.shtml"
  ],
  "related": [
    "standard-deviation",
    "z-score",
    "average"
  ],
  "changelog": []
}
