Where We Left Off

In Part 1, we fitted normal linear models and inspected coefficient tables.

We focused on questions like:

  • what is the outcome?
  • what are the predictors?
  • what does each coefficient mean?
  • how do estimates and confidence intervals support interpretation?

Today we use the same fitted models, but ask a more concrete question:

What does the model predict for meaningful predictor values?

Today

Today we use fitted linear models to make predictions.

By the end, you should be able to:

  • calculate predicted values from model coefficients
  • use predict() for meaningful predictor values
  • add confidence intervals for predicted means
  • plot observed data and model predictions with ggplot()
  • compare prediction lines from models without and with interactions
  • explain an interaction as a difference between slopes

Why Predictions?

Coefficients are compact, but they can be abstract.

Predictions show what the model expects for meaningful predictor values.

Good prediction plots help readers see:

  • direction of effects
  • size of effects
  • uncertainty
  • relation between model and observed data

Regression Formula Reminder

From Part 1, a linear model has two connected pieces:

\[ y_i \sim \mathcal{N}(\mu_i, \sigma) \]

\[ \mu_i = \beta_0 + \beta_1 x_i \]

For predictions, we use the fitted model:

  • replace \(\beta\) values with estimated coefficients
  • replace predictor values with values we want to predict for
  • calculate \(\hat{\mu}_i\), the predicted mean outcome

Manual Prediction: Continuous Predictor

Example model:

model_age <- lm(rt ~ age, data = blomkvist)
coef(model_age)
(Intercept)         age 
     314.74        5.77 

Formula:

\[ \hat{rt}_i = \hat{\beta}_0 + \hat{\beta}_\text{age} \cdot \text{age}_i \]

To calculate a prediction:

  1. take the estimated intercept
  2. take the estimated age slope
  3. substitute the age value you want to predict for
  4. add the pieces together

Try It: Continuous Predictor

Formula:

\[ \hat{rt}_i = \hat{\beta}_0 + \hat{\beta}_\text{age} \cdot \text{age}_i \]

After running a linear model, we found:

  • intercept = 500
  • age slope = 2

Question:

What is the predicted reaction time for a 40-year-old participant?

Working and answer:

Manual Prediction: Categorical Predictor

Example model:

model_sex <- lm(rt ~ sex, data = blomkvist)
coef(model_sex)
(Intercept)     sexmale 
      664.8       -64.9 

The predictor sex has two levels: female and male.

For categorical predictors, R creates 0/1 dummy codes. By default, levels are ordered alphabetically, so female is the reference level and male is the comparison level:

  • female: \(\text{sexmale}_i = 0\)
  • male: \(\text{sexmale}_i = 1\)

Formula:

\[ \hat{rt}_i = \hat{\beta}_0 + \hat{\beta}_\text{sex} \cdot \text{sexmale}_i \]

Try It: Categorical Predictor

Formula:

\[ \hat{rt}_i = \hat{\beta}_0 + \hat{\beta}_\text{sex} \cdot \text{sexmale}_i \]

After running a linear model, we found:

  • intercept = 550
  • sex slope = 40

Question:

What is the predicted reaction time for a male participant?

Remember: male is coded as \(\text{sexmale}_i = 1\).

Working and answer:

Manual Prediction: Continuous and Categorical

Example model:

model_age_sex <- lm(rt ~ age + sex, data = blomkvist)
coef(model_age_sex)
(Intercept)         age     sexmale 
     339.62        5.74      -59.29 

Formula:

\[ \hat{rt}_i = \hat{\beta}_0 + \hat{\beta}_\text{age} \cdot \text{age}_i + \hat{\beta}_\text{sex} \cdot \text{sexmale}_i \]

The age term changes with age. The sex term adds the group difference for the comparison group.

Try It: Continuous and Categorical

Formula:

\[ \hat{rt}_i = \hat{\beta}_0 + \hat{\beta}_\text{age} \cdot \text{age}_i + \hat{\beta}_\text{sex} \cdot \text{sexmale}_i \]

After running a linear model, we found:

  • intercept = 500
  • age slope = 2
  • sex slope = 40

Question:

What is the predicted reaction time for a 40-year-old male participant?

Remember: male is coded as \(\text{sexmale}_i = 1\).

Working and answer:

Predict By Hand First

For a simple model, predictions come from the coefficients.

