Today

Does the relationship between age and reaction time differ between groups?

  • Fit a model with different group intercepts and the same age slope.
  • Add an interaction, then interpret the group slopes and predictions.
  • Test whether the added slope difference improves the model.
  • Repeat the workflow independently and compare four model types.

Link To The Lecture

Four Model Shapes

Which model assumes … Match each description to a panel A–D.

  1. Both an effect of x and a constant difference between groups?
  2. An effect of x but no difference between groups?
  3. A difference between groups that changes with x?
  4. A difference between groups but no effect of x?

Start In RStudio

Open your module’s RStudio Project and check that workshop_data is inside it.

  1. Download week_08_workshop.R from NOW.
  2. Move it into your project folder using your file explorer.
  3. Open it in RStudio and wait for each checkpoint.

Keep your own coefficients visible while we discuss figures.

Demonstration Data

Does the association between age and reaction time differ between the two medication groups?

The prepared data are adapted from Blomkvist et al. (2017).

blomkvist <- read_csv("workshop_data/analysis_blomkvist.csv")
blomkvist <- drop_na(blomkvist, rt, age, meds_group)

Use this same complete-case data frame for every model we compare.

Exercise Checkpoint 1

Start Part A1. Outcome: rt; predictors: age and meds_group.

m_vi <- lm(rt ~ age + meds_group,
           data = blomkvist)

This varying-intercept model allows different group intercepts with the same age slope.

Fit the model, print its coefficients, and answer the short counting questions.

coef(m_vi)
            (Intercept) 
             233.679469 
                    age 
               6.949575 
meds_groupnot medicated 
              27.217418 

Interpret The Coefficients

For m_vi, “medicated” is the reference group.

  • (Intercept): reference-group predicted RT at age 0.
  • age: age slope shared by all groups.
  • meds_groupnot medicated: meds_group is the predictor; not medicated is the level. Its coefficient is the not-medicated minus medicated intercept.
coef(m_vi)
            (Intercept) 
             233.679469 
                    age 
               6.949575 
meds_groupnot medicated 
              27.217418 

Different Intercepts, Same Age Slope

coef(m_vi)
            (Intercept) 
             233.679469 
                    age 
               6.949575 
meds_groupnot medicated 
              27.217418 
  • Which coefficient gives the shared slope?
  • Which gives the difference between groups at the same age?
  • Which group does (Intercept) refer to, and what is its value if we extend the age axis to 0?

Add The Interaction

Start Part A2: allow the age slope to differ between the medication groups.

m_vivs <- lm(rt ~ age + meds_group +
               age:meds_group,
             data = blomkvist)

age:meds_group adds a slope difference. Keep both main effects.

The shorthand age * meds_group gives the same model. In Part A2, count the non-intercept coefficients (K), all beta coefficients (including the intercept) and total parameters (including sigma).

coef(m_vivs)
                (Intercept) 
                 161.478460 
                        age 
                   7.942906 
    meds_groupnot medicated 
                 122.516214 
age:meds_groupnot medicated 
                  -1.494283 

Read The Interaction Coefficients

Medicated is the reference group, so (Intercept) and age give this group’s intercept and age slope directly. Differences are not medicated minus medicated:

  • (Intercept) ≈ 161.48: the medicated group’s predicted mean at age 0.
  • age ≈ 7.94: the medicated group’s age slope (ms per year).
  • meds_groupnot medicated: the difference between the groups’ intercepts.
  • age:meds_groupnot medicated: the difference between the groups’ age slopes.

For medicated: predicted mean RT ≈ 161.48 + 7.94 × age.

coef(m_vivs)
                (Intercept) 
                 161.478460 
                        age 
                   7.942906 
    meds_groupnot medicated 
                 122.516214 
age:meds_groupnot medicated 
                  -1.494283 

Build The Not-Medicated Regression Line

Group intercept: (Intercept) + meds_groupnot medicated

\[ 161.48 + 122.52 \approx 284.00. \]

Group slope: age + age:meds_groupnot medicated

\[ 7.94 + (-1.49) \approx 6.45. \]

We now have the intercept and slope for not medicated.

coef(m_vivs)
                (Intercept) 
                 161.478460 
                        age 
                   7.942906 
    meds_groupnot medicated 
                 122.516214 
age:meds_groupnot medicated 
                  -1.494283 

Use The Regression Equation To Predict

The intercepts give predicted reaction times at age 0, outside the observed age range and not very meaningful here. We usually want predictions for both groups at ages of interest.

For either group:

\[ \begin{aligned} \hat{\mu} = {} & \text{group intercept} \\ & {} + \text{group slope} \times \text{age}. \end{aligned} \]

For medicated at age 40:

\[ 161.48 + 7.94 \times 40 \approx 479\text{ ms}. \]

For not medicated at age 40:

\[ 284.00 + 6.45 \times 40 \approx 542\text{ ms}. \]

coef(m_vivs)
                (Intercept) 
                 161.478460 
                        age 
                   7.942906 
    meds_groupnot medicated 
                 122.516214 
age:meds_groupnot medicated 
                  -1.494283 

