From bd3f959c2eee255aaa9a90eda6297cfeef234951 Mon Sep 17 00:00:00 2001 From: Mirko Birbaumer <mirko.birbaumer@hslu.ch> Date: Mon, 31 Mar 2025 16:56:57 +0000 Subject: [PATCH] Added Bayesian R^2 --- notebooks/Linear Regression/LR_7_2.ipynb | 102 ++++++++++++++++++++--- 1 file changed, 91 insertions(+), 11 deletions(-) diff --git a/notebooks/Linear Regression/LR_7_2.ipynb b/notebooks/Linear Regression/LR_7_2.ipynb index 3801238..87d766a 100644 --- a/notebooks/Linear Regression/LR_7_2.ipynb +++ b/notebooks/Linear Regression/LR_7_2.ipynb @@ -10,7 +10,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 16, "id": "6645d58f-73f2-4bb4-ae9f-17a0b334a652", "metadata": {}, "outputs": [], @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 17, "id": "a76946c6-bb24-4032-9b40-35a2303dec27", "metadata": { "scrolled": true @@ -65,9 +65,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 18, "id": "97ef4d21-6ffb-4b9e-a973-94c6815600b3", - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stderr", @@ -120,7 +122,37 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 19, + "id": "da7dd39e-0b36-4742-bc45-cc495b55cac8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "r2 0.825992\n", + "r2_std 0.000000\n", + "dtype: float64" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model_trz.predict(idata_trz, kind=\"response\")\n", + "y_pred = az.extract(idata_trz.posterior_predictive).Verkauf.values.T\n", + "y_obs = werbung.Verkauf.values\n", + "# number of predictor variables\n", + "p=3\n", + "# number of observations\n", + "n = len(y_obs)\n", + "pm.r2_score( y_pred, y_obs)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "id": "27f1854b-e66e-432e-aa61-66b928e73f16", "metadata": {}, "outputs": [], @@ -137,9 +169,11 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 10, "id": "45172e19-c1df-48ed-a524-cb8408a603e4", - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", @@ -162,6 +196,29 @@ "print(\"R²:\", r2)" ] }, + { + "cell_type": "code", + "execution_count": 15, + "id": "12b11a54-925b-4c1b-b3a2-4f6b479a11b0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "r2 0.897365\n", + "r2_std 0.000000\n", + "dtype: float64" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pm.r2_score(y_obs, y_pred )" + ] + }, { "cell_type": "markdown", "id": "67580aab-6f1d-47ac-bace-3212033a0e40", @@ -172,7 +229,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "id": "fd86c259-21a6-4ebc-a2c9-6d7165f6a13a", "metadata": {}, "outputs": [ @@ -227,7 +284,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "id": "125cbdce-ae89-456c-945b-b603d8cf3d7d", "metadata": {}, "outputs": [], @@ -243,7 +300,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, "id": "4990d7e2-8761-469b-aca8-7dc283fd8c99", "metadata": {}, "outputs": [ @@ -276,10 +333,33 @@ "Die beiden Modelle haben beinahe den identischen $R^2$-Wert, d.h., wir können auf die Prädiktorvariable `Zeitung` ohne Einbusse an der Güte des Modells verzichten." ] }, + { + "cell_type": "code", + "execution_count": 14, + "id": "28c5351e-08dd-420d-8d2b-56ec4c4173e9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "r2 0.897365\n", + "r2_std 0.000000\n", + "dtype: float64" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pm.r2_score(y_obs, y_pred )" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "d07bbeb8-05d2-4e61-a806-b9dbce147722", + "id": "8c724547-2917-4675-b39e-41f42a306a9f", "metadata": {}, "outputs": [], "source": [] -- GitLab