Dummy Coding

Learning Objectives

At the end of this lab, you will:

  1. Understand how to specify a baseline/reference level for categorical variables
  2. Understand how to specify dummy coding
  3. Interpret the output from a model using dummy coding
  4. Understand how to specify contrasts to test specific effects

What You Need

  1. Be up to date with lectures

Required R Packages

Remember to load all packages within a code chunk at the start of your RMarkdown file using library(). If you do not have a package and need to install, do so within the console using install.packages(" "). For further guidance on installing/updating packages, see Section C here.

For this lab, you will need to load the following package(s):

  • tidyverse
  • psych
  • kableExtra
  • emmeans

Presenting Results

All results should be presented following APA guidelines.If you need a reminder on how to hide code, format tables/plots, etc., make sure to review the rmd bootcamp.

The example write-up sections included as part of the solutions are not perfect - they instead should give you a good example of what information you should include and how to structure this. Note that you must not copy any of the write-ups included below for future reports - if you do, you will be committing plagiarism, and this type of academic misconduct is taken very seriously by the University. You can find out more here.

Lab Data

You can download the data required for this lab here or read it in via this link https://uoepsy.github.io/data/caffeinedrink.csv

Study Overview

Research Question

Does WPM differ by caffeine treatment condition?

To investigate if the number of words typed per minute (WPM) differs among caffeine treatment conditions, the researchers conducted an experiment where participants were randomly allocated to one of four treatment conditions. Two of these conditions included non-caffeinated drinks - control (water) and mint tea, and the other two caffeinated drinks - coffee and red bull.

Drink Caffeine Temp
Control (Water) No Cold
Red Bull Yes Cold
Coffee Yes Hot
Mint Tea No Hot

In addition to the above research question, the researchers were also interested in the following:

Comparisons

  • Whether having some kind of caffeine (i.e., red bull or coffee), rather than no caffeine (i.e., control - water or mint tea), resulted in a difference in average WPM

  • Whether there was a difference in average WPM between those with hot drinks (i.e., mint tea / coffee) in comparison to those with cold drinks (control - water / red bull)

Caffeine data codebook

Setup

Setup
  1. Create a new RMarkdown file
  2. Load the required package(s)
  3. Read the caffeinedrink dataset into R, assigning it to an object named caffeine

#load packages
library(tidyverse)
library(psych)
library(kableExtra)
library(emmeans)

#read in data
caffeine <- read_csv("https://uoepsy.github.io/data/caffeinedrink.csv")

Exercises

Study & Analysis Plan Overview

Question 1

Examine the dataset, and perform any necessary and appropriate data management steps.

  • The str() function will return the overall structure of the dataset, this can be quite handy to look at
  • Convert categorical variables to factors, and if needed, provide better variable names*
  • Label factors appropriately to aid with your model interpretations if required*
  • Check that the dataset is complete (i.e., are there any NA values?). We can check this using is.na()
  • Are scores within possible ranges (e.g., if we recorded people’s age, it would be impossible to have someone aged -31!)

*See the Overview (numeric outcomes & categorical predictors) flashcard.

Let’s have a look at the data to see what we’re working with:

#first look at dataset structure
str(caffeine)
spc_tbl_ [40 × 2] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ treatment: chr [1:40] "control" "control" "control" "control" ...
 $ wpm      : num [1:40] 109 114 113 110 116 ...
 - attr(*, "spec")=
  .. cols(
  ..   treatment = col_character(),
  ..   wpm = col_double()
  .. )
 - attr(*, "problems")=<externalptr> 
#now lets look at top 6 rows (or the head) of the dataset
head(caffeine)
# A tibble: 6 × 2
  treatment   wpm
  <chr>     <dbl>
1 control    109.
2 control    114.
3 control    113.
4 control    110.
5 control    116.
6 control    113.
#check for NAs - there are none - all FALSE
table(is.na(caffeine))

FALSE 
   80 

Let’s start with the ‘treatment’ condition variable. This should be coded as factor (<fctr>), but can see from above it is currently coded as a character (<chr>). Let’s correct this.

#Code treatment as a factor
caffeine <- caffeine %>%
  mutate(treatment = as_factor(treatment))

