Sum coding


Data Analysis for Psychology in R 2

Dr. Elizabeth Pankratz (elizabeth.pankratz@ed.ac.uk)


Department of Psychology
University of Edinburgh
2026–2027

Course overview


Introduction to linear models
(with Dr. Patrick Sturt)
Introduction to linear regression
Interpreting linear models
Testing predictors and evaluating linear models
F-tests and model comparison
Practice analysis: Linear models
Extending linear models
(with Dr. Elizabeth Pankratz)
Categorical predictors and treatment coding
Sum coding
Assumptions and diagnostics
Bootstrapping and confidence intervals
Practice analysis: Categorical predictors (led by Patrick)
Interactions
(with Dr. Elizabeth Pankratz)
Mean-centering and numeric/categorical interactions
Numeric/numeric interactions
Categorical/categorical interactions
Testing simple effects and correcting for multiple comparisons
Practice analysis: Interactions (led by Patrick)
Logistic regression
(with Dr. Elizabeth Pankratz)
Probabilities and log-odds
Modelling binary outcomes with logistic regression
Interactions, assumptions, diagnostics, comparisons
Practice analysis: Logistic regression (led by Patrick)
Report feedback, exam prep, full-course Q&A

Warm-up: Think back to treatment coding


With a treatment-coded categorical predictor, what do a model’s intercept and slope mean?


Code
xlim_lower <- -2.2
xlim_upper <-  2.2
ylim_lower <- -15
ylim_upper <-  55

set.seed(1)  # seed for constant jitter
p_xy_study <- score_data |>
  mutate(study_num = ifelse(study == 'alone', 0, 1)) |>
  ggplot(aes(x = study_num, y = score)) +
  geom_jitter(aes(colour = study), height = 0, alpha = 0.25, width = 0.1, size = 5) +
  scale_x_continuous(limits = c(xlim_lower, xlim_upper), expand = c(0, 0)) +
  scale_y_continuous(limits = c(ylim_lower, ylim_upper), expand = c(0, 0)) +
  geom_segment(x = xlim_lower, xend = xlim_upper, y = 0, yend = 0,
               arrow = arrow(ends = 'both', length = unit(12, 'pt')), colour = 'black') +
  geom_segment(x = 0, xend = 0, y = ylim_lower, yend = ylim_upper, 
               arrow = arrow(ends = 'both', length = unit(12, 'pt')), colour = 'black') +
  stat_summary(fun = mean, geom = 'point', colour = 'black', size = 8, show.legend = FALSE) +

  scale_colour_manual(values = pal) +
  theme(
    panel.grid.minor = element_blank(),
    legend.position = 'bottom'
  ) +
  labs(
    x = 'study (in numeric space)'
  ) +
 guides(colour = guide_legend(override.aes = list(alpha = 1))) + 
  geom_abline(slope = mean_others - mean_alone, intercept = mean_alone, colour = 'black', linewidth = 2) +
  NULL

p_xy_study

This week’s learning objectives


The most common contrast coding scheme is treatment coding. Another common one is sum coding. What is sum coding, and how is it different from treatment coding?

When we code predictors using sum coding, how do we interpret the linear model’s coefficients?

In sum coding, what hypotheses are tested for each coefficient?

Building an analysis workflow

Coefficients and null hypotheses (H0s)


Intercept \(\beta_0\) Slope \(\beta_j\)
Meaning H0 Meaning H0
Treatment coding Estimated mean outcome of reference level Estimated mean outcome of ref. level = 0 Non-ref. level mean minus ref. level mean Non-ref. level mean minus ref. level mean = 0
Sum coding


Sum coding for binary predictors

Sum coding: Another way to represent categorical predictors as numbers


Treatment coding:

  • Studying alone is coded as 0.
  • Studying with others is coded as 1.

By default, the alphabetically first level is coded as 0.

Sum coding:

  • Studying alone is coded as 1.
  • Studying with others is coded as –1.

By default, the alphabetically first level is coded as 1.

Same data as last week: Two study patterns

Code
score_data |>
  ggplot(aes(x = study, y = score, fill = study, colour = study)) +
  geom_violin(alpha = 0.5) +
  geom_jitter(height = 0, alpha = 0.5, width = 0.2, size = 5) +
  theme(legend.position = 'none') +
  scale_fill_manual(values = pal) +
  scale_colour_manual(values = pal) +
  stat_summary(fun = mean, geom = 'point', colour = 'black', size = 8) +
  NULL

Same data represented as different numbers

Treatment coding (from last week) uses 0 and 1.

Sum coding (this week) uses 1 and –1.

In sum coding,

  • the line’s intercept represents the score exactly between the group means, the line’s y value at x = 0.
    • This value is called the grand mean.
  • the line’s slope is the mean score for the group coded as 1, minus the grand mean.

