#---
# title: "CSC 171 - Lab 4"
# author: "Student Name"
# output: word_document
#---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

### 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 "<font color="789922"># INSERT CODE HERE"</font>, 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 4 "Insert Your Name"**.

* Use [this link](https://forms.gle/DPb8vMZHgoS4GTyj9) to upload your word file to my Google Drive folder. **Do not** upload the original .Rmd version. 

* This assignment is <font color="darkred">**due Thursday, July 7, 2022, no later than 9:30 am Eastern**</font>. Points will be deducted for late submissions. 

* <font color="darkblue">TIP:</font> 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)** 

In Chapter 4, we used logistic regression to predict the probability of `default` using `income` and `balance` on the `Default` data set. We will now estimate the test error of this logistic regression model using the validation set approach. Do not forget to set a random seed before beginning your analysis.

(A) Fit a logistic regression model that uses `income` and `balance` to predict `default`.

(B) Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:

  - Split the sample set into a training set and a validation set.
  
  - Fit a multiple logistic regression model using only the training observations.

  - Obtain a prediction of default status for each individual in the validation set by computing the posterior probability of default for that individual, and classifying the individual to the default category if the posterior probability is greater than 0.5.
  
  - Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
  
(C) Repeat the process in (B) three times, using three different splits of the observations into a training set and a validation set. Com- ment on the results obtained.

(D) Now consider a logistic regression model that predicts the probability of `default` using `income`, `balance`, and a dummy variable for `student`. Estimate the test error for this model using the validation set approach. Comment on whether or not including a dummy variable for `student` leads to a reduction in the test error rate.



```{r}
#insert code here
```

<font color="darkred">**ANSWER:**</font>






#### **Exercise 2. (3 points)** 

We continue to consider the use of a logistic regression model to predict the probability of `default` using `income` and `balance` on the `Default` data set. In particular, we will now compute estimates for the standard errors of the `income` and `balance` logistic regression coefficients in two different ways: (1) using the bootstrap, and (2) using the standard formula for computing the standard errors in the `glm()` function. Do not forget to set a random seed before beginning your analysis.

(A) Using the `summary()` and `glm()` functions, determine the estimated standard errors for the coefficients associated with `income` and `balance` in a multiple logistic regression model that uses both predictors.

(B) Write a function, `boot.fn()`, that takes as input the `Default` data set as well as an index of the observations, and that outputs the coefficient estimates for `income` and `balance` in the multiple logistic regression model.

(C) Use the `boot()` function together with your `boot.fn()` function to estimate the standard errors of the logistic regression coefficients for `income` and `balance`.

(D) Comment on the estimated standard errors obtained using the `glm()` function and using your bootstrap function.


```{r}
#insert code here
```

<font color="darkred">**ANSWER:**</font>