Next, let’s look at the ‘wpm’ variable. Here we want to check for impossible values. Although we don’t know if there is a maximum possible WPM, we do know that we cannot have a negative WPM.

# all looks ok - min and max both positive values
describe(caffeine$wpm)
   vars  n   mean   sd median trimmed  mad    min    max range skew kurtosis
X1    1 40 113.59 2.92 113.06  113.51 3.62 107.94 120.24  12.3 0.22    -0.84
     se
X1 0.46

All participant data was complete (no missing values), with WPM scores within possible ranges. Treatment was coded as a factor with four levels - control (water), coffee, red bull, and mint tea.


Question 2

Choose an appropriate reference level for the Treatment condition.

The Treatment factor has a group coded ‘Control (Water)’ which lends itself naturally to be the reference category.

#set 'Control' caffeine treatment condition as our reference group 
caffeine$treatment <- relevel(caffeine$treatment, "control")

#check levels - control should be first in the list
levels(caffeine$treatment)
[1] "control"  "coffee"   "red_bull" "mint_tea"


Question 3

Provide a brief overview of the study design and data, before detailing your analysis plan to address the research question.

  • Give the reader some background on the context of the study
  • State what type of analysis you will conduct in order to address the research question
  • Specify the model to be fitted to address the research question (note that you will need to specify the reference level of your categorical variable)
  • Specify your chosen significance (\(\alpha\)) level
  • State your hypotheses

Much of the information required can be found in the Study Overview codebook. The statistical models flashcards may also be useful to refer to.

The caffeine dataset contained information on 40 hypothetical participants who took part in an experiment examining whether the number of words typed per minute (WPM) differed among caffeine treatment conditions. Using a between-subjects design, the researchers collected information on participants’ WPM (average number of words typed per minute), and which one of four treatment conditions they were randomly assigned to (Control (Water), Coffee, Mint Tea, or Red Bull).

Boxplots will be used to visualise the associations among WPM and caffeine treatment conditions. To address the research question of whether WPM differs by caffeine treatment condition, we first need to define the dummy variables for Treatment:

\[ \text{Treatment}_\text{Coffee} = \begin{cases} 1 & \text{if Treatment is Coffee} \\ 0 & \text{otherwise} \end{cases} \quad \]

\[ \text{Treatment}_\text{Red Bull} = \begin{cases} 1 & \text{if Treatment is Red Bull} \\ 0 & \text{otherwise} \\ \end{cases} \quad \]

\[ \text{Treatment}_\text{Mint Tea} = \begin{cases} 1 & \text{if Treatment is Mint Tea} \\ 0 & \text{otherwise} \end{cases} \quad \]

\[ (\text{Control (Water) is base level}) \]

Based on the above dummy coding, we are going to fit the following regression model:

\[ \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} \]

Effects will be considered statistically significant at \(\alpha=.05\)

Our hypotheses are:

\(H_0:\) All \(\beta_j = 0\) (for \(j = 1, 2, 3\))

There are no differences in WPM based on caffeine treatment condition.

\(H_1:\) At least one \(\beta_j \neq 0\) (for \(j = 1, 2, 3\))

There are differences in WPM based on caffeine treatment condition.

Descriptive Statistics & Visualisations

Question 4

Provide a table of descriptive statistics and visualise your data.

Remember to interpret your plot in the context of the study (i.e., comment on any observed differences among treatment groups).

Review the many ways to numerically and visually explore your data by reading over the data exploration flashcards.

For examples, see flashcards on descriptives statistics tables - categorical and numeric values examples and data visualisation - bivariate examples, paying particular attention to the type of data that you’re working with.

Make sure to comment on any observed differences among the sample means of the four treatment conditions.

Descriptive statistics presented in a well formatted table, grouped by treatment condition:

caffeine %>%
  group_by(treatment) %>%
  summarise(n = n(), 
            Mean = mean(wpm), 
            SD = sd(wpm),
            Minimum = min(wpm),
            Maximum = max(wpm)) %>%
    kable(caption = "Descriptive Statistics", digits = 2) %>%
    kable_styling()
Table 1: Descriptive Statistics
Descriptive Statistics
treatment n Mean SD Minimum Maximum
control 10 112.15 1.98 109.43 116.23
coffee 10 114.48 1.82 112.07 117.16
red_bull 10 116.65 2.15 113.00 120.24
mint_tea 10 111.09 2.13 107.94 115.82

