{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# Tutorial 5: CPS Microdata\n",
                "\n",
                "The Current Population Survey (CPS) supplements provide specialized microdata on topics like tobacco use, voting, and food security.\n",
                "\n",
                "**Goal:** Analyze tobacco use patterns across states using multi-year CPS data."
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Setup"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import os\n",
                "from cendat import CenDatHelper\n",
                "from dotenv import load_dotenv\n",
                "\n",
                "load_dotenv()\n",
                "cdh = CenDatHelper(key=os.getenv(\"CENSUS_API_KEY\"))\n",
                "\n",
                "# Get multiple years of CPS Tobacco Use Supplement\n",
                "cdh.list_products(years=[2022, 2023], patterns=\"/cps/tobacco\")\n",
                "cdh.set_products()"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Step 2: Explore and Select Variables"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# See available variable groups\n",
                "cdh.list_groups()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Select specific variables\n",
                "# PEA1, PEA3: Tobacco use questions\n",
                "# PWNRWGT: Person weight\n",
                "cdh.set_variables([\"PEA1\", \"PEA3\", \"PWNRWGT\"])\n",
                "cdh.set_geos(\"state\", \"desc\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Step 3: Get Data"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "response = cdh.get_data(within={\"state\": [\"06\", \"48\"]})"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Step 4: Analyze with Pooled Weights\n",
                "\n",
                "When combining multiple survey years, divide the weights:"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "response.tabulate(\n",
                "    \"PEA1\",\n",
                "    \"PEA3\",\n",
                "    strat_by=\"state\",\n",
                "    weight_var=\"PWNRWGT\",\n",
                "    weight_div=3  # Divide weight for pooled years\n",
                ")"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "name": "python",
            "version": "3.11.0"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 4
}