---
title: "Homework 5"
subtitle: "Psychology 2812B FW23"
output: html_document
---

# Lab Component

## 1. UCI Wisconsin Breast Cancer Dataset

The [UCI Wisconsin Breast Cancer Dataset](https://archive.ics.uci.edu/dataset/17/breast+cancer+wisconsin+diagnostic) is a dataset of 569 breast cancer patients. Each patient has 30 features, including the patient's diagnosis (malignant or benign) and 29 features that describe the patient's tumour. The dataset is available in the `mclust` package. Install and then load the package, and use the `glimpse()` function to see the structure of the dataset.

```{r}
# your code goes here
```


## 2. Predicting cancer diagnosis using tumour radius: plot

We are interested in using the radius of the tumour (the `Radius_mean` variable) to predict whether the tumour is malignant or benign (the `Diagnosis` variable). Create a scatterplot with the radius on the horizontal axis and the diagnosis on the vertical axis. Use the `geom_jitter()` function to add a little random vertical jitter to the data points to aid in visualization (only vertical jitter, no horizontal jitter). Use the `geom_smooth()` function to add a logistic regression line to the plot. Use the `labs()` function to add a title, and to label the vertical axis as "Probability (Malignant)" and the horizontal axis as "Tumour Radius". I also like to set `size=2, stroke=0, alpha=0.5, fill="black"` in the `geom_jitter()` function to make the points look a little nicer.

```{r}
# your code goes here
```


## 3. Predicting cancer diagnosis using tumour radius: model

Use the `glm()` function with option `family="binomial"` to fit a logistic regression model to the data in which you use `Radius_mean` to predict `Diagnosis`. Use the `summary()` function to see the results of the model. Don't worry yet about testing the assumptions of logistic regression, you will do that later in the homework.

```{r}
# your code goes here
```


## 4. Predicting cancer diagnosis using tumour radius: predict

Use the `predict()` function to predict the probability that a tumour with radius 17 is malignant. 

```{r}
# your code goes here
```


## 5. Predicting 50% chance of a malignant tumour

According to the model, what tumour radius is associated with a 50% chance of malignancy? Give your answer to 3 decimal places.

To solve this, remember the equations for the logistic regression model:

$$
\log\left(\frac{p}{1-p}\right) = \beta_0 + \beta_1 (\mathrm{Radius\_mean})
$$

```{r}
# your code goes here
```

> your sentence goes here


## 6. Test the linearity of log odds assumption

### 6.1. Plot the log odds of the outcome against the predictor variable

One of the assumptions of logistic regression is that the predictor variable(s) is/are linearly related to the log odds of the outcome (the binary dependent variable). 

To test this assumption, we can plot the log odds of the outcome against the predictor variable. If the relationship is linear, then the points should fall along a straight line. If the relationship is not linear, then the points will not fall along a straight line. Since the dependent variable is binary however, we cannot compute the log odds of each individual data point. Instead, we can bin the data into groups along the predictor variable, and then compute the mean log odds of the outcome for each group.

Create a new tibble containing the binned data, call it `wdbc_binned`. Use the `cut()` function to bin the data into 5 groups based on the `Radius_mean` variable, and define the breaks using the `quantile()` function so that there are approximately equal `n` within each of the 5 groups. Use the `group_by()` and `summarize()` functions to compute the mean log odds of the outcome for each group.

```{r}
# your code goes here
```

Now plot the mean log odds of the outcome (on the vertical axis) against the mean of the predictor variable for each group (on the horizontal axis). Use the `geom_point()` function to add the points to the plot, and use the `geom_smooth()` function to add a linear regression line to the plot. Use the `labs()` function to add a title, and to label the vertical axis as "Mean Log Odds of Malignancy" and the horizontal axis as "Mean Tumour Radius".

```{r}
# your code goes here
```

What does the plot tell you about the assumption of linearity of log odds?

> your sentence goes here

### 6.2. Statistical test of the linearity of log odds assumption

Perform a Box-Tidwell test of the linearity of log odds assumption by testing for an interaction between `Radius_mean` and `log(Radius_mean)`. Use the `glm()` function with option `family="binomial"` to fit a logistic regression model to the data in which you use `Radius_mean` and `log(Radius_mean)` to predict `Diagnosis`. Use the `summary()` function to see the results of the model.

```{r}
# your code goes here
```

What do the results of this model tell you about the assumption of linearity of log odds?

> your sentence goes here


## 7. Test for outliers

Compute Cook's distance for each observation in the dataset. Use the `cooks.distance()` function to compute Cook's distance for each predicted probability in the dataset. Use the `plot()` function to plot Cook's distance against the observation number. Use the `geom_hline()` function to add a horizontal line at Cook's distance = 1.

```{r}
# your code goes here
```

What does the plot tell you about the presence of outliers in the dataset?

> your sentence goes here

# Home Component

## 8. Add more predictors to the model

You are interested in using more predictors than just tumour radius to predict malignancy.

Define a new tibble containing `Diagnosis` and only the following 5 predictor variables:

- `Radius_mean`
- `Texture_mean`
- `Smoothness_mean`
- `Concavity_mean`
- `Symmetry_mean`

```{r}
# your code goes here
```

Fit a new model that includes these 5 predictor variables and show the model output using `summary()`:

```{r}
# your code goes here
```


## 9. Test for collinearity

### 9.1. Plot the correlations between the predictors

Produce a plot using `ggpairs()` from the `GGally` package to visualize the correlation between the predictor variables.

```{r}
# your code goes here
```

What does the plot and the correlation coefficients tell you about the presence of collinearity in the dataset?

> your sentence goes here

### 9.2 Test for collinearity using the variance inflation factor (VIF)

Use the `vif()` function from the `car` package to compute the variance inflation factor (VIF) for each predictor variable in the model. Use the `summary()` function to see the results of the VIF test.

```{r}
# your code goes here
```

What do the results of the VIF test tell you about the presence of collinearity in the dataset?

> your sentence goes here


## 10. Model selection

Perform a stepwise procedure using `step()` to determine the best set of predictor variables for the model and interpret the output. Use the `direction="both"` option to perform a stepwise procedure that both adds and removes variables from the model. Describe and interpret the output of the stepwise procedure.

```{r}
# your code goes here
```

## 11. New model vs old model

### 11.1. Which model does a better job?

Which model does a better job of predicting malignancy in the training data, the model you fit in Question 3 above, or the model you fit in Question 10? Refer to a specific quantity or measure in your answer.

> your sentence goes here


### 11.2. Predicting the probability of malignancy

In Question 4 above you used the model you fit in Question 3 to predict the probability that a tumour with radius 17 is malignant. Use the model you fit in Question 10 to predict the probability that a tumour with radius 17 is malignant. For the values of the other predictor variables in the model in Question 10, use the mean values in the dataset as your predictor values. How do the two predictions compare?

```{r}
# your code goes here
```

> your sentence goes here
