{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Contrasts Overview" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2021-02-02T06:56:32.647611Z", "iopub.status.busy": "2021-02-02T06:56:32.646316Z", "iopub.status.idle": "2021-02-02T06:56:35.557269Z", "shell.execute_reply": "2021-02-02T06:56:35.557997Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import statsmodels.api as sm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This document is based heavily on this excellent resource from UCLA http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "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.\n", "\n", "To have a look at the contrast matrices in Patsy, we will use data from UCLA ATS. First let's load the data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Example Data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2021-02-02T06:56:35.573790Z", "iopub.status.busy": "2021-02-02T06:56:35.572961Z", "iopub.status.idle": "2021-02-02T06:56:35.867178Z", "shell.execute_reply": "2021-02-02T06:56:35.868479Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "url = 'https://stats.idre.ucla.edu/stat/data/hsb2.csv'\n", "hsb2 = pd.read_table(url, delimiter=\",\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2021-02-02T06:56:35.881101Z", "iopub.status.busy": "2021-02-02T06:56:35.872579Z", "iopub.status.idle": "2021-02-02T06:56:35.966214Z", "shell.execute_reply": "2021-02-02T06:56:35.966837Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " | id | \n", "female | \n", "race | \n", "ses | \n", "schtyp | \n", "prog | \n", "read | \n", "write | \n", "math | \n", "science | \n", "socst | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "70 | \n", "0 | \n", "4 | \n", "1 | \n", "1 | \n", "1 | \n", "57 | \n", "52 | \n", "41 | \n", "47 | \n", "57 | \n", "
1 | \n", "121 | \n", "1 | \n", "4 | \n", "2 | \n", "1 | \n", "3 | \n", "68 | \n", "59 | \n", "53 | \n", "63 | \n", "61 | \n", "
2 | \n", "86 | \n", "0 | \n", "4 | \n", "3 | \n", "1 | \n", "1 | \n", "44 | \n", "33 | \n", "54 | \n", "58 | \n", "31 | \n", "
3 | \n", "141 | \n", "0 | \n", "4 | \n", "3 | \n", "1 | \n", "3 | \n", "63 | \n", "44 | \n", "47 | \n", "53 | \n", "56 | \n", "
4 | \n", "172 | \n", "0 | \n", "4 | \n", "2 | \n", "1 | \n", "2 | \n", "47 | \n", "52 | \n", "57 | \n", "53 | \n", "61 | \n", "
5 | \n", "113 | \n", "0 | \n", "4 | \n", "2 | \n", "1 | \n", "2 | \n", "44 | \n", "52 | \n", "51 | \n", "63 | \n", "61 | \n", "
6 | \n", "50 | \n", "0 | \n", "3 | \n", "2 | \n", "1 | \n", "1 | \n", "50 | \n", "59 | \n", "42 | \n", "53 | \n", "61 | \n", "
7 | \n", "11 | \n", "0 | \n", "1 | \n", "2 | \n", "1 | \n", "2 | \n", "34 | \n", "46 | \n", "45 | \n", "39 | \n", "36 | \n", "
8 | \n", "84 | \n", "0 | \n", "4 | \n", "2 | \n", "1 | \n", "1 | \n", "63 | \n", "57 | \n", "54 | \n", "58 | \n", "51 | \n", "
9 | \n", "48 | \n", "0 | \n", "3 | \n", "2 | \n", "1 | \n", "2 | \n", "57 | \n", "55 | \n", "52 | \n", "50 | \n", "51 | \n", "
\n", " | 1 | \n", "2 | \n", "3 | \n", "4 | \n", "
---|---|---|---|---|
0 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
1 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
2 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
3 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
4 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
195 | \n", "0 | \n", "1 | \n", "0 | \n", "0 | \n", "
196 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
197 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
198 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
199 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "
200 rows × 4 columns
\n", "