{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "42259d82",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports\n",
    "\n",
    "from IPython.display import display, Markdown\n",
    "from scipy.stats import chi2_contingency\n",
    "from scipy.stats import t as t_test\n",
    "import numpy as np\n",
    "t_value = t_test.isf\n",
    "\n",
    "def _display_table(data_array, row_names=None, col_names=None):\n",
    "    _row_num = len(data_array)\n",
    "    _col_num = len(data_array[0])\n",
    "    _rows = []\n",
    "    for i in range(_row_num):\n",
    "        row = f'| {row_names[i]} |'\n",
    "        for j in range(_col_num):\n",
    "            row += f' {data_array[i][j]:.1f} |'\n",
    "        row += f' {sum(data_array[i]):.0f} |'\n",
    "        _rows.append(row)\n",
    "    _total_row = '| **Total** |'\n",
    "    _col_tots = 0\n",
    "    for i in range(_col_num):\n",
    "        col_tot = 0\n",
    "        for j in range(_row_num):\n",
    "            col_tot += data_array[j][i]\n",
    "        _col_tots += col_tot\n",
    "        _total_row += f' **{col_tot:.0f}** |'\n",
    "    _total_row += f' **{_col_tots:.0f}** |'\n",
    "    display(Markdown(\n",
    "    rf\"\"\"\n",
    "| | {' | '.join(col_names + ['Total'])} |\n",
    "{'|-'*(len(row_names) + 2)} |\n",
    "{'\\n'.join(_rows)}\n",
    "{_total_row}\n",
    "\"\"\"\n",
    "))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82a22acc",
   "metadata": {},
   "source": [
    "# Hyppigheds- og associationsmål"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9920c0f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Odds risiko\n",
    "# Relativ risiko\n",
    "# Risiko differens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d8e8f8ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "\n",
       "Prævalens (Høj skærmtid) $= \\frac{210}{460} = 0.4565 \\approx 45.65%$ \\%\n",
       "\n",
       "Prævalens (Lav skærmtid) $= \\frac{90}{360} = 0.2500 \\approx 25.00%$ \\%\n",
       "\n",
       "PPR $= \\frac{0.4565}{0.2500} = 1.8261$\n"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "kategorier = ['Høj skærmtid', 'Lav skærmtid']\n",
    "data = [[210, 460],\n",
    "        [90, 360]]\n",
    "\n",
    "\n",
    "\n",
    "####################################\n",
    "prævalens1 = data[0][0] / data[0][1]\n",
    "prævalens2 = data[1][0] / data[1][1]\n",
    "\n",
    "display(Markdown(\n",
    "    rf\"\"\"\n",
    "Prævalens ({kategorier[0]}) $= \\frac{{{data[0][0]}}}{{{data[0][1]}}} = {prævalens1:.4f} \\approx {prævalens1:.2%}$ \\%\n",
    "\n",
    "Prævalens ({kategorier[1]}) $= \\frac{{{data[1][0]}}}{{{data[1][1]}}} = {prævalens2:.4f} \\approx {prævalens2:.2%}$ \\%\n",
    "\n",
    "PPR $= \\frac{{{prævalens1:.4f}}}{{{prævalens2:.4f}}} = {prævalens1 / prævalens2:.4f}$\n",
    "\"\"\"\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ef266dc",
   "metadata": {},
   "source": [
    "# $\\Chi^2$-test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7c426592",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "### Observeret:"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "\n",
       "| | Hovedpine | Ingen hovedpine | Total |\n",
       "|-|-|-|- |\n",
       "| Høj skærmtid | 210.0 | 250.0 | 460 |\n",
       "| Lav skærmtid | 90.0 | 270.0 | 360 |\n",
       "| **Total** | **300** | **520** | **820** |\n"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "### Forventet:"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "\n",
       "| | Hovedpine | Ingen hovedpine | Total |\n",
       "|-|-|-|- |\n",
       "| Høj skærmtid | 168.3 | 291.7 | 460 |\n",
       "| Lav skærmtid | 131.7 | 228.3 | 360 |\n",
       "| **Total** | **300** | **520** | **820** |\n"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "\n",
       "$\\Chi^2$-værdi: 36.24\n",
       "\n",
       "$p$-værdi: 1.7e-09\n",
       "\n",
       "Frihedsgrader: 1\n"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "rækker = ['Høj skærmtid', 'Lav skærmtid']\n",
    "kolonner = ['Hovedpine', 'Ingen hovedpine']\n",
    "data = [[210, 250], \n",
    "        [90, 270]]\n",
    "\n",
    "\n",
    "\n",
    "############################################\n",
    "stat, p, dof, expected = chi2_contingency(data)\n",
    "\n",
    "display(Markdown('### Observeret:'))\n",
    "_display_table(data, rækker, kolonner)\n",
    "display(Markdown('### Forventet:'))\n",
    "_display_table(expected, rækker, kolonner)\n",
    "\n",
    "display(Markdown(\n",
    "    rf\"\"\"\n",
    "$\\Chi^2$-værdi: {stat:.2f}\n",
    "\n",
    "$p$-værdi: {p:.2}\n",
    "\n",
    "Frihedsgrader: {dof}\n",
    "\"\"\"\n",
    "))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "896b2de0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
