{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4e9afd04",
   "metadata": {},
   "source": [
    "\n",
    "# Handling Missing Data in Single-Case Experimental Design\n",
    "\n",
    "The `fill_missing` function provides a way to handle missing values in single-case experimental design (SCED) datasets. It ensures that missing values are interpolated without adding new time points.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3243b9b4",
   "metadata": {},
   "source": [
    "\n",
    "## Filling Missing Values with `fill_missing`\n",
    "\n",
    "The `fill_missing` function fills missing values in a single-case dataset using **linear interpolation**. It does **not** introduce new time points, ensuring that the original structure of the dataset is preserved.\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",
    "- `mvar`: The column name representing measurement time (default `\"mt\"`).\n",
    "- `na_rm`: If `True`, explicitly removes missing values before interpolation.\n",
    "\n",
    "The function processes each case separately and interpolates missing values based on existing time points.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4790cd62",
   "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": "0e62614d",
   "metadata": {},
   "source": [
    "\n",
    "### Example 1: Filling Missing Values in a Dataset\n",
    "\n",
    "This example demonstrates how missing values in the dependent variable column (`values`) are automatically filled.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7477c7e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  case  mt  values\n",
      "0    A   1     2.0\n",
      "1    A   2     3.5\n",
      "2    A   3     5.0\n",
      "3    A   4     6.0\n",
      "4    B   1     1.0\n",
      "5    B   2     2.0\n",
      "6    B   3     3.0\n",
      "7    B   4     4.0\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# Create a sample dataset with missing values\n",
    "df = pd.DataFrame({\n",
    "    \"case\": [\"A\", \"A\", \"A\", \"A\", \"B\", \"B\", \"B\", \"B\"],\n",
    "    \"mt\": [1, 2, 3, 4, 1, 2, 3, 4],\n",
    "    \"values\": [2, np.nan, 5, 6, 1, np.nan, np.nan, 4]\n",
    "})\n",
    "\n",
    "# Fill missing values\n",
    "df_filled = sc.fill_missing(df)\n",
    "print(df_filled)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26988f46",
   "metadata": {},
   "source": [
    "\n",
    "### Example 2: Keeping Explicit NA Values\n",
    "\n",
    "By setting `na_rm=False`, explicit `NaN` values are retained before interpolation.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1c79e123",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  case  mt  values\n",
      "0    A   1     2.0\n",
      "1    A   2     3.5\n",
      "2    A   3     5.0\n",
      "3    A   4     6.0\n",
      "4    B   1     1.0\n",
      "5    B   2     2.0\n",
      "6    B   3     3.0\n",
      "7    B   4     4.0\n"
     ]
    }
   ],
   "source": [
    "\n",
    "df_filled = sc.fill_missing(df, na_rm=False)\n",
    "print(df_filled)\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
}
