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

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

predict(model_age_sex, newdata = newdata)
  1   2   3 
454 569 684 
# The same newdata can be used with the interaction model.
predict(model_age_sex_interaction, newdata = newdata)
  1   2   3 
435 560 686 

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
predict(model_age_sex_interaction, newdata = newdata, interval = "confidence")
  fit lwr upr
1 435 388 482
2 560 531 590
3 686 663 709

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.

Prediction Data

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

ggplot Prediction Lines

A ggplot is built in layers.

Start with the 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 uses the prediction data for that layer
  • fit is the 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) 

Reporting Predictions

Useful reporting structure:

  • State the model
  • State the main pattern
  • Refer to the figure
  • Avoid describing every coefficient separately if the plot is clearer

Example:

The model predicted slower reaction times at older ages. This pattern was similar for male and female participants.

Exercise 1: Predictions Without Interaction

From the NOW learning room, open part-2-predictions-no-interaction.Rmd.

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

Two Related Models

Interaction Terms

Interactions are also often called moderation.

With one continuous predictor and one categorical predictor, the interaction model is also a varying-intercepts and varying-slopes model:

  • varying intercepts: groups can start at different values
  • varying slopes: groups can have different age effects

Here, the interaction estimate is the difference in age slopes between males and females.

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: stress score from 0 to 10
  • caffeine: none or evening

Reference level: caffeine = none.

Model:

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

After fitting the model, we find:

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

Discuss: what does the interaction estimate mean here?

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.

Prediction Lines: With Interaction

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

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

Discussion

  • 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?

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 formative dataset if you have one.

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.

Reporting Checklist

Your model-results section should include:

  • model formula
  • coefficient table or selected estimates
  • confidence intervals
  • prediction plot
  • plain-language interpretation

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.