{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Simple vs Multiple Regression\n",
        "\n",
        "Compare the education coefficient in a short wage regression and a controlled multiple-regression model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pathlib import Path\n",
        "\n",
        "DATASET = \"WAGE1.DTA\"\n",
        "VARIABLES = [\"lwage\", \"educ\", \"exper\", \"tenure\"]\n",
        "DATA_FOLDER = \"https://drive.google.com/drive/folders/1_STdcydIcst-opcbwOKRFzUXsgxQgBoS?usp=sharing\"\n",
        "\n",
        "if not Path(DATASET).exists():\n",
        "    raise FileNotFoundError(\n",
        "        \"Dataset file not installed yet\\n\"\n",
        "        f\"Dataset: WAGE1\\n\"\n",
        "        f\"Variables needed: {', '.join(VARIABLES)}\\n\"\n",
        "        f\"Course data folder: {DATA_FOLDER}\\n\"\n",
        "        \"Admin upload instruction: upload the dataset in Admin -> Datasets and make it available to this notebook.\"\n",
        "    )"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "\n",
        "df = pd.read_stata(DATASET)[VARIABLES].dropna()\n",
        "\n",
        "def ols(y, columns):\n",
        "    X = np.column_stack([np.ones(len(df)), df[columns].to_numpy()])\n",
        "    beta = np.linalg.lstsq(X, y.to_numpy(), rcond=None)[0]\n",
        "    fitted = X @ beta\n",
        "    residuals = y.to_numpy() - fitted\n",
        "    r_squared = 1 - np.sum(residuals ** 2) / np.sum((y.to_numpy() - y.mean()) ** 2)\n",
        "    return dict(zip([\"intercept\", *columns], beta)), r_squared\n",
        "\n",
        "simple_beta, simple_r2 = ols(df[\"lwage\"], [\"educ\"])\n",
        "multiple_beta, multiple_r2 = ols(df[\"lwage\"], [\"educ\", \"exper\", \"tenure\"])\n",
        "\n",
        "print(\"Simple educ coefficient:\", round(simple_beta[\"educ\"], 4))\n",
        "print(\"Multiple educ coefficient:\", round(multiple_beta[\"educ\"], 4))\n",
        "print(\"Simple R-squared:\", round(simple_r2, 4))\n",
        "print(\"Multiple R-squared:\", round(multiple_r2, 4))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Interpretation\n",
        "\n",
        "If the education coefficient changes after adding controls, the simple regression was mixing education with differences in the added variables."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