Defining sum coding in R

R uses treatment coding by default.

contrasts(score_data$study)
       others
alone       0
others      1


To sum-code our predictor, we use the function contr.sum().

contrasts(score_data$study) <- contr.sum(2)  # 2 because there are 2 levels
contrasts(score_data$study) <- contr.sum     # or leave out the (2), also works

contrasts(score_data$study)
       [,1]
alone     1
others   -1


If we use this variable as-is, the model coefficient would be named study1. That’s not super helpful for interpretation. Something like studyalone would be better.

Here’s an R trick to relabel the variable as alone, the level coded as 1:

dimnames(contrasts(score_data$study))[[2]] <- 'alone'
contrasts(score_data$study)
       alone
alone      1
others    -1

Model score ~ study

\[ \text{score} = \beta_0 + (\beta_1 \cdot \text{study}_\text{alone}) + \epsilon \]

m1 <- lm(score ~ study, data = score_data)
summary(m1)

Call:
lm(formula = score ~ study, data = score_data)

Residuals:
    Min      1Q  Median      3Q     Max 
-22.846  -5.417   0.154   5.393  18.154 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   25.727      0.504   51.07  < 2e-16 ***
studyalone    -2.881      0.504   -5.72  3.1e-08 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 7.88 on 248 degrees of freedom
Multiple R-squared:  0.116, Adjusted R-squared:  0.113 
F-statistic: 32.7 on 1 and 248 DF,  p-value: 3.09e-08

What does each coefficient represent?

            Estimate Std. Error t value  Pr(>|t|)
(Intercept)    25.73      0.504   51.07 1.34e-133
studyalone     -2.88      0.504   -5.72  3.09e-08

What does each coefficient represent?

            Estimate Std. Error t value  Pr(>|t|)
(Intercept)    25.73      0.504   51.07 1.34e-133
studyalone     -2.88      0.504   -5.72  3.09e-08


What does each coefficient represent?

            Estimate Std. Error t value  Pr(>|t|)
(Intercept)    25.73      0.504   51.07 1.34e-133
studyalone     -2.88      0.504   -5.72  3.09e-08


(Intercept) aka \(\beta_0\):

  • 25.73 is the grand mean outcome: the mean of the group mean outcomes.
m1_grand_mean <- c(
  mean_others, 
  mean_alone
) |> mean()

m1_grand_mean
[1] 25.7


studyalone aka \(\beta_1\):

  • -2.88 is the difference between the mean outcome of the level coded as 1, alone, and the grand mean outcome.
mean_alone - m1_grand_mean
[1] -2.88

Coefficients and null hypotheses (H0s)


Intercept \(\beta_0\) Slope \(\beta_j\)
Meaning H0 Meaning H0
Treatment coding Mean outcome of reference level Mean outcome of ref. level = 0 Non-ref. level mean minus ref. level mean Non-ref. level mean minus ref. level mean = 0
Sum coding Grand mean (mean of all group mean outcomes) Mean of level coded as 1 minus grand mean

What hypotheses does sum coding test?


            Estimate Std. Error t value Pr(>|t|)    
 (Intercept)   25.727      0.504   51.07  < 2e-16 ***
 studyalone    -2.881      0.504   -5.72  3.1e-08 ***


(Intercept):

  • Null hypothesis: The grand mean is equal to zero.
  • \(p\)-value: the probability of observing a grand mean of 25.727 (or a value more extreme), assuming that the true grand mean is zero.

Can we reject this null hypothesis?


studyalone:

  • Null hypothesis: The mean score of alone minus the grand mean is equal to zero.
  • \(p\)-value: the probability of observing a difference of -2.881 (or a value more extreme), assuming that the true difference is zero.

Can we reject this null hypothesis?

Coefficients and null hypotheses (H0s)


Intercept \(\beta_0\) Slope \(\beta_j\)
Meaning H0 Meaning H0
Treatment coding Mean outcome of reference level Mean outcome of ref. level = 0 Non-ref. level mean minus ref. level mean Non-ref. level mean minus ref. level mean = 0
Sum coding Grand mean (mean of all group mean outcomes) Grand mean = 0 Mean of level coded as 1 minus grand mean Mean of level coded as 1 minus grand mean = 0


In general, the H0 being tested for a given parameter is always “this parameter is equal to 0”.

What questions do you have right now?


Sum coding for >2 levels

Same data as last week: Three study methods

