lm_spec <- linear_reg() %>%
set_mode("regression") %>%
set_engine("lm")
lm_fit1 <- lm_spec %>%
fit(mpg ~ ..., data = Auto)Chapter 3 Applied Problems: Guided Instructions
Introduction
These problems are based on Applied Problems 8 and 9 from Chapter 3 of ISLR.
The suggestions identify useful functions, arguments, and code structures, but they are not complete solutions. The displayed R chunks are examples and are not evaluated when this instruction file is rendered. Your submission should contain your completed R code, the relevant output and plots, and written answers to every question that asks you to comment, explain, compare, or interpret.
The packages used in these problems include tidyverse, tidymodels, ISLR2, corrr, and GGally. Load them near the beginning of your own analysis.
Problem 1: Simple linear regression
Complete Problem 8 from Chapter 3 of the ISLR book (p. 123) using the modifications and suggestions below. This problem uses the Auto data from the ISLR2 package.
Part 1(a): Fit and interpret a simple linear regression
Fit a simple linear regression with mpg as the response and horsepower as the predictor. Use parsnip to specify and fit the model rather than calling lm() directly. A recipe and workflow are not needed for this problem.
First inspect the variables using glimpse() and make a scatterplot of mpg versus horsepower. This gives you a graphical expectation for the sign of the fitted slope and helps you judge later whether a straight-line model is reasonable.
Specify and fit the model
A parsnip linear-model specification has three important pieces: linear_reg(), regression mode, and the "lm" engine. Because this construction may be unfamiliar, use the following incomplete pattern as a guide:
Replace ... with the required predictor. The fitted parsnip object contains the underlying lm object, but it should usually be inspected using tidymodels functions.
Use tidy() for a coefficient table and glance() for one-row model summaries. To reproduce the more detailed base-R output requested in the assignment, extract the fitted engine and send it to summary(). The relevant pattern is lm_fit1 %>% extract_fit_engine() %>% summary().
Questions to answer from the output
Use your output to address all four textbook questions.
- Is there evidence of a relationship? Locate the p-value for the
horsepowerslope in thetidy()table. State the null hypothesis being tested and reach a conclusion in context. - How strong is the relationship? Use the \(R^2\) value from
glance()and describe it as the proportion of variation inmpgexplained by this linear model. Do not describe \(R^2\) as a correlation. - Is the relationship positive or negative? Use the sign of the estimated slope. Explain how predicted fuel economy changes as horsepower increases.
- What is predicted when horsepower is 98? Use
predict()with a one-row tibble rather than substituting directly into the fitted equation.
Prediction, confidence interval, and prediction interval
Create the new-data tibble requested in the assignment: Auto_test <- tibble(horsepower = 98). If your version of parsnip reports that it cannot safely convert a double to the integer type used during fitting, write the value as 98L instead. The L tells R to store the value as an integer.
Call predict() three times, changing its type argument:
- omit
typefor the fitted value; - use
type = "conf_int"for the confidence interval; and - use
type = "pred_int"for the prediction interval.
Each call needs the fitted model and new_data = Auto_test. Consult ?parsnip::predict.model_fit if the returned column names differ from what you expect.
Explain both intervals in context. The confidence interval estimates the mean mpg among all automobiles with 98 horsepower. The prediction interval gives a plausible range for the mpg of one individual automobile with 98 horsepower. Identify which interval is wider and explain why predicting an individual response involves more uncertainty than estimating a mean response.
Part 1(b): Display the fitted line in two ways
Use ggplot2 to create two scatterplots of mpg versus horsepower.
Plot using extracted coefficients
For the first plot, use tidy(lm_fit1) to obtain the coefficient table. The term column identifies the intercept and slope, while estimate contains their values. You can use filter() and pull(estimate) to store each estimate in a separate object.
Add the line with a layer of the form:
geom_abline(intercept = ..., slope = ..., color = ...)Replace the first two blanks with the extracted estimates. Use a line color that is clearly visible and colorblind friendly, and add meaningful axis labels.
Plot using geom_smooth()
For the second plot, let ggplot2 fit the same type of line by adding geom_smooth(method = "lm", se = FALSE). The argument se = FALSE removes the shaded confidence band, as requested.
The two fitted lines should coincide. If they do not, check that the response and predictor were placed on the correct axes and that you extracted the correct coefficient for the slope.
Part 1(c): Residual plot
Instead of producing all four base-R diagnostic plots, use augment() and ggplot2 to create a residual-versus-fitted plot.
Applying augment() to the fitted model and the original data adds columns such as .pred and .resid. A useful pipeline begins as follows:
lm_fit1 %>%
augment(new_data = Auto) %>%
ggplot(aes(x = .pred, y = .resid)) +
# add the points and a horizontal reference lineUse geom_point() for the residuals and add a horizontal line at zero with geom_hline(yintercept = 0, ...). You may add a smooth curve to make a systematic pattern easier to see, but it should not replace the zero reference line.
Comment on the following features:
- whether residuals are randomly scattered around zero or show curvature;
- whether their vertical spread stays approximately constant;
- whether any observations have unusually large residuals; and
- what any visible pattern suggests about using a straight-line model.
Problem 2: Multiple linear regression
Complete Problem 9 from Chapter 3 of the ISLR book (pp. 123–124) using the modifications and suggestions below. Continue using the Auto data.
Part 2(a): Scatterplot matrix
Create a scatterplot matrix that includes all quantitative variables but excludes name. Use select() to remove the column and ggpairs() from GGally to make the plot. The essential pipeline is Auto %>% select(-name) %>% ggpairs().
Because this is a large figure, increase the chunk’s fig-width and fig-height. Look beyond relationships with mpg: also identify strong relationships among predictors. These predictor-to-predictor relationships may matter when you later interpret a multiple regression model.
In your discussion, describe several prominent positive or negative relationships, possible nonlinear patterns, and any clusters or unusual observations.
Part 2(b): Correlation matrix
Use correlate() from corrr after dropping name. The beginning is again Auto %>% select(-name), but it should now be passed to correlate() rather than ggpairs().
The diagonal will be omitted because a variable’s correlation with itself is not informative. Focus on correlations involving mpg and also on pairs of predictors with large absolute correlations. Remember that a strong predictor-to-predictor correlation may make individual regression coefficients harder to interpret.
Part 2(c): Multiple regression using a recipe and workflow
Fit a multiple linear regression with mpg as the response and all remaining variables except name as predictors. For this part, use both a recipe and a workflow.
Build the recipe
Begin with mpg ~ . so that all other columns are initially treated as predictors. Then remove name with the required recipe step. This portion may be written as:
auto_rec <- recipe(mpg ~ ., data = Auto) %>%
step_rm("name")The recipe does not fit the regression. It records how the variables should be prepared when the workflow is fitted.
Assemble and fit the workflow
Use the lm_spec created in Problem 1. Add the model specification and recipe to a new workflow(), and then fit that workflow using Auto. The structure is:
auto_wf <- workflow() %>%
add_model(...) %>%
add_recipe(...)
auto_fit <- auto_wf %>%
fit(data = ...)Fill the blanks with the objects and dataset already created. Use glance(auto_fit) and tidy(auto_fit) to examine the fitted model.
Questions to answer from the output
Address at least the following textbook questions.
- Is there a relationship between the predictors and
mpg? Use the overall model p-value inglance(), together with \(R^2\) or adjusted \(R^2\). The overall test is different from the separate t-tests in the coefficient table. - Which predictors appear statistically significant? Use the p-values in
tidy()and state the significance level you are using. Interpret significance after accounting for the other predictors in the model. - What does the coefficient for
yearsuggest? Interpret the coefficient as the estimated change inmpgfor a one-year increase in model year while holding the other predictors fixed. Include its sign and units.
Some predictors, such as origin, are stored numerically even though their numbers represent groups. For this assignment follow the requested model, but recognize that treating group codes as quantitative imposes a particular pattern on their effect.
Part 2(d): Residual plot for the multiple regression
Use augment() with the fitted workflow and the original Auto data, then graph .resid versus .pred. The plotting structure is similar to Part 1(c), so reuse that approach rather than starting from a completely new plot.
Do not manually redefine the residual as .pred - mpg; augment() supplies .resid using the conventional definition, observed minus predicted.
Add a horizontal reference line at zero and comment on curvature, changing spread, and unusually large residuals. Compare this plot briefly with the residual plot from the simple regression: has including the additional predictors removed some of the earlier structure?
Part 2(e): Interaction terms
Use recipe steps rather than formula notation to add interaction terms. Explore several reasonable models rather than reporting only the first interaction you try.
Start from a recipe containing the response, relevant main effects, and the removal of name. Then add an interaction step with a structure such as:
step_interact(terms = ~ predictor_1:predictor_2)Replace the placeholders with predictors for which an interaction is scientifically plausible or suggested by the earlier plots. For example, ask whether the association between one measure of vehicle size or power and mpg might change depending on another vehicle characteristic.
Preserve the corresponding main effects whenever an interaction is included. Give each recipe, workflow, and fitted model a distinct name so that results are not overwritten.
Use tidy() to examine the interaction coefficient and its p-value. Use glance() to compare model-level quantities such as adjusted \(R^2\) and residual standard error. A statistically significant interaction indicates that the effect of one predictor depends on the value of the other; it should not be interpreted as an ordinary slope by itself.
Part 2(f): Nonlinear terms and transformations
Use recipe steps to explore models containing nonlinear terms or transformations. Reasonable possibilities include a squared quantitative predictor, a higher-degree polynomial, or a logarithm of a positive predictor.
For a squared term, step_mutate() can create a new predictor. The following deliberately leaves the variable choice incomplete:
step_mutate(new_squared_term = chosen_predictor^2)Alternatively, investigate step_poly() for polynomial terms or step_log() for a logarithmic transformation. Check that a variable is strictly positive, or use an appropriate offset, before applying a logarithm.
Fit several candidate recipes through workflows using the same linear-model specification. Use glance() and tidy() to compare them with the original additive model. Also examine their residual plots; an improved numerical summary is more convincing when the systematic residual pattern is reduced.
In your written conclusion:
- identify the transformations or nonlinear terms you tried;
- explain why they were reasonable choices;
- state which terms appear useful based on the output;
- compare model fit without relying only on ordinary \(R^2\), which cannot decrease when predictors are added; and
- avoid claiming that the most complicated model is automatically the best.
Submission reminder
Include the requested model output, prediction results, intervals, and plots, followed by concise written interpretations. Make sure that coefficient interpretations use the appropriate “holding other predictors fixed” language in the multiple-regression problem and that residual plots are discussed rather than merely displayed.