---
title: "IS5 in R: Stats Starts Here (Chapter 1)"
author: "Nicholas Horton (nhorton@amherst.edu)"
date: "December 19, 2020"
output: 
  pdf_document:
    fig_height: 3
    fig_width: 5
  html_document:
    fig_height: 3
    fig_width: 5
  word_document:
    fig_height: 4
    fig_width: 6
---


```{r, include = FALSE}
# Don't delete this chunk if you are using the mosaic package
# This loads the mosaic and dplyr packages
require(mosaic)
```

```{r, include = FALSE}
# knitr settings to control how R chunks work.
require(knitr)
opts_chunk$set(
  tidy = FALSE, # display code as typed
  size = "small" # slightly smaller font for code
)
```

## Introduction and background 

This document is intended to help describe how to undertake analyses introduced as examples in the Fifth Edition of *Intro Stats* (2018) by De Veaux, Velleman, and Bock.
This file as well as the associated R Markdown reproducible analysis source file used to create it can be found at http://nhorton.people.amherst.edu/is5.

This work leverages initiatives undertaken by Project MOSAIC (http://www.mosaic-web.org), an NSF-funded effort to improve the teaching of statistics, calculus, science and computing in the undergraduate curriculum.
In particular, we utilize the `mosaic` package, which was written to simplify the use of R for introductory statistics courses.
A short summary of the R needed to teach introductory statistics can be found in the mosaic package vignettes (https://cran.r-project.org/web/packages/mosaic).
A paper describing the mosaic approach was published in the *R Journal*: https://journal.r-project.org/archive/2017/RJ-2017-024.

## Chapter 1: Stats Starts Here

### Section 1.1: What is Statistics?
### Section 1.2: Data
### Section 1.3: Variables

See table on page 7.

```{r}
library(mosaic)
options(digits = 3)
Tour <-
  readr::read_csv("http://nhorton.people.amherst.edu/is5/data/Tour_de_France_2016.csv") %>%
  janitor::clean_names()
```

By default, `read_csv()` prints the variable names.
These messages can be suppressed using the `message=FALSE` code chunk option to save space and improve readability. 

```{r}
names(Tour)
glimpse(Tour)
head(Tour, 3)
tail(Tour, 8) %>%
  select(winner, year, country)
```

Piping (`%>%`) takes the output of the line of code and uses it in the next.  

#### Let's find who was the winner in 1998

We use the `filter()` command.

```{r}
filter(Tour, year == 1998) %>%
  select(winner, year, country)
```

#### How many stages were there in the tour in the year that Alberto Contador won?

We can also use the `filter()` command.

```{r}
filter(Tour, winner == "Contador Alberto") %>%
  select(winner, year, stages)
```

Note that the following command generates the same output.

```{r}
Tour %>%
  filter(winner == "Contador Alberto") %>%
  select(winner, year, stages)
```

The pipe operator (`%>%`) can be used to connect one dataframe or command to 
another.

#### What was the slowest average speed of any tour?  Fastest?

Again, we use `filter()` but this time in conjunction with the `min()` function.

```{r}
filter(Tour, average_speed == min(average_speed)) %>%
  select(year, average_speed)
filter(Tour, average_speed == max(average_speed)) %>%
  select(year, average_speed)
```

#### How can we summarize the distribution of Average Speeds?


```{r}
df_stats(~average_speed, data = Tour)
```

Note that `~x` denotes the simplest form of the general modelling language (used to indicate a single variable in mosaic).  