coefs <- coef(model_age_sex)
coefs
(Intercept)         age     sexmale 
     339.62        5.74      -59.29 
# Female is the reference group in this data.
coefs[1] + coefs["age"] * 40
(Intercept) 
        569 

predict() is useful because it applies the same logic reliably for many rows and more complex models.

Predict From a Model

Set up a data frame with hypothetical predictor combinations:

newdata <- data.frame(age = c(20, 40, 60),
                      sex = c("female", "female", "female"))

newdata
  age    sex
1  20 female
2  40 female
3  60 female

Predict From a Model

Predicted average outcomes

predict(model_age_sex, newdata = newdata)
  1   2   3 
454 569 684 

Add Confidence Intervals

predict(model_age_sex, newdata = newdata, interval = "confidence")
  fit lwr upr
1 454 415 494
2 569 542 596
3 684 661 707

The confidence interval describes uncertainty in the estimated mean.

Prediction Grid

prediction_grid <- expand.grid(age = seq(20, 80, by = 5),
                               sex = c("female", "male"))

head(prediction_grid)
  age    sex
1  20 female
2  25 female
3  30 female
4  35 female
5  40 female
6  45 female

This creates all combinations of age and sex we want to show.

Generate Predictions And Combine With Grid

predicted_values <- predict(model_age_sex,
                            newdata = prediction_grid,
                            interval = "confidence")

predictions <- data.frame(prediction_grid, predicted_values)

head(predictions)
  age    sex fit lwr upr
1  20 female 454 415 494
2  25 female 483 447 519
3  30 female 512 479 544
4  35 female 540 511 570
5  40 female 569 542 596
6  45 female 598 573 623

Exercise 1: Predictions Without Interaction

If you were here yesterday, open part-2-predictions-no-interaction.Rmd from the week-02-exercises folder in your RStudio Project.

If you missed yesterday, download week-02-exercises.zip from the NOW learning room, unzip it, and move the folder into your RStudio Project before opening the Rmd file.

You will:

  1. fit rt ~ age + sex
  2. predict values for ages 18 and 65
  3. add confidence intervals for predicted means
  4. create a prediction grid
  5. plot predictions with observed data
  6. write a short results paragraph

ggplot Prediction Lines

A ggplot is built in layers.

Start with observed data, then add layers with +.

Key parts:

  • ggplot() starts the plot
  • aes() maps variables to the plot
  • geom_point() adds observed data
  • geom_line() adds model predictions
  • data = predictions and y = fit adds predicted value from predict()
ggplot(blomkvist, aes(x = age, y = rt, colour = sex)) +
  geom_point() +
  geom_line(data = predictions,
            aes(y = fit))

Adding Confidence Intervals to ggplot

The confidence interval is added with a ribbon.

geom_ribbon() needs three y-values from the prediction data:

  • fit: predicted mean
  • lwr: lower confidence interval limit
  • upr: upper confidence interval limit

The ribbon layer uses inherit.aes = FALSE because it needs its own mapping: ymin = lwr and ymax = upr.

Prediction Plot With Confidence Intervals

head(predictions)
  age    sex fit lwr upr
1  20 female 454 415 494
2  25 female 483 447 519
3  30 female 512 479 544
4  35 female 540 511 570
5  40 female 569 542 596
6  45 female 598 573 623
  ... + # as before
  geom_ribbon(data = predictions,
              aes(x = age, 
                  ymin = lwr, 
                  ymax = upr, 
                  fill = sex),
              inherit.aes = FALSE) 

Two Related Models

Interaction Terms

Interactions are also often called moderation. Combinations of continuous and categorical predictor are also called varying-intercepts and varying-slopes model:

  • varying intercepts: groups can start at different values (age + sex)
  • varying slopes: groups can have different age effects (age * sex)

In this model, age:sexmale is the interaction term. The value is the difference in age slopes between males and females.

Its estimate is -1.344 ms per year.

coef(model_age_sex_interaction)
(Intercept)         age     sexmale age:sexmale 
     309.56        6.27       16.05       -1.34 

Prediction Lines: No Interaction

Without an interaction, the model predicts parallel lines.

The sex coefficient shifts the line up or down, but the age slope is shared.

*I’m not showing raw data, CIs, or any other form of dispersion in this plot. Don’t do this; it’s poor practice. I had to do it to emphasise the difference between predictions with and without interaction.

