{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Gaussian GLM" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:23:49.094651Z", "iopub.status.busy": "2023-10-14T01:23:49.094291Z", "iopub.status.idle": "2023-10-14T01:23:50.283296Z", "shell.execute_reply": "2023-10-14T01:23:50.282506Z", "shell.execute_reply.started": "2023-10-14T01:23:49.094628Z" } }, "outputs": [], "source": [ "from spglm.glm import GLM\n", "import libpysal\n", "import numpy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:23:50.947534Z", "iopub.status.busy": "2023-10-14T01:23:50.946850Z", "iopub.status.idle": "2023-10-14T01:23:50.956114Z", "shell.execute_reply": "2023-10-14T01:23:50.954841Z", "shell.execute_reply.started": "2023-10-14T01:23:50.947502Z" } }, "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", "\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:23:52.355767Z", "iopub.status.busy": "2023-10-14T01:23:52.355010Z", "iopub.status.idle": "2023-10-14T01:23:52.367917Z", "shell.execute_reply": "2023-10-14T01:23:52.366305Z", "shell.execute_reply.started": "2023-10-14T01:23:52.355719Z" } }, "outputs": [], "source": [ "# Estimate Gaussian GLM\n", "\n", "# First instantiate a GLM model object\n", "# -- Gaussian is the default family parameter so it doesn't need to be set\n", "model = GLM(y, X) \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:23:53.261041Z", "iopub.status.busy": "2023-10-14T01:23:53.260511Z", "iopub.status.idle": "2023-10-14T01:23:53.267351Z", "shell.execute_reply": "2023-10-14T01:23:53.266163Z", "shell.execute_reply.started": "2023-10-14T01:23:53.261004Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[46.42818268 0.62898397 -0.48488854]\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:23:54.091698Z", "iopub.status.busy": "2023-10-14T01:23:54.091257Z", "iopub.status.idle": "2023-10-14T01:23:54.097334Z", "shell.execute_reply": "2023-10-14T01:23:54.096406Z", "shell.execute_reply.started": "2023-10-14T01:23:54.091672Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[13.19175703 0.53591045 0.18267291]\n" ] } ], "source": [ "# Parameter standard errors\n", "print(results.bse)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:23:55.947271Z", "iopub.status.busy": "2023-10-14T01:23:55.946980Z", "iopub.status.idle": "2023-10-14T01:23:55.952002Z", "shell.execute_reply": "2023-10-14T01:23:55.950887Z", "shell.execute_reply.started": "2023-10-14T01:23:55.947249Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 3.51948437 1.17367365 -2.65440864]\n" ] } ], "source": [ "# Parameter t-values\n", "print(results.tvalues)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2023-10-14T01:23:57.273553Z", "iopub.status.busy": "2023-10-14T01:23:57.273234Z", "iopub.status.idle": "2023-10-14T01:23:57.278805Z", "shell.execute_reply": "2023-10-14T01:23:57.277856Z", "shell.execute_reply.started": "2023-10-14T01:23:57.273534Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "408.73548964604873\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 }