Since we have a continuous outcome and a categorical predictor - a boxplot would be most appropriate for visualisations:

ggplot(data = caffeine, aes(x = treatment, y = wpm, fill = treatment)) +
  geom_boxplot() +
  labs(x = 'Treatment Condition', y = 'WPM')
Figure 1: Association between Treatment Conditions and WPM
  • From the boxplots, it seems that those in the Red Bull condition, on average, typed the most WPM, whilst those in the Mint Tea condition the fewest.
  • Overall, the average WPM appears to be lower for those in the non-caffeine conditions (i.e., control - water / mint tea) in comparison to those in the caffeine drinks condition (red bull / coffee).

Model Fitting & Interpretation

Question 5

Fit the specified model, and assign it the name “caf_mdl1”.

Interpret your coefficients in the context of the study.

We can fit our multiple regression model using the lm() function. For a recap, see the statistical models flashcards.

Recall that R computes the dummy variables for us. Thus, each row in the summary() output of the model will correspond to one of the estimated \(\beta\)’s in the equation above. For a more in-depth recap, see the numeric outcomes & categorical predictors flashcards

#fit model
caf_mdl1 <- lm(wpm ~ treatment, data=caffeine)
#check model output
summary(caf_mdl1)

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

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 *  
treatmentred_bull   4.5060     0.9053   4.977 1.61e-05 ***
treatmentmint_tea  -1.0550     0.9053  -1.165   0.2516    
---
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

Let’s first map our coefficients and estimates:

Coefficient Estimate Relation between \(\beta\) and \(\mu\)
(Intercept) 112.1460 \(\beta_0 = \hat \mu_1\)
treatmentcoffee 2.3350 \(\beta_0 + \beta_1 = \hat \mu_2\)
treatmentred_bull 4.5060 \(\beta_0 + \beta_2 = \hat \mu_3\)
treatmentmint_tea -1.0550 \(\beta_0 + \beta_3 = \hat \mu_4\)
  • The estimate corresponding to (Intercept) contains \(\hat \beta_0 = \hat \mu_1 = 112.15\). The estimated average WPM for those in the control condition (water) was approximately 112.15.

  • The next estimate corresponds to treatmentcoffee and was \(\hat \beta_1 = 2.34\). The difference in mean WPM between Control and Coffee was estimated to be \(2.34\). Thus, \(\hat \mu_2 = 112.15 + 2.34 = 114.49\). In other words, people who have had coffee typed approximately 114.49 words, which was 2.34 words per minute more than those who have had water. This difference was statistically significant \((p = .01)\).

  • The estimate corresponding to treatmentred_bull was \(\hat \beta_2 = 4.51\). This was the estimated difference in mean WPM between Control and Red Bull, estimated to be \(4.51\). Thus, \(\hat \mu_3 = 112.15 + 4.51 = 116.66\). In other words, people who had red bull typed approximately 116.66 words, 4.51 words per minute more than those who had water. This difference was statistically significant \((p < .001)\).

  • The estimate corresponding to treatmentmint_tea was \(\hat \beta_3 = -1.06\). This was the estimated difference in mean WPM between Control and Mint Tea, estimated to be \(-1.06\). Thus, \(\hat \mu_4 = 112.15 + (-1.06) = 111.09\). In other words, people who had mint tea typed approximately 111.09 words, 1.06 words per minute less than those who had water. This difference was not statistically significant \((p = .25)\).

Planned Comparisons / Contrasts

Question 6

Formally state the two planned comparisons that the researchers were interested in as testable hypotheses.

Recall that the researchers were also interested in addressing these two questions:

  1. Whether having some kind of caffeine (i.e., red bull or coffee), rather than no caffeine (i.e., control - water or mint tea), resulted in a difference in average WPM
  2. Whether there was a difference in average WPM between those with hot drinks (i.e., mint tea / coffee) in comparison to those with cold drinks (control - water / red bull)

We can specify the two hypotheses as follows:

