DAPR2 Lab Exercises
  • Block 1: Intro LM
    • 01: Simple linear regression
    • 02: Multiple regression
    • 03: Significance tests
    • 04: Model comparison
    • 05: Practice write-up
  • Block 2: Extending LM
    • 06: Treatment coding
    • 07: Sum coding
    • 08: Assumptions and diagnostics
  • Block 3: Interactions
  • Block 4: Logistic regression

On this page

  • Get to know the data
  • Think about treatment coding
  • Set up the model
  • A new reference level
  • Optional: Knit your report to PDF

06: Treatment coding

This week, you’ll practice implementing and interpreting a treatment-coded categorical predictor with four levels.

NoteGet set up
  1. Open RStudio.
  2. Create a new .Rmd file for this week’s exercises.
  3. Save it somewhere you can find it again.
  4. Give it a clear name (for example, dapr2_lab06.Rmd).
  5. In the first code chunk, load the packages you’ll need this week (and install them if you don’t have them already):
    • tidyverse

To investigate if typing speed, measured in words per minute (WPM), differs between people who’ve consumed different kinds of beverages, the researchers conducted an experiment where 40 participants were each randomly allocated to one of four beverage conditions.

The data in caffeinedrink.csv contained two attributes collected from \(n=40\) participants:

variable description
treatment which one of four treatment conditions the participant was assigned - control (water), coffee, mint tea, or red bull
wpm average number of words typed per minute

Get to know the data

Question 1

Read in the data from https://uoepsy.github.io/data/caffeinedrink.csv and store it in a variable named wpm_data.

Solution

Solution 1.

wpm_data <- read_csv("https://uoepsy.github.io/data/caffeinedrink.csv")

Question 2

As you know, a quick-and-dirty way to plot a numeric outcome as a function of a categorical predictor is a boxplot:

wpm_data |>
  ggplot(aes(x = treatment, y = wpm)) +
  geom_boxplot()

But by summarising the data into median and inter-quartile range, you don’t actually see the information that’s helpful for linear modelling, where we care about group means, not medians. (And sometimes the distribution of data is just totally misrepresented; read more here if you’re curious).

Elizabeth’s favourite alternative is the violin plot (a kind of vertical density plot, which shows the lumps and bumps of the data’s distribution more faithfully) with jittered points over top (so that we can see each actual observed data point), and with each group’s mean shown as a big black dot.

To make a violin plot that shows how wpm varies based on beverage treatment, copy the following code template, replace the ... bits with the appropriate variable names, and create your own violin plot.

... |>
  ggplot(aes(x = ..., y = ...)) +
  geom_violin() +
  geom_jitter(height = 0, alpha = 0.5) +
  stat_summary(fun = mean, geom = 'point', size = 4)

