{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "86a20e4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from statsmodels.stats.weightstats import DescrStatsW\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d396ac72",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Weighted mean & SD\n",
    "def w_mean_sd(series, weights):\n",
    "    s = DescrStatsW(series, weights=weights, ddof=0)\n",
    "    return s.mean, s.std\n",
    "\n",
    "# Weighted % for categorical\n",
    "def w_percent(df, var, target=None):\n",
    "    d = df.copy()\n",
    "    if target is not None:\n",
    "        d = d[d['IRAMDEYR']==target]\n",
    "    return (d.groupby(var)['ANALWT2_C'].sum() / d['ANALWT2_C'].sum() * 100).round(1)\n",
    "\n",
    "# Weighted N (population estimate)\n",
    "def weighted_n(df):\n",
    "    return round(df['ANALWT2_C'].sum() / 1e6, 1)  # in million (approx population)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "16fa1242",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Columns you need\n",
    "cols = [\n",
    "    'IRAMDEYR','CATAG7','IRSEX','NEWRACE2',\n",
    "    'AKSSLR6WRST','WHODASDASC','UD5ILLANY','ANALWT2_C'\n",
    "]\n",
    "\n",
    "# Load dataset (tab-delimited)\n",
    "df = pd.read_csv(\"../data/raw/NSDUH_2023_Tab.txt\", sep='\\t', usecols=cols, low_memory=False)\n",
    "\n",
    "# Adults only (CATAG7 ≥ 4)\n",
    "df = df[df['CATAG7'] >= 4]\n",
    "\n",
    "# Drop missing target\n",
    "df = df[df['IRAMDEYR'].notna()]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "95d99ad9",
   "metadata": {},
   "outputs": [],
   "source": [
    "no_mde_raw = len(df[df.IRAMDEYR == 0])\n",
    "mde_raw = len(df[df.IRAMDEYR == 1])\n",
    "total_raw  = no_mde_raw + mde_raw\n",
    "\n",
    "# Percent share of total (based on sample)\n",
    "no_mde_pct = round(no_mde_raw / total_raw * 100, 1)\n",
    "mde_pct    = round(mde_raw / total_raw * 100, 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "096da4be",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ---------- Build rows ----------\n",
    "rows = []\n",
    "\n",
    "# --- 1️⃣ Total participants row ---\n",
    "rows.append([\n",
    "    \"Total participants\",\n",
    "    \"Total respondents in the analytic sample\",\n",
    "    f\"{no_mde_raw:,} ({no_mde_pct:,} %)\",\n",
    "    f\"{mde_raw:,} ({mde_pct:,} %)\"\n",
    "])\n",
    "\n",
    "# --- 2️⃣ Continuous variables (mean ± SD) ---\n",
    "for var in ['AKSSLR6WRST','WHODASDASC']:\n",
    "    mean0, sd0 = w_mean_sd(df[df.IRAMDEYR==0][var], df[df.IRAMDEYR==0]['ANALWT2_C'])\n",
    "    mean1, sd1 = w_mean_sd(df[df.IRAMDEYR==1][var], df[df.IRAMDEYR==1]['ANALWT2_C'])\n",
    "    rows.append([var, \"\", f\"{mean0:.2f} ± {sd0:.2f}\", f\"{mean1:.2f} ± {sd1:.2f}\"])\n",
    "\n",
    "# --- 3️⃣ Categorical variables (weighted %) ---\n",
    "for var in ['CATAG7','IRSEX','NEWRACE2','UD5ILLANY']:\n",
    "    no_mde = w_percent(df, var, 0)\n",
    "    mde    = w_percent(df, var, 1)\n",
    "    for level in sorted(no_mde.index):\n",
    "        rows.append([\n",
    "            f\"{var}_{level}\",\n",
    "            \"\",\n",
    "            f\"{no_mde.get(level, np.nan)} %\",\n",
    "            f\"{mde.get(level, np.nan)} %\"\n",
    "        ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ed2bf402",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ---------- Convert to DataFrame ----------\n",
    "table1 = pd.DataFrame(rows, columns=[\n",
    "    \"Feature (code)\",\n",
    "    \"Code Meaning\",\n",
    "    \"No MDE (Weighted %) or Mean ± SD\",\n",
    "    \"MDE (Weighted %) or Mean ± SD\"\n",
    "])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "27184f74",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ---------- Add readable code meanings ----------\n",
    "labels = {\n",
    "    'CATAG7_4':'Age 18–25 y','CATAG7_5':'Age 26–34 y',\n",
    "    'CATAG7_6':'Age 35–49 y','CATAG7_7':'Age 50+ y',\n",
    "    'IRSEX_1':'Male','IRSEX_2':'Female',\n",
    "    'NEWRACE2_1':'White','NEWRACE2_2':'Black or African American',\n",
    "    'NEWRACE2_3':'American Indian / Alaska Native',\n",
    "    'NEWRACE2_4':'Native Hawaiian / Pacific Islander',\n",
    "    'NEWRACE2_5':'Asian','NEWRACE2_6':'Two or more races','NEWRACE2_7':'Hispanic or Latino',\n",
    "    'UD5ILLANY_0':'No Drug Use Disorder','UD5ILLANY_1':'Has Drug Use Disorder',\n",
    "    'AKSSLR6WRST':'K6 Psychological Distress Score (0–24)',\n",
    "    'WHODASDASC':'WHODAS Disability Score (0–100)'\n",
    "}\n",
    "\n",
    "table1[\"Code Meaning\"] = table1[\"Feature (code)\"].map(labels).fillna(table1[\"Code Meaning\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e8c06b9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ Clean Table 1 saved as 'table1_Baseline_Characteristics.csv'\n",
      "       Feature (code)                              Code Meaning  \\\n",
      "0  Total participants  Total respondents in the analytic sample   \n",
      "1         AKSSLR6WRST    K6 Psychological Distress Score (0–24)   \n",
      "2          WHODASDASC           WHODAS Disability Score (0–100)   \n",
      "3            CATAG7_4                               Age 18–25 y   \n",
      "4            CATAG7_5                               Age 26–34 y   \n",
      "5            CATAG7_6                               Age 35–49 y   \n",
      "6            CATAG7_7                                 Age 50+ y   \n",
      "7             IRSEX_1                                      Male   \n",
      "8             IRSEX_2                                    Female   \n",
      "9          NEWRACE2_1                                     White   \n",
      "\n",
      "  No MDE (Weighted %) or Mean ± SD MDE (Weighted %) or Mean ± SD  \n",
      "0                  39,948 (88.5 %)                5,185 (11.5 %)  \n",
      "1                      1.19 ± 2.96                   9.01 ± 5.20  \n",
      "2                      0.79 ± 1.78                   4.93 ± 2.66  \n",
      "3                            4.3 %                         8.4 %  \n",
      "4                            7.6 %                        18.3 %  \n",
      "5                           14.8 %                        24.4 %  \n",
      "6                           73.2 %                        48.9 %  \n",
      "7                           49.9 %                        35.4 %  \n",
      "8                           50.1 %                        64.6 %  \n",
      "9                           60.7 %                        66.0 %  \n"
     ]
    }
   ],
   "source": [
    "# ---------- Export clean final table ----------\n",
    "table1.to_csv(\"../results/table1_Baseline_Characteristics.csv\", index=False)\n",
    "print(\"✅ Clean Table 1 saved as 'table1_Baseline_Characteristics.csv'\")\n",
    "print(table1.head(10))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
