1{
2 "cells": [
3  {
4   "cell_type": "markdown",
5   "metadata": {},
6   "source": [
7    "# Influence Measures for GLM Logit\n",
8    "\n",
9    "\n",
10    "Based on draft version for GLMInfluence, which will also apply to discrete Logit, Probit and Poisson, and eventually be extended to cover most models outside of time series analysis.\n",
11    "\n",
12    "The example for logistic regression was used by Pregibon (1981) \"Logistic Regression diagnostics\" and is based on data by Finney (1947).\n",
13    "\n",
14    "GLMInfluence includes the basic influence measures but still misses some measures described in Pregibon (1981), for example those related to deviance and effects on confidence intervals."
15   ]
16  },
17  {
18   "cell_type": "code",
19   "execution_count": null,
20   "metadata": {
21    "jupyter": {
22     "outputs_hidden": false
23    }
24   },
25   "outputs": [],
26   "source": [
27    "import os.path\n",
28    "import pandas as pd\n",
29    "import matplotlib.pyplot as plt\n",
30    "\n",
31    "from statsmodels.genmod.generalized_linear_model import GLM\n",
32    "from statsmodels.genmod import families\n",
33    "\n",
34    "plt.rc(\"figure\", figsize=(16, 8))\n",
35    "plt.rc(\"font\", size=14)"
36   ]
37  },
38  {
39   "cell_type": "code",
40   "execution_count": null,
41   "metadata": {
42    "jupyter": {
43     "outputs_hidden": false
44    }
45   },
46   "outputs": [],
47   "source": [
48    "import statsmodels.stats.tests.test_influence\n",
49    "\n",
50    "test_module = statsmodels.stats.tests.test_influence.__file__\n",
51    "cur_dir = cur_dir = os.path.abspath(os.path.dirname(test_module))\n",
52    "\n",
53    "file_name = \"binary_constrict.csv\"\n",
54    "file_path = os.path.join(cur_dir, \"results\", file_name)\n",
55    "df = pd.read_csv(file_path, index_col=0)"
56   ]
57  },
58  {
59   "cell_type": "code",
60   "execution_count": null,
61   "metadata": {
62    "jupyter": {
63     "outputs_hidden": false
64    }
65   },
66   "outputs": [],
67   "source": [
68    "res = GLM(\n",
69    "    df[\"constrict\"],\n",
70    "    df[[\"const\", \"log_rate\", \"log_volumne\"]],\n",
71    "    family=families.Binomial(),\n",
72    ").fit(attach_wls=True, atol=1e-10)\n",
73    "print(res.summary())"
74   ]
75  },
76  {
77   "cell_type": "markdown",
78   "metadata": {},
79   "source": [
80    "## get the influence measures\n",
81    "\n",
82    "GLMResults has a `get_influence` method similar to OLSResults, that returns and instance of the GLMInfluence class. This class has methods and (cached) attributes to inspect influence and outlier measures.\n",
83    "\n",
84    "This measures are based on a one-step approximation to the the results for deleting one observation. One-step approximations are usually accurate for small changes but underestimate the magnitude of large changes. Event though large changes are underestimated, they still show clearly the effect of influential observations\n",
85    "\n",
86    "In this example observation 4 and 18 have a large standardized residual and large Cook's distance, but not a large leverage. Observation 13 has the largest leverage but only small Cook's distance and not a large studentized residual.\n",
87    "\n",
88    "Only the two observations 4 and 18 have a large impact on the parameter estimates."
89   ]
90  },
91  {
92   "cell_type": "code",
93   "execution_count": null,
94   "metadata": {},
95   "outputs": [],
96   "source": [
97    "infl = res.get_influence(observed=False)"
98   ]
99  },
100  {
101   "cell_type": "code",
102   "execution_count": null,
103   "metadata": {
104    "jupyter": {
105     "outputs_hidden": false
106    }
107   },
108   "outputs": [],
109   "source": [
110    "summ_df = infl.summary_frame()\n",
111    "summ_df.sort_values(\"cooks_d\", ascending=False)[:10]"
112   ]
113  },
114  {
115   "cell_type": "code",
116   "execution_count": null,
117   "metadata": {
118    "jupyter": {
119     "outputs_hidden": false
120    }
121   },
122   "outputs": [],
123   "source": [
124    "fig = infl.plot_influence()\n",
125    "fig.tight_layout(pad=1.0)"
126   ]
127  },
128  {
129   "cell_type": "code",
130   "execution_count": null,
131   "metadata": {
132    "jupyter": {
133     "outputs_hidden": false
134    }
135   },
136   "outputs": [],
137   "source": [
138    "fig = infl.plot_index(y_var=\"cooks\", threshold=2 * infl.cooks_distance[0].mean())\n",
139    "fig.tight_layout(pad=1.0)"
140   ]
141  },
142  {
143   "cell_type": "code",
144   "execution_count": null,
145   "metadata": {
146    "jupyter": {
147     "outputs_hidden": false
148    }
149   },
150   "outputs": [],
151   "source": [
152    "fig = infl.plot_index(y_var=\"resid\", threshold=1)\n",
153    "fig.tight_layout(pad=1.0)"
154   ]
155  },
156  {
157   "cell_type": "code",
158   "execution_count": null,
159   "metadata": {
160    "jupyter": {
161     "outputs_hidden": false
162    }
163   },
164   "outputs": [],
165   "source": [
166    "fig = infl.plot_index(y_var=\"dfbeta\", idx=1, threshold=0.5)\n",
167    "fig.tight_layout(pad=1.0)"
168   ]
169  },
170  {
171   "cell_type": "code",
172   "execution_count": null,
173   "metadata": {
174    "jupyter": {
175     "outputs_hidden": false
176    }
177   },
178   "outputs": [],
179   "source": [
180    "fig = infl.plot_index(y_var=\"dfbeta\", idx=2, threshold=0.5)\n",
181    "fig.tight_layout(pad=1.0)"
182   ]
183  },
184  {
185   "cell_type": "code",
186   "execution_count": null,
187   "metadata": {
188    "jupyter": {
189     "outputs_hidden": false
190    }
191   },
192   "outputs": [],
193   "source": [
194    "fig = infl.plot_index(y_var=\"dfbeta\", idx=0, threshold=0.5)\n",
195    "fig.tight_layout(pad=1.0)"
196   ]
197  },
198  {
199   "cell_type": "code",
200   "execution_count": null,
201   "metadata": {
202    "tags": []
203   },
204   "outputs": [],
205   "source": []
206  }
207 ],
208 "metadata": {
209  "kernelspec": {
210   "display_name": "Python 3",
211   "language": "python",
212   "name": "python3"
213  },
214  "language_info": {
215   "codemirror_mode": {
216    "name": "ipython",
217    "version": 3
218   },
219   "file_extension": ".py",
220   "mimetype": "text/x-python",
221   "name": "python",
222   "nbconvert_exporter": "python",
223   "pygments_lexer": "ipython3",
224   "version": "3.8.10"
225  }
226 },
227 "nbformat": 4,
228 "nbformat_minor": 4
229}
230