Step 1: Package Loading and Data Exploration

First, we load the necessary packages (Stat2Data, dplyr, and ggplot2) and load the Kershaw dataset. We then inspect scatterplots of all pitch types to observe visual groupings.

library(Stat2Data)
library(dplyr)
library(ggplot2)
data("Kershaw")

# Plot 1: Plain scatterplot of all data
ggplot(Kershaw, aes(x = StartSpeed, y = EndSpeed)) +
  geom_point() +
  labs(
    title = "Kershaw Pitches: Start Speed vs. End Speed",
    x = "Start Speed (mph)",
    y = "End Speed (mph)"
  )

# Plot 2: Scatterplot colored and shaped by PitchType
ggplot(Kershaw, aes(x = StartSpeed, y = EndSpeed, color = PitchType, shape = PitchType)) +
  geom_point() +
  labs(
    title = "Kershaw Pitches by Pitch Type",
    x = "Start Speed (mph)",
    y = "End Speed (mph)"
  )

Observation: They are grouped together by color and shape.


Step 2: Filtering to Fastballs and Measuring Association

Next, we filter the dataset to focus exclusively on fastballs (PitchType == "FF"), plot the subset, and calculate the correlation coefficient (\(r\)).

# Filter dataset for fastballs ("FF")
my_group <- Kershaw %>%
  filter(PitchType == "FF")

# Scatterplot of fastballs
ggplot(my_group, aes(x = StartSpeed, y = EndSpeed)) +
  geom_point(alpha = 0.6) +
  labs(
    title = "Kershaw Fastballs: Start Speed vs. End Speed",
    x = "Start Speed (mph)",
    y = "End Speed (mph)"
  )

# Calculate correlation coefficient r
cor(my_group$StartSpeed, my_group$EndSpeed, use = "complete.obs")
## [1] 0.7177341

Analysis: The correlation coefficient is approximately \(r = 0.7177341\). The association between StartSpeed and EndSpeed is moderate and positive, which matches the positive correlation coefficient value.


Step 3: Simple Linear Regression Model

We fit a simple linear regression model with EndSpeed as the response variable and StartSpeed as the explanatory variable, extracting the estimated coefficients.

# Fit the linear model
model <- lm(EndSpeed ~ StartSpeed, data = my_group)

# View the coefficients
coef(model)
## (Intercept)  StartSpeed 
##  14.1772225   0.7641677

Coefficients & Interpretation: - Intercept: 14.1772225 - Slope: 0.7641677

Interpretation: Every 1 step on the x axis (1 mph increase in StartSpeed) corresponds to a 0.7641677 increase on the y axis (predicted EndSpeed in mph).


Step 4: Plotting the Regression Line

Finally, we overlay the fitted linear regression line onto our scatterplot using the calculated slope and intercept.

# Scatterplot with fitted linear regression line
ggplot(my_group, aes(x = StartSpeed, y = EndSpeed)) +
  geom_point(alpha = 0.6) +
  geom_abline(intercept = 14.1772225, slope = 0.7641677, color = "steelblue", linewidth = 1) +
  labs(
    title = "Kershaw Fastballs: Start Speed vs. End Speed",
    subtitle = "EndSpeed = 14.1772225 + 0.7641677(StartSpeed) | r = 0.7177341",
    x = "Start Speed (mph)",
    y = "End Speed (mph)"
  )

Visual Verification: The regression line successfully passes through the middle of the scatterplot data points.