{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f1bbafca",
   "metadata": {},
   "source": [
    "# Standardized Mean Difference (SMD)\n",
    "\n",
    "The `smd` function computes standardized mean differences (effect sizes) for single-case experimental design (SCED) data. It supports both single-case and multiple-case datasets, and provides several effect size metrics including Cohen's d, Hedges' g, and Glass' delta."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb329253",
   "metadata": {},
   "source": [
    "## Function Overview\n",
    "\n",
    "The `smd` function calculates effect sizes between two phases (typically A and B) for SCED data. It can handle both a single DataFrame or a list of DataFrames (for multiple cases).\n",
    "\n",
    "### Arguments\n",
    "- `data`: DataFrame or list of DataFrames containing SCED data.\n",
    "- `dvar`: Name of the dependent variable column (default: \"values\").\n",
    "- `pvar`: Name of the phase variable column (default: \"phase\").\n",
    "- `mvar`: Name of the measurement time variable column (default: \"mt\").\n",
    "- `phases`: Tuple or list of two phases to compare (default: (1, 2)).\n",
    "\n",
    "### Returns\n",
    "A DataFrame with effect size metrics for each case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a084fb37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "📖 scia 1.101.0.dev6 - For Documentation, visit: https://ahsankhodami.github.io/scia/intro.html\n"
     ]
    }
   ],
   "source": [
    "import scia as sc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20c58fc7",
   "metadata": {},
   "source": [
    "## Example 1: Single-Case SMD Calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "432146f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Case</th>\n",
       "      <th>mA</th>\n",
       "      <th>mB</th>\n",
       "      <th>sdA</th>\n",
       "      <th>sdB</th>\n",
       "      <th>sd cohen</th>\n",
       "      <th>sd hedges</th>\n",
       "      <th>Glass' delta</th>\n",
       "      <th>Hedges' g</th>\n",
       "      <th>Hedges' g correction</th>\n",
       "      <th>Hedges' g durlak correction</th>\n",
       "      <th>Cohen's d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Default_Case</td>\n",
       "      <td>6.4</td>\n",
       "      <td>15.666667</td>\n",
       "      <td>1.341641</td>\n",
       "      <td>2.581989</td>\n",
       "      <td>2.057507</td>\n",
       "      <td>2.122193</td>\n",
       "      <td>6.906966</td>\n",
       "      <td>4.366552</td>\n",
       "      <td>3.992276</td>\n",
       "      <td>3.61115</td>\n",
       "      <td>4.503833</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Case   mA         mB       sdA       sdB  sd cohen sd hedges  \\\n",
       "0  Default_Case  6.4  15.666667  1.341641  2.581989  2.057507  2.122193   \n",
       "\n",
       "  Glass' delta Hedges' g Hedges' g correction Hedges' g durlak correction  \\\n",
       "0     6.906966  4.366552             3.992276                     3.61115   \n",
       "\n",
       "  Cohen's d  \n",
       "0  4.503833  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a single-case AB design\n",
    "df = sc.create_scd(\n",
    "    values=[5, 7, 8, 5, 7, 12, 16, 18, 15, 14, 19],\n",
    "    phase_starts={\"A\": 1, \"B\": 6}\n",
    ")\n",
    "\n",
    "# Compute standardized mean differences\n",
    "sc.smd(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b5b5ace",
   "metadata": {},
   "source": [
    "## Example 2: Multiple-Case SMD Calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c6af3621",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Case</th>\n",
       "      <th>mA</th>\n",
       "      <th>mB</th>\n",
       "      <th>sdA</th>\n",
       "      <th>sdB</th>\n",
       "      <th>sd cohen</th>\n",
       "      <th>sd hedges</th>\n",
       "      <th>Glass' delta</th>\n",
       "      <th>Hedges' g</th>\n",
       "      <th>Hedges' g correction</th>\n",
       "      <th>Hedges' g durlak correction</th>\n",
       "      <th>Cohen's d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Charlotte</td>\n",
       "      <td>7.8</td>\n",
       "      <td>13.833333</td>\n",
       "      <td>3.114482</td>\n",
       "      <td>4.622409</td>\n",
       "      <td>3.941235</td>\n",
       "      <td>4.022621</td>\n",
       "      <td>1.937187</td>\n",
       "      <td>1.499851</td>\n",
       "      <td>1.371293</td>\n",
       "      <td>1.240381</td>\n",
       "      <td>1.530823</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Theresa</td>\n",
       "      <td>3.75</td>\n",
       "      <td>8.142857</td>\n",
       "      <td>0.957427</td>\n",
       "      <td>2.544836</td>\n",
       "      <td>1.92261</td>\n",
       "      <td>2.15012</td>\n",
       "      <td>4.58819</td>\n",
       "      <td>2.043075</td>\n",
       "      <td>1.867955</td>\n",
       "      <td>1.689629</td>\n",
       "      <td>2.284841</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Antonia</td>\n",
       "      <td>7.333333</td>\n",
       "      <td>12.6</td>\n",
       "      <td>1.36626</td>\n",
       "      <td>3.974921</td>\n",
       "      <td>2.972092</td>\n",
       "      <td>2.838883</td>\n",
       "      <td>3.854805</td>\n",
       "      <td>1.855189</td>\n",
       "      <td>1.696173</td>\n",
       "      <td>1.534246</td>\n",
       "      <td>1.77204</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Case        mA         mB       sdA       sdB  sd cohen sd hedges  \\\n",
       "0  Charlotte       7.8  13.833333  3.114482  4.622409  3.941235  4.022621   \n",
       "1    Theresa      3.75   8.142857  0.957427  2.544836   1.92261   2.15012   \n",
       "2    Antonia  7.333333       12.6   1.36626  3.974921  2.972092  2.838883   \n",
       "\n",
       "  Glass' delta Hedges' g Hedges' g correction Hedges' g durlak correction  \\\n",
       "0     1.937187  1.499851             1.371293                    1.240381   \n",
       "1      4.58819  2.043075             1.867955                    1.689629   \n",
       "2     3.854805  1.855189             1.696173                    1.534246   \n",
       "\n",
       "  Cohen's d  \n",
       "0  1.530823  \n",
       "1  2.284841  \n",
       "2   1.77204  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create multiple cases\n",
    "charlotte_test = sc.create_scd(\n",
    "    values={\"A\": [5, 7, 10, 5, 12], \"B\": [7, 10, 18, 15, 14, 19]},\n",
    "    name=\"Charlotte\",\n",
    "    mt=list(range(11))\n",
    ")\n",
    "\n",
    "theresa_test = sc.create_scd(\n",
    "    values={\"A\": [3, 4, 3, 5], \"B\": [7, 4, 7, 9, 8, 10, 12]},\n",
    "    name=\"Theresa\",\n",
    "    mt=list(range(11))\n",
    ")\n",
    "\n",
    "antonia_test = sc.create_scd(\n",
    "    values={\"A\": [9, 8, 8, 7, 5, 7], \"B\": [6, 14, 15, 12, 16]},\n",
    "    name=\"Antonia\",\n",
    "    mt=list(range(11))\n",
    ")\n",
    "\n",
    "mbd_test = [charlotte_test, theresa_test, antonia_test]\n",
    "\n",
    "# Compute SMD for all cases\n",
    "sc.smd(mbd_test)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3f79a6b",
   "metadata": {},
   "source": [
    "## Output\n",
    "\n",
    "The output DataFrame contains the following columns for each case:\n",
    "- `Case`: Case name\n",
    "- `mA`, `mB`: Means for phases A and B\n",
    "- `sdA`, `sdB`: Standard deviations for phases A and B\n",
    "- `Glass' delta`: Glass' delta effect size\n",
    "- `sd hedges`: Pooled standard deviation for Hedges' g\n",
    "- `Hedges' g`: Hedges' g effect size\n",
    "- `Hedges' g correction`: Hedges' g with small sample correction\n",
    "- `Hedges' g durlak correction`: Hedges' g with Durlak correction\n",
    "- `sd cohen`: Pooled standard deviation for Cohen's d\n",
    "- `Cohen's d`: Cohen's d effect size"
   ]
  }
 ],
 "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.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
