---
title: "Yawning"
author: "Wells"
date: "3/19/2021"
output: pdf_document
---

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

This .Rmd contains code to visualize the process of randomizing participants into two groups. You don't need to understand how the code works, although you should spend time thinking about what the output means.



The following code chunk loads the yawning data set, including the participant's ID, whether the participant originally yawned, and the participant's original group.

```{r eval = F}
data("mythbusters_yawn")
View(mythbusters_yawn)
```

The next code chunk shows the original division of participants into seeded and control groups, using color to indicate whether the participant originally yawned. Numeric labels indicate the participant's ID number.

```{r}
mythbusters_yawn %>% mutate(
group = rep(c("seed", "control"), c(34,16)),
         r = (subj-1+ifelse(group == "control", 2, 0)) %% 6,
         m = (subj-1+ifelse(group == "control", 2, 0) - r)/6
         ) %>% 
  ggplot(aes(x = m, y = r, color = yawn))+
  geom_point( size = 10)+
  geom_vline(xintercept = 5.5)+
  annotate(geom= "text", x = 3, y = 7, label = "Seed")+
  annotate(geom= "text", x = 7, y = 7, label = "Control")+
  geom_text(aes(x = m, y = r,label = subj), color = "black")+
  theme(axis.title.y=element_blank(),
           axis.text.y=element_blank(), 
           axis.ticks.y=element_blank(),
           axis.title.x=element_blank(),
           axis.text.x=element_blank(), 
           axis.ticks.x=element_blank())
```

The next code chunk selects 34 of the participants at random to be in the `seed` group and puts the remaining participants in the `control` group and plots the results. The color of each ball indicates whether the participant originally yawned and numeric labels indicate the participant's ID. 

*Note that here, we are just assigning group labels to the participants. We are not repeating the experiment. For example, participant 1 will always yawn, regardless of which group they are in.*


```{r}

N<- 1 ##Change this to 8 after you've ran this code twice
      ## and read all of the instructions in this .Rmd

samples_seeded<-mythbusters_yawn %>% 
  rep_sample_n(size = 50, reps = N) %>% 
  mutate(order_drawn =  1:50, 
         group = rep(c("seed", "control"), c(34,16)),
         r = (order_drawn-1+ifelse(group == "control", 2, 0)) %% 6,
         m = (order_drawn-1+ifelse(group == "control", 2, 0) - r)/6
         )


samples_seeded %>% 
  ggplot(aes(x = m, y = r, color = yawn))+
  geom_point( size = 10)+
  geom_vline(xintercept = 5.5)+
  annotate(geom= "text", x = 3, y = 7, label = "Seed")+
  annotate(geom= "text", x = 7, y = 7, label = "Control")+
  geom_text(aes(x = m, y = r,label = subj), color = "black")+
  facet_wrap(~replicate, ncol = 2)+
  theme(axis.title.y=element_blank(),
           axis.text.y=element_blank(), 
           axis.ticks.y=element_blank(),
           axis.title.x=element_blank(),
           axis.text.x=element_blank(), 
           axis.ticks.x=element_blank())
```

Inspect the number of yawns in the control group, and explain why this is sufficient information to determine the numbers of yawns in the seed group.


The following code chunk computes the proportion of yawns in each group and the difference in proportion (`seed` - `control`):

```{r}
samples_seeded %>% 
  group_by(replicate,group) %>% 
  summarize(Yawns = sum(yawn == "yes"), n = n()) %>% 
  mutate(Prop_yawn = Yawns / n, Difference = Prop_yawn - lag(Prop_yawn)) 
```


Record the proportions and the difference in proportion (Seeded - Control) in the googlesheet (link in Slack).


Repeat the process once more by running the above code chunks (pay attention to what changed and what stayed the same).


Then repeat the above procedure at least 8 more times (or more if time permits). To speed up the process, in the first line of the code, change the value of `N` to `8` to perform 8 repetitions. (If the plot is hard to read, you may want to use the "Show in New Window" button in the upper right corner of the plot)




