---
title: "USCOTS 2019 tidyverse workshop"
author: "Albert Y. Kim"
date: "5/15/2019"
output: html_document
---

# Chapter 2

```{r}
# Load packages
library(nycflights13)
library(dplyr)

# data
flights
```

```{r}
# One way to explore:
glimpse(flights)
```

```{r, eval = FALSE}
# Another way to explore
View(flights)
```

```{r, eval = FALSE}
# See help file
?flights
```

```{r}
library(readr)
africa <- read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vSttoZ6MRXOTqw6yMUJY5rUxgFuoqLq-VQIQsccSsxONSwJ935evaIrBhZv3oR6CIo4wLuTbvdlErTT/pub?gid=0&single=true&output=csv")
View(africa)
```



***



# Chapter 3: Data visualization

## Scatterplot

```{r}
library(dplyr)
library(ggplot2)

# -c stands for "combine"/"concatenate"
# A tibble is a "tidy table" AKA a data frame
hot_and_cold <- tibble(
  A = c(1, 2, 3, 4),
  B = c(2, 1, 4, 3),
  C = c(3, 2, 1, 2),
  D = c("hot", "hot", "cold", "cold")
)
hot_and_cold
ggplot(data = hot_and_cold, mapping = aes(x = A, y = B, size = C, color = D)) +
  geom_point()
```

## Exercise

```{r}
ggplot(data = africa, mapping = aes(x = height, y = num_countries_africa)) +
  geom_point()

ggplot(data = africa, mapping = aes(x = factor(num_countries_range_guess), y = num_countries_africa)) +
  geom_boxplot() +
  labs(x = "Priming Number", y = "Number of Countries", title = "Africa quiz vs experiment")
```



***



# Chapter 4: Data wrangling

```{r}
fruit_basket <- tibble(
  type = c("apple", "orange", "grape", "apple", "orange", "apple"),
  cost_dollars = c(1, 2, 2, 3, 1, 3)
)
fruit_basket
```


## filter

```{r}
fruit_basket %>% 
  filter(type == "apple")
```

## summarize

```{r}
fruit_basket %>% 
  summarize(total_cost_dollars = sum(cost_dollars))
```

## group_by

```{r}
fruit_basket %>% 
  group_by(type)
```

```{r}
fruit_basket %>% 
  group_by(type) %>% 
  summarize(total_cost = sum(cost_dollars))
```

## mutate

```{r}
fruit_basket %>% 
  mutate(cost_cents = cost_dollars * 100)
```

## arrange

```{r}
fruit_basket %>% 
  arrange(cost_dollars)
```

```{r}
fruit_basket %>% 
  arrange(desc(cost_dollars))
```


## Exercise

```{r}
# Note this isn't saved anywhere, it is merely output on screen
africa %>% 
  group_by(num_countries_range_guess) %>% 
  summarize(mean_guess = mean(num_countries_africa))

africa <- africa %>% 
  mutate(height_in_cm = 2.54 * height)
```



***


# Chapter 6: Simple regression

## Simple linear regression

```{r}
library(dplyr)
library(ggplot2)
library(moderndive)

# Data:
slr_example <- tibble(
  x = c(0, 0.5, 1),
  y = c(1, 0, 2)
)

# EDA:
ggplot(data = slr_example, mapping = aes(x = x, y = y)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE)
```

```{r}
# 1. Fit model
model_1 <- lm(y ~ x, data = slr_example)

# 2.a) Get regression table
get_regression_table(model_1)
# 2.b) Get regression points: Note intuitive column/variable names
get_regression_points(model_1)
# 2.c) Scalar summaries
get_regression_summaries(model_1)
```

## Simple regression with categorical predictor

```{r}
# Data: 
example <- tibble(
  name = c("Bert", "Bert", "Bert", "Jenny", "Jenny", "Jenny", "Miles", "Miles", "Miles"),
  points = c(9, 10, 11, 11, 12, 13, 8, 9, 10)
)

# EDA:
ggplot(example, aes(x = name, y = points)) +
  geom_point()

# 1. Fit model
model_2 <- lm(points ~ name, data = example)
# 2.a) Get regression points
get_regression_table(model_2)
# 2.b) Get regression points
get_regression_points(model_2)
```


## Exercise

```{r}
ggplot(data = africa, mapping = aes(x = height, y = num_countries_africa)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(x = "height", y = "countries guess")

# 1. Fit model
model_a <- lm(num_countries_africa ~ height, data = africa)

# 2.a) Get regression table
get_regression_table(model_a)

# 1. Fit model
africa <- africa %>% 
  mutate(num_countries_range_guess = factor(num_countries_range_guess))

model_b <- lm(num_countries_africa ~ num_countries_range_guess, data = africa)

# 2.a) Get regression table
get_regression_table(model_b)
```