library(dplyr)
library(ggplot2)
load(url(
"https://github.com/GarciaRios/govt_3990/raw/gh-pages/Labs/lab6/acs.RData"
))Lab 9: Simple Linear Regression
From correlation to regression
Download the student lab template
American salaries
In Lab 8, we used the American Community Survey (ACS) to compare average income across groups.
Today we ask a different kind of question:
Among employed adults, how is annual income related to the number of hours worked each week?
Both variables are numerical. That lets us move from a scatterplot and correlation to a simple linear regression model.
Our workflow will be:
scatterplot → correlation → regression line → model → interpretation → prediction
The data
Load the packages and ACS data:
Keep employed respondents with positive reported income and observed weekly work hours:
acs_emp <- acs |>
filter(
employment == "employed",
income > 0,
!is.na(hrs_work)
)The main numerical variables we will use are:
| Variable | Description |
|---|---|
income |
Annual wage and salary income |
hrs_work |
Usual weekly hours worked |
age |
Age |
time_to_work |
Travel time to work |
Take a quick look:
glimpse(acs_emp)1. Start with a scatterplot
Visualize weekly hours worked and annual income.
ggplot(
acs_emp,
aes(x = hrs_work, y = income)
) +
geom_point(alpha = 0.30) +
labs(
x = "Hours worked per week",
y = "Annual income ($)",
title = "Annual income and weekly hours worked"
) +
theme_minimal()Exercise 1
Describe the relationship between hrs_work and income.
Comment on:
- direction;
- form;
- strength;
- unusual observations.
Does a straight line seem like a useful summary of the overall relationship?
2. Quantify the relationship with correlation
If the relationship is approximately linear, we can summarize its direction and strength with the correlation coefficient.
acs_emp |>
summarise(
r = cor(hrs_work, income)
)Exercise 2
- What is the correlation between weekly hours worked and annual income?
- Is it positive or negative?
- Would you describe the linear association as weak, moderate, or strong?
- Does the value of \(r\) match what you saw in the scatterplot?
Correlation measures linear association. Always inspect the scatterplot before relying on the number alone.
3. From correlation to a regression line
Correlation tells us the direction and strength of a linear relationship. Regression gives us an equation for the line that summarizes that relationship.
First, add the fitted line to the scatterplot:
ggplot(
acs_emp,
aes(x = hrs_work, y = income)
) +
geom_point(alpha = 0.30) +
geom_smooth(
method = "lm",
formula = y ~ x,
se = FALSE
) +
labs(
x = "Hours worked per week",
y = "Annual income ($)",
title = "Annual income and weekly hours worked",
subtitle = "Simple linear regression line"
) +
theme_minimal()Exercise 3
Look at the fitted line.
- Does its direction agree with the sign of the correlation?
- What does the line summarize that the correlation coefficient alone does not?
- What features of the data are still not captured well by a single straight line?
4. Fit the linear model
Now fit the model with lm():
income_hours_model <- lm(
income ~ hrs_work,
data = acs_emp
)
summary(income_hours_model)The formula
income ~ hrs_work
means:
Model annual income as a function of weekly hours worked.
The fitted regression equation has the form
\[ \widehat{income} = b_0 + b_1(hrs\_work). \]
The coefficient table gives estimates of the intercept \(b_0\) and slope \(b_1\).
Exercise 4
Using the model output:
- Write the fitted regression equation.
- Interpret the slope in context.
- Interpret the intercept mathematically.
- Is the intercept substantively meaningful in this example? Explain.
The slope describes an association in these observational data. It does not establish that working additional hours would cause income to change by exactly the amount of the slope.
5. How much does the model explain?
The model summary reports Multiple R-squared, or simply \(R^2\).
\(R^2\) tells us what proportion of the observed variability in the response variable is accounted for by the linear relationship with the explanatory variable.
Exercise 5
- Report \(R^2\).
- Interpret it in the context of annual income and weekly work hours.
- Compare it with the correlation from Exercise 2.
For simple linear regression,
\[ R^2 = r^2. \]
Check this directly:
r_hours_income <- cor(
acs_emp$hrs_work,
acs_emp$income
)
r_hours_income^2Does the result match the \(R^2\) reported by summary()?
6. Use the model for prediction
A fitted regression equation can be used to predict the response for a particular value of the explanatory variable.
Predict annual income for someone who reports working 40 hours per week:
predict(
income_hours_model,
newdata = data.frame(
hrs_work = 40
)
)Now compare several values:
prediction_cases <- data.frame(
hrs_work = c(20, 40, 60)
)
prediction_cases |>
mutate(
predicted_income = predict(
income_hours_model,
newdata = prediction_cases
)
)Exercise 6
- What income does the model predict at 40 hours per week?
- How do the predictions change from 20 to 40 to 60 hours?
- Connect that change to the slope of the regression equation.
- Why should these be described as predicted incomes rather than what a person will earn?
Extrapolation
Before using a regression model for prediction, check the observed range of the explanatory variable:
range(acs_emp$hrs_work)Exercise 7
Suppose someone asks you to use this model to predict income for a person working a number of hours far outside the observed range.
- Why would that prediction be risky?
- What assumption would you be making about the relationship?
Using a regression line to predict beyond the observed range of the explanatory variable is called extrapolation. R can calculate the value, but the relationship may not continue in the same way outside the data.
7. Compare another predictor
The same outcome can have many possible predictors. Now compare weekly work hours with another numerical variable.
Start with age:
ggplot(
acs_emp,
aes(x = age, y = income)
) +
geom_point(alpha = 0.30) +
geom_smooth(
method = "lm",
formula = y ~ x,
se = FALSE
) +
labs(
x = "Age",
y = "Annual income ($)",
title = "Annual income and age"
) +
theme_minimal()Calculate the correlation:
acs_emp |>
summarise(
r = cor(age, income, use = "complete.obs")
)Fit the model:
income_age_model <- lm(
income ~ age,
data = acs_emp
)
summary(income_age_model)Exercise 8
- Describe the age-income scatterplot.
- What is the correlation?
- Write the fitted regression equation.
- Interpret the slope.
- Report \(R^2\).
- Which variable accounts for more of the observed variation in income:
hrs_workorage? - Does a straight line seem equally appropriate for both relationships?
A larger \(R^2\) means that a linear model accounts for more of the variation in the response. It does not automatically mean that the model is causal or that it is the best possible model.
On Your Own: Compare another numerical predictor
Now apply the same workflow to another numerical ACS relationship.
Use time_to_work as the explanatory variable and income as the response.
1. Scatterplot
Create a scatterplot of commute time and annual income.
Describe the direction, form, strength, and any unusual observations.
2. Correlation
Calculate the correlation.
Interpret the result in context.
3. Regression model
Fit a simple linear regression predicting annual income from commute time.
Then:
- write the fitted equation;
- interpret the slope;
- report and interpret \(R^2\).
4. Prediction
Choose one reasonable commute time within the observed data and calculate the model’s predicted income.
Explain what the prediction means.
5. Compare the three relationships
Compare the models using:
hrs_work;age;time_to_work.
Which has the strongest linear relationship with income?
Support your answer using the scatterplots, correlations, and \(R^2\) values.
Finally, identify one important limitation of using any of these simple regression models to explain differences in annual income.
The simple-regression workflow is:
scatterplot → correlation → regression line → slope and intercept → \(R^2\) → prediction → limitations