Patsy: Contrast Coding Systems for categorical variables

Note

This document is based on this excellent resource from UCLA.

A categorical variable of K categories, or levels, usually enters a regression as a sequence of K-1 dummy variables. This amounts to a linear hypothesis on the level means. That is, each test statistic for these variables amounts to testing whether the mean for that level is statistically significantly different from the mean of the base category. This dummy coding is called Treatment coding in R parlance, and we will follow this convention. There are, however, different coding methods that amount to different sets of linear hypotheses.

In fact, the dummy coding is not technically a contrast coding. This is because the dummy variables add to one and are not functionally independent of the model’s intercept. On the other hand, a set of contrasts for a categorical variable with k levels is a set of k-1 functionally independent linear combinations of the factor level means that are also independent of the sum of the dummy variables. The dummy coding is not wrong per se. It captures all of the coefficients, but it complicates matters when the model assumes independence of the coefficients such as in ANOVA. Linear regression models do not assume independence of the coefficients and thus dummy coding is often the only coding that is taught in this context.

To have a look at the contrast matrices in Patsy, we will use data from UCLA ATS. First let’s load the data.

Example Data

In [1]: import pandas

In [2]: url = 'https://stats.idre.ucla.edu/stat/data/hsb2.csv'

In [3]: hsb2 = pandas.read_csv(url)
---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
File /usr/lib/python3.11/urllib/request.py:1348, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
   1347 try:
-> 1348     h.request(req.get_method(), req.selector, req.data, headers,
   1349               encode_chunked=req.has_header('Transfer-encoding'))
   1350 except OSError as err: # timeout error

File /usr/lib/python3.11/http/client.py:1282, in HTTPConnection.request(self, method, url, body, headers, encode_chunked)
   1281 """Send a complete request to the server."""
-> 1282 self._send_request(method, url, body, headers, encode_chunked)

File /usr/lib/python3.11/http/client.py:1328, in HTTPConnection._send_request(self, method, url, body, headers, encode_chunked)
   1327     body = _encode(body, 'body')
-> 1328 self.endheaders(body, encode_chunked=encode_chunked)

File /usr/lib/python3.11/http/client.py:1277, in HTTPConnection.endheaders(self, message_body, encode_chunked)
   1276     raise CannotSendHeader()
-> 1277 self._send_output(message_body, encode_chunked=encode_chunked)

File /usr/lib/python3.11/http/client.py:1037, in HTTPConnection._send_output(self, message_body, encode_chunked)
   1036 del self._buffer[:]
-> 1037 self.send(msg)
   1039 if message_body is not None:
   1040 
   1041     # create a consistent interface to message_body

File /usr/lib/python3.11/http/client.py:975, in HTTPConnection.send(self, data)
    974 if self.auto_open:
--> 975     self.connect()
    976 else:

File /usr/lib/python3.11/http/client.py:1447, in HTTPSConnection.connect(self)
   1445 "Connect to a host on a given (SSL) port."
-> 1447 super().connect()
   1449 if self._tunnel_host:

File /usr/lib/python3.11/http/client.py:941, in HTTPConnection.connect(self)
    940 sys.audit("http.client.connect", self, self.host, self.port)
--> 941 self.sock = self._create_connection(
    942     (self.host,self.port), self.timeout, self.source_address)
    943 # Might fail in OSs that don't implement TCP_NODELAY

File /usr/lib/python3.11/socket.py:851, in create_connection(address, timeout, source_address, all_errors)
    850 if not all_errors:
--> 851     raise exceptions[0]
    852 raise ExceptionGroup("create_connection failed", exceptions)

File /usr/lib/python3.11/socket.py:836, in create_connection(address, timeout, source_address, all_errors)
    835     sock.bind(source_address)
--> 836 sock.connect(sa)
    837 # 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)
Cell In [3], line 1
----> 1 hsb2 = pandas.read_csv(url)

File /usr/lib/python3/dist-packages/pandas/util/_decorators.py:311, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
    305 if len(args) > num_allow_args:
    306     warnings.warn(
    307         msg.format(arguments=arguments),
    308         FutureWarning,
    309         stacklevel=stacklevel,
    310     )
--> 311 return func(*args, **kwargs)

File /usr/lib/python3/dist-packages/pandas/io/parsers/readers.py:586, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)
    571 kwds_defaults = _refine_defaults_read(
    572     dialect,
    573     delimiter,
   (...)
    582     defaults={"delimiter": ","},
    583 )
    584 kwds.update(kwds_defaults)
--> 586 return _read(filepath_or_buffer, kwds)