\[ \begin{aligned} 1. \quad \text{H}_0 &: \mu_\text{No Caffeine} = \mu_\text{Caffeine} \\ \quad \text{H}_0 &: \frac{1}{2} (\mu_\text{Control} + \mu_\text{Mint Tea}) = \frac{1}{2} (\mu_\text{Coffee} + \mu_\text{Red Bull}) \\ \\ \quad \text{H}_1 &: \mu_\text{No Caffeine} \neq \mu_\text{Caffeine} \\ \quad \text{H}_1 &: \frac{1}{2} (\mu_\text{Control} + \mu_\text{Mint Tea}) \neq \frac{1}{2} (\mu_\text{Coffee} + \mu_\text{Red Bull}) \\ \\ \end{aligned} \]

\[ \begin{aligned} 2. \quad H_0 &: \mu_\text{Hot Drink} = \mu_\text{Cold Drink} \\ \quad H_0 &: \frac{1}{2} (\mu_\text{Coffee} + \mu_\text{Mint Tea}) = \frac{1}{2} (\mu_\text{Control} + \mu_\text{Red Bull}) \\ \\ \quad H_1 &: \mu_\text{Hot Drink} \neq \mu_\text{Cold Drink} \\ \quad H_1 &: \frac{1}{2} (\mu_\text{Coffee} + \mu_\text{Mint Tea}) \neq \frac{1}{2} (\mu_\text{Control} + \mu_\text{Red Bull}) \\ \end{aligned} \]


Question 7

After checking the levels of the factor treatment, use emmeans() to obtain the estimated treatment means and uncertainties for your factor.

levels(caffeine$treatment)
[1] "control"  "coffee"   "red_bull" "mint_tea"

Use the emmeans() to get the estimated means of our groups:

treatment_mean <- emmeans(caf_mdl1, ~ treatment)
treatment_mean
 treatment emmean   SE df lower.CL upper.CL
 control      112 0.64 36      111      113
 coffee       114 0.64 36      113      116
 red_bull     117 0.64 36      115      118
 mint_tea     111 0.64 36      110      112

Confidence level used: 0.95 
plot(treatment_mean)


Question 8

Specify the coefficients of the comparisons and run the contrast analysis, obtaining 95% confidence intervals.

See the manual contrasts flashcards.

Remember that ordering matters here - look again at the output of levels(caffeine$treatment) as this will help you when assigning your weights.

As shown above via levels(), the ordering of the treatment factor is:

  1. Control (no caffeine / cold drink)
  2. Coffee (caffeine / hot drink)
  3. Red Bull (caffeine / cold drink)
  4. Mint Tea (no caffeine / hot drink)

From this ordering, we can specify our weights - based on the hypothesis, lets assign positive values to the no caffeine and hot drink conditions:

treatment_comp <- list("No Caffeine - Caffeine" = c(1/2, -1/2, -1/2, 1/2),
             "Hot Drink - Cold Drink" = c(-1/2, 1/2, -1/2, 1/2)
             )

Now lets run our contrast analysis and get confidence intervals - to do so we use the contrast() function from emmeans():

#run contrast analysis
treatment_comp_test <- contrast(treatment_mean, method = treatment_comp)

#examine output
treatment_comp_test
 contrast               estimate   SE df t.ratio p.value
 No Caffeine - Caffeine    -3.95 0.64 36  -6.167  <.0001
 Hot Drink - Cold Drink    -1.61 0.64 36  -2.520  0.0163
#obtain confidence intervals
confint(treatment_comp_test)
 contrast               estimate   SE df lower.CL upper.CL
 No Caffeine - Caffeine    -3.95 0.64 36    -5.25   -2.650
 Hot Drink - Cold Drink    -1.61 0.64 36    -2.91   -0.315

Confidence level used: 0.95 


Question 9

Interpret the results of the contrast analysis in the context of the researchers hypotheses.

We performed a test against \(H_0: \frac{1}{2}(\mu_1 + \mu_4) - \frac{1}{2}(\mu_2 + \mu_3) = 0\). At the 5% significance level, there was evidence that the mean WPM for those who were in the no caffeine condition was significantly different from those in a caffeine condition \((t(36) = -6.17, p < .001, \text{two-sided})\), and this difference was estimated to be -3.95. We are 95% confident that those who consumed no caffeine typed, on average, between 2.7 and 5.3 words less per minute than those who consumed some form of caffeine \(CI_{95}[-5.25, -2.65]\).

