{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# LM Test with CRIME1\n\nLearning goal: Compute the n-R-squared LM statistic and compare with an F test.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Dataset check\nThis cell confirms the dataset file is available. Simulation notebooks mount a harmless CSV so public file delivery is still validated.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\n",
        "DATASET = \"CRIME1.csv\"\n",
        "VARIABLES = [\"narr86\",\"pcnv\",\"avgsen\",\"tottime\",\"ptime86\",\"qemp86\"]\n",
        "if not os.path.exists(DATASET):\n",
        "    raise FileNotFoundError(\n",
        "        \"Dataset file not installed yet\\n\"\n",
        "        \"Dataset: CRIME1\\n\"\n",
        "        \"Variables needed: narr86, pcnv, avgsen, tottime, ptime86, qemp86\\n\"\n",
        "        \"Course data folder: https://drive.google.com/drive/folders/1_STdcydIcst-opcbwOKRFzUXsgxQgBoS?usp=sharing\\n\"\n",
        "        \"Admin upload instruction: upload the dataset in Admin -> Datasets, publish it, and make the file available to the notebook runner.\"\n",
        "    )\n",
        "print(\"Ready:\", DATASET)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Run the lab\nRun the code, inspect the table or graph, and connect the result to the formula in the lesson.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import statsmodels.api as sm\n",
        "from scipy.stats import chi2\n",
        "\n",
        "crime = pd.read_csv(\"CRIME1.csv\")\n",
        "cols = [\"narr86\", \"pcnv\", \"avgsen\", \"tottime\", \"ptime86\", \"qemp86\"]\n",
        "crime = crime.dropna(subset=cols)\n",
        "X_r = sm.add_constant(crime[[\"pcnv\", \"ptime86\", \"qemp86\"]])\n",
        "restricted = sm.OLS(crime[\"narr86\"], X_r).fit()\n",
        "crime[\"u_restricted\"] = restricted.resid\n",
        "X_aux = sm.add_constant(crime[[\"pcnv\", \"ptime86\", \"qemp86\", \"avgsen\", \"tottime\"]])\n",
        "auxiliary = sm.OLS(crime[\"u_restricted\"], X_aux).fit()\n",
        "LM = len(crime) * auxiliary.rsquared\n",
        "q = 2\n",
        "p_value = 1 - chi2.cdf(LM, q)\n",
        "print(\"LM statistic:\", LM)\n",
        "print(\"p-value:\", p_value)\n",
        "print(\"Auxiliary R-squared:\", auxiliary.rsquared)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Formula explanation\nExplain the probability limit, asymptotic approximation, standard-error pattern, or LM statistic in words. Do not treat output as automatic causal evidence.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Short exercise\nChange one sample size, regressor, or restriction. Write two sentences: what changed mechanically, and what assumption still matters?\n\n## Check your understanding\nDoes increasing n fix nonnormality, endogeneity, heteroskedasticity, or omitted variables? Explain.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}