Code
set.seed(1)
p_viol_method <- score_data |>
  ggplot(aes(x = method, y = score, fill = method, colour = method)) +
  geom_violin(alpha = 0.5) +
  geom_jitter(height = 0, alpha = 0.5, width = 0.2, size = 5) +
  theme(legend.position = 'none') +
  scale_fill_manual(values = pal) +
  scale_colour_manual(values = pal) +
  stat_summary(fun = mean, geom = 'point', colour = 'black', size = 5) +
  NULL
p_viol_method

Sum coding has dummy variables too

contrasts(score_data$method) <- contr.sum(3)
contrasts(score_data$method)
          [,1] [,2]
read         1    0
summarise    0    1
self-test   -1   -1


Like in treatment coding, each column contains one dummy variable.

  1. Column 1 compares read (the 1 in that column) to the grand mean.
  2. Column 2 compares summarise (the 1 in that column) to the grand mean.


Naming the dummy variables “1” and “2” isn’t really helpful, so here’s that R trick to rename those columns more informatively:

dimnames(contrasts(score_data$method))[[2]] <- c('read', 'summ')
contrasts(score_data$method)
          read summ
read         1    0
summarise    0    1
self-test   -1   -1

Sum coding has no “reference level”. Instead, the level coded as all –1 is the “dropped level”.

No slope is estimated for the dropped level

contrasts(
  score_data$method
)
          read summ
read         1    0
summarise    0    1
self-test   -1   -1

The thick black line shows the grand mean = the model’s intercept.

The little red vertical lines represents each dummy variable’s slope.

Note: once sum coding uses >2 levels, the line between group means does not give us the correct intercept or slope anymore :(

The mathematical model formulation for a
sum coded predictor

contrasts(score_data$method)
          read summ
read         1    0
summarise    0    1
self-test   -1   -1


Like in treatment coding, each dummy variable gets its own \(\beta\):

\[ \text{score} = \beta_0 + (\beta_1 \cdot \text{method}_\text{read}) + (\beta_2 \cdot \text{method}_\text{summ}) + \epsilon \]


In R:

m2 <- lm(score ~ method, data = score_data)

Challenge: Guess the coefficients

Here’s what the coefficients mean in sum coding:

  • Intercept = Grand mean (mean of all group mean outcomes)
  • Slope = Mean of level coded as 1 minus grand mean (i.e., group mean – grand mean)

Mean of read:

mean_read
[1] 23.4

Mean of self-test:

mean_self
[1] 23.6

Mean of summarise:

mean_summ
[1] 28.2
contrasts(score_data$method)
          read summ
read         1    0
summarise    0    1
self-test   -1   -1

This slide has all the information you need to guess the coefficient values for the model score ~ method.

Work individually or with your neighbour(s).

  • What’s the value of the intercept?
  • There’ll be a predictor called methodread. What is its value?
  • There’ll also be a predictor called methodsumm. What is its value?

Model score ~ method

summary(m2)

Call:
lm(formula = score ~ method, data = score_data)

Residuals:
    Min      1Q  Median      3Q     Max 
-23.414  -5.359  -0.196   5.750  17.804 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   25.062      0.518   48.41  < 2e-16 ***
methodread    -1.648      0.720   -2.29    0.023 *  
methodsumm     3.134      0.702    4.47 0.000012 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 8.08 on 247 degrees of freedom
Multiple R-squared:  0.0756,    Adjusted R-squared:  0.0681 
F-statistic: 10.1 on 2 and 247 DF,  p-value: 0.0000605


confint(m2)
            2.5 % 97.5 %
(Intercept) 24.04  26.08
methodread  -3.07  -0.23
methodsumm   1.75   4.52

Interpret coefficients

 Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
 (Intercept)   25.062      0.518   48.41  < 2e-16 ***
 methodread    -1.648      0.720   -2.29    0.023 *  
 methodsumm     3.134      0.702    4.47 0.000012 ***


  • (Intercept):
    • The grand mean score across all study methods is 25.06 points.
  • methodread:
    • Studying by reading is associated with a score 1.65 points below the grand mean.
  • methodsumm:
    • Studying by summarising is associated with a score 3.13 points above the grand mean.

There’s no coefficient for self-test because it’s the dropped level (coded as all –1s).

Having a dropped level is weird, but it’s mathematically necessary for sum coding to work.

When to do treatment coding vs. sum coding

When to do treatment coding vs. sum coding


The choice of contrast coding scheme depends on your RQ and the hypotheses you want to test.

  • If you are interested in how one level of a categorical predictor differs from another: use treatment coding.
  • If you are interested in how one level of a categorical predictor differs from the grand mean: use sum coding.


Let’s practice: I’ll show you a few different research questions, and you vote on which contrast coding scheme to use.

Building an analysis workflow

What questions do you have right now?


Back matter

Revisiting this week’s learning objectives

The most common contrast coding scheme is treatment coding. Another common one is sum coding. What is sum coding, and how is it different from treatment coding?

  • Treatment coding uses 0/1, and one level of the predictor (the one coded as 0) is the reference level.
  • Sum coding uses –1/1, and there is no reference level. The level coded as –1 is the “dropped” level.
  • Outside of DAPR, you might also see “sum coding” called “effects coding”, “deviation coding”, or “sum-to-zero coding”.)

