{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "daddf681-9429-4cca-ad68-33146a13050a",
   "metadata": {},
   "source": [
    "# Correlatie | Wetenschap Welbeschouwd\n",
    "#### www.wetenschapwelbeschouwd.nl/correlatie.html"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e9e31dc-6f44-44bf-bff1-ee2c8ddcedb4",
   "metadata": {},
   "source": [
    "## Data inlezen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "021803ab-50dd-404b-aba7-e1ecc35be27b",
   "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>Regio</th>\n",
       "      <th>Inkomen</th>\n",
       "      <th>Huizenprijs</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Oost-Groningen</td>\n",
       "      <td>47.6</td>\n",
       "      <td>240.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Delfzijl en omgeving</td>\n",
       "      <td>47.1</td>\n",
       "      <td>213.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Overig Groningen</td>\n",
       "      <td>48.6</td>\n",
       "      <td>283.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Noord-Friesland</td>\n",
       "      <td>49.3</td>\n",
       "      <td>254.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Zuidwest-Friesland</td>\n",
       "      <td>53.7</td>\n",
       "      <td>301.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Zuidoost-Friesland</td>\n",
       "      <td>52.2</td>\n",
       "      <td>301.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Noord-Drenthe</td>\n",
       "      <td>54.9</td>\n",
       "      <td>321.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Zuidoost-Drenthe</td>\n",
       "      <td>50.8</td>\n",
       "      <td>258.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Zuidwest-Drenthe</td>\n",
       "      <td>54.1</td>\n",
       "      <td>302.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Noord-Overijssel</td>\n",
       "      <td>56.1</td>\n",
       "      <td>333.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Zuidwest-Overijssel</td>\n",
       "      <td>54.1</td>\n",
       "      <td>339.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Twente</td>\n",
       "      <td>54.3</td>\n",
       "      <td>318.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Veluwe</td>\n",
       "      <td>58.5</td>\n",
       "      <td>396.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Achterhoek</td>\n",
       "      <td>55.9</td>\n",
       "      <td>333.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Arnhem/Nijmegen</td>\n",
       "      <td>53.1</td>\n",
       "      <td>349.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>Zuidwest-Gelderland</td>\n",
       "      <td>59.7</td>\n",
       "      <td>376.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Utrecht</td>\n",
       "      <td>61.5</td>\n",
       "      <td>447.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>Kop van Noord-Holland</td>\n",
       "      <td>55.1</td>\n",
       "      <td>330.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Alkmaar en omgeving</td>\n",
       "      <td>57.9</td>\n",
       "      <td>392.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>IJmond</td>\n",
       "      <td>57.3</td>\n",
       "      <td>400.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>Agglomeratie Haarlem</td>\n",
       "      <td>64.3</td>\n",
       "      <td>541.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>Zaanstreek</td>\n",
       "      <td>54.0</td>\n",
       "      <td>372.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>Groot-Amsterdam</td>\n",
       "      <td>58.0</td>\n",
       "      <td>497.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>Het Gooi en Vechtstreek</td>\n",
       "      <td>70.3</td>\n",
       "      <td>525.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>Agglomeratie Leiden en Bollenstreek</td>\n",
       "      <td>60.7</td>\n",
       "      <td>432.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>Agglomeratie 's-Gravenhage</td>\n",
       "      <td>55.4</td>\n",
       "      <td>374.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>Delft en Westland</td>\n",
       "      <td>57.0</td>\n",
       "      <td>377.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>Oost-Zuid-Holland</td>\n",
       "      <td>59.6</td>\n",
       "      <td>371.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>Groot-Rijnmond</td>\n",
       "      <td>54.1</td>\n",
       "      <td>332.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>Zuidoost-Zuid-Holland</td>\n",
       "      <td>56.9</td>\n",
       "      <td>322.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>Zeeuwsch-Vlaanderen</td>\n",
       "      <td>51.8</td>\n",
       "      <td>263.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Overig Zeeland</td>\n",
       "      <td>54.0</td>\n",
       "      <td>289.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>West-Noord-Brabant</td>\n",
       "      <td>56.2</td>\n",
       "      <td>346.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>Midden-Noord-Brabant</td>\n",
       "      <td>55.3</td>\n",
       "      <td>351.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>Noordoost-Noord-Brabant</td>\n",
       "      <td>59.2</td>\n",
       "      <td>390.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>Zuidoost-Noord-Brabant</td>\n",
       "      <td>57.3</td>\n",
       "      <td>384.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>Noord-Limburg</td>\n",
       "      <td>53.5</td>\n",
       "      <td>315.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Midden-Limburg</td>\n",
       "      <td>54.3</td>\n",
       "      <td>315.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>Zuid-Limburg</td>\n",
       "      <td>49.2</td>\n",
       "      <td>266.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Flevoland</td>\n",
       "      <td>56.1</td>\n",
       "      <td>347.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  Regio  Inkomen  Huizenprijs\n",
       "1                        Oost-Groningen     47.6        240.0\n",
       "2                  Delfzijl en omgeving     47.1        213.0\n",
       "3                      Overig Groningen     48.6        283.0\n",
       "4                       Noord-Friesland     49.3        254.0\n",
       "5                    Zuidwest-Friesland     53.7        301.0\n",
       "6                    Zuidoost-Friesland     52.2        301.0\n",
       "7                         Noord-Drenthe     54.9        321.0\n",
       "8                      Zuidoost-Drenthe     50.8        258.0\n",
       "9                      Zuidwest-Drenthe     54.1        302.0\n",
       "10                     Noord-Overijssel     56.1        333.0\n",
       "11                  Zuidwest-Overijssel     54.1        339.0\n",
       "12                               Twente     54.3        318.0\n",
       "13                               Veluwe     58.5        396.0\n",
       "14                           Achterhoek     55.9        333.0\n",
       "15                      Arnhem/Nijmegen     53.1        349.0\n",
       "16                  Zuidwest-Gelderland     59.7        376.0\n",
       "17                              Utrecht     61.5        447.0\n",
       "18                Kop van Noord-Holland     55.1        330.0\n",
       "19                  Alkmaar en omgeving     57.9        392.0\n",
       "20                               IJmond     57.3        400.0\n",
       "21                 Agglomeratie Haarlem     64.3        541.0\n",
       "22                           Zaanstreek     54.0        372.0\n",
       "23                      Groot-Amsterdam     58.0        497.0\n",
       "24              Het Gooi en Vechtstreek     70.3        525.0\n",
       "25  Agglomeratie Leiden en Bollenstreek     60.7        432.0\n",
       "26           Agglomeratie 's-Gravenhage     55.4        374.0\n",
       "27                    Delft en Westland     57.0        377.0\n",
       "28                    Oost-Zuid-Holland     59.6        371.0\n",
       "29                       Groot-Rijnmond     54.1        332.0\n",
       "30                Zuidoost-Zuid-Holland     56.9        322.0\n",
       "31                  Zeeuwsch-Vlaanderen     51.8        263.0\n",
       "32                       Overig Zeeland     54.0        289.0\n",
       "33                   West-Noord-Brabant     56.2        346.0\n",
       "34                 Midden-Noord-Brabant     55.3        351.0\n",
       "35              Noordoost-Noord-Brabant     59.2        390.0\n",
       "36               Zuidoost-Noord-Brabant     57.3        384.0\n",
       "37                        Noord-Limburg     53.5        315.0\n",
       "38                       Midden-Limburg     54.3        315.0\n",
       "39                         Zuid-Limburg     49.2        266.0\n",
       "40                            Flevoland     56.1        347.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Waardes zijn in duizendtallen en uitgedrukt in euro's.\n",
      "Bron: StatLine, CBS\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import geopandas as gpd\n",
    "import fiona\n",
    "from adjustText import adjust_text\n",
    "\n",
    "df = pd.read_csv('Regionale_kerncijfers_Nederland_27112025_175259.csv', sep=';', skiprows=5, \n",
    "                 names=['Regio', 'Jaar', 'Huizenprijs', 'Inkomen']).dropna()\n",
    "df = df.drop(columns=['Jaar'])\n",
    "df = df[['Regio', 'Inkomen', 'Huizenprijs']]\n",
    "df.index = range(1, len(df) + 1)\n",
    "df['Regio'] = df['Regio'].str.replace(' (CR)', '')\n",
    "df['Inkomen'] = df['Inkomen'].str.replace(',', '.').astype(float)\n",
    "\n",
    "display(df)\n",
    "print(\"Waardes zijn in duizendtallen en uitgedrukt in euro's.\\nBron: StatLine, CBS\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f26f2ad-ef9f-4855-8fa9-79758082d4f2",
   "metadata": {},
   "source": [
    "## Data visualiseren"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3d6e3678-50d2-4a6b-b277-a7a66578dfcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 237720 characters]",
      "text/plain": [
       "<Figure size 1600x800 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# COROP-gebieden inlezen\n",
    "corop = gpd.read_file('cbsgebiedsindelingen2026.gpkg', layer='coropgebied_gegeneraliseerd')\n",
    "\n",
    "# Merge met de CBS dataset\n",
    "merged = corop.merge(df, left_on='statnaam', right_on='Regio')\n",
    "\n",
    "# Twee kaarten naast elkaar plotten\n",
    "fig, axes = plt.subplots(1, 2, figsize=(16, 8))\n",
    "\n",
    "# Kaart 1: Inkomen\n",
    "merged.plot(column='Inkomen', cmap='winter_r', legend=True, ax=axes[0])\n",
    "axes[0].set_title('Gemiddelde inkomens 2023\\n(x 1.000 €)', size=15)\n",
    "axes[0].axis('off')\n",
    "\n",
    "# Kaart 2: Huizenprijs\n",
    "merged.plot(column='Huizenprijs', cmap='autumn_r', legend=True, ax=axes[1])\n",
    "axes[1].set_title('Gemiddelde huizenprijzen 2023\\n(x 1.000 €)', size=15)\n",
    "axes[1].axis('off')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53058c6d-9416-4d83-bde5-c5551d69dae9",
   "metadata": {},
   "source": [
    "## Scatterplot maken"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fdf062a5-448c-4ac0-ba1f-42f7e69bfaa9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 79956 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(df['Inkomen'], df['Huizenprijs'], color='purple')\n",
    "\n",
    "plt.xlabel('Inkomen (x 1.000 €)')\n",
    "plt.ylabel('Huizenprijs (x 1.000 €)')\n",
    "\n",
    "texts = [plt.text(df['Inkomen'].iloc[i], df['Huizenprijs'].iloc[i], df['Regio'].iloc[i], alpha=0.5, size=6) for i in range(len(df))]\n",
    "adjust_text(texts, arrowprops=dict(arrowstyle='->', color='purple', alpha=0.5))\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5ece7e5-333f-4ef4-b546-ab35c9bb2488",
   "metadata": {},
   "source": [
    "## Correlatiecoëfficiënt (<i>r</i>) en correlatiedeterminant (<i>R<sup>2</sup></i>) berekenen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0bc6a4c8-f2b7-43d2-a768-a90cda8175bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "r = 0.91\n",
      "R^2 = 82 %\n"
     ]
    }
   ],
   "source": [
    "cc = df['Inkomen'].corr(df['Huizenprijs'])\n",
    "cd = cc ** 2\n",
    "\n",
    "print('r =', round(cc, 2))\n",
    "print('R^2 =', round(cd * 100), '%')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
