{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0d0376d",
   "metadata": {},
   "source": [
    "# Descriptive Statistics in Single-Case Experimental Design\n",
    "\n",
    "The `describe` function calculates comprehensive descriptive statistics for single-case experimental design (SCED) datasets. It provides detailed statistical information for each phase and case in the data."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "630a9a9b",
   "metadata": {},
   "source": [
    "## Computing Descriptive Statistics with `describe`\n",
    "\n",
    "The `describe` function computes various descriptive statistics for each phase in a single-case dataset. It handles multiple cases and phases, providing a comprehensive statistical overview.\n",
    "\n",
    "### Required Arguments:\n",
    "- `data`: A Pandas DataFrame or list of DataFrames containing SCED data.\n",
    "\n",
    "### Optional Arguments:\n",
    "- `dvar`: The column name of the dependent variable (default determined automatically).\n",
    "- `pvar`: The column name of the phase variable (default determined automatically).\n",
    "- `mvar`: The column name representing measurement time (default determined automatically).\n",
    "- `return_dict`: If True, returns a dictionary with additional metadata (default `False`).\n",
    "\n",
    "The function calculates the following statistics for each phase:\n",
    "- `n`: Number of observations\n",
    "- `mis`: Number of missing values\n",
    "- `m`: Mean\n",
    "- `md`: Median\n",
    "- `sd`: Standard deviation\n",
    "- `mad`: Median absolute deviation\n",
    "- `min`: Minimum value\n",
    "- `max`: Maximum value\n",
    "- `trend`: Linear trend coefficient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f7f5a9bc",
   "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": "7f2dbee0",
   "metadata": {},
   "source": [
    "### Example 1: Basic Descriptive Statistics\n",
    "\n",
    "This example demonstrates how to compute basic descriptive statistics for a simple AB design dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a9fdbf40",
   "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>Design</th>\n",
       "      <th>n.A</th>\n",
       "      <th>mis.A</th>\n",
       "      <th>m.A</th>\n",
       "      <th>md.A</th>\n",
       "      <th>sd.A</th>\n",
       "      <th>mad.A</th>\n",
       "      <th>min.A</th>\n",
       "      <th>max.A</th>\n",
       "      <th>trend.A</th>\n",
       "      <th>n.B</th>\n",
       "      <th>mis.B</th>\n",
       "      <th>m.B</th>\n",
       "      <th>md.B</th>\n",
       "      <th>sd.B</th>\n",
       "      <th>mad.B</th>\n",
       "      <th>min.B</th>\n",
       "      <th>max.B</th>\n",
       "      <th>trend.B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Case1</td>\n",
       "      <td>A-B</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.4</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.341641</td>\n",
       "      <td>1.4826</td>\n",
       "      <td>5.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.666667</td>\n",
       "      <td>15.5</td>\n",
       "      <td>2.581989</td>\n",
       "      <td>2.9652</td>\n",
       "      <td>12.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.742857</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Case Design  n.A  mis.A  m.A  md.A      sd.A   mad.A  min.A  max.A  \\\n",
       "0  Case1    A-B  5.0    0.0  6.4   7.0  1.341641  1.4826    5.0    8.0   \n",
       "\n",
       "   trend.A  n.B  mis.B        m.B  md.B      sd.B   mad.B  min.B  max.B  \\\n",
       "0      0.2  6.0    0.0  15.666667  15.5  2.581989  2.9652   12.0   19.0   \n",
       "\n",
       "    trend.B  \n",
       "0  0.742857  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\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 descriptive statistics\n",
    "sc.describe(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "17d99b1e",
   "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>Design</th>\n",
       "      <th>n.A</th>\n",
       "      <th>mis.A</th>\n",
       "      <th>m.A</th>\n",
       "      <th>md.A</th>\n",
       "      <th>sd.A</th>\n",
       "      <th>mad.A</th>\n",
       "      <th>min.A</th>\n",
       "      <th>max.A</th>\n",
       "      <th>trend.A</th>\n",
       "      <th>n.B</th>\n",
       "      <th>mis.B</th>\n",
       "      <th>m.B</th>\n",
       "      <th>md.B</th>\n",
       "      <th>sd.B</th>\n",
       "      <th>mad.B</th>\n",
       "      <th>min.B</th>\n",
       "      <th>max.B</th>\n",
       "      <th>trend.B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Case1</td>\n",
       "      <td>A-B</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.800000</td>\n",
       "      <td>7.0</td>\n",
       "      <td>3.114482</td>\n",
       "      <td>2.9652</td>\n",
       "      <td>5.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>1.200000</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>13.833333</td>\n",
       "      <td>14.5</td>\n",
       "      <td>4.622409</td>\n",
       "      <td>5.9304</td>\n",
       "      <td>7.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>1.971429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Case2</td>\n",
       "      <td>A-B</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.750000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.957427</td>\n",
       "      <td>0.7413</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.142857</td>\n",
       "      <td>8.0</td>\n",
       "      <td>2.544836</td>\n",
       "      <td>1.4826</td>\n",
       "      <td>4.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Case3</td>\n",
       "      <td>A-B</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.333333</td>\n",
       "      <td>7.5</td>\n",
       "      <td>1.366260</td>\n",
       "      <td>0.7413</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>-0.571429</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.600000</td>\n",
       "      <td>14.0</td>\n",
       "      <td>3.974921</td>\n",
       "      <td>2.9652</td>\n",
       "      <td>6.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1.800000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Case Design  n.A  mis.A       m.A  md.A      sd.A   mad.A  min.A  max.A  \\\n",
       "0  Case1    A-B  5.0    0.0  7.800000   7.0  3.114482  2.9652    5.0   12.0   \n",
       "1  Case2    A-B  4.0    0.0  3.750000   3.5  0.957427  0.7413    3.0    5.0   \n",
       "2  Case3    A-B  6.0    0.0  7.333333   7.5  1.366260  0.7413    5.0    9.0   \n",
       "\n",
       "    trend.A  n.B  mis.B        m.B  md.B      sd.B   mad.B  min.B  max.B  \\\n",
       "0  1.200000  6.0    0.0  13.833333  14.5  4.622409  5.9304    7.0   19.0   \n",
       "1  0.500000  7.0    0.0   8.142857   8.0  2.544836  1.4826    4.0   12.0   \n",
       "2 -0.571429  5.0    0.0  12.600000  14.0  3.974921  2.9652    6.0   16.0   \n",
       "\n",
       "    trend.B  \n",
       "0  1.971429  \n",
       "1  1.000000  \n",
       "2  1.800000  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "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))  # Force mt to start from 0\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))  # Force mt to start from 0\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))  # Force mt to start from 0\n",
    ")\n",
    "\n",
    "mbd_test = [charlotte_test, theresa_test, antonia_test]\n",
    "sc.describe(mbd_test)"
   ]
  }
 ],
 "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
}