(Once you have this code running, if you’re not sure what part of it does, then comment out (that is, put a hashtag # at the start of the line) or delete that bit, try running what’s left over, and see what changes.)

🗂️ See Exploring data visually > Violin plot flash card.

Solution

Solution 2.

wpm_data |>
  ggplot(aes(x = treatment, y = wpm)) +
  geom_violin() +
  geom_jitter(height = 0, alpha = 0.5) +
  stat_summary(fun = mean, geom = 'point', size = 4)


Bonus, if you want to get fancy with colour:

wpm_data |>
  ggplot(aes(x = treatment, y = wpm, fill = treatment, colour = treatment)) +
  geom_violin(alpha = 0.5) +
  geom_jitter(alpha = 0.5) +
  stat_summary(fun = mean, geom = 'point', colour = 'black', size = 4) +
  theme(legend.position = 'none') 

Think about treatment coding

Question 3

Which of the four levels of treatment do you think would be a suitable reference level? Write down which level you’d choose, and write one sentence to describe why you chose that one.

🗂️ See Treatment coding flash card.

Solution

Solution 3. I personally would make the reference level the control level of the variable treatment, which represents people just drinking water.

I think this is an appropriate reference level because we can think of water as the “default” or “baseline” beverage. The other beverages (coffee, mint tea, and red bull) are all more specific, and it makes the most sense to compare the most default/general case to each of the more specific cases.

Question 4

Based on the reference level you chose above, you’ll now try to predict what the contrast matrix for the treatment variable will look like. (You’ll get R to generate the actual contrast matrix later, and you can check your predictions then.)

Here is some code for creating a Markdown table. Copy this code to your own Rmd file, placing it outside a code chunk (in other words: treat it as if it’s text, not R code).

|            | ... | ... | ... |
| ---------- | --- | --- | --- |
| `coffee`   | ... | ... | ... |
| `control`  | ... | ... | ... |
| `mint_tea` | ... | ... | ... |
| `red_bull` | ... | ... | ... |

Your task is to replace all the ... bits in this table with appropriate values.

Specifically, fill in the column names with the appropriate levels of treatment, and fill in the other empty cells with 0s and 1s.

🗂️ See Treatment coding > Three-level predictor flash card.

Solution

Solution 4. If the reference level is control, then we know a couple things about how the contrast matrix must look:

  • the columns, which represent the three dummy variables, will be named after the other three levels;
  • the row labelled control will be filled with 0s all the way across.
coffee mint_tea red_bull
coffee … … …
control 0 0 0
mint_tea … … …
red_bull … … …


Further, each of the non-reference level rows will contain a 1 in the column with the same name.

coffee mint_tea red_bull
coffee 1 … …
control 0 0 0
mint_tea … 1 …
red_bull … … 1


And finally, every remaining ... will be replaced by a 0.

coffee mint_tea red_bull
coffee 1 0 0
control 0 0 0
mint_tea 0 1 0
red_bull 0 0 1


Here’s the code for the finished Markdown table:

|            | `coffee` | `mint_tea` | `red_bull` |
| ---------- | -------- | ---------- | ---------- |
| `coffee`   |        1 |          0 |          0 |
| `control`  |        0 |          0 |          0 |
| `mint_tea` |        0 |          1 |          0 |
| `red_bull` |        0 |          0 |          1 |

Question 5

Now you’ll check your work from the previous question.

Using mutate(), convert treatment into a factor and reorder the levels so that your chosen reference level comes first.

🗂️ See Treatment coding > Changing reference level flash card.

Then, run the following code to produce the contrast matrix:

contrasts(wpm_data$treatment)

Check: Did your predictions match this output? (Don’t worry if the rows are in a different order! Just pay attention to the column names and where the 0s and 1s are placed.)

Solution

Solution 5.

wpm_data <- wpm_data |>
  mutate(
    treatment = factor(treatment, levels = c('control', 'coffee', 'mint_tea', 'red_bull'))
  )

contrasts(wpm_data$treatment)
         coffee mint_tea red_bull
control       0        0        0
coffee        1        0        0
mint_tea      0        1        0
red_bull      0        0        1

The rows here are in a different order than in the Markdown table in Q4 above. That’s because the contrast matrix always orders the rows based on the current order of factor levels, with the reference level at the top.

We reordered the factor levels at the beginning of this step, so the order of the rows in the contrast matrix we created here will match the new order of levels, not the old one.

Set up the model

Question 6

Write the mathematical model formulation for the model that we’re about to fit, using the dummy variables from the contrast matrix generated by R in Q5.

🗂️ See Treatment coding > Mathematical model formulation flash card.

Solution

Solution 6. \[ \text{WPM} = \beta_0 + (\beta_1 \cdot \text{Treatment}_\text{Coffee}) + (\beta_2 \cdot \text{Treatment}_\text{Red Bull}) + (\beta_3 \cdot \text{Treatment}_\text{Mint Tea}) + \epsilon \]

Write:

$$
\text{WPM} = \beta_0 + 
(\beta_1 \cdot \text{Treatment}_\text{Coffee}) + 
(\beta_2 \cdot \text{Treatment}_\text{Red Bull}) + 
(\beta_3 \cdot \text{Treatment}_\text{Mint Tea}) + 
\epsilon
$$


If you want line breaks mid-expression but you still want to keep everything nicely aligned, try this:

\[ \begin{align} \text{WPM} ~=~& \beta_0 + (\beta_1 \cdot \text{Treatment}_\text{Coffee}) + (\beta_2 \cdot \text{Treatment}_\text{Red Bull}) + \\ & (\beta_3 \cdot \text{Treatment}_\text{Mint Tea}) + \epsilon \end{align} \]

Write:

\begin{align}
\text{WPM} ~=~& \beta_0 + (\beta_1 \cdot \text{Treatment}_\text{Coffee}) + (\beta_2 \cdot \text{Treatment}_\text{Red Bull}) ~ + \\
& (\beta_3 \cdot \text{Treatment}_\text{Mint Tea}) + \epsilon
\end{align}

(Depending on your operating system, you might need to write aligned instead of align.)


Note that the following mathematical model formulation is incorrect.

\[ \text{WPM} = \beta_0 + (\beta_1 \cdot \text{Treatment}) + \epsilon \] If treatment were continuous, it would be fine. But because treatment is categorical with four different levels, the model must contain three dummy variables and the corresponding number of \(\beta\) coefficients. (The number of \(\beta\)s in the equation should always match the number of coefficients produced by the model summary.)

Question 7

The model you’ll fit will have four coefficients: one intercept and three slopes which each compare one of the non-reference levels back to the reference level.

Based on the plot you created back in Q2, write down your best guess about the approximate value that each of the model’s four coefficients will have.

If you’re really not sure, at least write down whether you think each coefficient will be positive or negative.

After you fit the model, you’ll check your predictions. (I know this is a hard task, but I promise that making these predictions will help your understanding in the long run.)

🗂️ See Treatment coding > Model fit and interpretation flash card.

Solution

Solution 7. For convenience, here’s the plot again (notice how the levels of treatment are now reordered, so that the reference level control comes first!):

wpm_data |>
  ggplot(aes(x = treatment, y = wpm, fill = treatment, colour = treatment)) +
  geom_violin(alpha = 0.5) +
  geom_jitter(alpha = 0.5) +
  stat_summary(fun = mean, geom = 'point', colour = 'black', size = 3) +
  theme(legend.position = 'none') 

I would guess that the four coefficients will be:

  • (Intercept) \(\beta_0\):
    • The estimated mean of the reference level control, which appears to be about 112 WPM.
  • treatmentcoffee \(\beta_1\):
    • Estimated WPM for coffee minus estimated WPM for control (i.e., the difference between coffee and control), which appears to be a bit less than 3 WPM.
  • treatmentmint_tea \(\beta_2\):
    • Estimated WPM for mint_tea minus estimated WPM for control, which appears to be about –1 WPM.
  • treatmentred_bull \(\beta_3\):
    • Estimated WPM for red_bull minus estimated WPM for control, which appears to be just under 5 WPM.

Question 8

Now fit the model in R and display the model summary.

Check: Were your predictions about the coefficients correct?

If they were, celebrate! If not, try to figure out how you can update your thinking to better match the results (and ask a tutor/ask on Piazza if you’re not sure!).

🗂️ See Treatment coding > Model fit and interpretation flash card.

Solution

Solution 8.

wpm_m1 <- lm(wpm ~ treatment, data = wpm_data)
summary(wpm_m1)

Call:
lm(formula = wpm ~ treatment, data = wpm_data)

Residuals:
   Min     1Q Median     3Q    Max 
-3.652 -1.362 -0.151  1.125  4.729 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
(Intercept)       112.1460     0.6402 175.179  < 2e-16 ***
treatmentcoffee     2.3350     0.9053   2.579   0.0141 *  
treatmentmint_tea  -1.0550     0.9053  -1.165   0.2516    
treatmentred_bull   4.5060     0.9053   4.977 1.61e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.024 on 36 degrees of freedom
Multiple R-squared:  0.5563,    Adjusted R-squared:  0.5194 
F-statistic: 15.05 on 3 and 36 DF,  p-value: 1.651e-06

A new reference level

Question 9

For these last two questions, imagine changing the reference level to coffee (or if you already used coffee as your reference level, imagine changing it to something else).

If the reference level were updated to coffee, what would the model’s coefficients be estimating?

Using the plot again, visually estimate the approximate value (or at least the sign: positive or negative) of all four model coefficients.

🗂️ See Treatment coding > Model fit and interpretation flash card.

Solution

Solution 9. For convenience, here’s the plot again:

wpm_data |>
  ggplot(aes(x = treatment, y = wpm, fill = treatment, colour = treatment)) +
  geom_violin(alpha = 0.5) +
  geom_jitter(alpha = 0.5) +
  stat_summary(fun = mean, geom = 'point', colour = 'black', size = 3) +
  theme(legend.position = 'none')

The four coefficients will be:

  • (Intercept):
    • The mean of the reference level coffee, which appears to be just above 114 WPM.
  • treatmentcontrol:
    • WPM for control minus WPM for coffee (i.e., the difference between control and coffee), which appears to be between –2 and –3.
  • treatmentmint_tea:
    • The difference between mint_tea and coffee, which appears to be a bit less than –4 WPM.
  • treatmentred_bull:
    • The difference between red_bull and coffee, which appears to be about 2 WPM.

Question 10

Now you’ll actually be changing the reference level and seeing what else changes. For this whole question, we recommend copying and pasting your code from earlier and just modifying the relevant bits of it—that way you don’t have to retype everything!

First: change the reference level of treatment to coffee (or if you already used coffee as your reference level, change it to something else).

Next, generate the contrast matrix to verify your new reference level.

🗂️ See Treatment coding > Changing reference level flash card.

Then re-fit the model and display the model summary.

🗂️ See Treatment coding > Model fit and interpretation flash card.

Check: Were your predictions from Q9 supported? Why or why not?

Optional but recommended (your future self will thank you!): After checking your predictions, write a little guide for yourself, in your own words, about how to interpret treatment-coded predictors.

Solution

Solution 10.

wpm_data <- wpm_data |>
  mutate(
    treatment = factor(treatment, levels = c('coffee', 'control', 'mint_tea', 'red_bull'))
  )

contrasts(wpm_data$treatment)
         control mint_tea red_bull
coffee         0        0        0
control        1        0        0
mint_tea       0        1        0
red_bull       0        0        1

Good! coffee is the reference level. We can tell because it’s got 0s all the way across its row.

wpm_m2 <- lm(wpm ~ treatment, data = wpm_data)
summary(wpm_m2)

Call:
lm(formula = wpm ~ treatment, data = wpm_data)

Residuals:
   Min     1Q Median     3Q    Max 
-3.652 -1.362 -0.151  1.125  4.729 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
(Intercept)       114.4810     0.6402 178.827  < 2e-16 ***
treatmentcontrol   -2.3350     0.9053  -2.579 0.014138 *  
treatmentmint_tea  -3.3900     0.9053  -3.744 0.000631 ***
treatmentred_bull   2.1710     0.9053   2.398 0.021795 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.024 on 36 degrees of freedom
Multiple R-squared:  0.5563,    Adjusted R-squared:  0.5194 
F-statistic: 15.05 on 3 and 36 DF,  p-value: 1.651e-06

Optional: Knit your report to PDF

Note: During the labs, please spend your time on the exercises above, not on rendering your document. This guidance is here in case you have extra time at the end.

Rmarkdown is a useful format because it can be used as a foundation for generating different kinds of documents. For the group report, you’ll be asked to render your Rmd file to PDF. Why not practice now?

First, make sure you have tinytex installed in R. This is what lets you convert things into PDF format.

Run the following code to install tinytex (you’ll only need to run it once).

install.packages("tinytex")
tinytex::install_tinytex()

Then ensure that the “yaml” (rhymes with “mammal”) header—the bit at the top of your Rmd document—looks something like this:

---
title: "this is my report title"
author: "B1234506"
date: "07/09/2025"
output: bookdown::pdf_document2
---

Then follow the instructions from the Rmd bootcamp to knit your Rmd document to PDF.

If you encounter errors, refer to the DAPR1 formatting resources / knitting checklist.

TipWhat to do if you cannot knit to PDF

If you are having issues knitting directly to PDF, try the following:

  • Knit to HTML file
  • Open your HTML in a web-browser (e.g. Chrome, Firefox)
  • Print to PDF (Ctrl+P, then choose to save to PDF)
  • Open file to check formatting
TipHiding code and/or output from code chunks

Review Lesson 5 of the rmd bootcamp for a detailed description/worked examples.

Hiding R Code

To not show the code of an R code chunk, and only show the output, write:

```{r, echo=FALSE}
# code goes here
```

Hiding R Output:

To show the code of an R code chunk, but hide the output, write:

```{r, results='hide'}
# code goes here
```

Hiding R code AND output

To hide both code and output of an R code chunk, write:

```{r, include=FALSE}
# code goes here
```