File /usr/lib/python3/dist-packages/pandas/io/parsers/readers.py:482, in _read(filepath_or_buffer, kwds)
    479 _validate_names(kwds.get("names", None))
    481 # Create the parser.
--> 482 parser = TextFileReader(filepath_or_buffer, **kwds)
    484 if chunksize or iterator:
    485     return parser

File /usr/lib/python3/dist-packages/pandas/io/parsers/readers.py:811, in TextFileReader.__init__(self, f, engine, **kwds)
    808 if "has_index_names" in kwds:
    809     self.options["has_index_names"] = kwds["has_index_names"]
--> 811 self._engine = self._make_engine(self.engine)

File /usr/lib/python3/dist-packages/pandas/io/parsers/readers.py:1040, in TextFileReader._make_engine(self, engine)
   1036     raise ValueError(
   1037         f"Unknown engine: {engine} (valid options are {mapping.keys()})"
   1038     )
   1039 # error: Too many arguments for "ParserBase"
-> 1040 return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]

File /usr/lib/python3/dist-packages/pandas/io/parsers/c_parser_wrapper.py:51, in CParserWrapper.__init__(self, src, **kwds)
     48 kwds["usecols"] = self.usecols
     50 # open handles
---> 51 self._open_handles(src, kwds)
     52 assert self.handles is not None
     54 # Have to pass int, would break tests using TextReader directly otherwise :(

File /usr/lib/python3/dist-packages/pandas/io/parsers/base_parser.py:222, in ParserBase._open_handles(self, src, kwds)
    218 def _open_handles(self, src: FilePathOrBuffer, kwds: dict[str, Any]) -> None:
    219     """
    220     Let the readers open IOHandles after they are done with their potential raises.
    221     """
--> 222     self.handles = get_handle(
    223         src,
    224         "r",
    225         encoding=kwds.get("encoding", None),
    226         compression=kwds.get("compression", None),
    227         memory_map=kwds.get("memory_map", False),
    228         storage_options=kwds.get("storage_options", None),
    229         errors=kwds.get("encoding_errors", "strict"),
    230     )

File /usr/lib/python3/dist-packages/pandas/io/common.py:609, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    602     raise ValueError(
    603         f"Invalid value for `encoding_errors` ({errors}). Please see "
    604         + "https://docs.python.org/3/library/codecs.html#error-handlers "
    605         + "for valid values."
    606     )
    608 # open URLs
--> 609 ioargs = _get_filepath_or_buffer(
    610     path_or_buf,
    611     encoding=encoding,
    612     compression=compression,
    613     mode=mode,
    614     storage_options=storage_options,
    615 )
    617 handle = ioargs.filepath_or_buffer
    618 handles: list[Buffer]

File /usr/lib/python3/dist-packages/pandas/io/common.py:312, in _get_filepath_or_buffer(filepath_or_buffer, encoding, compression, mode, storage_options)
    310 # assuming storage_options is to be interpreted as headers
    311 req_info = urllib.request.Request(filepath_or_buffer, headers=storage_options)
--> 312 with urlopen(req_info) as req:
    313     content_encoding = req.headers.get("Content-Encoding", None)
    314     if content_encoding == "gzip":
    315         # Override compression based on Content-Encoding header

File /usr/lib/python3/dist-packages/pandas/io/common.py:212, in urlopen(*args, **kwargs)
    206 """
    207 Lazy-import wrapper for stdlib urlopen, as that imports a big chunk of
    208 the stdlib.
    209 """
    210 import urllib.request
--> 212 return urllib.request.urlopen(*args, **kwargs)

File /usr/lib/python3.11/urllib/request.py:216, in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    214 else:
    215     opener = _opener
--> 216 return opener.open(url, data, timeout)

File /usr/lib/python3.11/urllib/request.py:519, in OpenerDirector.open(self, fullurl, data, timeout)
    516     req = meth(req)
    518 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 519 response = self._open(req, data)
    521 # post-process response
    522 meth_name = protocol+"_response"

File /usr/lib/python3.11/urllib/request.py:536, in OpenerDirector._open(self, req, data)
    533     return result
    535 protocol = req.type
--> 536 result = self._call_chain(self.handle_open, protocol, protocol +
    537                           '_open', req)
    538 if result:
    539     return result

File /usr/lib/python3.11/urllib/request.py:496, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args)
    494 for handler in handlers:
    495     func = getattr(handler, meth_name)
--> 496     result = func(*args)
    497     if result is not None:
    498         return result