We performed a test against \(H_0: \frac{1}{2}(\mu_2 + \mu_4) - \frac{1}{2}(\mu_1 + \mu_3) = 0\). At the 5% significance level, there was evidence that the average WPM for those in the hot drink condition significantly differed from those in the cold drink condition \((t(36) = -2.52, p = .02, \text{two-sided})\), and this difference was estimated to be -1.61. We are 95% confident that those who consumed a hot drink typed, on average, between 0.3 and 2.9 words less per minute than those who consumed a cold drink \(CI_{95}[-2.91, -0.32]\).

Study Design

Question 10

For each of the below experiment descriptions, note (1) the design, (2) number of variables of interest, (3) levels of categorical variables, (4) what you think the reference group should be and why.

A group of researchers were interested in whether sleep deprivation influenced reaction time. They hypothesised that sleep deprived individuals would have slower reaction times than non-sleep deprived individuals.

To test this, they recruited 60 participants who were matched on a number of demographic variables including age and sex. One member of each pair (e.g., female, aged 18) was placed into a different sleep condition - ‘Sleep Deprived’ (4 hours per night) or ‘Non-Sleep Deprived’ (8 hours per night).

A group of researchers were interested in replicating an experiment testing the Stroop Effect.

They recruited 50 participants who took part in Task A (word colour and meaning are congruent) and Task B (word colour and meaning are incongruent) where they were asked to name the color of the ink instead of reading the word. The order of presentation was counterbalanced across participants. The researchers hypothesised that participants would take significantly more time (‘response time’ measured in seconds) to complete Task B than Task A.

You can test yourself here for fun: Stroop Task

A group of researchers wanted to test a hypothesised theory according to which patients with amnesia will have a deficit in explicit memory but not implicit memory. Huntingtons patients, on the other hand, will display the opposite: they will have no deficit in explicit memory, but will have a deficit in implicit memory.

To test this, researchers designed a study that included two variables: ‘Diagnosis’ (Amnesic, Huntingtons, Control) and ‘Task’ (Grammar, Classification, Recognition) where participants were randomly assigned to a Task condition. The first two tasks (Grammar and Classification) are known to reflect implicit memory processes, whereas the Recognition task is known to reflect explicit memory processes.

  1. Design = Between-person: Matched pairs
  2. Number of variables of interest = 2 - Sleep Condition and Reaction Time
  3. Levels of variables = Sleep Condition has 2 levels - Sleep Deprived and Non-Sleep Deprived; Reaction Time is a continuous measure, so has no associated levels
  4. Reference Group = Sleep Condition - Non-Sleep Deprived because the research question stated that the researchers were interested in how the sleep deprived group differed from the non-sleep deprived group.
  1. Design = Within-person: Repeated measures (this is a study design that you will learn more about in DAPR3!)
  2. Number of variables of interest = 2 - Task and Response Time
  3. Levels of variables = Task has 2 levels - A and B; Response Time is a continuous measure, so has no associated levels
  4. Reference Group = Task - A because the research question stated that the researchers were interested in how response time in Task B differed from the response time in Task A.
  1. Design = Between-person: 3×3 factorial design
  2. Number of variables of interest = 2 - Diagnosis and Task
  3. Levels of variables = Diagnosis has 3 levels - Amnesic, Huntingtons, and Control; Task has 3 levels - Grammar, Classification, and Recognition
  4. Reference Groups = Diagnosis - Control; Task - Recognition. We have chosen Control since the other two groups have some form of cognitive impairment; and the Recognition task since it measures explicit memory whilst the other two task types implicit.

Compile Report

Compile Report

Knit your report to PDF, and check over your work. To do so, you should make sure:

  • Only the output you want your reader to see is visible (e.g., do you want to hide your code?)
  • Check that the tinytex package is installed
  • Ensure that the ‘yaml’ (bit at the very top of your document) looks something like this:
---
title: "this is my report title"
author: "B1234506"
date: "07/09/2024"
output: bookdown::pdf_document2
---

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

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

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

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

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

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

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

You must make sure you have tinytex installed in R so that you can “Knit” your Rmd document to a PDF file:

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

You should end up with a PDF file. If you have followed the above instructions and still have issues with knitting, speak with a Tutor.