---
title: "Linear Regression Lab 1"
date: "June 27th, 2022"
output: html_document
---

## Goals

We will briefly review linear modeling, focusing on building and assessing linear models in R. We have four main goals in this lab:

+ use exploratory data analysis (EDA) and visualization to determine a) whether two variables have a linear relationship, and b) among a set of explanatory variables, which one(s) seem like the best candidates for predicting a given output variable.

+ fit and interpret simple regression models,

+ look at diagnostic plots to determine whether a linear model is a good fit for our data,

+ assess our fitted linear models.

## Data

Execute the following code chunk to load the heart disease dataset we worked with before:

```{r init_data, warning = FALSE, message = FALSE}
library(tidyverse)
heart_disease <- read_csv("http://www.stat.cmu.edu/cmsac/sure/2022/materials/data/health/intro_r/heart_disease.csv")
```

This dataset consists of 788 heart disease patients (608 women, 180 men). Your goal is to predict the `Cost` column, which corresponds to the patient's total cost of claims by subscriber (i.e., `Cost` is the response variable). You have access to the following explanatory variables:

+ `Age`: Age of subscriber (years)
+ `Gender`: Gender of subscriber
+ `Interventions`: Total number of interventions or procedures carried out
+ `Drugs`: Categorized number of drugs prescribed: 0 if none, 1 if one, 2 if more than one
+ `ERVisit`: Number of emergency room visits
+ `Complications`: Whether or not the subscriber had complications: 1 if yes, 0 if no
+ `Comorbidities`: Number of other diseases that the subscriber had
+ `Duration`: Number of days of duration of treatment condition

## Exercises

### 1. __EDA__

Spend time exploring the dataset, to visually assess which of the __explanatory__ variables listed above is most associated with our response `Cost`. Create scatterplots between the response and each __continuous__ explanatory variable (either `Interventions`, `ERVist`, `Comorbidities`, or `Duration`). __Do any of the relationship appear to be linear?__ Describe the direction and strength of the association between the explanatory and response variables.

In your opinion, __which of the possible continuous explanatory variables displays the strongest relationship with cost__?

### 2. __Fit a simple linear model__

Now that you've performed some EDA, it's time to actually fit some linear models to the data. Start the variable you think displays the strongest relationship with the response variable. __Update the following code by replacing INSERT_VARIABLE with your selected variable, and run to fit the model__:

```{r, eval = FALSE}
init_cost_lm <- lm(Cost ~ INSERT_VARIABLE, data = heart_disease)
```

Before check out the `summary()` of this model, __you need to check the diagnostics__ to see if it meets the necessary assumptions. To do this you can try running `plot(init_cost_lm)` in the console (what happens?). Equivalently, another way to make the same plots but with `ggplot2` perks is with the [`ggfortify`](https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_lm.html) package by running the following code:

```{r, eval = FALSE}
# First install the package by running the following line (uncomment it!) in the console
# install.packages("ggfortify")
library(ggfortify)
autoplot(init_cost_lm) +
  theme_bw()
```

The first plot is __residuals vs. fitted__: this plot should NOT display any clear patterns in the data, no obvious outliers, and be symmetric around the horizontal line at zero. The smooth line provided is just for reference to see how the residual average changes. __Do you see any obvious patterns in your plot for this model?__

