1
$\begingroup$

The accepted answer to this question on SO accomplishes exactly what I need: Comparing all factor levels to the grand mean: can I tweak contrasts in linear model fitting to show all levels?

However, I don't understand exactly what the process is doing. Could you explain how sum-to-zero contrast coding is being used for this purpose?

$\endgroup$
8
  • $\begingroup$ Can you explain what you want to know a bit more precisely? For one, there are easier ways to calculate contrasts (in R). Do you want a short explanation of the SO code (in which case -- this question might be off-topic). Or do you want a short explanation of contrasts? $\endgroup$
    – dipetkov
    Commented Jan 7, 2023 at 14:09
  • $\begingroup$ Does this or this answer help your understanding? $\endgroup$
    – statmerkur
    Commented Jan 7, 2023 at 14:20
  • $\begingroup$ Take a look at the contrast package and its vignette or the emmeans package and its vignette about contrasts. In most cases there is no need to calculate contrasts explicitly. $\endgroup$
    – dipetkov
    Commented Jan 7, 2023 at 18:47
  • $\begingroup$ @dipetkov- I understand what contrasts are. I don't understand the process they used so that we can compare all levels to the grand mean, rather than leaving one level as the reference level. $\endgroup$
    – Adam_G
    Commented Jan 7, 2023 at 21:27
  • $\begingroup$ So let's say on the balance you are interested in an explanation of the SO answer code. $\endgroup$
    – dipetkov
    Commented Jan 8, 2023 at 1:49

1 Answer 1

1
+50
$\begingroup$

Say that we have a vector $\begin{bmatrix} x_A & x_B & x_C \end{bmatrix}$ for the categories, where the $x_i$ can take values 0 or 1,

  • then the model without contrasts is:

    $$\hat{y}(x_A,x_B,x_C) = \mu + \beta_1 \begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix} \cdot \begin{bmatrix} x_A\\ x_B \\ x_C \end{bmatrix} + \beta_2 \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} x_A\\ x_B \\ x_C \end{bmatrix} = \mu + \beta_1 x_B + \beta_2 x_C$$

    or for the cases where exactly one of the values $x$ is equal to 1

    $$\hat{y}(x_A,x_B,x_C) = \begin{cases} \mu & \quad \text{if $x_A= 1$, $x_B = 0$ and $x_C = 0$}\\ \mu + \beta_1 & \quad \text{if $x_A= 0$, $x_B = 1$ and $x_C = 0$}\\ \mu + \beta_2 & \quad \text{if $x_A= 0$, $x_B = 0$ and $x_C = 1$}\\ \end{cases}$$

  • then the model with contrasts is:

    $$\hat{y}(x_A,x_B,x_C) = \mu + \beta_1 \begin{bmatrix} 1 \\ 0 \\ -1 \end{bmatrix} \cdot \begin{bmatrix} x_A\\ x_B \\ x_C \end{bmatrix} + \beta_2 \begin{bmatrix} 0 \\ 1 \\ -1 \end{bmatrix} \cdot \begin{bmatrix} x_A\\ x_B \\ x_C \end{bmatrix} = \mu + \beta_1 (x_A-x_C) + \beta_2 (x_B-x_C)$$

    or for the cases where exactly one of the values $x$ is equal to 1

    $$\hat{y}(x_A,x_B,x_C) = \begin{cases} \mu + \beta_1 & \quad \text{if $x_A= 1$, $x_B = 0$ and $x_C = 0$}\\ \mu + \beta_2 & \quad \text{if $x_A= 0$, $x_B = 1$ and $x_C = 0$}\\ \mu - \beta_1 - \beta_2 & \quad \text{if $x_A= 0$, $x_B = 0$ and $x_C = 1$}\\ \end{cases}$$

However, I don't understand exactly what the process is doing. Could you explain how sum-to-zero contrast coding is being used for this purpose?

What this second model, with the contrasts, is doing is that you get that the intercept $\mu$ is equal to the sum of the estimate for the three groups $$\hat{y}(1,0,0) + \hat{y}(0,1,0) + \hat{y}(0,0,1) = \mu$$ So the intercept is now the mean of the groups (instead of the mean of the group A).


Inference.

In the output table of lm only $\beta_1$ and $\beta_2$ are shown, and you also want to know whether $-\beta_1-\beta_2$ is significant. This is what you need the covariance matrix for (to compute the standard error of $-\beta_1-\beta_2$). It accounts for the fact that the distribution of the estimated $\beta_1$ and $\beta_2$ are correlated. Say that an individual from group C in your sample has a larger value, then this has an effect on both $\beta_1$ and $\beta_2$, the two estimates are not independent.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.