{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9b5902f6",
   "metadata": {},
   "source": [
    "\n",
    "# Computing Autocorrelations in Single-Case Experimental Design\n",
    "\n",
    "The `autocorr` function calculates autocorrelation values for different phases and overall in a single-case dataset. It helps in analyzing the dependency between observations across time.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad5fae89",
   "metadata": {},
   "source": [
    "\n",
    "## Analyzing Temporal Dependencies with `autocorr`\n",
    "\n",
    "The `autocorr` function computes autocorrelations within each phase and across all phases in a dataset. This is useful for understanding whether measurements at one time point influence future values.\n",
    "\n",
    "### Required Arguments:\n",
    "- `data`: A Pandas DataFrame containing SCED data.\n",
    "\n",
    "### Optional Arguments:\n",
    "- `dvar`: The column name of the dependent variable (default `\"values\"`).\n",
    "- `pvar`: The column name of the phase variable (default `\"phase\"`).\n",
    "- `mvar`: The column name representing measurement time (default `\"mt\"`).\n",
    "- `lag_max`: The maximum lag for which autocorrelations should be computed (default `3`).\n",
    "\n",
    "The function processes each case separately and computes autocorrelations within each phase and across all observations.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d76b6a7f",
   "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": [
    "\n",
    "import scia as sc\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2acb519",
   "metadata": {},
   "source": [
    "\n",
    "### Example 1: Computing Autocorrelations in a Dataset\n",
    "\n",
    "This example calculates autocorrelations up to a lag of 3 for each phase and overall.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6454fa71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Case         Phase     Lag 1     Lag 2     Lag 3\n",
      "0    A      Baseline  0.000000 -0.500000       NaN\n",
      "1    A  Intervention -0.500000       NaN       NaN\n",
      "2    A           all  0.335135 -0.072973 -0.345946\n",
      "3    B      Baseline -0.500000       NaN       NaN\n",
      "4    B  Intervention -0.023810 -0.476190       NaN\n",
      "5    B           all  0.461538 -0.153846 -0.461538\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# Create a sample dataset\n",
    "df = pd.DataFrame({\n",
    "    \"case\": [\"A\", \"A\", \"A\", \"A\", \"A\", \"B\", \"B\", \"B\", \"B\", \"B\"],\n",
    "    \"phase\": [\"Baseline\", \"Baseline\", \"Baseline\", \"Intervention\", \"Intervention\",\n",
    "              \"Baseline\", \"Baseline\", \"Intervention\", \"Intervention\", \"Intervention\"],\n",
    "    \"values\": [5, 6, 7, 8, 10, 3, 4, 6, 8, 9],\n",
    "    \"mt\": [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]\n",
    "})\n",
    "\n",
    "# Compute autocorrelations\n",
    "df_ac = sc.autocorr(df, lag_max=3)\n",
    "print(df_ac)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "312db584",
   "metadata": {},
   "source": [
    "\n",
    "### Example 2: Computing Autocorrelations with a Higher Lag\n",
    "\n",
    "Setting `lag_max=5` allows computing autocorrelations for more time points.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "29898adb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Case         Phase     Lag 1     Lag 2     Lag 3     Lag 4  Lag 5\n",
      "0    A      Baseline  0.000000 -0.500000       NaN       NaN    NaN\n",
      "1    A  Intervention -0.500000       NaN       NaN       NaN    NaN\n",
      "2    A           all  0.335135 -0.072973 -0.345946 -0.416216    NaN\n",
      "3    B      Baseline -0.500000       NaN       NaN       NaN    NaN\n",
      "4    B  Intervention -0.023810 -0.476190       NaN       NaN    NaN\n",
      "5    B           all  0.461538 -0.153846 -0.461538 -0.346154    NaN\n"
     ]
    }
   ],
   "source": [
    "\n",
    "df_ac = sc.autocorr(df, lag_max=5)\n",
    "print(df_ac)\n"
   ]
  }
 ],
 "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
}