When we code predictors using sum coding, how do we interpret the linear model’s coefficients?

  • Intercept: The grand mean of the outcome (grand mean = the mean of every group’s mean).
  • Slope: The mean of the group coded as 1 minus the grand mean.

In sum coding, what hypotheses are tested for each coefficient?

  • The intercept’s H0: The grand mean of the outcome is equal to zero.
  • The slopes’ H0: The mean of the group coded as 1 minus the grand mean is equal to zero (if multiple regression: holding other predictors constant).

This week


Tasks:


Work on exercises in labs


Complete the weekly quiz

Get support:


Consult the flash cards


Ask questions anonymously on Piazza


We really like seeing you in office hours!

Prediction equations: Sum coding, three levels

The linear expression telling us model predictions:

\[ \text{outcome}= \beta_0 + (\beta_1 \cdot \text{Predictor1}) + (\beta_2 \cdot \text{Predictor2}) \]

We can combine the estimated betas to compute the means of each level. To do this, we plug in the values for Predictor1 and Predictor2 that correspond to each level of the three-level variable. We get these values from the rows of our our coding matrix.


contr.sum(3)
  [,1] [,2]
1    1    0
2    0    1
3   -1   -1

Level 1 is represented as Predictor1 = 1, Predictor2 = 0
(first row of contrast matrix).

\[ \begin{align} \text{outcome}_{\text{level 1}} &= \beta_0 + (\beta_1 \cdot \text{Predictor1}) + (\beta_2 \cdot \text{Predictor2})\\ &= \beta_0 + (\beta_1 \cdot 1) + (\beta_2 \cdot 0)\\ &= \beta_0 + \beta_1\\ \end{align} \]

Prediction equations: Sum coding, three levels

contr.sum(3)
  [,1] [,2]
1    1    0
2    0    1
3   -1   -1

Level 2 is represented as Predictor1 = 0, Predictor2 = 1
(second row of contrast matrix).

\[ \begin{align} \text{outcome}_{\text{level 2}} &= \beta_0 + (\beta_1 \cdot \text{Predictor1}) + (\beta_2 \cdot \text{Predictor2})\\ &= \beta_0 + (\beta_1 \cdot 0) + (\beta_2 \cdot 1)\\ &= \beta_0 + \beta_2\\ \end{align} \]


Level 3 is represented as Predictor1 = -1, Predictor2 = -1
(third row of contrast matrix).

\[ \begin{align} \text{outcome}_{\text{level 3}} &= {\beta_0} + ({\beta_1} \cdot \text{Predictor1}) + ({\beta_2} \cdot \text{Predictor2})\\ &= {\beta_0} + ({\beta_1} \cdot -1) + ({\beta_2} \cdot -1)\\ &= {\beta_0} - {\beta_1} - {\beta_2}\\ &= {\beta_0} - ({\beta_1} + {\beta_2})\\ \end{align} \]

Why do we have a dropped level?

Why can’t we just compare each group individually to the overall mean?

Such a model might look something like this:

\[ \text{score} = \beta_0 + (\beta_1 \cdot \text{read}) + (\beta_2 \cdot \text{selftest}) + (\beta_3 \cdot \text{summarise}) + \epsilon \]

Each score is given by an intercept plus the difference between that intercept and each group’s scores. The mean \(\mu\) of each group would be:

\[ \begin{align} \mu_\text{read} &= \beta_0 + (\beta_1 \cdot \text{read})\\ \mu_\text{selftest} &= \beta_0 + (\beta_2 \cdot \text{selftest})\\ \mu_\text{summarise} &= \beta_0 + (\beta_3 \cdot \text{summarise})\\ \end{align} \]

For technical reasons, we cannot model this data this way.

We have three group means: \(\mu_\text{read}\), \(\mu_\text{selftest}\), \(\mu_\text{summarise}\). And for mathematical reasons, that means we can only have three parameters in our model. But the model above has four: \(\beta_0\), \(\beta_1\), \(\beta_2\), \(\beta_3\).

In technical terms, we say the model above is overparameterised.


Treatment coding with a reference level is one way to achieve three parameters for three group means: one intercept for the reference level and two differences between groups for the other two levels.

Sum coding with a dropped level is another way to achieve three parameters for three group means: one intercept for the grand mean, which leaves over only two possible comparisons.