Where do 284 and 6.45 come from? Neither appears directly in the coefficient output.

Different Intercepts And Different Slopes

coef(m_vivs)
                (Intercept) 
                 161.478460 
                        age 
                   7.942906 
    meds_groupnot medicated 
                 122.516214 
age:meds_groupnot medicated 
                  -1.494283 
  • Which coefficients describe the medicated line’s intercept and slope?
  • Which describe the differences in intercept and slope for not medicated? Show how the lines differ.
  • The interaction coefficient is −1.49. What does this tell us about how the difference in predicted reaction time between groups changes per year of age? Explain using the two lines in the figure.

Exercise Checkpoint 2

Finish Part A2.

  • Calculate both age slopes and the not-medicated intercept.
  • Predict not-medicated RT at age 40 by hand, then check with predict().
  • Compare both groups at ages 40 and 70 in both models.
  • Explain what the interaction coefficient adds to the model.

Does a negative interaction coefficient necessarily mean a negative age slope?

Stop before Part A3.

Short Break

We have allowed different age slopes. After the break, ask:

Does the evidence justify adding the slope difference?

Compare Nested Models

Start Part A3. In Weeks 6–7, the overall F-test assessed a model against a model without predictors.

Here the model-comparison F-test specifically tests the evidence for adding the interaction: does it improve the additive model?

Here both models already include age and medication-group differences. We are testing the added slope difference.

anova(m_vi, m_vivs)
  • Put the simpler model first and use exactly the same observations.
  • lm() fits the models; anova() assesses what the added interaction term contributes.

How Much Do The Added Terms Explain?

anova(m_vi, m_vivs)
Analysis of Variance Table

Model 1: rt ~ age + meds_group
Model 2: rt ~ age + meds_group + age:meds_group
  Res.Df     RSS Df Sum of Sq       F  Pr(>F)
1    169 3577043                             
2    168 3554803  1  22239.67 1.05105 0.30674
  • Adding the interaction reduces RSS by approximately 22,239.67: this is the additional ESS, shown as Sum of Sq. TSS stays the same.
  • F uses the RSS of both models to assess how much the added terms explain, accounting for the extra coefficients and the remaining unexplained variation.
  • A smaller RSS alone is not enough: the F-test asks whether the improvement provides evidence for the added terms.

The second-row p ≈ 0.307 gives insufficient evidence for different slopes; it does not establish that the slopes are identical.

Exercise Checkpoint 3

Finish Part A3 using anova(m_vi, m_vivs).

  • Read F and Pr(>F) from the second row.
  • State the null hypothesis: the two population age slopes are equal.
  • Does the result provide evidence that the interaction improves the model?
  • Use coefficient counts to work out the residual df for each model: n − K − 1.

Both models already allow an age effect and a medication-group difference. This comparison tests the added slope difference.

Independent Practice

Complete Part B.

  1. Three smoking groups: fit models without and with the age interaction; read coefficients, calculate the three slopes and count coefficients/parameters.
  2. Compare the models on the same rows. Interpret the interaction F-test.
  3. Predict mean RT at age 40 for each smoking group.
  4. Four-model R² check: return to the guided medication data and fit the age-only and group-only models. Compare their R² with the two models already fitted.

Bring your comparisons to the debrief. The problem set provides consolidation after the workshop.

Compare The Models’ R²

Formula R²
rt ~ age 0.469
rt ~ meds_group 0.152
rt ~ age + meds_group 0.471
rt ~ age + meds_group + age:meds_group 0.475

Which model has the largest R²? Does that alone justify the interaction?

These are the four model types in the opening figure. Use the interaction comparison to assess the added slope difference.

Discuss Your Output

  • What does the interaction allow the model to do?
  • How did you calculate the age slope for a non-reference group?
  • What does the model-comparison F-test tell you about the group slopes?
  • Why is a smaller RSS or larger raw R² alone insufficient?
  • What increased when you added the interaction: predictor variables, coefficients or parameters?

After The Workshop

Recommended reading: Andrews (2021), Chapter 9, Normal Linear Models.

Complete the Week 8 problem set on NOW after the workshop. Practise distinguishing group differences, slope differences, and predictions.

Core R Functions

Function / syntax What it does
lm() / coef() fit models and extract coefficients
+ / : in formulas add main effects / interactions
tibble() / predict() specify predictor values and predict means
summary() read R²
anova(simple, complex) test additional terms in nested models

Leave With

  • age + group: different intercepts, one shared age slope.
  • age + group + age:group: slopes can differ too.
  • Interpret coefficients relative to the reference group and predictor value 0.
  • Compare nested models on the same rows and state the tested hypothesis.

References

Andrews, M. (2021). Doing data science in R: An Introduction for Social Scientists. SAGE Publications Ltd.

Blomkvist, A. W., Eika, F., Rahbek, M. T., Eikhof, K. D., Hansen, M. D., Søndergaard, M., Ryg, J., Andersen, S., & Jørgensen, M. G. (2017). Reference data on reaction time and aging using the Nintendo Wii Balance Board: A cross-sectional study of 354 subjects from 20 to 99 years of age. PLoS One, 12(12), e0189598.