In Lab 9, we predicted fuel economy using one variable at a time. Here, we will use more than one predictor and investigate what changes when we account for additional information.
Our goals are to:
Use \(\alpha=.05\) throughout. Save each model and dataset with the name specified so you can return to it later. Give brief answers in context; you do not need to transcribe every number in the output.
We will continue with the 32 vehicles in mtcars. This lab starts with a fresh working copy, so you do not need to load your Lab 9 work.
| Variable | Description |
|---|---|
mpg |
Fuel economy, in miles per gallon |
wt |
Vehicle weight, in thousands of pounds |
transmission |
Automatic or manual, created from am |
library(dplyr)
library(ggplot2)
theme_set(theme_bw())
cars <- mtcars
cars$transmission <- factor(cars$am, levels = c(0, 1),
labels = c("automatic", "manual"))
The reference category is automatic. A coefficient named transmissionmanual compares manual cars with automatic cars.
group_by() and summarize() to find the number of cars, mean fuel economy, and mean weight for each transmission group. Save and display the result as transmission_summary.transmission_fit and display its summary. Interpret the manual coefficient, state its null hypothesis, and draw a conclusion using its p-value.To include two predictors, separate them with +:
## Do not run this, this is just an example
lm(outcome ~ predictor1 + predictor2, data = dataset)
For weight and transmission, our fitted equation has the form
\[ \hat y = \hat\beta_0 + \hat\beta_{\mathrm{wt}}\,\mathrm{wt} + \hat\beta_{\mathrm{manual}}\,\mathbf{1}_{\mathrm{manual}}. \]
The indicator is 1 for manual and 0 for automatic cars. This model gives both transmission groups the same weight slope, but allows their intercepts to differ. We are not including an interaction.
Each coefficient now describes a comparison holding the other predictor fixed. For example, the transmission coefficient compares the predicted means of manual and automatic cars at the same weight.
weight_transmission_fit and display its summary. Write the fitted equation and interpret both the weight and manual coefficients. Be explicit about what is held fixed.same_weight_cars <- data.frame(
wt = c(3, 3),
transmission = factor(c("automatic", "manual"),
levels = levels(cars$transmission))
)
same_weight_cars$predicted_mpg <- predict(
weight_transmission_fit, newdata = same_weight_cars
)
same_weight_cars
# Save the fitted predictions alongside the original observations.
car_predictions <- cars %>%
mutate(predicted_mpg = fitted(weight_transmission_fit))
ggplot(car_predictions, aes(wt, mpg, color = transmission)) +
geom_point() +
geom_line(aes(y = predicted_mpg, group = transmission)) +
labs(x = "Weight (1,000 pounds)", y = "Fuel economy (mpg)",
color = "Transmission")
The lines come from the saved model. Adding geom_smooth(method = lm) separately for each transmission group would estimate two slopes rather than the common slope in our additive model.
We now have two questions we could ask about the combined model:
Use summary() for the overall test. To compare nested models, use anova(reduced_model, full_model), putting the smaller model first. Both models must use the same outcome and observations.
weight_fit, and display its summary. Compare its \(R^2\) and residual standard error with those of weight_transmission_fit. Does adding transmission noticeably improve the fit? Is an increase in ordinary \(R^2\) alone enough to justify keeping an additional predictor?summary(weight_transmission_fit). State its null hypothesis and give a conclusion. Does rejecting this null establish that both predictors contribute after accounting for the other?anova(weight_fit, weight_transmission_fit)