{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a013cc19",
   "metadata": {},
   "source": [
    "# Reliable Change Index (RCI)\n",
    "\n",
    "The `rci` function calculates the Reliable Change Index (RCI) for single-case data. It provides a statistical assessment of whether the change between two phases (typically A and B) is statistically reliable, considering measurement error and reliability."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05e596f0",
   "metadata": {},
   "source": [
    "## Function Overview\n",
    "\n",
    "The `rci` function computes the RCI for a single-case dataset. It prints descriptive statistics, confidence intervals, and RCI values for the specified phases.\n",
    "\n",
    "### Arguments\n",
    "- `data`: DataFrame containing SCED data (single case only).\n",
    "- `dvar`: Name of the dependent variable column (default: \"values\").\n",
    "- `pvar`: Name of the phase variable column (default: \"phase\").\n",
    "- `rel`: Reliability of the measurement (default: 0.8).\n",
    "- `ci`: Confidence level (default: 0.95).\n",
    "- `graph`: Whether to display a graph of confidence intervals (default: False).\n",
    "- `phases`: Tuple of phases to include (default: (\"A\", \"B\")).\n",
    "\n",
    "The function prints all results directly and does not return a value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "573d3062",
   "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": "f0367da6",
   "metadata": {},
   "source": [
    "## Example 1: Single-Case RCI Calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f343f4a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reliable Change Index\n",
      "\n",
      "Mean Difference = 9.267\n",
      "Standardized Difference = 1.768\n",
      "Standard error of differences = 0.849\n",
      "Reliability of measurements = 0.8\n",
      "\n",
      "Descriptives:\n",
      "         n       mean        SD        SE\n",
      "A-Phase  5   6.400000  1.341641  0.600000\n",
      "B-Phase  6  15.666667  2.581989  1.154701\n",
      "\n",
      "95% Confidence Intervals:\n",
      "             Lower      Upper\n",
      "A-Phase   5.217270   7.582730\n",
      "B-Phase  13.390501  17.942832\n",
      "\n",
      "Reliable Change Indices:\n",
      "                               RCI\n",
      "Jacobson et al.          15.444444\n",
      "Christensen and Mendoza  10.920871\n"
     ]
    }
   ],
   "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 and print RCI\n",
    "sc.rci(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd6fafda",
   "metadata": {},
   "source": [
    "## Example 2: RCI for Each Case in a Multiple-Case Design\n",
    "\n",
    "The `rci` function is designed for single-case data. To use it with multiple cases, simply call it for each case in your list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b32e1649",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Case: Charlotte\n",
      "Reliable Change Index\n",
      "\n",
      "Mean Difference = 6.033\n",
      "Standardized Difference = 1.219\n",
      "Standard error of differences = 1.970\n",
      "Reliability of measurements = 0.8\n",
      "\n",
      "Descriptives:\n",
      "         n       mean        SD        SE\n",
      "A-Phase  5   7.800000  3.114482  1.392839\n",
      "B-Phase  6  13.833333  4.622409  2.067204\n",
      "\n",
      "95% Confidence Intervals:\n",
      "            Lower      Upper\n",
      "A-Phase  5.067867  10.532133\n",
      "B-Phase  9.778394  17.888273\n",
      "\n",
      "Reliable Change Indices:\n",
      "                              RCI\n",
      "Jacobson et al.          4.331681\n",
      "Christensen and Mendoza  3.062961\n",
      "\n",
      "Case: Theresa\n",
      "Reliable Change Index\n",
      "\n",
      "Mean Difference = 4.393\n",
      "Standardized Difference = 1.458\n",
      "Standard error of differences = 0.606\n",
      "Reliability of measurements = 0.8\n",
      "\n",
      "Descriptives:\n",
      "         n      mean        SD        SE\n",
      "A-Phase  4  3.750000  0.957427  0.428174\n",
      "B-Phase  7  8.142857  2.544836  1.138085\n",
      "\n",
      "95% Confidence Intervals:\n",
      "            Lower      Upper\n",
      "A-Phase  2.909551   4.590449\n",
      "B-Phase  5.908949  10.376765\n",
      "\n",
      "Reliable Change Indices:\n",
      "                               RCI\n",
      "Jacobson et al.          10.259504\n",
      "Christensen and Mendoza   7.254565\n",
      "\n",
      "Case: Antonia\n",
      "Reliable Change Index\n",
      "\n",
      "Mean Difference = 5.267\n",
      "Standardized Difference = 1.368\n",
      "Standard error of differences = 0.864\n",
      "Reliability of measurements = 0.8\n",
      "\n",
      "Descriptives:\n",
      "         n       mean        SD        SE\n",
      "A-Phase  6   7.333333  1.366260  0.611010\n",
      "B-Phase  5  12.600000  3.974921  1.777639\n",
      "\n",
      "95% Confidence Intervals:\n",
      "            Lower      Upper\n",
      "A-Phase  6.087089   8.579577\n",
      "B-Phase  8.974247  16.225753\n",
      "\n",
      "Reliable Change Indices:\n",
      "                              RCI\n",
      "Jacobson et al.          8.619607\n",
      "Christensen and Mendoza  6.094982\n"
     ]
    }
   ],
   "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 and print RCI for each case\n",
    "for case in mbd_test:\n",
    "    print(f\"\\nCase: {case['case'].iloc[0]}\")\n",
    "    sc.rci(case)"
   ]
  }
 ],
 "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
}