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

## Introduction

Chapter 6 talks about experimental and statistical control. The following examples help illustrate a few items that were discussed. Follow all instructions to complete Chapter 6.

## Random Assignment

1. Let's start by loading the `tidyverse` package (you can ignore the notes that you see below that it gives you once you load it) and the `furniture` package.
```{r}
library(tidyverse)
library(furniture)
```
2. We are going to use a ficticious, experimental data set that is inputted below. `posttest` is the posttest scores regarding words recognized accurately from a person with a motor speech disorder; `pretest` is the initial accurately recognized words; `therapy` is the experimental group where `1` is the intervention group and `0` is the control group.
```{r}
## Don't change this code :)
set.seed(843)
df <- data_frame(
  posttest  = c(2,4,6,6,9,10,12, 6,7,9,9,12,12,15),
  pretest = c(1,3,7,10,13,17,19, 1,5,7,9,13,16,19),
  therapy  = c(1,1,1,1,1,1,1, 0,0,0,0,0,0,0)
) %>%
  mutate(gain = posttest - pretest)
```
3. Let's take a look at this visually.
```{r}
df %>%
  mutate(therapy = factor(therapy, labels = c("No Therapy", "Therapy"))) %>%
  ggplot(aes(pretest, posttest, group = therapy, color = therapy)) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    scale_color_manual(values = c("darkorchid", "firebrick1"))
```

4. Let's use a t-test to assess if there are differences between the therapy group in the gain scores. Is this difference significant?
```{r}
df %>%
  t.test(gain ~ therapy,
         data = .,
         var.equal = TRUE)
```
5. We could do the same analysis using regression with `gain` and `therapy`. 
```{r}
df %>%
  lm(gain ~ therapy,
     data = .) %>%
  summary()
```
6. Since we have information on the pretest, we could use that to increase the precision of our estimates. We can do that by using multiple regression with `pretest` as a covariate. What did it do to the estimate? Why?
```{r}
df %>%
  lm(gain ~ therapy + pretest,
     data = .) %>%
  summary()
```
7. Did it increase the precision of the estimate on `therapy`?
8. Can random assignment make groups that aren't equal? If so, what can be done to help?


## Conclusion

Regression is well adapted for both experimental and observational research designs. Using both experimental and statistical controls within the same design can increase validity and statistical power of the analyses.