Prediction Lines: With Interaction

With an interaction, the model can predict different age slopes for the two sex groups.

In this fitted model, the age-by-sex interaction estimate is -1.344 ms per year.

That means the male age slope is -1.344 ms per year different from the female age slope.

*I’m not showing raw data, CIs, or any other form of dispersion in this plot. Don’t do this; it’s poor practice. I had to do it to emphasise the difference between predictions with and without interaction.

What Could Interaction Estimates Mean?

Assume female is the reference group. The interaction asks:

How much different is the age effect for males compared to females?

Interaction estimate Meaning
10 male age slope is 10 ms per year more positive than the female age slope
1 male age slope is 1 ms per year more positive than the female age slope
0 male and female age slopes are about the same
-10 male age slope is 10 ms per year less positive than the female age slope

Positive and negative signs describe the difference between slopes, not just which group is higher.

The reference group matters: the ordinary age coefficient is the age slope for the reference group, and the interaction coefficient tells us how much the comparison group slope differs from that reference slope.

Toy Check: Interaction Meaning

Outcome: sleep duration in hours. Predictors: stress from 0 to 10 and caffeine with levels afternoon and evening.

Reference level: caffeine = afternoon; here caffeine_i = 0 for afternoon and 1 for evening.

\[ \widehat{sleep}_i = \hat{\beta}_0 + \hat{\beta}_\text{stress} \cdot \text{stress}_i + \hat{\beta}_\text{caffeine} \cdot \text{caffeine}_i + \hat{\beta}_\text{stress:caffeine} \cdot \text{stress}_i \cdot \text{caffeine}_i \]

After fitting the model, we find:

  • stress slope = -0.20
  • interaction estimate = -0.10

What does the interaction estimate mean?

A. In the evening-caffeine group, the stress slope is -0.10 hours per stress point.

B. In the afternoon-caffeine group, the stress slope is -0.10 hours per stress point more negative than in the evening-caffeine group.

C. In the evening-caffeine group, the stress slope is -0.10 hours per stress point more negative than in the afternoon-caffeine group.

Exercise 2: Predictions With Interaction

Then open part-2-predictions-interaction.Rmd.

You will:

  1. fit rt ~ age + sex and rt ~ age * sex
  2. predict values for ages 18 and 65 from both models
  3. add confidence intervals for predicted means
  4. plot predictions for both models
  5. compare the prediction plots

When Creating Predictions, Think About

  • What values did you predict for?
  • Why are those values meaningful?
  • Does the plot clarify the model?
  • What would a reader need to understand the figure?

Link to Week 3: Model Comparison

Formative Practice Choices

If you brought your own dataset, open:

  • part-2-formative-own-data-guidance.Rmd

Use it to create predictions for your own formative model and draft a short interpretation paragraph.

If you do not yet have your own dataset, open:

  • part-2-formative-no-own-data.Rmd

Use the Blomkvist data to practise the same prediction workflow while you keep looking for a dataset.

Homework After the Session

Reproduce the prediction workflow with your own dataset.

Try to:

  • fit the model you want to explain
  • choose meaningful predictor values for predictions
  • create a small prediction dataset
  • use predict() to calculate predicted values
  • make a prediction plot
  • write a short paragraph explaining the model using the prediction plot

If you do not yet have your own dataset, complete part-2-formative-no-own-data.Rmd.

Reading After the Session

Read after the workshop:

  • Required: Mark Andrews, Doing Data Science in R, Chapter 9: Normal Linear Models (Andrews, 2021)
  • Optional/foundation: Danielle Navarro, Learning Statistics with R, Chapter 15: Linear Regression https://learningstatisticswithr.com/book/regression.html
  • Optional/foundation: Field, Miles, and Field, Discovering Statistics Using R, chapters on correlation and regression (Field et al., 2012)
  • Optional/advanced: Gelman, Hill, and Vehtari, Regression and Other Stories, sections on linear regression and prediction (Gelman et al., 2020)

Use these to revise why predictions are often clearer than coefficient tables alone.

References

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

Field, A., Miles, J., & Field, Z. (2012). Discovering statistics using R. Sage publications.

Gelman, A., Hill, J., & Vehtari, A. (2020). Regression and other stories. Cambridge University Press.