The second plot is a [Q-Q plot](http://www.stat.cmu.edu/~cshalizi/TALR/TALR.pdf) (p. 93).  Without getting too much into the math behind them, __the closer the observations are to the dashed reference line, the better your model fit is.__  It is bad for the observations to diverge from the dashed line in a systematic way - that means we are violating the assumption of normality discussed in lecture. __How do your points look relative to the dashed reference line?__

The third plot looks at the square root of the absolute value of the standardized residiuals.  We want to check for homoskedascity of errors (equal, constant variance).  __If we did have constant variance, what would we expect to see?__ __What does your plot look like?__

The fourth plot is residuals vs. leverage which helps us identify __influential__ points. __Leverage__ quanitifies the influence the observed response for a particular observation has on its predicted value, i.e. if the leverage is small then the observed response has a small role in the value of its predicted reponse, while a large leverage indicates the observed response plays a large role in the predicted response. Its a value between 0 and 1, where the sum of all leverage values equals the number of coefficients (including the intercept). Specifically the leverage for observation $i$ is computed as:

$$h_{ii} = \frac{1}{n} + \frac{(x_i - \bar{x})^2}{\sum_i^n (x_i - \bar{x})^2}$$
where $\bar{x}$ is the average value for variable $x$ across all observations. [See page 191 for more details on leverage and the regression hat matrix](http://www.stat.cmu.edu/~cshalizi/TALR/TALR.pdf). We're looking for points in the upper right or lower right corners, where dashed lines for [Cook's distance](https://en.wikipedia.org/wiki/Cook%27s_distance) values would indicate potential outlier points that are displaying too much influence on the model results. __Do you observed any such influential points in upper or lower right corners?__

__What is your final assessment of the diagnostics, do you believe all assumptions are met? Any potential outlier observations to remove?__

### 3. __Transform the `Cost` variable__

An obvious result from looking at the residual diagnostics above is that we are clearly violating the assumption of Normality. __Why do you think we're violating this assumption?__ (HINT: Display a histogram of the `Cost` variable.)

One way of addressing this concern is to apply a transformation to the response variable, in this case `Cost`. A common transformation for any type of dollar amount is to use the `log()` transformation. Run the following code chunk to create a new `log_cost` variable that we will use for the remainder of the lab. 

```{r, eval = FALSE}
heart_disease <- heart_disease %>%
  mutate(log_cost = log(Cost + 1))
```

__Why did we need to `+ 1` before taking the `log()`?__ (HINT: Look at the minimum of `Cost`.) Now make another histogram, this time for the new `log_cost` variable - what happened to the distribution?

### 4. __Assess the model summary__

Now fit the same model as before using the following code chunk. __Update the following code by replacing INSERT_VARIABLE with your selected variable, and run to fit the model__:

```{r, eval = FALSE}
log_cost_lm <- lm(log_cost ~ INSERT_VARIABLE, data = heart_disease)
```

Following the example in lecture, interpret the results from the `summary()` function on your initial model. __Do you think there is sufficient evidence to reject the null hypothesis that the coefficient is 0? What is the interpretation of the $R^2$ value?__
Compare the square root of the raw (unadjusted) $R^2$ of your linear model to the correlation between that explanatory variable and the response using the `cor()` function (e.g., `cor(heart_disease$INSERT_VARIABLE, heart_disease$log_cost)` - but replace `INSERT_VARIABLE` with your variable). __What do you notice?__

To assess the fit of a linear model, we can also plot the predicted values vs the actual values, to see how closely our predictions align with reality, and to decide whether our model is making any systematic errors. Execute the following code chunk to show the actual log(`Cost`) against our model's predictions

```{r, eval = FALSE}
heart_disease %>%
  mutate(model_preds = predict(log_cost_lm)) %>%
  ggplot(aes(x = model_preds, y = log_cost)) +
  geom_point(alpha = 0.75) +
  geom_abline(slope = 1, intercept = 0,
              linetype = "dashed", color = "red") +
  theme_bw() +
  labs(x = "Predictions", y = "Observed log(Cost + 1)")
```

### 5. __Repeat steps 2 and 3 above for each of the different continuous variables__

Which of the variables do you think is the most appropriate variable for modeling the cost?

### 6. __Include multiple covariates in your regression__

Repeat steps 2 and 3 above but including more than one variable in your model. You can easily do this in the `lm()` function by adding another variable to the formula with the `+` operator as so (but just replace the `INSERT_VARIABLE_X` parts):

```{r eval = FALSE}
multi_cost_lm <- lm(log_cost ~ INSERT_VARIABLE_1 + INSERT_VARIABLE_2, 
                   data = heart_disease)
```

__Experiment with different sets of the continuous variables__. What sets of continuous variables do you think models log(`Cost`) best? (Remember to use the __Adjusted $R^2$__ when comparing models that have different numbers of variables).

Beware collinearity! Load the `car` library (install it if necessary!) and use the `vif()` function to check for possible (multi)collinearity. The `vif()` function computes the __variance inflation factor (VIF)__ where for predictor $x_j$ for $j \in 1,\dots, p$:

$$
VIF_j = \frac{1}{1 - R^2_j}
$$

where $R^2_j$ is the $R^2$ from a variable with variable $x_j$ as the response and the other $p-1$ predictors as the explanatory variables. VIF values close to 1 indicate the variable is not correlated with other predictors, while VIF values over 5 indicate strong presence of collinearity. If present, remove a variable with VIF over 5, and redo the fit. Rinse, lather, and repeat until the `vif()` outputs are all less than 5. The follow code chunk displays an example of using this function:

```{r vif,eval = FALSE}
# First install the package by uncommenting out the following line:
# install.packages("car")
library(car)
vif(multi_cost_lm)
```


## Tomorrow

Tomorrow's lab will focus on categorical variables, interactions, and holdout data predictions.


