Categorical Predictors in Regression: Practice

A note on AI tools: You may use AI assistants (ChatGPT, Claude, etc.) to help debug your R code — that’s a realistic professional skill. You may not use AI to generate your interpretations. Every write-up sentence must reflect your own reading of the output. If you use AI for any part of this assignment, note where at the end.

This assignment uses two datasets that ship with base R — no downloading required.

Part 1: Dichotomous Predictor — mtcars

mtcars$am codes transmission type (0 = automatic, 1 = manual) as a number. First, turn it into a proper factor with meaningful labels:

data(mtcars)
mtcars$am_f <- factor(mtcars$am, labels = c("automatic", "manual"))
table(mtcars$am_f)

Predict first. Which transmission type do you expect to get better gas mileage (mpg) — automatic or manual? By roughly how much?

model1 <- lm(mpg ~ am_f, data = mtcars)
summary(model1)
  1. Report the intercept and the am_fmanual coefficient. Which one is the mean mpg for automatic cars, and which is the difference between manual and automatic?
  2. Is the slope significant? Report t, df, and p.

Now verify the equivalence directly:

t.test(mpg ~ am_f, data = mtcars, var.equal = TRUE)
  1. Compare the t-statistic and p-value here to the regression’s t-test on the am_fmanual coefficient from Question 2. Are they the same test? What’s different between the two outputs (besides which number is reported as the “reference”)?

Part 2: Categorical Predictor with 3+ Levels — iris

iris$Species has three levels (setosa, versicolor, virginica) — a direct test of the one-way-ANOVA equivalence.

data(iris)
levels(iris$Species)
table(iris$Species)
model2 <- lm(Sepal.Length ~ Species, data = iris)
summary(model2)
  1. Which species is the reference level? How do you know, without being told?
  2. Report the intercept and both species coefficients. What is the mean Sepal.Length for each of the three species? (You’ll need to add each coefficient to the intercept for the non-reference species.)

Now verify against a proper ANOVA:

model2_aov <- aov(Sepal.Length ~ Species, data = iris)
summary(model2_aov)
anova(model2)
  1. Compare the F-statistic and p-value from aov() to the F-statistic and p-value from anova(model2) (the regression version). Are they identical?
  2. The regression summary() output only directly compares versicolor and virginica each to setosa. What comparison is missing, and what R function (from Ch 17/18) would you use to get it?

Part 3: Changing the Reference Level

iris$Species2 <- relevel(iris$Species, ref = "virginica")
model3 <- lm(Sepal.Length ~ Species2, data = iris)
summary(model3)
  1. Which coefficients changed compared to model2, and which stayed the same? Specifically: did the overall F-statistic change? Why or why not?

Part 4: Reporting

  1. Write one complete APA-style sentence reporting the dichotomous regression result from Part 1 (transmission type predicting mpg).
  2. A classmate says: “Since am_fmanual was significant, that proves switching a car from automatic to manual transmission would improve its mpg.” What’s wrong with this claim, using the language from the chapter’s own causation caveat?