Regression diagnostics

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]:
%matplotlib inline

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())
/build/statsmodels-JytjB9/statsmodels-0.8.0/.pybuild/cpython3_3.6_statsmodels/build/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
  from pandas.core import datetools
---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
/usr/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1317                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318                           encode_chunked=req.has_header('Transfer-encoding'))
   1319             except OSError as err: # timeout error

/usr/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1238         """Send a complete request to the server."""
-> 1239         self._send_request(method, url, body, headers, encode_chunked)
   1240 

/usr/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1284             body = _encode(body, 'body')
-> 1285         self.endheaders(body, encode_chunked=encode_chunked)
   1286 

/usr/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
   1233             raise CannotSendHeader()
-> 1234         self._send_output(message_body, encode_chunked=encode_chunked)
   1235 

/usr/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 

/usr/lib/python3.6/http/client.py in send(self, data)
    963             if self.auto_open:
--> 964                 self.connect()
    965             else:

/usr/lib/python3.6/http/client.py in connect(self)
    935         self.sock = self._create_connection(
--> 936             (self.host,self.port), self.timeout, self.source_address)
    937         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

/usr/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
    723     if err is not None:
--> 724         raise err
    725     else:

/usr/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
    712                 sock.bind(source_address)
--> 713             sock.connect(sa)
    714             # Break explicitly a reference cycle

ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

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

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

/usr/lib/python3/dist-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    431     compression = _infer_compression(filepath_or_buffer, compression)
    432     filepath_or_buffer, _, compression = get_filepath_or_buffer(
--> 433         filepath_or_buffer, encoding, compression)
    434     kwds['compression'] = compression
    435 

/usr/lib/python3/dist-packages/pandas/io/common.py in get_filepath_or_buffer(filepath_or_buffer, encoding, compression)
    188 
    189     if _is_url(filepath_or_buffer):
--> 190         req = _urlopen(filepath_or_buffer)
    191         content_encoding = req.headers.get('Content-Encoding', None)
    192         if content_encoding == 'gzip':

/usr/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    221     else:
    222         opener = _opener
--> 223     return opener.open(url, data, timeout)
    224 
    225 def install_opener(opener):

/usr/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
    524             req = meth(req)
    525 
--> 526         response = self._open(req, data)
    527 
    528         # post-process response

/usr/lib/python3.6/urllib/request.py in _open(self, req, data)
    542         protocol = req.type
    543         result = self._call_chain(self.handle_open, protocol, protocol +
--> 544                                   '_open', req)
    545         if result:
    546             return result

/usr/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502         for handler in handlers:
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:
    506                 return result

/usr/lib/python3.6/urllib/request.py in http_open(self, req)
   1344 
   1345     def http_open(self, req):
-> 1346         return self.do_open(http.client.HTTPConnection, req)
   1347 
   1348     http_request = AbstractHTTPHandler.do_request_

/usr/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1318                           encode_chunked=req.has_header('Transfer-encoding'))
   1319             except OSError as err: # timeout error
-> 1320                 raise URLError(err)
   1321             r = h.getresponse()
   1322         except:

URLError: <urlopen error [Errno 111] Connection refused>

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-2-ee73171ffab2> 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-3-91e9869152bc> 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-4-760546914e5a> 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-5-eb7313ad0e8d> 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-6-be81e152ec40> 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-7-86dbfc541fc0> 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-8-a9c0bcd95a9d> 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-9-a506880c3348> 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