{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Binomial GLM" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:22:52.996793Z", "iopub.status.busy": "2023-10-14T01:22:52.996204Z", "iopub.status.idle": "2023-10-14T01:22:54.292332Z", "shell.execute_reply": "2023-10-14T01:22:54.291240Z", "shell.execute_reply.started": "2023-10-14T01:22:52.996686Z" } }, "outputs": [], "source": [ "from spglm.glm import GLM\n", "from spglm.family import Binomial\n", "import numpy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:22:54.296350Z", "iopub.status.busy": "2023-10-14T01:22:54.295680Z", "iopub.status.idle": "2023-10-14T01:22:54.316200Z", "shell.execute_reply": "2023-10-14T01:22:54.314911Z", "shell.execute_reply.started": "2023-10-14T01:22:54.296296Z" } }, "outputs": [], "source": [ "# Load sample dataset - Subset of london house price dataset\n", "#db = ps.open(ps.get_path(\"columbus.dbf\"), \"r\")\n", "\n", "#Set dependent variable\n", "y = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,\n", " 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n", " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\n", "y = y.reshape((316,1))\n", "\n", "#Set independent variable (FLOORSZ)\n", "X = numpy.array([ 77, 75, 64, 95, 107, 100, 81, 151, 98, 260, 171, 161, 91,\n", " 80, 50, 85, 52, 69, 60, 84, 155, 97, 69, 126, 90, 43,\n", " 51, 41, 140, 80, 52, 86, 66, 60, 40, 155, 138, 97, 115,\n", " 148, 206, 60, 53, 96, 88, 160, 31, 43, 154, 60, 131, 60,\n", " 46, 61, 125, 150, 76, 92, 96, 100, 105, 72, 48, 41, 72,\n", " 65, 60, 65, 98, 33, 144, 111, 91, 108, 38, 48, 95, 63,\n", " 98, 129, 108, 51, 131, 66, 48, 127, 76, 68, 52, 64, 57,\n", " 121, 67, 76, 112, 96, 90, 53, 93, 64, 97, 58, 44, 157,\n", " 53, 70, 71, 167, 47, 70, 96, 77, 75, 71, 67, 47, 71,\n", " 90, 69, 64, 65, 95, 60, 60, 65, 54, 121, 105, 50, 85,\n", " 69, 69, 62, 65, 93, 93, 70, 62, 155, 68, 117, 80, 80,\n", " 75, 98, 114, 86, 70, 50, 51, 163, 124, 59, 95, 51, 63,\n", " 85, 53, 46, 102, 114, 83, 47, 40, 63, 123, 100, 63, 110,\n", " 79, 98, 99, 120, 52, 48, 37, 81, 30, 88, 50, 35, 116,\n", " 67, 45, 80, 86, 109, 59, 75, 60, 71, 141, 121, 50, 168,\n", " 90, 51, 133, 75, 133, 127, 37, 68, 105, 61, 123, 151, 110,\n", " 77, 220, 94, 77, 70, 100, 98, 126, 55, 105, 60, 176, 104,\n", " 68, 62, 70, 48, 102, 80, 97, 66, 80, 102, 160, 55, 60,\n", " 71, 125, 85, 85, 190, 137, 48, 41, 42, 51, 57, 60, 114,\n", " 88, 84, 108, 66, 85, 42, 98, 90, 127, 100, 55, 76, 82,\n", " 63, 80, 71, 76, 121, 109, 92, 160, 109, 185, 100, 90, 90,\n", " 86, 88, 95, 116, 135, 61, 74, 60, 235, 76, 66, 100, 49,\n", " 50, 37, 100, 88, 90, 52, 95, 81, 79, 96, 75, 91, 86,\n", " 83, 180, 108, 80, 96, 49, 117, 117, 86, 46, 66, 95, 57,\n", " 120, 137, 68, 240])\n", "X = X.reshape((316,1))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:22:54.317859Z", "iopub.status.busy": "2023-10-14T01:22:54.317442Z", "iopub.status.idle": "2023-10-14T01:22:54.328544Z", "shell.execute_reply": "2023-10-14T01:22:54.327834Z", "shell.execute_reply.started": "2023-10-14T01:22:54.317826Z" } }, "outputs": [], "source": [ "# Estimate Binomial GLM\n", "\n", "# First instantiate a GLM model object\n", "# -- Set family to Binomial family object for Binomial GLM\n", "model = GLM(y, X, family=Binomial()) \n", "\n", "# Then use the fit method to estimate coefficients and compute diagnostics\n", "results = model.fit()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:22:54.330385Z", "iopub.status.busy": "2023-10-14T01:22:54.329865Z", "iopub.status.idle": "2023-10-14T01:22:54.335674Z", "shell.execute_reply": "2023-10-14T01:22:54.334451Z", "shell.execute_reply.started": "2023-10-14T01:22:54.330362Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-5.33638276 0.0287754 ]\n" ] } ], "source": [ "# Estimated prameters, intercept is always the first column on the left\n", "print(results.params)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:22:54.337573Z", "iopub.status.busy": "2023-10-14T01:22:54.337242Z", "iopub.status.idle": "2023-10-14T01:22:54.342482Z", "shell.execute_reply": "2023-10-14T01:22:54.341622Z", "shell.execute_reply.started": "2023-10-14T01:22:54.337545Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.64499904 0.00518312]\n" ] } ], "source": [ "# Parameter standard errors\n", "print(results.bse)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:22:54.344204Z", "iopub.status.busy": "2023-10-14T01:22:54.343721Z", "iopub.status.idle": "2023-10-14T01:22:54.349208Z", "shell.execute_reply": "2023-10-14T01:22:54.348295Z", "shell.execute_reply.started": "2023-10-14T01:22:54.344177Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-8.27347396 5.55175826]\n" ] } ], "source": [ "# Parameter t-values\n", "print(results.tvalues)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:22:54.354033Z", "iopub.status.busy": "2023-10-14T01:22:54.353469Z", "iopub.status.idle": "2023-10-14T01:22:54.358243Z", "shell.execute_reply": "2023-10-14T01:22:54.356998Z", "shell.execute_reply.started": "2023-10-14T01:22:54.354007Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "155.1934753034247\n" ] } ], "source": [ "# Model AIC\n", "print(results.aic)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:py311_spglm]", "language": "python", "name": "conda-env-py311_spglm-py" }, "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.11.6" } }, "nbformat": 4, "nbformat_minor": 4 }