Exercise 14: Mediation#

This homework assignment is designed to give you practice with mediation. You won’t need to load in any data for this homework – we’ll simulate data instead.


1. Simulating data (3 points)#

In this homework, we’re going to simulate the relationship between age, word reading experience, and reading comprehension skill. Older children tend to be better at reading comprehension tasks, but this isn’t just an inherent part of getting older. It’s a combination of gaining more experience with reading, oral language skills, and executive functions development. In this homework, though, we’re going to simplify this relationship down to a direct effect of age on reading comprehension (since improvements in oral language skills and executive functions are more guaranteed as children age than reading practice is), and a mediation of word reading experience.

In graphical form: picture

Writing the relationship out formally:

\[x = \beta_{xa} a + \beta_{x0} + \epsilon_{x}\]
\[c = \beta_{ca} a + \beta_{cx} x + \beta_{c0} + \epsilon_c\]

\(c\) is reading comprehension, \(x\) is word reading experience, and \(a\) is age. \(\beta_{c0}\) and \(\beta_{x0}\) are intercept terms for their respective formulas, and \(\epsilon_{c}\) and \(\epsilon_{x}\) are gaussian noise terms. We’ll start by writing a function, simulate_data, that can simulate this relationship. The input values are set and the function structure is provided below. Complete the function such that it takes in those input variables and generates \(age\), \(x\), and \(c\) values. Use runif() to generate ages. Use rnorm() to generate the noise terms.

sample_size = 100 # How many children in data set?
age_lo = 80     # minimum age, in months
age_hi = 200    # maximum age, in months
beta_xa = 0.5   # amount by which experience changes for increase of one month in age
beta_x0 = -5    # amount of experience when age = 0 (not interpretable, since minimum age for this data is 80 months)
sd_x = 50       # standard dev of gaussian noise term, epsilon_x
beta_ca = 0.8   # amount that comprehension score improves for every increase of one unit in age
beta_cx = 3     # amount that comprehension score improves for every increase of one unit in reading experience
beta_c0 = 10    # comprehension score when reading experience is 0.
sd_c = 85      # standard dev of gaussian noise term, epsilon_c

simulate_data <- function(sample_size, age_lo, age_hi, beta_xa,
                          beta_x0, sd_x, beta_ca, beta_cx, beta_c0, sd_c) {
      # WRITE YOUR CODE HERE

}

#dat <- simulate_data(sample_size, age_lo, age_hi, beta_xa, beta_x0, sd_x, beta_ca, beta_cx, beta_c0, sd_c)
#head(dat)

2. Visualizing Data (2 point)#

Load the tidyverse library.

# WRITE YOUR CODE HERE

a) Create a scatter plot showing the relationship between age and reading comprehension.

# WRITE YOUR CODE HERE

b) Create a scatter plot showing the relationship among age, reading experience, and reading comprehension.

Hint: You can represent reading comprehension using an additional visual dimension such as the color or size of the points.

# WRITE YOUR CODE HERE

3. Mediation Analysis (4 points)#

Load the mediation library.

# WRITE YOUR CODE HERE

Use the mediate function to determine whether x mediates the relationship between age and c. Use summary() to print the model results. Hint: see the mediation and moderation tutorial.

# WRITE YOUR CODE HERE

5. Reflection (1 point)#

Is there a significant direct or indirect effect? What do you conclude based on the analyses above?

Write your response here

DUE: 11:59pm EST, March 26, 2026

IMPORTANT Did you collaborate with anyone on this assignment? If so, list their names here.

Someone’s Name