Instructions

In this lab, you will begin to get oriented with R and work with some data.

How to complete this assignment.

  • Attempt each exercise in order.

  • In each code chunk, if you see “# INSERT CODE HERE”, then you are expected to add some code to create the intended output (Make sure to erase “# INSERT CODE HERE” and place your code in its place).

  • If my instructions say to “Run the code below…” then you do not need to add any code to the chunk.

  • Many exercises may require you to type some text below the code chunk, interpreting the output and answering the questions.

  • Please follow the Davidson Honor Code and rules from the course syllabus regarding seeking help with this assignment.

How to submit this assignment.

  • When you are finished, click the “Knit” button at the top of this panel. If there are no errors, an word file should pop up after a few seconds.

  • Take a look at the resulting word file that pops up. Make sure everything looks correct, your name is listed at the top, and that there is no ‘junk’ code or output.

  • Save the word file (to your local computer, and/or to a cloud location) as: Lab 3 “Insert Your Name”.

  • Use this link to upload your word file to my Google Drive folder. Do not upload the original .Rmd version.

  • This assignment is due Thursday, June 30, 2022, no later than 9:30 am Eastern. Points will be deducted for late submissions.

  • TIP: Start early so that you can troubleshoot any issues with knitting to word.

Grading Rubric

There are 6 possible points on this assignment.

Baseline (C level work)

  • Your .Rmd file knits to word without errors.
  • You answer questions correctly but do not use complete sentences.
  • There are typos and ‘junk code’ throughout the document.
  • You do not put much thought or effort into the Reflection answers.

Average (B level work)

  • You use complete sentences to answer questions.
  • You attempt every exercise/question.

Advanced (A level work)

  • Your code is simple and concise.
  • Unnecessary messages from R are hidden from being displayed in the word.
  • Your document is typo-free.
  • At the discretion of the instructor, you give exceptionally thoughtful or insightful responses.

Exercise 1. (3 points)

This question should be answered using the Weekly data set, which is part of the ISLR2 package.

  1. Produce some numerical and graphical summaries of the Weekly data. Do there appear to be any patterns?

  2. Use the full data set to perform a logistic regression with Direction as the response and the five lag variables plus Volume as predictors. Use the summary function to print the results. Do any of the predictors appear to be statistically significant? If so, which ones?

  3. Compute the confusion matrix and overall fraction of correct predictions. Explain what the confusion matrix is telling you about the types of mistakes made by logistic regression.

  4. Now fit the logistic regression model using a training data period from 1990 to 2008, with Lag2 as the only predictor. Compute the confusion matrix and the overall fraction of correct predictions for the held out data (that is, the data from 2009 and 2010).

#insert code here

ANSWER:

Exercise 2. (3 points)

In this problem, you will develop a model to predict whether a given car gets high or low gas mileage based on the Auto data set.

  1. Create a binary variable, mpg01, that contains a 1 if mpg contains a value above its median, and a 0 if mpg contains a value below its median. You can compute the median using the median() function.

  2. Explore the data graphically in order to investigate the association between mpg01 and the other features. Which of the other features seem most likely to be useful in predicting mpg01? Scatterplots and boxplots may be useful tools to answer this question. Describe your findings.

  3. Split the data into a training set and test set. (Note: Check out the rsample package. Additionally, you will want to use set.seed(). Read HOML 2.2)

  4. Perform logistic regression on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in part (B). What is the test error of the model obtained?

#insert code here

ANSWER: