Generalized Estimating Equations estimate generalized linear models for panel, cluster or repeated measures data when the observations are possibly correlated withing a cluster but uncorrelated across clusters. It supports estimation of the same one-parameter exponential families as Generalized Linear models (GLM).
See Module Reference for commands and arguments.
The following illustrates a Poisson regression with exchangeable correlation within clusters using data on epilepsy seizures.
In [1]: import statsmodels.api as sm
In [2]: import statsmodels.formula.api as smf
In [3]: data = sm.datasets.get_rdataset('epil', package='MASS').data
---------------------------------------------------------------------------
URLError Traceback (most recent call last)
<ipython-input-3-4e55b8bec212> in <module>()
----> 1 data = sm.datasets.get_rdataset('epil', package='MASS').data
/build/statsmodels-0.8.0/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/datasets/utils.pyc in get_rdataset(dataname, package, cache)
288 "master/doc/"+package+"/rst/")
289 cache = _get_cache(cache)
--> 290 data, from_cache = _get_data(data_base_url, dataname, cache)
291 data = read_csv(data, index_col=0)
292 data = _maybe_reset_index(data)
/build/statsmodels-0.8.0/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/datasets/utils.pyc in _get_data(base_url, dataname, cache, extension)
219 url = base_url + (dataname + ".%s") % extension
220 try:
--> 221 data, from_cache = _urlopen_cached(url, cache)
222 except HTTPError as err:
223 if '404' in str(err):
/build/statsmodels-0.8.0/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/datasets/utils.pyc in _urlopen_cached(url, cache)
210 # not using the cache or didn't find it in cache
211 if not from_cache:
--> 212 data = urlopen(url).read()
213 if cache is not None: # then put it in the cache
214 _cache_it(data, cache_path)
/usr/lib/python2.7/urllib2.pyc in urlopen(url, data, timeout)
125 if _opener is None:
126 _opener = build_opener()
--> 127 return _opener.open(url, data, timeout)
128
129 def install_opener(opener):
/usr/lib/python2.7/urllib2.pyc in open(self, fullurl, data, timeout)
402 req = meth(req)
403
--> 404 response = self._open(req, data)
405
406 # post-process response
/usr/lib/python2.7/urllib2.pyc in _open(self, req, data)
420 protocol = req.get_type()
421 result = self._call_chain(self.handle_open, protocol, protocol +
--> 422 '_open', req)
423 if result:
424 return result
/usr/lib/python2.7/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
380 func = getattr(handler, meth_name)
381
--> 382 result = func(*args)
383 if result is not None:
384 return result
/usr/lib/python2.7/urllib2.pyc in https_open(self, req)
1220
1221 def https_open(self, req):
-> 1222 return self.do_open(httplib.HTTPSConnection, req)
1223
1224 https_request = AbstractHTTPHandler.do_request_
/usr/lib/python2.7/urllib2.pyc in do_open(self, http_class, req)
1182 except socket.error, err: # XXX what error?
1183 h.close()
-> 1184 raise URLError(err)
1185 else:
1186 try:
URLError: <urlopen error [Errno -2] Name or service not known>
In [4]: fam = sm.families.Poisson()
In [5]: ind = sm.cov_struct.Exchangeable()
In [6]: mod = smf.gee("y ~ age + trt + base", "subject", data,
...: cov_struct=ind, family=fam)
...:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-6-f8d3b31d24f3> in <module>()
1 mod = smf.gee("y ~ age + trt + base", "subject", data,
----> 2 cov_struct=ind, family=fam)
/build/statsmodels-0.8.0/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/genmod/generalized_estimating_equations.pyc in from_formula(cls, formula, groups, data, subset, time, offset, exposure, *args, **kwargs)
668
669 if type(groups) == str:
--> 670 groups = data[groups]
671
672 if type(time) == str:
KeyError: 'subject'
In [7]: res = mod.fit()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-7-deef2687e692> in <module>()
----> 1 res = mod.fit()
NameError: name 'mod' is not defined
In [8]: print(res.summary())
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-a8dc848a1f25> in <module>()
----> 1 print(res.summary())
NameError: name 'res' is not defined
Several notebook examples of the use of GEE can be found on the Wiki: Wiki notebooks for GEE
GEE(endog, exog, groups[, time, family, ...]) | Estimation of marginal regression models using Generalized Estimating Equations (GEE). |
GEEResults(model, params, cov_params, scale) | This class summarizes the fit of a marginal regression model using GEE. |
GEEMargins(results, args[, kwargs]) | Estimated marginal effects for a regression model fit with GEE. |
The dependence structures currently implemented are
CovStruct([cov_nearest_method]) | A base class for correlation and covariance structures of grouped data. |
Autoregressive([dist_func]) | A first-order autoregressive working dependence structure. |
Exchangeable() | An exchangeable working dependence structure. |
GlobalOddsRatio(endog_type) | Estimate the global odds ratio for a GEE with ordinal or nominal data. |
Independence([cov_nearest_method]) | An independence working dependence structure. |
Nested([cov_nearest_method]) | A nested working dependence structure. |
The distribution families are the same as for GLM, currently implemented are
Family(link, variance) | The parent class for one-parameter exponential families. |
Binomial([link]) | Binomial exponential family distribution. |
Gamma([link]) | Gamma exponential family distribution. |
Gaussian([link]) | Gaussian exponential family distribution. |
InverseGaussian([link]) | InverseGaussian exponential family. |
NegativeBinomial([link, alpha]) | Negative Binomial exponential family. |
Poisson([link]) | Poisson exponential family. |
The link functions are the same as for GLM, currently implemented are the following. Not all link functions are available for each distribution family. The list of available link functions can be obtained by
>>> sm.families.family.<familyname>.links
Link | A generic link function for one-parameter exponential family. |
CDFLink([dbn]) | The use the CDF of a scipy.stats distribution |
CLogLog | The complementary log-log transform |
Log | The log transform |
Logit | The logit transform |
NegativeBinomial([alpha]) | The negative binomial link function |
Power([power]) | The power transform |
cauchy() | The Cauchy (standard Cauchy CDF) transform |
cloglog | The CLogLog transform link function. |
identity() | The identity transform |
inverse_power() | The inverse transform |
inverse_squared() | The inverse squared transform |
log | The log transform |
logit | |
nbinom([alpha]) | The negative binomial link function. |
probit([dbn]) | The probit (standard normal CDF) transform |