Logo

Regression diagnosticsΒΆ

Link to Notebook GitHub

This example file shows how to use a few of the statsmodels regression diagnostic tests in a real-life context. You can learn about more tests and find out more information abou the tests here on the Regression Diagnostics page.

Note that most of the tests described here only return a tuple of numbers, without any annotation. A full description of outputs is always included in the docstring and in the online statsmodels documentation. For presentation purposes, we use the zip(name,test) construct to pretty-print short descriptions in the examples below.

Estimate a regression model

In [1]:
from __future__ import print_function
from statsmodels.compat import lzip
import statsmodels
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
import statsmodels.stats.api as sms
import matplotlib.pyplot as plt

# Load data
url = 'http://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat = pd.read_csv(url)

# Fit regression model (using the natural log of one of the regressaors)
results = smf.ols('Lottery ~ Literacy + np.log(Pop1831)', data=dat).fit()

# Inspect the results
print(results.summary())
Populating the interactive namespace from numpy and matplotlib

---------------------------------------------------------------------------
URLError                                  Traceback (most recent call last)
<ipython-input-3-5ab57e184000> in <module>()
     10 # Load data
     11 url = 'http://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
---> 12 dat = pd.read_csv(url)
     13 
     14 # Fit regression model (using the natural log of one of the regressaors)

/usr/lib/python2.7/dist-packages/pandas/io/parsers.pyc in parser_f(filepath_or_buffer, sep, dialect, compression, doublequote, escapechar, quotechar, quoting, skipinitialspace, lineterminator, header, index_col, names, prefix, skiprows, skipfooter, skip_footer, na_values, na_fvalues, true_values, false_values, delimiter, converters, dtype, usecols, engine, delim_whitespace, as_recarray, na_filter, compact_ints, use_unsigned, low_memory, buffer_lines, warn_bad_lines, error_bad_lines, keep_default_na, thousands, comment, decimal, parse_dates, keep_date_col, dayfirst, date_parser, memory_map, float_precision, nrows, iterator, chunksize, verbose, encoding, squeeze, mangle_dupe_cols, tupleize_cols, infer_datetime_format, skip_blank_lines)
    463                     skip_blank_lines=skip_blank_lines)
    464 
--> 465         return _read(filepath_or_buffer, kwds)
    466 
    467     parser_f.__name__ = name

/usr/lib/python2.7/dist-packages/pandas/io/parsers.pyc in _read(filepath_or_buffer, kwds)
    227 
    228     filepath_or_buffer, _ = get_filepath_or_buffer(filepath_or_buffer,
--> 229                                                    encoding)
    230 
    231     if kwds.get('date_parser', None) is not None:

/usr/lib/python2.7/dist-packages/pandas/io/common.pyc in get_filepath_or_buffer(filepath_or_buffer, encoding)
    116 
    117     if _is_url(filepath_or_buffer):
--> 118         req = _urlopen(str(filepath_or_buffer))
    119         return maybe_read_encoded_stream(req, encoding)
    120 

/usr/lib/python2.7/urllib2.pyc in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    152     else:
    153         opener = _opener
--> 154     return opener.open(url, data, timeout)
    155 
    156 def install_opener(opener):

/usr/lib/python2.7/urllib2.pyc in open(self, fullurl, data, timeout)
    429             req = meth(req)
    430 
--> 431         response = self._open(req, data)
    432 
    433         # post-process response

/usr/lib/python2.7/urllib2.pyc in _open(self, req, data)
    447         protocol = req.get_type()
    448         result = self._call_chain(self.handle_open, protocol, protocol +
--> 449                                   '_open', req)
    450         if result:
    451             return result

/usr/lib/python2.7/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
    407             func = getattr(handler, meth_name)
    408 
--> 409             result = func(*args)
    410             if result is not None:
    411                 return result

/usr/lib/python2.7/urllib2.pyc in http_open(self, req)
   1225 
   1226     def http_open(self, req):
-> 1227         return self.do_open(httplib.HTTPConnection, req)
   1228 
   1229     http_request = AbstractHTTPHandler.do_request_