File /usr/lib/python3.11/urllib/request.py:1391, in HTTPSHandler.https_open(self, req)
   1390 def https_open(self, req):
-> 1391     return self.do_open(http.client.HTTPSConnection, req,
   1392         context=self._context, check_hostname=self._check_hostname)

File /usr/lib/python3.11/urllib/request.py:1351, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
   1348         h.request(req.get_method(), req.selector, req.data, headers,
   1349                   encode_chunked=req.has_header('Transfer-encoding'))
   1350     except OSError as err: # timeout error
-> 1351         raise URLError(err)
   1352     r = h.getresponse()
   1353 except:

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

It will be instructive to look at the mean of the dependent variable, write, for each level of race ((1 = Hispanic, 2 = Asian, 3 = African American and 4 = Caucasian)).

In [4]: hsb2.groupby('race')['write'].mean()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [4], line 1
----> 1 hsb2.groupby('race')['write'].mean()

NameError: name 'hsb2' is not defined

Treatment (Dummy) Coding

Dummy coding is likely the most well known coding scheme. It compares each level of the categorical variable to a base reference level. The base reference level is the value of the intercept. It is the default contrast in Patsy for unordered categorical factors. The Treatment contrast matrix for race would be

In [5]: from patsy.contrasts import Treatment

In [6]: levels = [1,2,3,4]

In [7]: contrast = Treatment(reference=0).code_without_intercept(levels)

