{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Regression Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from statsmodels.compat import lzip\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import statsmodels.api as sm\n",
    "from statsmodels.formula.api import ols\n",
    "\n",
    "plt.rc(\"figure\", figsize=(16, 8))\n",
    "plt.rc(\"font\", size=14)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Duncan's Prestige Dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use a utility function to load any R dataset available from the great <a href=\"https://vincentarelbundock.github.io/Rdatasets/\">Rdatasets package</a>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prestige = sm.datasets.get_rdataset(\"Duncan\", \"carData\", cache=True).data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prestige.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prestige_model = ols(\"prestige ~ income + education\", data=prestige).fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(prestige_model.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Influence plots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Influence plots show the (externally) studentized residuals vs. the leverage of each observation as measured by the hat matrix.\n",
    "\n",
    "Externally studentized residuals are residuals that are scaled by their standard deviation where \n",
    "\n",
    "$$var(\\hat{\\epsilon}_i)=\\hat{\\sigma}^2_i(1-h_{ii})$$\n",
    "\n",
    "with\n",
    "\n",
    "$$\\hat{\\sigma}^2_i=\\frac{1}{n - p - 1 \\;\\;}\\sum_{j}^{n}\\;\\;\\;\\forall \\;\\;\\; j \\neq i$$\n",
    "\n",
    "$n$ is the number of observations and $p$ is the number of regressors. $h_{ii}$ is the $i$-th diagonal element of the hat matrix\n",
    "\n",
    "$$H=X(X^{\\;\\prime}X)^{-1}X^{\\;\\prime}$$\n",
    "\n",
    "The influence of each point can be visualized by the criterion keyword argument. Options are Cook's distance and DFFITS, two measures of influence."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.influence_plot(prestige_model, criterion=\"cooks\")\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see there are a few worrisome observations. Both contractor and reporter have low leverage but a large residual. <br />\n",
    "RR.engineer has small residual and large leverage. Conductor and minister have both high leverage and large residuals, and, <br />\n",
    "therefore, large influence."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Partial Regression Plots (Duncan)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since we are doing multivariate regressions, we cannot just look at individual bivariate plots to discern relationships. <br />\n",
    "Instead, we want to look at the relationship of the dependent variable and independent variables conditional on the other <br />\n",
    "independent variables. We can do this through using partial regression plots, otherwise known as added variable plots. <br />\n",
    "\n",
    "In a partial regression plot, to discern the relationship between the response variable and the $k$-th variable, we compute <br />\n",
    "the residuals by regressing the response variable versus the independent variables excluding $X_k$. We can denote this by <br />\n",
    "$X_{\\sim k}$. We then compute the residuals by regressing $X_k$ on $X_{\\sim k}$. The partial regression plot is the plot <br />\n",
    "of the former versus the latter residuals. <br />\n",
    "\n",
    "The notable points of this plot are that the fitted line has slope $\\beta_k$ and intercept zero. The residuals of this plot <br />\n",
    "are the same as those of the least squares fit of the original model with full $X$. You can discern the effects of the <br />\n",
    "individual data values on the estimation of a coefficient easily. If obs_labels is True, then these points are annotated <br />\n",
    "with their observation label. You can also see the violation of underlying assumptions such as homoskedasticity and <br />\n",
    "linearity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_partregress(\n",
    "    \"prestige\", \"income\", [\"income\", \"education\"], data=prestige\n",
    ")\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_partregress(\"prestige\", \"income\", [\"education\"], data=prestige)\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see the partial regression plot confirms the influence of conductor, minister, and RR.engineer on the partial relationship between income and prestige. The cases greatly decrease the effect of income on prestige. Dropping these cases confirms this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "subset = ~prestige.index.isin([\"conductor\", \"RR.engineer\", \"minister\"])\n",
    "prestige_model2 = ols(\n",
    "    \"prestige ~ income + education\", data=prestige, subset=subset\n",
    ").fit()\n",
    "print(prestige_model2.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a quick check of all the regressors, you can use plot_partregress_grid. These plots will not label the <br />\n",
    "points, but you can use them to identify problems and then use plot_partregress to get more information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_partregress_grid(prestige_model)\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Component-Component plus Residual (CCPR) Plots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The CCPR plot provides a way to judge the effect of one regressor on the <br />\n",
    "response variable by taking into account the effects of the other  <br />\n",
    "independent variables. The partial residuals plot is defined as  <br />\n",
    "$\\text{Residuals} + B_iX_i \\text{ }\\text{ }$   versus $X_i$. The component adds $B_iX_i$ versus  <br />\n",
    "$X_i$ to show where the fitted line would lie. Care should be taken if $X_i$  <br />\n",
    "is highly correlated with any of the other independent variables. If this  <br />\n",
    "is the case, the variance evident in the plot will be an underestimate of  <br />\n",
    "the true variance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_ccpr(prestige_model, \"education\")\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see the relationship between the variation in prestige explained by education conditional on income seems to be linear, though you can see there are some observations that are exerting considerable influence on the relationship. We can quickly look at more than one variable by using plot_ccpr_grid."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_ccpr_grid(prestige_model)\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Single Variable Regression Diagnostics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The plot_regress_exog function is a convenience function that gives a 2x2 plot containing the dependent variable and fitted values with confidence intervals vs. the independent variable chosen, the residuals of the model vs. the chosen independent variable, a partial regression plot, and a CCPR plot. This function can be used for quickly checking modeling assumptions with respect to a single regressor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_regress_exog(prestige_model, \"education\")\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fit Plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The plot_fit function plots the fitted values versus a chosen independent variable. It includes prediction confidence intervals and optionally plots the true dependent variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_fit(prestige_model, \"education\")\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Statewide Crime 2009 Dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compare the following to http://www.ats.ucla.edu/stat/stata/webbooks/reg/chapter4/statareg_self_assessment_answers4.htm\n",
    "\n",
    "Though the data here is not the same as in that example. You could run that example by uncommenting the necessary cells below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dta = pd.read_csv(\"http://www.stat.ufl.edu/~aa/social/csv_files/statewide-crime-2.csv\")\n",
    "# dta = dta.set_index(\"State\", inplace=True).dropna()\n",
    "# dta.rename(columns={\"VR\" : \"crime\",\n",
    "#                    \"MR\" : \"murder\",\n",
    "#                    \"M\"  : \"pctmetro\",\n",
    "#                    \"W\"  : \"pctwhite\",\n",
    "#                    \"H\"  : \"pcths\",\n",
    "#                    \"P\"  : \"poverty\",\n",
    "#                    \"S\"  : \"single\"\n",
    "#                    }, inplace=True)\n",
    "#\n",
    "# crime_model = ols(\"murder ~ pctmetro + poverty + pcths + single\", data=dta).fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dta = sm.datasets.statecrime.load_pandas().data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "crime_model = ols(\"murder ~ urban + poverty + hs_grad + single\", data=dta).fit()\n",
    "print(crime_model.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Partial Regression Plots (Crime Data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_partregress_grid(crime_model)\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_partregress(\n",
    "    \"murder\", \"hs_grad\", [\"urban\", \"poverty\", \"single\"], data=dta\n",
    ")\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Leverage-Resid<sup>2</sup> Plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Closely related to the influence_plot is the leverage-resid<sup>2</sup> plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.plot_leverage_resid2(crime_model)\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Influence Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = sm.graphics.influence_plot(crime_model)\n",
    "fig.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Using robust regression to correct for outliers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Part of the problem here in recreating the Stata results is that M-estimators are not robust to leverage points. MM-estimators should do better with this examples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from statsmodels.formula.api import rlm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rob_crime_model = rlm(\n",
    "    \"murder ~ urban + poverty + hs_grad + single\",\n",
    "    data=dta,\n",
    "    M=sm.robust.norms.TukeyBiweight(3),\n",
    ").fit(conv=\"weights\")\n",
    "print(rob_crime_model.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# rob_crime_model = rlm(\"murder ~ pctmetro + poverty + pcths + single\", data=dta, M=sm.robust.norms.TukeyBiweight()).fit(conv=\"weights\")\n",
    "# print(rob_crime_model.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is not yet an influence diagnostics method as part of RLM, but we can recreate them. (This depends on the status of [issue #888](https://github.com/statsmodels/statsmodels/issues/808))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "weights = rob_crime_model.weights\n",
    "idx = weights > 0\n",
    "X = rob_crime_model.model.exog[idx.values]\n",
    "ww = weights[idx] / weights[idx].mean()\n",
    "hat_matrix_diag = ww * (X * np.linalg.pinv(X).T).sum(1)\n",
    "resid = rob_crime_model.resid\n",
    "resid2 = resid ** 2\n",
    "resid2 /= resid2.sum()\n",
    "nobs = int(idx.sum())\n",
    "hm = hat_matrix_diag.mean()\n",
    "rm = resid2.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from statsmodels.graphics import utils\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(16, 8))\n",
    "ax.plot(resid2[idx], hat_matrix_diag, \"o\")\n",
    "ax = utils.annotate_axes(\n",
    "    range(nobs),\n",
    "    labels=rob_crime_model.model.data.row_labels[idx],\n",
    "    points=lzip(resid2[idx], hat_matrix_diag),\n",
    "    offset_points=[(-5, 5)] * nobs,\n",
    "    size=\"large\",\n",
    "    ax=ax,\n",
    ")\n",
    "ax.set_xlabel(\"resid2\")\n",
    "ax.set_ylabel(\"leverage\")\n",
    "ylim = ax.get_ylim()\n",
    "ax.vlines(rm, *ylim)\n",
    "xlim = ax.get_xlim()\n",
    "ax.hlines(hm, *xlim)\n",
    "ax.margins(0, 0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
