# Setup

```{r}
# Read in the data
# If necessary, install the "readr" package with: install.packages("readr")
chemo_study_data <- readr::read_csv("https://www.dropbox.com/s/vl06j75a8afw8ct/chemo_study.csv?dl=1")

# Load the ggplot2 package
```

# Exercise 1: Plots and confounders

The first step in any data analysis is to visualize your data. Let's refamiliarize ourselves with the `ggplot2` package in R. It may be helpful to have [this ggplot2 cheat sheet](https://rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf) open. Make sure to load the `ggplot2` package by including `library(ggplot2)` at the top of your RMarkdown document.

## Part a

Get a feel for the data by visualizing the distributions of all 4 variables individually (4 plots total). Your code should look something like below.

```{r}
ggplot(chemo_study_data, aes(x = pre_tumor_size)) + ???
```

## Part b

Is pre-treatment tumor size predictive of whether or not a patient received chemotherapy? Make a plot to assess this, and briefly state what conclusions can be drawn from the plot. (Hint: it will be helpful to look at the second page of the cheat sheet in the section labeled "Position Adjustments".)

```{r}
ggplot(chemo_study_data, aes(x = pre_tumor_size, fill = treated)) + ???
```

## Part c

Is pre-treatment tumor size predictive of whether or not a patient recovered? Make a plot, and briefly state your conclusions.

## Part d

A variable is a **confounder** if it is a common cause of both the treatment and outcome. This is shown in the diagram below. Given your results from parts b and c, could pre-treatment tumor size be a confounder of the relationship between chemotherapy treatment and recovery?

## Part e

Could *post*-treatment tumor size be a confounder of the relationship between chemotherapy treatment and recovery? Support your answer with plots and an explanation.


Note: the causal relationships of interest so far can be depicted in a **causal diagram**, shown below. An arrow between two variables indicates that one is a cause of the other (an arrow points from a cause to its effect).

```{r fig.width=6, fig.height=2, fig.align="center"}
par(mar = rep(0,4))
plot(1, type = "n", xaxt = "n", yaxt = "n", bty = "n", xlab = "", ylab = "", xlim = c(0,6), ylim = c(1,4))
text(c("Chemotherapy", "Pre-treatment tumor size", "Recovery"), x = c(1,3,5), y = c(1,4,1), cex = 1.1)
arrows(x0 = c(3,3), y0 = c(4,4)-0.2, x1 = c(1,5), y1 = c(1,1)+0.2, angle = 25, lwd = 4)
arrows(x0 = 1.7, y0 = 1, x1 = 4.5, y1 = 1, angle = 25, lwd = 4)
```


<br><br>


# Exercise 2: Implications of confounding

## Part a

What is the relationship between confounders and the saying "Correlation does not imply causation"?

## Part b

Explain how regression models can be used to remove the influence of confounding variables.

## Part c

Suppose that chemotherapy generally works to promote recovery. Suppose also that sicker patients tend to receive chemotherapy, and healthier patients tend to not receive chemotherapy. What would you expect if you directly compared recovery rates in the chemotherapy and standard of care patients (no adjustments for other variables)? What would you expect if you held appropriate confounders constant?


<br><br>


# Exercise 3: Logistic regression models

We can model recovery using a **logistic regression model** (used when the outcome variable is binary). In R, we can fit a logistic regression model using code like the following:

```{r}
# Fit the model and store it in the "mod" object
mod <- glm(outcome_variable ~ predictor_variable1+predictor_variable2,
    family = "binomial", data = your_data)

# Display model output
summary(mod)

# Obtain confidence intervals (CIs) for model coefficients
# (95% CIs by default)
confint(mod)
```

## Part a

Fit a logistic regression model with only treatment as a predictor. Interpret the treatment coefficient. Is the sign of the coefficient what you expected? Why is this model misleading?

## Part b

Fit a logistic regression model that adjusts for pre-treatment tumor size. Interpret the treatment coefficient. Is the sign of the coefficient what you expected? Also report the 95% confidence interval for the treatment coefficient and what information you learn from it. (Side note: [This article](https://medium.com/@EpiEllie/having-confidence-in-confidence-intervals-8f881712d837) by Eleanor Murray has a great explanation of the most common misinterpretation of confidence intervals.)

## Part c

Fit a logistic regression model that adjusts for pre- and post-treatment tumor size. Interpret the treatment coefficient. Is the sign of the coefficient what you expected?


<br><br>


# Exercise 4: Warming up to causal ideas

## Part a

Consider the statement: "The causal effect of chemotherapy on recovery rates (as compared to no chemotherapy) is a change (difference) of 10%." What do you think "causal effect" means? What would you like it to mean?

## Part b

Which model do you think best estimates the causal effect of chemotherapy on recovery rates? Explain.

