Indicator Variable and Model 1 (Simple Linear Regression)
# Create indicator variable: 1 for Male, 0 otherwise (Female)
my_data$MaleIND <- ifelse(my_data$Sex == "Male", 1, 0)
# Model 1: Simple linear regression of Wing on Temp
model1 <- lm(Wing ~ Temp, data = my_data)
coef(model1)
## (Intercept) Temp
## 18.8698426 -0.2350416
Model 2: Indicator Variable (Parallel Lines)
# Model 2: Parallel lines model with MaleIND
model2 <- lm(Wing ~ Temp + MaleIND, data = my_data)
coef(model2)
## (Intercept) Temp MaleIND
## 19.3354676 -0.2350416 -0.9312500
# Plot points colored by Sex with parallel lines (Female = MaleIND 0, Male = MaleIND 1)
ggplot(my_data, aes(x = Temp, y = Wing, color = Sex)) +
geom_point() +
geom_abline(intercept = 19.33, slope = -0.235, color = "#F8766D") +
geom_abline(intercept = 18.3988, slope = -0.235, color = "#00BFC4") +
labs(
title = "Butterfly Wing Length vs. Temperature with Parallel Lines by Sex",
x = "Average Temperature of Previous Summer (degrees Celsius)",
y = "Wing Length (mm)"
)

Model 3: Interaction Model (Non-Parallel Lines)
# Model 3: Interaction model allowing separate slopes by sex
model3 <- lm(Wing ~ Temp * MaleIND, data = my_data)
coef(model3)
## (Intercept) Temp MaleIND Temp:MaleIND
## 19.343860562 -0.238813736 -0.948035900 0.007544225
# Plot points colored by Sex with separate interaction lines
ggplot(my_data, aes(x = Temp, y = Wing, color = Sex)) +
geom_point() +
geom_abline(intercept = 19.34, slope = -0.23, color = "#F8766D") +
geom_abline(intercept = 18.40, slope = -0.222456, color = "#00BFC4") +
labs(
title = "Butterfly Wing Length vs. Temperature with Interaction by Sex",
x = "Average Temperature of Previous Summer (degrees Celsius)",
y = "Wing Length (mm)"
)

Model Comparison (R-squared and Residual Standard Error)
# R-squared and Residual Standard Error (sigma) for each model
c(model1_r2 = summary(model1)$r.squared,
model2_r2 = summary(model2)$r.squared,
model3_r2 = summary(model3)$r.squared)
## model1_r2 model2_r2 model3_r2
## 0.1103779 0.8315876 0.8316160
c(model1_sigma = summary(model1)$sigma,
model2_sigma = summary(model2)$sigma,
model3_sigma = summary(model3)$sigma)
## model1_sigma model2_sigma model3_sigma
## 0.5341001 0.2363570 0.2405203
# Optional: Adjusted R-squared and predictions at Temp = 2
c(model1_adj_r2 = summary(model1)$adj.r.squared,
model2_adj_r2 = summary(model2)$adj.r.squared,
model3_adj_r2 = summary(model3)$adj.r.squared)
## model1_adj_r2 model2_adj_r2 model3_adj_r2
## 0.08072379 0.81997297 0.81357491
predict(model2, newdata = data.frame(Temp = 2, MaleIND = c(0, 1)))
## 1 2
## 18.86538 17.93413
Diagnostics for Model 2
plot(model2, which = 1) # Residuals vs. Fitted

plot(model2, which = 2) # Normal Q-Q plot
