{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#Import GLM and pysal\n",
    "import os\n",
    "import numpy as np\n",
    "os.chdir('/Users/toshan/dev/pysal/pysal/contrib/glm')\n",
    "from glm import GLM\n",
    "import pysal\n",
    "import pandas as pd\n",
    "import statsmodels.formula.api as smf\n",
    "import statsmodels.api as sm\n",
    "from family import Gaussian, Binomial, Poisson, QuasiPoisson\n",
    "\n",
    "from statsmodels.api import families"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#Prepare some test data - columbus example\n",
    "db = pysal.open(pysal.examples.get_path('columbus.dbf'),'r')\n",
    "y = np.array(db.by_col(\"HOVAL\"))\n",
    "y = np.reshape(y, (49,1))\n",
    "X = []\n",
    "#X.append(np.ones(len(y)))\n",
    "X.append(db.by_col(\"INC\"))\n",
    "X.append(db.by_col(\"CRIME\"))\n",
    "X = np.array(X).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 46.42818268]\n",
      " [  0.62898397]\n",
      " [ -0.48488854]]\n"
     ]
    }
   ],
   "source": [
    "#First fit pysal OLS model\n",
    "from pysal.spreg import ols\n",
    "OLS = ols.OLS(y, X)\n",
    "print OLS.betas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'family.Gaussian'>\n",
      "<class 'family.Gaussian'>\n",
      "<class 'family.Gaussian'>\n",
      "[ 46.42818268   0.62898397  -0.48488854]\n",
      "[ 46.42818268   0.62898397  -0.48488854]\n"
     ]
    }
   ],
   "source": [
    "#Then fit Gaussian GLM\n",
    "\n",
    "#create Gaussian GLM model object\n",
    "model = GLM(y, X, Gaussian())\n",
    "model\n",
    "\n",
    "#Fit model to estimate coefficients and return GLMResults object\n",
    "results = model.fit()\n",
    "\n",
    "#Check coefficients - R betas [46.4282, 0.6290, -0.4849]\n",
    "print results.params\n",
    "\n",
    "# Gaussian GLM results from statsmodels\n",
    "sm_model = smf.GLM(y, sm.add_constant(X), family=families.Gaussian())\n",
    "sm_results = sm_model.fit()\n",
    "print sm_results.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 2\n",
      "<class 'family.Gaussian'>\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "<class 'family.Gaussian'>\n",
      "<class 'family.Gaussian'>\n",
      "<class 'family.Gaussian'>\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print results.df_model, sm_results.df_model\n",
    "print np.allclose(results.aic, sm_results.aic)\n",
    "print np.allclose(results.bic, sm_results.bic)\n",
    "print np.allclose(results.deviance, sm_results.deviance)\n",
    "print np.allclose(results.df_model, sm_results.df_model)\n",
    "print np.allclose(results.df_resid, sm_results.df_resid)\n",
    "print np.allclose(results.llf, sm_results.llf)\n",
    "print np.allclose(results.mu, sm_results.mu)\n",
    "print np.allclose(results.n, sm_results.nobs)\n",
    "print np.allclose(results.null, sm_results.null)\n",
    "print np.allclose(results.null_deviance, sm_results.null_deviance)\n",
    "print np.allclose(results.params, sm_results.params)\n",
    "print np.allclose(results.pearson_chi2, sm_results.pearson_chi2)\n",
    "print np.allclose(results.resid_anscombe, sm_results.resid_anscombe)\n",
    "print np.allclose(results.resid_deviance, sm_results.resid_deviance)\n",
    "print np.allclose(results.resid_pearson, sm_results.resid_pearson)\n",
    "print np.allclose(results.resid_response, sm_results.resid_response)\n",
    "print np.allclose(results.resid_working, sm_results.resid_working)\n",
    "print np.allclose(results.scale, sm_results.scale)\n",
    "print np.allclose(results.normalized_cov_params, sm_results.normalized_cov_params)\n",
    "print np.allclose(results.cov_params(), sm_results.cov_params())\n",
    "print np.allclose(results.bse, sm_results.bse)\n",
    "print np.allclose(results.conf_int(), sm_results.conf_int())\n",
    "print np.allclose(results.pvalues, sm_results.pvalues)\n",
    "print np.allclose(results.tvalues, sm_results.tvalues)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'family.Poisson'>\n",
      "<class 'family.Poisson'>\n",
      "<class 'family.Poisson'>\n",
      "[ 3.92159085  0.01183491 -0.01371397]\n",
      "[ 3.92159085  0.01183491 -0.01371397]\n"
     ]
    }
   ],
   "source": [
    "#Now fit a Poisson GLM \n",
    "\n",
    "poisson_y = np.round(y).astype(int)\n",
    "\n",
    "#create Poisson GLM model object\n",
    "model = GLM(poisson_y, X, Poisson())\n",
    "model\n",
    "\n",
    "#Fit model to estimate coefficients and return GLMResults object\n",
    "results = model.fit()\n",
    "\n",
    "#Check coefficients - R betas [3.91926, 0.01198, -0.01371]\n",
    "print results.params.T\n",
    "\n",
    "# Poisson GLM results from statsmodels\n",
    "sm_results = smf.GLM(poisson_y, sm.add_constant(X), family=families.Poisson()).fit()\n",
    "print sm_results.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'family.Poisson'>\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "<class 'family.Poisson'>\n",
      "<class 'family.Poisson'>\n",
      "<class 'family.Poisson'>\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "[ 0.13049161  0.00511599  0.00193769] [ 0.13049161  0.00511599  0.00193769]\n"
     ]
    }
   ],
   "source": [
    "print np.allclose(results.aic, sm_results.aic)\n",
    "print np.allclose(results.bic, sm_results.bic)\n",
    "print np.allclose(results.deviance, sm_results.deviance)\n",
    "print np.allclose(results.df_model, sm_results.df_model)\n",
    "print np.allclose(results.df_resid, sm_results.df_resid)\n",
    "print np.allclose(results.llf, sm_results.llf)\n",
    "print np.allclose(results.mu, sm_results.mu)\n",
    "print np.allclose(results.n, sm_results.nobs)\n",
    "print np.allclose(results.null, sm_results.null)\n",
    "print np.allclose(results.null_deviance, sm_results.null_deviance)\n",
    "print np.allclose(results.params, sm_results.params)\n",
    "print np.allclose(results.pearson_chi2, sm_results.pearson_chi2)\n",
    "print np.allclose(results.resid_anscombe, sm_results.resid_anscombe)\n",
    "print np.allclose(results.resid_deviance, sm_results.resid_deviance)\n",
    "print np.allclose(results.resid_pearson, sm_results.resid_pearson)\n",
    "print np.allclose(results.resid_response, sm_results.resid_response)\n",
    "print np.allclose(results.resid_working, sm_results.resid_working)\n",
    "print np.allclose(results.scale, sm_results.scale)\n",
    "print np.allclose(results.normalized_cov_params, sm_results.normalized_cov_params)\n",
    "print np.allclose(results.cov_params(), sm_results.cov_params())\n",
    "print np.allclose(results.bse, sm_results.bse)\n",
    "print np.allclose(results.conf_int(), sm_results.conf_int())\n",
    "print np.allclose(results.pvalues, sm_results.pvalues)\n",
    "print np.allclose(results.tvalues, sm_results.tvalues)\n",
    "print results.bse, sm_results.bse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-5.33638276  0.0287754 ]\n",
      "[-5.33638276  0.0287754 ]\n"
     ]
    }
   ],
   "source": [
    "#Now fit a binomial GLM\n",
    "londonhp = pd.read_csv('/Users/toshan/projects/londonhp.csv')\n",
    "#londonhp = pd.read_csv('/Users/qszhao/Dropbox/pysal/pysal/contrib/gwr/londonhp.csv')\n",
    "y = londonhp['BATH2'].values\n",
    "y = np.reshape(y, (316,1))\n",
    "X = londonhp['FLOORSZ'].values\n",
    "X = np.reshape(X, (316,1))\n",
    "\n",
    "#create logistic GLM model object\n",
    "model = GLM(y, X, Binomial())\n",
    "model\n",
    "\n",
    "#Fit model to estimate coefficients and return GLMResults object\n",
    "results = model.fit()\n",
    "\n",
    "#Check coefficients - R betas [-5.33638, 0.02878]\n",
    "print results.params.T\n",
    "\n",
    "# Logistic GLM results from statsmodels\n",
    "sm_results = smf.GLM(y, sm.add_constant(X), family=families.Binomial()).fit()\n",
    "print sm_results.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print results.df_model, sm_results.df_model\n",
    "print np.allclose(results.aic, sm_results.aic)\n",
    "print np.allclose(results.bic, sm_results.bic)\n",
    "print np.allclose(results.deviance, sm_results.deviance)\n",
    "print np.allclose(results.df_model, sm_results.df_model)\n",
    "print np.allclose(results.df_resid, sm_results.df_resid)\n",
    "print np.allclose(results.llf, sm_results.llf)\n",
    "print np.allclose(results.mu, sm_results.mu)\n",
    "print np.allclose(results.n, sm_results.nobs)\n",
    "print np.allclose(results.null, sm_results.null)\n",
    "print np.allclose(results.null_deviance, sm_results.null_deviance)\n",
    "print np.allclose(results.params, sm_results.params)\n",
    "print np.allclose(results.pearson_chi2, sm_results.pearson_chi2)\n",
    "print np.allclose(results.resid_anscombe, sm_results.resid_anscombe)\n",
    "print np.allclose(results.resid_deviance, sm_results.resid_deviance)\n",
    "print np.allclose(results.resid_pearson, sm_results.resid_pearson)\n",
    "print np.allclose(results.resid_response, sm_results.resid_response)\n",
    "print np.allclose(results.resid_working, sm_results.resid_working)\n",
    "print np.allclose(results.scale, sm_results.scale)\n",
    "print np.allclose(results.normalized_cov_params, sm_results.normalized_cov_params)\n",
    "print np.allclose(results.cov_params(), sm_results.cov_params())\n",
    "print np.allclose(results.bse, sm_results.bse)\n",
    "print np.allclose(results.conf_int(), sm_results.conf_int())\n",
    "print np.allclose(results.pvalues, sm_results.pvalues)\n",
    "print np.allclose(results.tvalues, sm_results.tvalues)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'family.QuasiPoisson'>\n",
      "<class 'family.QuasiPoisson'>\n",
      "<class 'family.QuasiPoisson'>\n"
     ]
    }
   ],
   "source": [
    "#create QUasiPoisson GLM model object\n",
    "model = GLM(poisson_y, X, QuasiPoisson())\n",
    "model\n",
    "\n",
    "#Fit model to estimate coefficients and return GLMResults object\n",
    "results = model.fit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
