{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# WAGE1 Multiple Regression\n",
        "\n",
        "Estimate log wage on education, experience, and tenure. This notebook does not include saved regression output; run the cells after installing the dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pathlib import Path\n",
        "\n",
        "DATASET = \"WAGE1.DTA\"\n",
        "VARIABLES = [\"wage\", \"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",
        "    )\n",
        "\n",
        "print(\"Dataset found:\", DATASET)"
      ]
    },
    {
      "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",
        "print(df.head())\n",
        "print(\"Rows used:\", len(df))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "y = df[\"lwage\"].to_numpy()\n",
        "X = np.column_stack([np.ones(len(df)), df[[\"educ\", \"exper\", \"tenure\"]].to_numpy()])\n",
        "names = [\"intercept\", \"educ\", \"exper\", \"tenure\"]\n",
        "beta = np.linalg.lstsq(X, y, rcond=None)[0]\n",
        "fitted = X @ beta\n",
        "residuals = y - fitted\n",
        "r_squared = 1 - np.sum(residuals ** 2) / np.sum((y - y.mean()) ** 2)\n",
        "\n",
        "for name, value in zip(names, beta):\n",
        "    print(f\"{name}: {value:.4f}\")\n",
        "print(\"R-squared:\", round(r_squared, 4))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Interpretation\n",
        "\n",
        "Read the education coefficient as the predicted change in log wage for one more year of education, holding experience and tenure fixed. This is a conditional association unless the regression assumptions are justified."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