/usr/lib/python2.7/urllib2.pyc in do_open(self, http_class, req, **http_conn_args)
   1195         except socket.error, err: # XXX what error?
   1196             h.close()
-> 1197             raise URLError(err)
   1198         else:
   1199             try:

URLError: <urlopen error [Errno -2] Name or service not known>

Normality of the residuals

Jarque-Bera test:

In [2]:
name = ['Jarque-Bera', 'Chi^2 two-tail prob.', 'Skew', 'Kurtosis']
test = sms.jarque_bera(results.resid)
lzip(name, test)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-911bd7c54c5b> in <module>()
      1 name = ['Jarque-Bera', 'Chi^2 two-tail prob.', 'Skew', 'Kurtosis']
----> 2 test = sms.jarque_bera(results.resid)
      3 lzip(name, test)

NameError: name 'results' is not defined

Omni test:

In [3]:
name = ['Chi^2', 'Two-tail probability']
test = sms.omni_normtest(results.resid)
lzip(name, test)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-5d66593e2f9b> in <module>()
      1 name = ['Chi^2', 'Two-tail probability']
----> 2 test = sms.omni_normtest(results.resid)
      3 lzip(name, test)

NameError: name 'results' is not defined

Influence tests

Once created, an object of class OLSInfluence holds attributes and methods that allow users to assess the influence of each observation. For example, we can compute and extract the first few rows of DFbetas by:

In [4]:
from statsmodels.stats.outliers_influence import OLSInfluence
test_class = OLSInfluence(results)
test_class.dfbetas[:5,:]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-b1e3d0f42941> in <module>()
      1 from statsmodels.stats.outliers_influence import OLSInfluence
----> 2 test_class = OLSInfluence(results)
      3 test_class.dfbetas[:5,:]

NameError: name 'results' is not defined

Explore other options by typing dir(influence_test)

Useful information on leverage can also be plotted:

In [5]:
from statsmodels.graphics.regressionplots import plot_leverage_resid2
fig, ax = plt.subplots(figsize=(8,6))
fig = plot_leverage_resid2(results, ax = ax)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-7590c2fd5fb2> in <module>()
      1 from statsmodels.graphics.regressionplots import plot_leverage_resid2
      2 fig, ax = plt.subplots(figsize=(8,6))
----> 3 fig = plot_leverage_resid2(results, ax = ax)

NameError: name 'results' is not defined

Other plotting options can be found on the Graphics page.

Multicollinearity

Condition number:

In [6]:
np.linalg.cond(results.model.exog)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-c5af1dc95c76> in <module>()
----> 1 np.linalg.cond(results.model.exog)

NameError: name 'results' is not defined

Heteroskedasticity tests

Breush-Pagan test:

In [7]:
name = ['Lagrange multiplier statistic', 'p-value',
        'f-value', 'f p-value']
test = sms.het_breushpagan(results.resid, results.model.exog)
lzip(name, test)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-a4e5ba55306f> in <module>()
      1 name = ['Lagrange multiplier statistic', 'p-value',
      2         'f-value', 'f p-value']
----> 3 test = sms.het_breushpagan(results.resid, results.model.exog)
      4 lzip(name, test)

NameError: name 'results' is not defined

Goldfeld-Quandt test

In [8]:
name = ['F statistic', 'p-value']
test = sms.het_goldfeldquandt(results.resid, results.model.exog)
lzip(name, test)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-08d227eb273b> in <module>()
      1 name = ['F statistic', 'p-value']
----> 2 test = sms.het_goldfeldquandt(results.resid, results.model.exog)
      3 lzip(name, test)

NameError: name 'results' is not defined

Linearity

Harvey-Collier multiplier test for Null hypothesis that the linear specification is correct:

In [9]:
name = ['t value', 'p value']
test = sms.linear_harvey_collier(results)
lzip(name, test)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-239f988c7dd2> in <module>()
      1 name = ['t value', 'p value']
----> 2 test = sms.linear_harvey_collier(results)
      3 lzip(name, test)

NameError: name 'results' is not defined

This Page