Step 1: Exploratory Data Analysis & Group Differences

We begin by loading the necessary packages and the MetroCommutes dataset. To explore overall patterns and see how commute dynamics vary by city, we plot scatterplots for all data points combined and separated by city.

library(Stat2Data)
library(dplyr)
library(ggplot2)

data("MetroCommutes")

# Scatterplot of ALL data
ggplot(MetroCommutes, aes(x = Distance, y = Time)) +
  geom_point() +
  labs(
    title = "Commute Time vs. Distance for All Cities",
    x = "Distance (miles)",
    y = "Time (minutes)"
  )

# Scatterplot colored and shaped by City
ggplot(MetroCommutes, aes(x = Distance, y = Time, color = City, shape = City)) +
  geom_point() +
  labs(
    title = "Commute Time vs. Distance by City",
    x = "Distance (miles)",
    y = "Time (minutes)",
    color = "City",
    shape = "City"
  )

Interpretation: Most points across the cities are concentrated towards the bottom-left corner of the graph closer to \((0,0)\), representing shorter distances and shorter commute times.


Step 2: Filtering and Correlation Analysis for Houston

Next, we restrict our focus strictly to commuters in Houston. We filter the dataset, plot a scatterplot, and calculate the correlation coefficient \(r\).

# Filter data to keep only Houston
my_group <- MetroCommutes %>%
  filter(City == "Houston")

# Scatterplot for Houston
ggplot(my_group, aes(x = Distance, y = Time)) +
  geom_point(alpha = 0.6) +
  labs(
    title = "Commute Time vs. Distance in Houston",
    x = "Distance (miles)",
    y = "Time (minutes)"
  )

# Compute correlation coefficient r
cor(my_group$Distance, my_group$Time, use = "complete.obs")
## [1] 0.8295624

Interpretation: The association between distance and commute time in Houston is positive, linear, and moderate in strength with an upward trend. The computed value of \(r\) matches this visual pattern.


Step 3: Fitting the Simple Linear Model

We fit a simple linear regression model with Time as the response variable (\(y\)) and Distance as the explanatory variable (\(x\)), then extract the model coefficients.

# Fit linear model
model <- lm(Time ~ Distance, data = my_group)

# Extract intercept and slope
coef(model)
## (Intercept)    Distance 
##    7.321356    1.127960

Model Coefficients & Interpretation: * Intercept: 7.321356 * Slope: 1.127960

Interpretation: For every 1 additional mile of commute distance in Houston, the predicted commute time increases by 1.127960 minutes.


Step 4: Regression Line Visualization

Finally, we overlay the regression line on our Houston scatterplot using geom_abline() with our exact slope and intercept.

ggplot(my_group, aes(x = Distance, y = Time)) +
  geom_point(alpha = 0.6) +
  geom_abline(intercept = 7.321356, slope = 1.127960, color = "steelblue", linewidth = 1) +
  labs(
    title = "Commute Time vs. Distance in Houston",
    subtitle = "Time = 7.321356 + 1.127960 * Distance",
    x = "Distance (miles)",
    y = "Time (minutes)"
  )

Line Verification: The fitted line runs through the center of the scatterplot data points, confirming that the line correctly captures the trend.