{
  "id": "chi-square",
  "version": "f7bc9da0fd0b",
  "status": "published",
  "name": "Chi-Square Calculator",
  "question": "What is my chi-square test result?",
  "summary": "Computes the chi-square statistic, degrees of freedom, and p-value of a goodness-of-fit test or a test of independence on a contingency table.",
  "category": "statistics",
  "subcategory": "tests",
  "url": "https://www.acalculator.org/statistics/chi-square-calculator",
  "markdown": "https://www.acalculator.org/statistics/chi-square-calculator.md",
  "kind": "function",
  "method": "χ² = Σ (O − E)² ÷ E; goodness of fit: E from the expected values scaled to the observed total, df = categories − 1; independence: E = row total × column total ÷ grand total, df = (rows − 1)(columns − 1); p-value = P(χ² with df degrees of freedom ≥ χ²).",
  "assumptions": [
    "The observations are independent counts. The chi-square p-value is an approximation that works best when every expected count is 5 or more.",
    "The statistic is exact on the counts you type, then rounded once; the p-value is the chi-square upper tail, computed directly."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "test": {
        "title": "Test",
        "description": "Goodness of fit compares one row of counts with expected counts; independence tests whether the rows and columns of a table are related.",
        "type": "string",
        "enum": [
          "gof",
          "independence"
        ]
      },
      "obs": {
        "title": "Observed counts",
        "description": "The count in each category, separated by commas or spaces.",
        "type": "array",
        "items": {
          "type": "number"
        }
      },
      "exp": {
        "title": "Expected counts or proportions",
        "description": "The expected count, proportion, or percent for each category, in the same order. They are scaled to the observed total. Leave empty for equal counts.",
        "type": "array",
        "items": {
          "type": "number"
        }
      },
      "table": {
        "title": "Observed counts table",
        "description": "The contingency table: one row per group and one column per outcome, each cell a count.",
        "type": "array",
        "items": {
          "type": "array",
          "items": {
            "type": "number"
          }
        }
      },
      "alpha": {
        "title": "Significance level (α)",
        "description": "The cut-off for the p-value, for example 0.05.",
        "type": "number",
        "minimum": 1e-10,
        "maximum": 0.5
      }
    }
  },
  "outputs": {
    "statistic": {
      "label": "Chi-square statistic (χ²)",
      "description": "The sum over all cells of (observed − expected)² ÷ expected.",
      "format": "number"
    },
    "df": {
      "label": "Degrees of freedom",
      "description": "Categories − 1 for goodness of fit; (rows − 1) × (columns − 1) for independence.",
      "format": "integer"
    },
    "pValue": {
      "label": "P-value",
      "description": "The chance of a chi-square statistic at least this large if the null hypothesis is true.",
      "format": "number"
    },
    "critical": {
      "label": "Critical value",
      "description": "The chi-square value with an upper tail of α: the statistic must reach it to reject.",
      "format": "number"
    },
    "decision": {
      "label": "Decision",
      "description": "Reject the null hypothesis when the p-value is α or less.",
      "format": "text"
    },
    "smallExpected": {
      "label": "Expected counts below 5",
      "description": "How many expected counts are below 5; with many, the chi-square approximation is rough.",
      "format": "integer"
    },
    "n": {
      "label": "Total count",
      "description": "All the observed counts added up.",
      "format": "number"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "test": "gof",
      "obs": "15, 12, 9, 9, 15",
      "table": [
        [
          111,
          96,
          48
        ],
        [
          96,
          133,
          61
        ],
        [
          91,
          150,
          53
        ]
      ],
      "alpha": 0.05
    },
    "outputs": {
      "statistic": 3,
      "df": 4,
      "pValue": 0.5578254003710746,
      "critical": 9.487729036781156,
      "decision": "Do not reject the null hypothesis",
      "smallExpected": 0,
      "n": 60
    },
    "text": "χ² = 3 with 4 degrees of freedom and a p-value of 0.557825. Do not reject the null hypothesis at α = 0.05."
  },
  "examples": [
    {
      "given": {
        "test": "gof",
        "obs": [
          15,
          12,
          9,
          9,
          15
        ],
        "alpha": 0.05
      },
      "expect": {
        "statistic": 3,
        "df": 4,
        "pValue": 0.5578254003710745,
        "decision": "Do not reject the null hypothesis",
        "n": 60
      },
      "source": "OpenStax, Introductory Statistics 2e, §11.2 Goodness-of-Fit Test. https://openstax.org/books/introductory-statistics-2e/pages/11-2-goodness-of-fit-test, Example 11.2 (χ² = 3, df = 4, p-value 0.5578)"
    },
    {
      "given": {
        "test": "independence",
        "table": [
          [
            111,
            96,
            48
          ],
          [
            96,
            133,
            61
          ],
          [
            91,
            150,
            53
          ]
        ],
        "alpha": 0.05
      },
      "expect": {
        "statistic": 12.990918513170868,
        "df": 4,
        "pValue": 0.01132025305418837,
        "decision": "Reject the null hypothesis"
      },
      "source": "OpenStax, Introductory Statistics 2e, §11.3 Test of Independence. https://openstax.org/books/introductory-statistics-2e/pages/11-3-test-of-independence, Example 11.6 (χ² = 12.99, df = 4, p-value 0.0113)"
    },
    {
      "given": {
        "test": "gof",
        "obs": [
          30,
          14,
          34,
          45,
          57,
          20
        ],
        "exp": [
          20,
          20,
          30,
          40,
          60,
          30
        ],
        "alpha": 0.05
      },
      "expect": {
        "statistic": 11.441666666666666,
        "df": 5,
        "critical": 11.070497693516353,
        "pValue": 0.04329313031580498
      },
      "source": "hand calculation in content.mdx: 5 + 1.8 + 0.533333 + 0.625 + 0.15 + 3.333333 = 11.441667; NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.5.15 Chi-Square Goodness-of-Fit Test. https://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm"
    },
    {
      "given": {
        "test": "gof",
        "obs": [
          15,
          12,
          9,
          9,
          15
        ],
        "exp": [
          0.2,
          0.2,
          0.2,
          0.2,
          0.2
        ],
        "alpha": 0.05
      },
      "expect": {
        "statistic": 3,
        "pValue": 0.5578254003710745
      },
      "source": "hand calculation in content.mdx: 0.2 × 60 = 12 each; OpenStax, Introductory Statistics 2e, §11.2 Goodness-of-Fit Test. https://openstax.org/books/introductory-statistics-2e/pages/11-2-goodness-of-fit-test"
    }
  ],
  "sources": [
    "NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.5.15 Chi-Square Goodness-of-Fit Test. https://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm",
    "OpenStax, Introductory Statistics 2e, §11.2 Goodness-of-Fit Test. https://openstax.org/books/introductory-statistics-2e/pages/11-2-goodness-of-fit-test",
    "OpenStax, Introductory Statistics 2e, §11.3 Test of Independence. https://openstax.org/books/introductory-statistics-2e/pages/11-3-test-of-independence"
  ],
  "related": [
    "p-value",
    "critical-value",
    "expected-value",
    "t-test"
  ],
  "changelog": []
}
