Chapter 2 Applied Problems
Introduction
In addition to labs we will complete Applied Problem sets for each chapter of the ISLR book. These will build on concepts introduced in the labs. Many of the problems will be from the Applied Exercises that are provided at the end of each chapter in the textbook. Some of these will be modified to use functions from the tidyverse and tidymodels packages or to incorporate additional topics. Other problems will be original problems that are not from the textbook.
There are guidelines for Applied Problem submissions on our Moodle page that you should read before you begin working on this problem set.
Problems
- Complete problem 8 from Chapter 2 of the ISLR book (PP. 54-56) with the following modifications:
- Use the
read_csvfunction instead of theread.csvfunction. Read the data directly from the website using the URL instead of downloading the file and reading in the copy you downloaded. - Do not set the row names, since tibbles do not have row names. In addition to using the
Viewfunction, also use theglimpsefunction to look at the data. Note that the first column of the data (the one that contains the university names) is called something like …1. Use therenamefunction to change the name of this colum to UniName. There is a little bit of a trick to this. Although R prints the name as …1, it is actually `…1`. You will need to use this version of the column name when you use therenamefunction. R will often surround the name of a column with single quotes like this when there is something unusual about it. This will occur, for example, if the name has spaces or if it starts with something other than a letter. - Also use the
countfunction to count the number of private and public institutions in the data set. - Use the
selectfunction to select columns 2 through 11 (note that we have an extra column that the book has converted to row names at this point). Your code should use a pipe and should containcollege %>% select(2:11). Use theggpairsfunction from theGGallypackage instead of thepairsfunction. You will probably find that it is difficult to see any relationships in the data given the size of the points in the plots. One way to handle this is to define the figure size at the top of the code block that you use to produce your plot. Try adding the line#| fig.width: 14followed by the line#| fig.height: 14at the top of the block. This will increase the size of the figure while keeping the size of the points the same, making it easier to see relationships. - Use ggplot to create the boxplots. Use the classic theme. Note that when you are asked to plot Variable 1 versus Variable 2, it is standard to put Variable 1 on the vertical (y) axis. We will use this convention.
- Use the
mutatefunction to add the Elite variable to the data. I recommend using theif_elsefunction to determine the values of Elite rather than taking the approach used in the textbook (you will need to look up how to use theif_elsefunction). Create a related variable called Selective that has three categories, Less, More, and Most, indicating that students from the top 10% of their high school make up less than 20%, between 20% and 50%, and more than 50% of their student population. Use themutatefunction to add the new column, and use thecase_whenfunction (also from dplyr) to do assign the categories (you will need look up how to use this function). Check your work using View. Instead of usingsummary, use thecountfunction to count the number of Elite schools in the data. Also use thecountfunction to count the number of Less, More, and Most selective schools. Instead of the plot suggested by the text, create side-by-side boxplots of Outstate vs Selective. Use ggplot instead of plot, and use the classic theme. Use the mutate function to add a variable to the data called Accept.Perc that indicates the percentage of students that are accepted out of those that applied. After you have added the Selective, Elite, and Accept.Perc variables to the data and checked to make sure that their values make sense, save the new data set in your clean_data directory as an RDS file. - Use ggplot to create the histograms. Instead of using
par(mfrow = c(2, 2))as the textbook suggests, create at least one faceted plot in which the histograms are faceted by Selective. This should result in Three histogram plots arranged in a column. You need to decide whether to use different scales on the x and y axes for each facet. Use Selective to determine the fill color of the histograms. Choose a color scale that is more accessible to people with color blindness. - Explore the data using plots and numerical summaries. Explore several variables and their relationships. Create at least one additional variable. Create at least one scatterplot that compares two quantitative variables in which the point shapes and colors are determined by the Selective variable you created. Produce all of your plots using ggplot. Create at least one set of boxplots that also shows a scatterplot of the data that is created using
geom_jitter(like we did in the lab). Summarize your results.
- Also use the
- Use the
- Complete problem 10 from Chapter 2 of the ISLR book (P. 57). Convert the Boston data to a tibble (
Boston <- Boston %>% as_tibble()). Use functions from the tidyverse to work with the data, and use ggplot and ggpairs to create plots. Use functions from the corrr package and create plots to help you answer part c.