Logo

ANOVA

Analysis of Variance models

Examples

In [1]: import statsmodels.api as sm

In [2]: from statsmodels.formula.api import ols

In [3]: moore = sm.datasets.get_rdataset("Moore", "car",
   ...:                                  cache=True) # load data
   ...: 
---------------------------------------------------------------------------
URLError                                  Traceback (most recent call last)
<ipython-input-3-924d52d36060> in <module>()
      1 moore = sm.datasets.get_rdataset("Moore", "car",
----> 2                                  cache=True) # load data

/build/statsmodels-0.8.0~rc1+git43-g1ac3f11/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/datasets/utils.pyc in get_rdataset(dataname, package, cache)
    287                      "master/doc/"+package+"/rst/")
    288     cache = _get_cache(cache)
--> 289     data, from_cache = _get_data(data_base_url, dataname, cache)
    290     data = read_csv(data, index_col=0)
    291     data = _maybe_reset_index(data)

/build/statsmodels-0.8.0~rc1+git43-g1ac3f11/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/datasets/utils.pyc in _get_data(base_url, dataname, cache, extension)
    218     url = base_url + (dataname + ".%s") % extension
    219     try:
--> 220         data, from_cache = _urlopen_cached(url, cache)
    221     except HTTPError as err:
    222         if '404' in str(err):

/build/statsmodels-0.8.0~rc1+git43-g1ac3f11/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/datasets/utils.pyc in _urlopen_cached(url, cache)
    209     # not using the cache or didn't find it in cache
    210     if not from_cache:
--> 211         data = urlopen(url).read()
    212         if cache is not None:  # then put it in the cache
    213             _cache_it(data, cache_path)

/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 https_open(self, req)
   1238         def https_open(self, req):
   1239             return self.do_open(httplib.HTTPSConnection, req,
-> 1240                 context=self._context)
   1241 
   1242         https_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 -3] Temporary failure in name resolution>

In [4]: data = moore.data
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-07b15baaa84d> in <module>()
----> 1 data = moore.data

NameError: name 'moore' is not defined

In [5]: data = data.rename(columns={"partner.status":
   ...:                             "partner_status"}) # make name pythonic
   ...: 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-76a0688779cd> in <module>()
----> 1 data = data.rename(columns={"partner.status":
      2                             "partner_status"}) # make name pythonic
      3 

NameError: name 'data' is not defined

In [6]: moore_lm = ols('conformity ~ C(fcategory, Sum)*C(partner_status, Sum)',
   ...:                 data=data).fit()
   ...: 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-0a291eef3047> in <module>()
      1 moore_lm = ols('conformity ~ C(fcategory, Sum)*C(partner_status, Sum)',
----> 2                 data=data).fit()

NameError: name 'data' is not defined

In [7]: table = sm.stats.anova_lm(moore_lm, typ=2) # Type 2 ANOVA DataFrame
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-4e32df33effd> in <module>()
----> 1 table = sm.stats.anova_lm(moore_lm, typ=2) # Type 2 ANOVA DataFrame

NameError: name 'moore_lm' is not defined

In [8]: print(table)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-d1f157ed9e2f> in <module>()
----> 1 print(table)

NameError: name 'table' is not defined

A more detailed example can be found here:

Module Reference

anova_lm(*args, **kwargs) ANOVA table for one or more fitted linear models.

Table Of Contents

Previous topic

statsmodels.discrete.discrete_model.MultinomialModel.score

Next topic

statsmodels.stats.anova.anova_lm

This Page