{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Robust Standard Errors after WLS\n",
        "\n",
        "Module 8 notebook lab. This notebook uses original Ceteris Lab teaching data and does not report real empirical findings.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Learning goal\n",
        "Compare WLS conventional and robust standard errors.\n",
        "\n",
        "Dataset: MODULE8_FGLS_DEMO. Variables: observation_id, outcome, income, age, treatment, variance_driver.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import statsmodels.api as sm\n",
        "\n",
        "df = pd.read_csv(\"/data/module-8/module8_fgls_demo.csv\")\n",
        "X = sm.add_constant(df[[\"income\", \"age\", \"treatment\"]])\n",
        "ols_hc1 = sm.OLS(df[\"outcome\"], X).fit(cov_type=\"HC1\")\n",
        "weights = 1 / df[\"true_variance\"]\n",
        "wls = sm.WLS(df[\"outcome\"], X, weights=weights).fit()\n",
        "wls_hc1 = sm.WLS(df[\"outcome\"], X, weights=weights).fit(cov_type=\"HC1\")\n",
        "print(\"HC1 OLS SE:\", ols_hc1.bse.round(4).to_dict())\n",
        "print(\"WLS conventional SE:\", wls.bse.round(4).to_dict())\n",
        "print(\"WLS HC1 SE:\", wls_hc1.bse.round(4).to_dict())\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Reflection\n",
        "Write two sentences: one sentence explaining what the diagnostic or robust result says, and one sentence explaining a limitation or next step.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.11"
    },
    "ceteris_lab": {
      "module": "Module 8",
      "lesson": "What If the WLS Variance Model Is Wrong?",
      "dataset": "MODULE8_FGLS_DEMO",
      "copyright": "Original Ceteris Lab notebook. No external text, tables, or figures are reproduced."
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
