{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Poisson GLM" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:25:20.621872Z", "iopub.status.busy": "2023-10-14T01:25:20.621661Z", "iopub.status.idle": "2023-10-14T01:25:22.812768Z", "shell.execute_reply": "2023-10-14T01:25:22.811710Z", "shell.execute_reply.started": "2023-10-14T01:25:20.621847Z" } }, "outputs": [], "source": [ "from spglm.glm import GLM\n", "from spglm.family import Poisson\n", "import libpysal\n", "import numpy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:25:22.816050Z", "iopub.status.busy": "2023-10-14T01:25:22.815535Z", "iopub.status.idle": "2023-10-14T01:25:22.827971Z", "shell.execute_reply": "2023-10-14T01:25:22.827075Z", "shell.execute_reply.started": "2023-10-14T01:25:22.816027Z" } }, "outputs": [], "source": [ "# Load sample dataset - columbus dataset\n", "db = libpysal.io.open(libpysal.examples.get_path(\"columbus.dbf\"))\n", "\n", "# Set dependent variable\n", "y = numpy.array(db.by_col(\"HOVAL\"))\n", "y = numpy.reshape(y, (49, 1))\n", "# Round dependent variable and convert to integer\n", "# for the example since Poisson is for discrete data\n", "y = numpy.round(y).astype(int)\n", "\n", "# Set indepdent varibLES\n", "X = []\n", "X.append(db.by_col(\"INC\"))\n", "X.append(db.by_col(\"CRIME\"))\n", "X = numpy.array(X).T" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:25:22.830327Z", "iopub.status.busy": "2023-10-14T01:25:22.829264Z", "iopub.status.idle": "2023-10-14T01:25:22.838574Z", "shell.execute_reply": "2023-10-14T01:25:22.837728Z", "shell.execute_reply.started": "2023-10-14T01:25:22.830299Z" } }, "outputs": [], "source": [ "# Estimate Poisson GLM\n", "\n", "# First instantiate a GLM model object\n", "# -- Set family to Poisson family object for Poisson GLM\n", "model = GLM(y, X, family=Poisson()) \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:25:22.840741Z", "iopub.status.busy": "2023-10-14T01:25:22.840162Z", "iopub.status.idle": "2023-10-14T01:25:22.847860Z", "shell.execute_reply": "2023-10-14T01:25:22.846183Z", "shell.execute_reply.started": "2023-10-14T01:25:22.840709Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 3.92159085 0.01183491 -0.01371397]\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:25:22.849667Z", "iopub.status.busy": "2023-10-14T01:25:22.849168Z", "iopub.status.idle": "2023-10-14T01:25:22.856454Z", "shell.execute_reply": "2023-10-14T01:25:22.855375Z", "shell.execute_reply.started": "2023-10-14T01:25:22.849638Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.13049161 0.00511599 0.00193769]\n" ] } ], "source": [ "# Parameter standard errors\n", "print(results.bse)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:25:22.858131Z", "iopub.status.busy": "2023-10-14T01:25:22.857667Z", "iopub.status.idle": "2023-10-14T01:25:22.862737Z", "shell.execute_reply": "2023-10-14T01:25:22.861829Z", "shell.execute_reply.started": "2023-10-14T01:25:22.858109Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[30.0524361 2.31331634 -7.07748998]\n" ] } ], "source": [ "# Parameter t-values\n", "print(results.tvalues)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:25:22.867196Z", "iopub.status.busy": "2023-10-14T01:25:22.866846Z", "iopub.status.idle": "2023-10-14T01:25:22.873009Z", "shell.execute_reply": "2023-10-14T01:25:22.870993Z", "shell.execute_reply.started": "2023-10-14T01:25:22.867174Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "500.8518417993878\n" ] } ], "source": [ "# Model AIC\n", "print(results.aic)" ] } ], "metadata": { "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 }