
---
title: "Exploring Class Data"
output: 
  html_document: 
    highlight: tango
    theme: spacelab
---

```{r load-libraries, message = F}
library(tidyverse)
```

Read in the data
```{r read-data, message = F}
class_data <- read_csv("https://dyurovsky.github.io/85309/data/demos/85309_data.csv")
```

Let's look at the first few rows
```{r head_data}
class_data
```

Use `ggplot` to make a separate histogram for each of the three things we measured. 
```{r, fig.width = 4, fig.height = 4}
# use the classic plot theme--a little cleaner than the default
theme_set(theme_classic(base_size = 16))

# plot height
ggplot(class_data, aes(x = height)) + 
  geom_histogram(binwidth = 2) 
  
# plot month
ggplot(class_data, aes(x = birth_month)) + 
   geom_histogram(binwidth = 1)  +
   scale_x_continuous(breaks = 1:12)
  
# plot siblings
ggplot(class_data, aes(x = siblings)) + 
  geom_histogram(binwidth = 1)
```


The current `class_data` tibble is in what's called *wide format* -- each row has multiple observations for a single person. To analyze it, I want it in *long format*, where each row has only a single data point.

Munge the data (process it to get it into a tidy format). I'm going to use the `pivot_longer` function from the `tidyverse` package. Right now, a single row has a value for `siblings`, `height`, and `birth_month` as well as an identifier `andrew_id`. I want it to only have a single `value` column and another column `measurement` that indicates which of three variables the value corresponds to. 

So what I'll do is to use the `pivot_longer` to take all of the columns *except* for `andrew_id` and put them in a new column called `value` with a column called `measure` that identifies what that value corresponds to (height, siblings, or birth months).

```{r munge_data}
class_data_long <- class_data %>%
  pivot_longer(-andrew_id, names_to = "measure", values_to = "value")
# take all of the columns except for email and put them under one column

class_data_long
```

Now I want to compute some descriptive statistics--the mean and median. I'll use the `group_by` function from the `tidyverse` package to let `R` know that I want to call these functions separately for each `measure` in my dataset. Then I'll use `summarise`, which takes in all of the data for each group, and reduces it down to a single number using the functions I ask for. Here, `mean` and `median`.

```{r compute_descriptives}
descriptives <- class_data_long %>%
  group_by(measure) %>%
  summarise(mean = mean(value),
            median = median(value))

#let's print out these descriptives
descriptives
```


Ok, let's plot all of the histograms together! And let's also put a line on each indicating the position of the mean (in red), and the median (in blue). That way, we can see how skew in our distributions changes the relationship between them.

```{r facet_plot, fig.width = 8, fig.height = 4, message = FALSE}
ggplot(class_data_long, aes(x = value)) +
  facet_wrap(~ measure, scales = "free_x") + # make a separate panel for each measure
  geom_histogram(fill = 'gray', color = 'black') + 
  geom_vline(aes(xintercept = mean), data = descriptives, color = "red") + 
  geom_vline(aes(xintercept = median), data = descriptives, color = "blue") 
```


Now lets make box plots so we can see how they compare to the histograms. They still show a lot of the same information--skew, variability, center--but they also compress a lot of data about exactly how many people had exactly what height.

```{r boxplot, fig.width = 6, fig.height = 4}
ggplot(class_data_long, aes(x = measure, y = value)) +
  geom_boxplot() 
```

