PS04: Linear Regression — One Numerical Predictor

Published

May 18, 2026

Overview

Practice simple linear regression with one numerical predictor — including visualisation, correlation, model fitting, interpretation, and prediction — using demographic and voting data from the 2016 US presidential election.

Read Chapter 5 of ModernDive before attempting this problem set.


Download

Download the problem set template, open it in RStudio, and complete the exercises directly in the document.

PS04.zip


Setup

Run this at the top of your document to install and load the required packages:

if (!require(pacman)) install.packages("pacman")
pacman::p_load(dplyr, ggplot2, readr, moderndive)

Exercises

Worked example: white poverty and Trump support

Follow a guided regression analysis — visualisation, correlation, model fitting, interpretation, and prediction.

Independent analysis: non-white population and Trump support

Apply the same workflow independently using a different explanatory variable.


Saving your plots

Remember you can save any plots you create to the figures/ folder using ggsave(). Use descriptive file names that reflect the content of the plot:

non_white_plot <- ggplot(data = trump, aes(x = non_white, y = trump_support)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE)

ggsave("figures/trump-support-vs-non-white.png", plot = non_white_plot, 
       width = 16/2, height = 9/2)

When you are done, render to HTML and submit on Moodle. Name your file PS04_yourname.html.