Problems
- Complete problem 8 from Chapter 3 of the ISLR book (P. 123) with the following modifications:
- Use parsnip to specify and fit the model (not a recipe or workflow for this problem). Use the “lm” engine. Use
summary to print the results, but also use glance and tidy. Comment on the output by answering all the questions i. - iv. For question iv. use the predict function. Create a very simple tibble Auto_test <- tibble(horsepower = 98) and use it for the new_data argument when you use the predict function. Read the help file ?parsnip::predict.model_fit to determine how to find a prediction interval. Explain the difference between a 95% confidence interval and a 95% prediction interval in this context. Which is wider? Explain why this should be the case.
- Use ggplot. Prepare two plots. In the first plot, extract the coefficients using
tidy function and use geom_abline to add the regression line to the scatterplot. In the second plot, use geom_smooth to add the regression line instead. Do not display the confidence interval around the line. Make your plots look good (and colorblind friendly).
- Instead of doing this, create a residual plot and comment on any problems you see. Use the
augment function and ggplot.
- Complete problem 9 from Chapter 3 of the ISLR book (PP. 123-124) with the following modifications:
- Use
select to drop the name column. Then use ggpairs from the GGally package.
- Use
correlate from the corrr package (drop the name column first).
- Use a recipe and a workflow to set up and fit the model. When you set up the recipe start out by treating mpg as the response and all of the variables as predictors. Then create a step to remove the variable name. The syntax is
step_rm("name"). Use glance and tidy instead of summary. Address at least all of i. - iii. in your comments.
- Instead, create a residual plot using
augment and ggplot. Comment on any problems you see with the fit.
- Add interaction terms by adding steps to a recipe instead of including them in a formula. Explore several models with different combinations of interactions. Use
glance and tidy to explore the results.
- Add the nonlinear terms by adding steps to a recipe. Explore several models with nonlinear terms. Use
glance and tidy to explore the results.
- Complete problem 15 from Chapter 3 of the ISLR book (P. 128) with the following modifications:
- You may set up and train parsnip models or you may use workflows and recipes to produce the models. If you have some experience with R, you may be able to figure out how to do this efficiently by writing functions to extract model coefficients and statistics and the
map function. If you are interested in diggin into this, there is a useful example in the Chapter 25 of the R for Data Science book. Otherwise you will fit 12 different simple linear models for this problem.
- Use a recipe and a workflow.
- Use ggplot. Create a tibble with columns for the predictor name, the regression coefficients (slopes) for the multiple regression, the regression coefficients (slopes) for the univariate regressions, and the p-values obtained for each of the coefficients in the multiple regression. Your tibble should have 12 rows and 4 columns to begin with. Use the mutate command to add a fifth (binary, categorical) column that indicates whether the p-value is less than 0.05 or not. Create a scatter plot (as described in the text) with your tibble using ggplot, but use the column that indicates whether the coefficient was significant in the multiple regression to color the points. You should notice that there is one point that is isolated from the others in the plot. Create a second plot that matches the first, but use the
xlim and ylim functions to focus on the region of the plot that contains the other 11 points. Use the same values for xlim and ylim so your axes are square. Use geom_abline to add a line with intercept 0 and slope 1 to your plot (the line \(y=x\)). Discuss your results. How do they relate to the p-values of the coefficients from the multiple regression? Explain why the coefficients could be so different for the same term between the multiple regression and the univariate regression.
- See the note for part a about doing this efficiently. Use recipes, including steps to add the nonlinear terms, and use workflows.