In [8]: print(contrast.matrix)
[[0. 0. 0.]
 [1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

Here we used reference=0, which implies that the first level, Hispanic, is the reference category against which the other level effects are measured. As mentioned above, the columns do not sum to zero and are thus not independent of the intercept. To be explicit, let’s look at how this would encode the race variable.

In [9]: contrast.matrix[hsb2.race-1, :][:20]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [9], line 1
----> 1 contrast.matrix[hsb2.race-1, :][:20]

NameError: name 'hsb2' is not defined

This is a bit of a trick, as the race category conveniently maps to zero-based indices. If it does not, this conversion happens under the hood, so this will not work in general but nonetheless is a useful exercise to fix ideas. The below illustrates the output using the three contrasts above

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

In [11]: mod = ols("write ~ C(race, Treatment)", data=hsb2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [11], line 1
----> 1 mod = ols("write ~ C(race, Treatment)", data=hsb2)

NameError: name 'hsb2' is not defined

In [12]: res = mod.fit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [12], line 1
----> 1 res = mod.fit()

NameError: name 'mod' is not defined

In [13]: print(res.summary())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [13], line 1
----> 1 print(res.summary())

NameError: name 'res' is not defined

We explicitly gave the contrast for race; however, since Treatment is the default, we could have omitted this.

Simple Coding

Like Treatment Coding, Simple Coding compares each level to a fixed reference level. However, with simple coding, the intercept is the grand mean of all the levels of the factors. See User-Defined Coding for how to implement the Simple contrast.

In [14]: contrast = Simple().code_without_intercept(levels)

In [15]: print(contrast.matrix)
[[-0.25 -0.25 -0.25]
 [ 0.75 -0.25 -0.25]
 [-0.25  0.75 -0.25]
 [-0.25 -0.25  0.75]]

In [16]: mod = ols("write ~ C(race, Simple)", data=hsb2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [16], line 1
----> 1 mod = ols("write ~ C(race, Simple)", data=hsb2)

NameError: name 'hsb2' is not defined

In [17]: res = mod.fit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [17], line 1
----> 1 res = mod.fit()

NameError: name 'mod' is not defined

In [18]: print(res.summary())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [18], line 1
----> 1 print(res.summary())

NameError: name 'res' is not defined

Sum (Deviation) Coding

Sum coding compares the mean of the dependent variable for a given level to the overall mean of the dependent variable over all the levels. That is, it uses contrasts between each of the first k-1 levels and level k In this example, level 1 is compared to all the others, level 2 to all the others, and level 3 to all the others.

In [19]: from patsy.contrasts import Sum

In [20]: contrast = Sum().code_without_intercept(levels)

In [21]: print(contrast.matrix)
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]
 [-1. -1. -1.]]

In [22]: mod = ols("write ~ C(race, Sum)", data=hsb2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [22], line 1
----> 1 mod = ols("write ~ C(race, Sum)", data=hsb2)

NameError: name 'hsb2' is not defined

In [23]: res = mod.fit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [23], line 1
----> 1 res = mod.fit()

NameError: name 'mod' is not defined

In [24]: print(res.summary())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [24], line 1
----> 1 print(res.summary())

NameError: name 'res' is not defined

This corresponds to a parameterization that forces all the coefficients to sum to zero. Notice that the intercept here is the grand mean where the grand mean is the mean of means of the dependent variable by each level.

In [25]: hsb2.groupby('race')['write'].mean().mean()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [25], line 1
----> 1 hsb2.groupby('race')['write'].mean().mean()

NameError: name 'hsb2' is not defined

Backward Difference Coding

In backward difference coding, the mean of the dependent variable for a level is compared with the mean of the dependent variable for the prior level. This type of coding may be useful for a nominal or an ordinal variable.

In [26]: from patsy.contrasts import Diff

In [27]: contrast = Diff().code_without_intercept(levels)

In [28]: print(contrast.matrix)
[[-0.75 -0.5  -0.25]
 [ 0.25 -0.5  -0.25]
 [ 0.25  0.5  -0.25]
 [ 0.25  0.5   0.75]]

In [29]: mod = ols("write ~ C(race, Diff)", data=hsb2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [29], line 1
----> 1 mod = ols("write ~ C(race, Diff)", data=hsb2)

NameError: name 'hsb2' is not defined

In [30]: res = mod.fit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [30], line 1
----> 1 res = mod.fit()

NameError: name 'mod' is not defined

In [31]: print(res.summary())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [31], line 1
----> 1 print(res.summary())

NameError: name 'res' is not defined

For example, here the coefficient on level 1 is the mean of write at level 2 compared with the mean at level 1. Ie.,

In [32]: res.params["C(race, Diff)[D.1]"]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [32], line 1
----> 1 res.params["C(race, Diff)[D.1]"]

NameError: name 'res' is not defined

In [33]: hsb2.groupby('race').mean()["write"].loc[2] - \
   ....:     hsb2.groupby('race').mean()["write"].loc[1]
   ....: 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [33], line 1
----> 1 hsb2.groupby('race').mean()["write"].loc[2] - \
      2     hsb2.groupby('race').mean()["write"].loc[1]

NameError: name 'hsb2' is not defined

Helmert Coding

Our version of Helmert coding is sometimes referred to as Reverse Helmert Coding. The mean of the dependent variable for a level is compared to the mean of the dependent variable over all previous levels. Hence, the name ‘reverse’ being sometimes applied to differentiate from forward Helmert coding. This comparison does not make much sense for a nominal variable such as race, but we would use the Helmert contrast like so:

In [34]: from patsy.contrasts import Helmert

In [35]: contrast = Helmert().code_without_intercept(levels)

In [36]: print(contrast.matrix)
[[-1. -1. -1.]
 [ 1. -1. -1.]
 [ 0.  2. -1.]
 [ 0.  0.  3.]]

In [37]: mod = ols("write ~ C(race, Helmert)", data=hsb2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [37], line 1
----> 1 mod = ols("write ~ C(race, Helmert)", data=hsb2)

NameError: name 'hsb2' is not defined

In [38]: res = mod.fit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [38], line 1
----> 1 res = mod.fit()

NameError: name 'mod' is not defined

In [39]: print(res.summary())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [39], line 1
----> 1 print(res.summary())

NameError: name 'res' is not defined

To illustrate, the comparison on level 4 is the mean of the dependent variable at the previous three levels taken from the mean at level 4

In [40]: grouped = hsb2.groupby('race')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [40], line 1
----> 1 grouped = hsb2.groupby('race')

NameError: name 'hsb2' is not defined

In [41]: grouped.mean()["write"].loc[4] - grouped.mean()["write"].loc[:3].mean()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [41], line 1
----> 1 grouped.mean()["write"].loc[4] - grouped.mean()["write"].loc[:3].mean()

NameError: name 'grouped' is not defined

As you can see, these are only equal up to a constant. Other versions of the Helmert contrast give the actual difference in means. Regardless, the hypothesis tests are the same.

In [42]: k = 4

In [43]: 1./k * (grouped.mean()["write"].loc[k] - grouped.mean()["write"].loc[:k-1].mean())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [43], line 1
----> 1 1./k * (grouped.mean()["write"].loc[k] - grouped.mean()["write"].loc[:k-1].mean())

NameError: name 'grouped' is not defined

In [44]: k = 3

In [45]: 1./k * (grouped.mean()["write"].loc[k] - grouped.mean()["write"].loc[:k-1].mean())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [45], line 1
----> 1 1./k * (grouped.mean()["write"].loc[k] - grouped.mean()["write"].loc[:k-1].mean())

NameError: name 'grouped' is not defined

Orthogonal Polynomial Coding

The coefficients taken on by polynomial coding for k=4 levels are the linear, quadratic, and cubic trends in the categorical variable. The categorical variable here is assumed to be represented by an underlying, equally spaced numeric variable. Therefore, this type of encoding is used only for ordered categorical variables with equal spacing. In general, the polynomial contrast produces polynomials of order k-1. Since race is not an ordered factor variable let’s use read as an example. First we need to create an ordered categorical from read.

In [46]: _, bins = np.histogram(hsb2.read, 3)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [46], line 1
----> 1 _, bins = np.histogram(hsb2.read, 3)

NameError: name 'hsb2' is not defined

In [47]: try: # requires numpy main
   ....:     readcat = np.digitize(hsb2.read, bins, True)
   ....: except:
   ....:     readcat = np.digitize(hsb2.read, bins)
   ....: 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [47], line 2
      1 try: # requires numpy main
----> 2     readcat = np.digitize(hsb2.read, bins, True)
      3 except:

NameError: name 'hsb2' is not defined

During handling of the above exception, another exception occurred:

NameError                                 Traceback (most recent call last)
Cell In [47], line 4
      2     readcat = np.digitize(hsb2.read, bins, True)
      3 except:
----> 4     readcat = np.digitize(hsb2.read, bins)

NameError: name 'hsb2' is not defined

In [48]: hsb2['readcat'] = readcat
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [48], line 1
----> 1 hsb2['readcat'] = readcat

NameError: name 'readcat' is not defined

In [49]: hsb2.groupby('readcat').mean()['write']
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [49], line 1
----> 1 hsb2.groupby('readcat').mean()['write']

NameError: name 'hsb2' is not defined
In [50]: from patsy.contrasts import Poly

In [51]: levels = hsb2.readcat.unique().tolist()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [51], line 1
----> 1 levels = hsb2.readcat.unique().tolist()

NameError: name 'hsb2' is not defined

In [52]: contrast = Poly().code_without_intercept(levels)

In [53]: print(contrast.matrix)
[[-0.6708  0.5    -0.2236]
 [-0.2236 -0.5     0.6708]
 [ 0.2236 -0.5    -0.6708]
 [ 0.6708  0.5     0.2236]]

In [54]: mod = ols("write ~ C(readcat, Poly)", data=hsb2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [54], line 1
----> 1 mod = ols("write ~ C(readcat, Poly)", data=hsb2)

NameError: name 'hsb2' is not defined

In [55]: res = mod.fit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [55], line 1
----> 1 res = mod.fit()

NameError: name 'mod' is not defined

In [56]: print(res.summary())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [56], line 1
----> 1 print(res.summary())

NameError: name 'res' is not defined

As you can see, readcat has a significant linear effect on the dependent variable write but not a significant quadratic or cubic effect.

User-Defined Coding

If you want to use your own coding, you must do so by writing a coding class that contains a code_with_intercept and a code_without_intercept method that return a patsy.contrast.ContrastMatrix instance.

In [57]: from patsy.contrasts import ContrastMatrix
   ....: 
   ....: def _name_levels(prefix, levels):
   ....:     return ["[%s%s]" % (prefix, level) for level in levels]
   ....: 

In [58]: class Simple(object):
   ....:     def _simple_contrast(self, levels):
   ....:         nlevels = len(levels)
   ....:         contr = -1./nlevels * np.ones((nlevels, nlevels-1))
   ....:         contr[1:][np.diag_indices(nlevels-1)] = (nlevels-1.)/nlevels
   ....:         return contr
   ....: 
   ....:     def code_with_intercept(self, levels):
   ....:         contrast = np.column_stack((np.ones(len(levels)),
   ....:                                    self._simple_contrast(levels)))
   ....:         return ContrastMatrix(contrast, _name_levels("Simp.", levels))
   ....: 
   ....:    def code_without_intercept(self, levels):
   ....:        contrast = self._simple_contrast(levels)
   ....:        return ContrastMatrix(contrast, _name_levels("Simp.", levels[:-1]))
   ....: 
  File <tokenize>:13
    def code_without_intercept(self, levels):
    ^
IndentationError: unindent does not match any outer indentation level


In [60]: mod = ols("write ~ C(race, Simple)", data=hsb2)
   ....: res = mod.fit()
   ....: print(res.summary())
   ....: 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [60], line 1
----> 1 mod = ols("write ~ C(race, Simple)", data=hsb2)
      2 res = mod.fit()
      3 print(res.summary())

NameError: name 'hsb2' is not defined